Attempting to solve a linear planning problem with scipy.optimization fails

Asked 2 years ago, Updated 2 years ago, 21 views

You are trying to solve the following constrained linear planning problems:

min A*x
s.t.
Ceq*x-Deq=0
Cineq*x-Dineq>=0

This time, we are solving the problem by converting it into an unrestricted problem using the fine law.
However, when I execute the following code, I get the error "Linear search failed" and the optimization fails. I tried L-BFGS-B and TNC, but both failed.Since it's a linear planning problem, I think I'm making some elementary mistakes because I should be able to solve it with a solver, but what's wrong?How can I optimize it?
I would like to use a nonlinear solver if possible because I am thinking of expanding to a nonlinear problem in the future.
Thank you for your cooperation.

import numpy as np
from scipy.optimize import minimize

# objective function: A*x
A = np.mat([0., 0., 0., 0., 0., 0., 0., 0., 0., -0.5, -0.5, 0., 0., 0., 0., 0., 0., 1.0, 1.0, 0.]);

# matrices for constraints
Crg = np.mat([ 0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.]);
Drg = np.mat([0.]).T;

Chm = np.mat([ 1.,1.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.]);
Dhm = np.mat([ 1.]).T;

Cex = np.mat([ 1.,1.,1.,1.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.]);
Dex = np.mat([ 1.]).T;

Cg = np.mat([[ 1.,1.,1.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0., 0.,0.],
 [ 0.,1.,1.,1.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0., 0.,0.]]);
Dg = np.mat([ 0.,1.]).T;

Cdm = np.mat([[ 1.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,-1.,0., 0.,0.],
 [ 0.,1.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,-1., 0.,0.],
 [ 0.,0.,1.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,-1.,0.],
 [ 0.,0.,0.,1.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0., 0.,-1.]]);
Ddm = np.mat([ 0.,0.,0.,0.]).T;

Cdr = np.mat([[ 0.,0.,0.,0.,0.,0.,0.,0.,-1.,0.,0.,0.,0.,0.,0.,0.,1.,0., 0.,0.],
 [ 0.,0.,0.,0.,0.,0.,0.,0.,0.,-1.,0.,0.,0.,0.,0.,0.,0.,1., 0.,0.],
 [ 0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,-1.,0.,0.,0.,0.,0.,0.,0., 1.,0.],
 [ 0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,-1.,0.,0.,0.,0.,0.,0., 0.,1.]]);
Ddr = np.mat([0.,0.,0.,0.]).T;

Cx0 = np.mat ([[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], 0.
 [ 0.,0.,0.,0.,0.,1.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0., 0.,0.]]);
Dx0 = np.mat([1.,0.]).T;

Cz0 = np.mat ([[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
 [ 0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,1.,0.,0.,0.,0., 0.,0.]]);
Dz0 = np.mat([1.,0.]).T;

Cnc = np.mat ([[0. , 0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
 [ 0.,0.,0.,0.,0.,0.,1.,1.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0., 0.,0.]]);
Dnc=np.mat([1.,1.]).T;

Cnp = np.mat ([[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
 [ 0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,1.,1.,0.,0., 0.,0.]]);
Dnp = np.mat([1.,1.]).T;

Cfc = np.mat ([[0. , 0. , 0. , 0. , 0. , 1. , 0. , - 1. , 0. , - 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ] ,
 [ 0.,0.,0.,0.,0.,1.,0.,-1.,0.,1.,-1.,0.,0.,0.,0.,0.,0.,0., 0.,0.]]);
Dfc=np.mat([0,0.]).T;

Cfp = np.mat ([[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , - 1. , 0. , 0. , 0. , 0. , - 1. , 1. , 1. , 0. ],
 [ 0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,1.,0.,-1.,0.,1.,-1.,0.]]);
Dfp = np.mat([0,0.]).T;

# equality constraints Cx-D = 0
Ceq = [Crg, Chm, Cex, Cdm, Cx0, Cz0, Cnc, Cnp, Cfc, Cfp];
Deq = [Drg, Dhm, Dex, Ddm, Dx0, Dz0, Dnc, Dnp, Dfc, Dfp ];

# inequality constraints Cx-D = > 0
Cineq = [Cg, Cdr];
Dineq = [Dg, Ddr];

bnd = ((0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,

x0 = [0. , 1. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ] ;
# xopt = [0. , 1. , 0. , 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ] ;

wpenalty=100;

defl(x):
  ret = (A*np.mat(x).T) [0,0];

  # penalty: lambda* | Cx-D |
  for i, Cin enumerate (Ceq):
    c=np.abs(C*np.mat(x).T-Deq[i]);
    lmd = np.mat(np.ones(len(Deq[i])))*wpenalty;
    ret+=(lmd*c)[0,0];
  for i,Cin enumerate (Cineq):
    c=C*np.mat(x).T-Dineq[i];
    j,k = np.where(c>0);
    c[j,k] = 0.0;
    c=np.abs(c);
    lmd = np.mat(np.ones(len(Dineq[i])))*wpenalty;
    ret+=(lmd*c)[0,0];
  print ret;
  return;

defdl(x):
  ret = A.A.flatten();

  # differences of patents —lambda*C*sign(x)
  for i, Cin enumerate (Ceq):
    lmd = np.mat(np.ones(len(Deq[i])))*wpenalty;
    ret+=(lmd*C).A.flatten()*np.sign(x);
  for i,Cin enumerate (Cineq):
    lmd = np.ones(len(Dineq[i]))*wpenalty;
    c=(C*np.mat(x).T-Dineq[i]).T.A.flatten();
    lmd [np.where(c>0)] = 0.0;
    ret+=(np.mat(lmd)*C).A.flatten()*np.sign(x);
  return;

#res=minimize(l, x0, method='L-BFGS-B', jac=dl, bound=bnd, options={'disp':True});
res=minimize(l, x0, method='TNC', jac=dl, bound=bnd, options={'disp':True});
print res.fun;# should be 0.5
print res.x;# should be equal to xopt

python

2022-09-30 14:45

1 Answers

I solved myself.
When I used matlab's optimization function, the input was exactly the same, but it worked.
Therefore, using scipy.optimize is not recommended.


2022-09-30 14:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.