I have an optimization problem where I'm trying to find an array that needs to optimize two functions simultaneously.
In the minimal example below I have two known arrays w
and x
and an unknown array y
. I initialize array y
to contains only 1s.
I then specify function np.sqrt(np.sum((x-np.array)**2)
and want to find the array y
where
np.sqrt(np.sum((x-y)**2)
approaches 5
np.sqrt(np.sum((w-y)**2)
approaches 8
The code below can be used to successfully optimize y
with respect to a single array, but I would like to find that the solution that optimizes y
with respect to both x
and y
simultaneously, but am unsure how to specify the two constraints.
y
should only consist of values greater than 0.
Any ideas on how to go about this ?
w = np.array([6, 3, 1, 0, 2])
x = np.array([3, 4, 5, 6, 7])
y = np.array([1, 1, 1, 1, 1])
def func(x, y):
z = np.sqrt(np.sum((x-y)**2)) - 5
return np.zeros(x.shape[0],) + z
r = opt.root(func, x0=y, method='hybr')
print(r.x)
# array([1.97522498 3.47287981 5.1943792 2.10120135 4.09593969])
print(np.sqrt(np.sum((x-r.x)**2)))
# 5.0
One option is to use
scipy.optimize.minimize
instead ofroot
, Here you have multiple solver options and some of them (ieSLSQP
) allow you to specify multiple constraints. Note that I changed the variable names so thatx
is the array you want to optimise andy
andz
define the constraints.This is a minimizer so besides the constraints it also wants a function to minimize, I chose just the norm of
y
, but setting the function to a constant (ie lambda x: 1) would have also worked. Note also that the constraints are not exactly fulfilled, you can increase the accuracy by setting optional argumentftol
to a smaller value ie1e-10
. For more information see also the documentation and the corresponding sections for each solver.