Using linprog to find a solution to the following linear programming problem

Viewed 69

Using linprog to find a solution to the following linear programming problem:

f(x)=−34x1+20x2−12x3+6x4→MAXg,
G={14x1−8x2−x3+9x4+x5=0,
   12x1−12x2−12x3+3x4+x6=0,
   x3+3x4+x7=0}

The error is as follows(Value error: Invalid input for linprog: A_eq must have exactly two dimensions, and the number of columns in A_eq must be equal to the size of c).Help me find out what could be the error?

import numpy as np
from scipy.optimize import linprog
c = np.array([0.75,-20.0,0.5,-6.0,0.0,0.0])
A_eq = np.array([[0.25,-8.0,-1.0,9.0,1.0,0.0,0.0],
                 [0.5,-12.0,-0.5,3.0,0.0,1.0,0.0],
                 [0.0,0.0,0.0,3.0,0.0,0.0,1.0]])
b_eq = np.array([0.0, 0.0,0.0])
x1_bounds = (0, None)
x2_bounds = (0, None)
x3_bounds = (0, None)
x4_bounds = (0, None)
x5_bounds = (0, None)
x6_bounds = (0, None)
x7_bounds = (0, None)
bounds = [x1_bounds, x2_bounds,x3_bounds,x4_bounds,x5_bounds,x6_bounds,x7_bounds]
result = linprog(c,A_eq=A_eq,b_eq=b_eq,bounds=bounds,  method='revised simplex')#Value error in this string
print(result)
1 Answers

The variables x1-x7 in c and A_eq are defined just by their index inside the arrays, that's why all array sizes and orders need to be consistent. Your c is missing an element. Do this instead:

#                  x1     x2    x3   x4   x5   x6   x7
c = np.array([    0.75,-20.0,  0.5,-6.0, 0.0, 0.0, 0.0])
A_eq = np.array([[0.25, -8.0, -1.0, 9.0, 1.0, 0.0, 0.0],
                 [0.5, -12.0, -0.5, 3.0, 0.0, 1.0, 0.0],
                 [0.0,   0.0,  0.0, 3.0, 0.0, 0.0, 1.0]])

Then you get an "unbounded problem" issue, but that's a different problem. Presumably that's because you want to maximize, while linprog minimizes.

Related