Python Forum
Need an alternative to brute force optimization loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need an alternative to brute force optimization loop
#1
Hello,
I have a function with 3 intricated loops to get the best parameters from about 16000000 combinations. This takes a lot of time.
I am looking for an alternative.
Here is the function.

            for a in range(700, 800, 1):
                a = a / 100
                for c in range(100, 200, 1):
                    #c = c / 10.0
                    for seed in range(min_seed, max_seed + 1):
                        #seed = seed/20.0
                        generated_sequence = generate_sequence(a, c, best_m, seed, len(adjusted_values))
                        cubed_difference = calculate_mean_squared_error(generated_sequence, adjusted_values)

                        if cubed_difference < min_cubed_difference:
                            min_cubed_difference = cubed_difference
                            best_a = a
                            best_c = c
                            best_seed = seed
buran write Nov-30-2023, 07:57 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
You can try to perform all calculations, either using vectorisation (numpy/pandas) or multiprocessing and only then finding best/min_cube_difference
RockBlok likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
The biggest performance bump will come from doing less. Not knowing what generate_sequence or calculate_mean_squared_error do I cannot offer any definitive suggestions on how this could be done. Essentially you are looking for the roots of a function. Sometimes this can be done analytically. No looping is always the fastest solution. If your function is linear, you could use something like Newton-Raphson to find the roots. If there are several roots you may have to repeat this process multiple times for different regions of the problem space.

A better algorithm can make your code millions of times faster, but if that isn't possible you can make the code thousands of times faster by not doing it in Python. As buran mentions, you can use numpy to do the calculations in C. If your equations are not amenable to numpy vectorization, you can write a C library that does the calculations and returns a result. This can be called from Python, or maybe just forget about Python and do everything in C.
RockBlok likes this post
Reply
#4
Why so cagey?

Tell us what the functions and variables are!

As the guys said, np is faster than Python.

Quote:It is sometimes said that Python, compared to low-level languages such as C++, improves development time at the expense of runtime.

For this example, from realpython.com, numpy is nearly 100 times faster:

import numpy as np
from timeit import timeit

def profit(prices):
    max_px = 0
    min_px = prices[0]
    for px in prices[1:]:
        min_px = min(min_px, px)
        max_px = max(px - min_px, max_px)
    return max_px

def profit_with_numpy(prices):
    """Price minus cumulative minimum price, element-wise."""
    prices = np.asarray(prices)
    return np.max(prices - cummin(prices))

cummin = np.minimum.accumulate
# compare speeds
seq = np.random.randint(0, 100, size=100000)
setup = ('from __main__ import profit_with_numpy, profit, seq;' ' import numpy as np')
num = 250
pytime = timeit('profit(seq)', setup=setup, number=num)
nptime = timeit('profit_with_numpy(seq)', setup=setup, number=num)
print('Speed difference: {:0.1f}x'.format(pytime / nptime))
Output:
Speed difference: 93.5x
Reply
#5
A better algorithm can speed up your code millions of times, but if that isn't possible, you can speed it up thousands of times by not writing it in Python. As buran mentioned, you may do the calculations in C using numpy. If your equations are not vectorizable with numpy, you can develop a C library that performs the calculations and returns a result. This can be called from Python, or you can simply ignore Python and do everything in C.
Reply
#6
I've heard using integers is faster than float numbers if you are doing millions and millions of calculations. Maybe you could find some way to use integers instead of floats? Ie.. shift the float input the number of sig figs you require, do your calculations as ints, then shift it back when complete?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Twilio alternative jmair 3 3,923 Feb-08-2024, 01:55 PM
Last Post: Sharmi
  Pillow alternative? kucingkembar 4 886 Jul-27-2023, 10:50 AM
Last Post: Larz60+
  Solving an equation by brute force within a range alexfrol86 3 2,812 Aug-09-2022, 09:44 AM
Last Post: Gribouillis
  force a program to exit ? Armandito 3 2,544 May-12-2022, 04:03 PM
Last Post: Gribouillis
  How to use scipy.optimization.brute for multivariable function Shiladitya 9 6,257 Oct-28-2020, 10:40 PM
Last Post: scidam
  Alternative for Cairosvg? Maryan 0 2,469 Oct-26-2020, 01:27 PM
Last Post: Maryan
  best way to force an exception Skaperen 2 2,070 Oct-21-2020, 05:59 AM
Last Post: Gribouillis
  I need advise with developing a brute forcing script fatjuicypython 11 5,087 Aug-21-2020, 09:20 PM
Last Post: Marbelous
  another alternative to np.interp evelynow 1 2,931 Aug-22-2019, 03:32 PM
Last Post: Larz60+
  Multithreading alternative MartinV279 1 2,803 Aug-01-2019, 11:41 PM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020