python - Solving constrained maximization with SciPy -
function maximize:
x[0] + x[1] + x[2]
constraints:
0.2 * x[0] + 0.4 * x[1] - 0.33 * x[2] <= 25 5 * x[0] + 8.33 * x[2] <= 130 ... x[0] >= 0 x[1] >= 0 x[2] >= 0
my code looks like:
from numpy import * scipy.optimize import minimize cons = ({'type': 'ineq', 'fun': lambda x: array([25 - 0.2 * x[0] - 0.4 * x[1] - 0.33 * x[2]])}, {'type': 'ineq', 'fun': lambda x: array([130 - 5 * x[0] - 8.33 * x[2]])}, {'type': 'ineq', 'fun': lambda x: array([16 - 0.6 * x[1] - 0.33 * x[2]])}, {'type': 'ineq', 'fun': lambda x: array([7 - 0.2 * x[0] - 0.1 * x[1] - 0.33 * x[2]])}, {'type': 'ineq', 'fun': lambda x: array([14 - 0.5 * x[1]])}, {'type': 'ineq', 'fun': lambda x: array([x[0]])}, {'type': 'ineq', 'fun': lambda x: array([x[1]])}, {'type': 'ineq', 'fun': lambda x: array([x[2]])}) f = lambda x: -1 * (x[0] + x[1] + x[2]) res = minimize(f, [0, 0, 0], method='slsqp', constraints=cons, options={'disp': true}) print(res)
unfortunately, result got is:
positive directional derivative linesearch (exit mode 8) current function value: -18240083542.4 iterations: 20 function evaluations: 180 gradient evaluations: 16 x: array([ 6.05846118e+09, 6.05846118e+09, 6.12316118e+09]) jac: array([ 0., 0., 0., 0.]) message: 'positive directional derivative linesearch' fun: -18240083542.377449 status: 8 njev: 16 nfev: 180 nit: 20 success: false
i can solve problem in excel solver, guess wrong in python.
bug in scipy, code works under python2, refuses work under python3
issue on github: https://github.com/scipy/scipy/issues/4240
Comments
Post a Comment