'Find maximum value of outputs from custom function

I have a lot of Python code that takes a lot of data, does something with it, and the last output is then one single value. All my Python code have many parameters, but most are fixed except one. And it is this one that eventually changes the output.

So basically I have something looking like this:

optimum_list = []
for x in range(1, 100, 1):
    <a bunch of Python code>
    optimum_list.append(<value_from_Python_code>)

And then when I plot the optimum_list I will eventually get some kind of curve with a maximum (or more than one).

As such I could do it like this. However, the issue is that the <a bunch of Python code> takes a good amount of time for each iteration. So doing this 100 times might take half an hour or so. Doable, but annoying.

So how would I go about using some kind of optimization algorithm for this type of "function", without having to go through 100 iterations etc. I've seen it done with equations, however I don't think that fully translate into this type of optimization problem - but I might be mistaken.

So yeah, how would one proceed ?



Solution 1:[1]

By using threading you can do all the iterations at the same time : try this code

from threading import Thread

threads = []
optimum_list = []
queue = 0

def aBunchOfPythonCode():
    global queue
    queue += 1
    print("a bunch of python code")
    optimum_list.append("value from python code")
    queue -= 1

for i in range(100):
    newThread = Thread(target=aBunchOfPythonCode)
    threads.append(newThread)
    newThread.start() 

while(queue > 0) : 
    #waiting
    pass

print("A bunch of python code is finished")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marco