0

I need to optimize the objective function (minimize).

The sum of x1+x2+x3 = 1 (This is my unique constranint). Following this, the bounds of the x1,x2,x3 has to be between (+0,<1). Positive values, but less than 1.

I dont have any initial guess. In this example I copy the answear of the problem that I made it on paper. But the scipy answer it's not the same, so it's not right.

I tried using scipy and puLP. But with neither of both I could solve it. I don't know if I am using the perfect tool for this problem.

Thanks you.

from scipy.optimize import minimize
import numpy as np

def objective(x):
    x1=x[0]
    x2=x[1]
    x3=x[2]
    return 0.3442 * x1 **2 + 0.3814 * x2 + 0.2901 **2 + 2*(-0.0116)* x1 *x2+ 2*(-0.30373)* x2 * x3 +2*(0.1883)* x1 *x3

def equality(x):
    x1=x[0]
    x2=x[1]
    x3=x[2]
    return x1+x2+x3-1

cons = {'type':'eq', 'fun': equality}

x0=[0.1944,0.4056,0.4000]   #the correct answear for the values of x1, x2, x3

bounds=[[0,1],[0,1],[0,1]]

print(minimize(objective, x0, constraints=cons, method='SLSQP'))
FBruzzesi
  • 6,385
  • 3
  • 15
  • 37
  • Adding `bounds` parameter works for me. Also try different initial values. – FBruzzesi May 12 '20 at 07:17
  • @FBruzzesi but i put bounds in this exercise. Ando how do i know which numbers put on initiql guess? – searchandprint May 12 '20 at 12:34
  • 1
    This is in general non-convex. You won't be able to formulate it in pulp (lp = convex) and scipy's optimizers (at least most of them) will guarantee (at least most of them) a local-optimum, but not a global-optimum (and the others do guarantee even less). Global-optimization is much harder and software is less accessible, e.g. CoinOR Couenne. So either grab some global-opt solver (as i said: much more hard to to use) or try to improve your solution by randomized / grid-search based multi-start picking the best after (start multiple times with different start-vectors), like most kmeans impl do – sascha May 12 '20 at 13:25
  • @searchandprint you defined the bounds variable, but it is not in the minimize function – FBruzzesi May 12 '20 at 14:32
  • @FBruzzesi and how it is? Could you show me? – searchandprint May 12 '20 at 14:44
  • 1
    `minimize(objective, x0, constraints=cons, bounds=bounds, method='SLSQP')` This is giving me different results for different initial x0, since as sascha mentioned there are various local optima. – FBruzzesi May 12 '20 at 14:46

0 Answers0