Acad
Acad

Reputation: 161

SciPy Optimization using SLSQP gives solution that does not satisfy constraints

I'm trying to minimize the function B1=0.00299540627439527*x[0]**2 + 0.00701825276534463*x[0]*x[1] + 0.0672877782113971*x[0] + 0.00456646480250456*x[1]**2 + 0.054080834634827*x[1] + 0.298938755491431 over the state space of x1=[-10,10] and x2=[-10,10] given the following inequality constriant EqX0=[[(x[0]+5)**2+x[1]**2-2.5]]<=0 where x[0] and x[1] have been symbolically defined as x1 and x2

ga=1 is a parameter

However, when I use SLSQP to solve the non linear constrained optimization problem, answer leads to x=[-10,10] which does not satisfy the inequality constraint.

Here is the piece of code:

def Objective2(ax):
    B2=B1.copy()
    B2=B2.subs((x[i],ax[i]) for i in range(len(ax)))
    return ga-B2

def Constraint1(ax):
    EqX0c=EqX0.copy()
    cc=[]
    for pp in range(len(EqX0c)):
        cc.append(-EqX0c[0][pp].subs((x[i],ax[i]) for i in range(len(ax))))
    return cc

con1= {'type': 'ineq','fun': Constraint1}
bounds=Bounds([-10,10],[-10,10])
sol2=minimize(Objective2,x0,method="SLSQP",bounds=bounds,constraints=con1)

This is the output produced. Program terminates successfully but gives the wrong results.

fun: -0.12123115088124994 jac: array([-0.07756218, -0.0752276 ]) message: 'Optimization terminated successfully.' nfev: 4 nit: 5 njev: 1 status: 0 success: True x: array([-10., 10.])

Any idea why this is happening and how I can tackle it?

Upvotes: 0

Views: 621

Answers (1)

Mario
Mario

Reputation: 26

I think you have wrongly defined Bounds. Try Bounds([-10,-10],[10,10]).

See the docs.

Upvotes: 1

Related Questions