Consider a black box function, f, which inputs x and return a real number, a.
def foo(x): ... return a
We know that f is a deterministic function of x. Suppose
x = []
for i in range(2):
x.append({'type': 'eq', 'fun': lambda x: x[i] + x[3+i] - 1000})
and
y = [{'type': 'eq', 'fun': lambda x: x[0] + x[3+0] - 1000},
{'type': 'eq', 'fun': lambda x: x[1] + x[3+1] - 1000}
]
I get that foo(x) != foo(y)
, which implies that x is not the same as y.
How is it possible that x
is not the same as y
? What are the differences between x
and y
?
Here is a verifiable and complete example:
import pandas as pd
import numpy as np
from scipy.optimize import minimize, Bounds
X0 = [0,0,0,0]
x = []
for i in range(2):
x.append({'type': 'eq', 'fun': lambda x: x[i] + x[2+i] - 1000})
y = [{'type': 'eq', 'fun': lambda x: x[0] + x[2] - 1000},
{'type': 'eq', 'fun': lambda x: x[1] + x[3] - 1000}
]
def f(a):
return a[0] + a[1] + a[2] + a[3]
print(minimize(f, X0, constraints=x,
bounds = bounds, options={'disp': False} ).x)
print(minimize(f, X0, constraints=y,
bounds = bounds, options={'disp': False} ).x)
This prints:
[724.07734394 500. 0. 500. ]
[500. 500. 500. 500.]
Actually, y gives the correct result. Why is x giving an incorrect result?
Answering my own question:
Do this:
import pandas as pd
import numpy as np
from scipy.optimize import minimize, Bounds
X0 = [0,0,0,0]
x = []
for i in range(2):
x.append({'type': 'eq', 'fun': lambda a, i=i: a[i] + a[split+i] - 1000})
y = [{'type': 'eq', 'fun': lambda x: x[0] + x[2] - 1000},
{'type': 'eq', 'fun': lambda x: x[1] + x[3] - 1000}
]
def f(a):
return a[0] + a[1] + a[2] + a[3]
print(minimize(f, X0, constraints=x,
bounds = bounds, options={'disp': False} ).x)
print(minimize(f, X0, constraints=y,
bounds = bounds, options={'disp': False} ).x)