2

I have a function def objective(u,v) which returns two values return obj1,obj2 in which each corresponds to one of u or v how can I call either obj1 or obj2 to be used in the minimize function?

Tal bouham
  • 21
  • 1
  • Pass your second variable (not the optimization variable) to `minimize()` as an argument using `args`. See the [docs](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html#scipy.optimize.minimize) – SuperKogito May 22 '19 at 12:58
  • Possible duplicate of [Multiple variables in SciPy's optimize.minimize](https://stackoverflow.com/questions/13670333/multiple-variables-in-scipys-optimize-minimize) – lrnzcig May 22 '19 at 13:15

2 Answers2

0

I just had the same problem and did not find a solution.

I came up with a pretty dirty solution:

In my case, I have to return a larger number, usuall a few-thousands and a float, smaller than 1.

Since I don't need to optimize with a high(small number!) tolerance, I just add these two numbers:

I am optimizing for result_A

opti_function(a,b):

    ..... some function .....

    return result_A + result_B

this works, since result_B is way smaller than the tolerance needed for optimisation of result_A.

result = minimize_scalar(lambda a: opti_function([a,b]),tol=10)

As promised, pretty dirty....

Hu gePanic
  • 90
  • 6
0

You can write a wrapper function and return only one value:

def wrapper1(u, v):
    obj1, obj2 = objective(u, v)
    return obj1

def wrapper2(u, v):
    obj1, obj2 = objective(u, v)
    return obj2

Call the wrapper1(u,v) or wrapper2(u,v) in the minimize( ) instead of the original minimize.

Y. Zhang
  • 51
  • 3