0

I have been playing with minimize function and would like to make an simple loop that would optimize a function using different optimization methods. Unfortunately, I have not been able to implement a list of desired methods I would like to try into my minimize function.

The problem I am encountering is how do I add "" symbols when taking an item from a list when defining what method I want to use?

methods = ["Nelder-Mead", "Powell"]
for numb, type in enumerate(methods):
    x0 = [10, 20]
    res = minimize(calculate_loss, x0, method = methods[type])
    print("x0 = " + str(res.x) + (" ") + str(res.success))

Thank you for all suggestions, have a nice day.

majyno
  • 3
  • 1
  • calculate_loss is a custom function made by yourself? I don't recommend you to use type as a variable, because it is already a function, try a different name. Also, maybe the issue is your trying to call the list methods indexing the method name. For loops get already the names, so you only have to write `res = minimize(calculate_loss, x0, method = type)`. But as I said, I would use another name for "type", like algorithm, alg, etc... – RobertoT Nov 23 '21 at 19:40
  • @RobertoT, thank you for your response, I did not realize that I can directly write minimize(...., method = type). Now it is working wonderfully. – majyno Nov 23 '21 at 21:00
  • The best practical way to learn how something works or what is wrong is using print function. For example in this case: print(num,type) after the loop line. And you'll see what's giving the for loop. Also, maybe you don't need to use enumerate. If you only want the type and not using numb: `for type in methods:` is enough. – RobertoT Nov 24 '21 at 09:04

0 Answers0