Reputation: 1512
as an experiment, I'd like to minimize the following objective function:
The parameters w1 and w2 are bounded by the lambdas:
The constraints are the following:
I haven't found a decent Scipy example on how to optimize a multivariate case such as this. If someone could lend a guidance on this problem, I appreciate it.
Upvotes: 2
Views: 1328
Reputation: 33532
Code:
import numpy as np
from scipy.optimize import minimize
def fun(x):
return np.sum(x[2:])
x0 = np.zeros(4) # lambda1, lambda 2, w1, w2
cons = ({'type': 'ineq', 'fun': lambda x: x[2] - 2 + 10 * x[0] + 3 * x[1]},
{'type': 'ineq', 'fun': lambda x: x[3] - 2 + 5 * x[0] + 5 * x[1]},
{'type': 'ineq', 'fun': lambda x: x[2]},
{'type': 'ineq', 'fun': lambda x: x[3]},
{'type': 'eq', 'fun': lambda x: x[0] + x[1] - 1})
res = minimize(fun, x0, constraints=cons)
print(res)
print(np.round(res.x, 2))
Output:
fun: -3.3306690738754696e-16
jac: array([ 0., 0., 1., 1.])
message: 'Optimization terminated successfully.'
nfev: 7
nit: 1
njev: 1
status: 0
success: True
x: array([ 5.00000000e-01, 5.00000000e-01, -3.33066907e-16,
0.00000000e+00])
[ 0.5 0.5 -0. 0. ]
This is basically only using information from the official docs.
Edit I used the general optimization functions here, but you probably should use scipy.optimize.linprog as this is an LP!
I did not check it, but linprog-usage looks somewhat like:
from scipy.optimize import linprog
c = [0, 0, 1, 1]
A = [[-10, -3, -1, 0], [-5, -5, 0, -1]]
b = [-2, -2]
A_eq = [[1, 1, 0, 0]]
b_eq = [1]
x0_bnds = (-np.inf, -np.inf, 0, 0)
x1_bnds = (np.inf, np.inf, np.inf, np.inf)
res = linprog(c, A, b, A_eq, b_eq, bounds=list(zip(x0_bnds, x1_bnds)))
print(res)
Output:
fun: -0.0
message: 'Optimization terminated successfully.'
nit: 4
slack: array([ 0., 3.])
status: 0
success: True
x: array([-0.14285714, 1.14285714, 0. , 0. ])
Upvotes: 3