1

Please help me to find out where is the mistake of the following code. I've seen that there is a thread with a similar problem, but the solution provided there does not solve my problem, unfortunately.

code:

A_ub=[[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -1], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, -1], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, -1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1]]

b_ub=[0, 0, 0, 0]

A_eq=[[0, -0.7092198581560284, 0.7092198581560284, 0, 0, 0, 0, 0, 0, 0, 0], [-1, -0.7092198581560284, -0.7092198581560284, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0.7092198581560284, 0, 1, 0, 1, 1, 0, 0, 0, 0], [0, 0.7092198581560284, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, -0.7092198581560284, 0, -1, -1, 0, 0, 1, 0, 0], [0, 0, 0.7092198581560284, 0, 0, 0, 0, 0, 0, 1, 0]]

b_eq=[0, 1, 0, 1, 0, 0, 0, 0]

c=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]

res = linprog(c, A_ub, b_ub,A_eq, b_eq)

print res

I obtain the following error:

fun: 2.0

message: 'Optimization failed. Unable to find a feasible starting point.'

nit: 5

status: 2

success: False

x: nan

Cleb
  • 25,102
  • 20
  • 116
  • 151
user3380098
  • 31
  • 2
  • 6
  • How did you check whether there exists a feasible solution? Did you check [this thread](http://stackoverflow.com/questions/29941958/scipy-optimize-linprog-unable-to-find-a-feasible-starting-point-despite-a-feasib)? – Cleb Feb 02 '17 at 16:25
  • I did check that thread and tried to apply the solution there provided, but with no luck. – user3380098 Feb 03 '17 at 19:34
  • I know there is a solution to the problem because Matlab gives me the solution. – user3380098 Feb 03 '17 at 19:35

1 Answers1

1

Your solution space is empty, no feasible point exists. Check your second equality constraint for instance: [-1, -0.7092198581560284, -0.7092198581560284, 0, 0, 0, 0, 0, 0, 0, 0] multiplied by the vector of decision variables shall equal 1.

Decision variables are assumed to be non negative by default and since no positive coefficient for that constraint exists it is easy to see one example why the model is infeasible. So -1 * x1 -0.7092198581560284 * x2 -0.7092198581560284 * x3 = 1 has no feasible solution if all x have to be greater or equal to 0.

The bounds on x will not solve your problem though. Even if x is real, the solution space is empty. My guess is that the equality constraints, which limit the solution space strongly, are contradictional. Depending on what you are trying to model, you will have to look at the entire program to be sure where the problem lies though.

Tristan
  • 1,576
  • 9
  • 12
  • But then I tried exactly with the same input in Matlab and also with linprog function and it gave the solution, so I can't understand why the problem is in the input. – user3380098 Feb 02 '17 at 17:00
  • 1
    There are three types of constraints: lower equal, greater equal and equality. Are you sure that MATLAB is also correctly interpreting the type of constraint? As I have shown in the example there can not be a solution to that equality under the given assumption of a non negative x. – Tristan Feb 02 '17 at 17:07
  • Yes. Both Matlab and python use upper bound equalities. The present model is a tiny one, so I also could check the solution of Matlab and makes complete sense. I would continue working with matlab, but I need to make it in python for incompatibility with a third software. – user3380098 Feb 02 '17 at 19:39
  • 1
    As I pointed out there can not be a solution to the presented problem. There must be some difference to the implementation in MATLAB. Carefully compare the implementation and you will find a difference if you obtain a solution in MATLAB. – Tristan Feb 02 '17 at 19:53
  • Still couldn't find why it is not providing the solution matlab gives me... – user3380098 Feb 03 '17 at 19:32
  • Could you supply a minimal mathematical notation of a problem that does work in MATLAB but not in script? Then I could probably tell you more. – Tristan Feb 03 '17 at 20:01
  • I'm not sure I understand what you mean. The input in matlab is exactly the same, just that it is written is matlab syntax (no comas between items etc.) The function is also exactly the same: linprog. – user3380098 Feb 03 '17 at 21:26
  • Matlab gives the following solution to the problem: 1.0000 -1.4142 -1.4142 0.3342 0.3342 0.6684 -0.0026 1.0000 0.0026 1.0000 1.0000 – user3380098 Feb 03 '17 at 21:28
  • In your first comment you mentioned that the second equality constrain has a problem. This is: [-1, -0.7092198581560284, -0.7092198581560284, 0, 0, 0, 0, 0, 0, 0, 0]. This equation equals 1. The solution that matlab gives says that 1*-1 + (-1.4*-0,7) +(-1.4*-0,7) = 1. This works well. – user3380098 Feb 03 '17 at 21:34
  • 1
    By convention of linear programming, the decision variables in scipy are assumed to be non negative by default. Your solution x = [1, -1.4, -1.4] shows that you allow non negative values. This solution would not be allowed in scipy with your code. To allow it as bounds=(None, None) as argument of linprog. This means x may be real. I think your equation system in your original post may still not have a solution though. – Tristan Feb 03 '17 at 21:42
  • I don't understand this very well. Does this mean I cannot have negative variables at all?? – user3380098 Feb 03 '17 at 21:44
  • 1
    By default the variables must not be negative. If you add bounds=(None, None) as argument of linprog they may be negative too. This means at least the one equation from my example has a feasible solution. I don't know if there is a feasible solution for the whole system though. – Tristan Feb 03 '17 at 21:46
  • Using bounds=(None, None) it gave me a solution finally!!! But not the same as Matlab... – user3380098 Feb 03 '17 at 21:47
  • 1
    There may be more than one optimal solution, actually there could be infinitely many. Does the solution have the same objective value? – Tristan Feb 03 '17 at 21:51
  • The solution is different but also a good one, and I'm happy with this one too :) – user3380098 Feb 03 '17 at 22:00
  • 1
    No problem I am happy I could help :) – Tristan Feb 03 '17 at 22:08