Search code examples
linear-programming

Python - Error using linprog ("Invalid input for linprog: provide a 3 x 2 array for bounds, not a 2 x 3 array")


I am trying to use the linprog in python to solve this problem:

# Minimize = (0.035*x1) + (0.015*x2) + (0.025*x3)
# x1+x2+x3=1.2
# 0<=x1<=0.7
# 0<=x2<=0.3
# 0<=x3<=0.5

c = [0.035, 0.015, 0.025] #objective function
A_eq = [[1, 1, 1]]
b = [1.2]
lb = (0, 0, 0)
up = (0.7, 0.3, 0.5)
from scipy.optimize import linprog
linprog(c, A_ub=None, b_ub=None, A_eq=A_eq, b_eq=b, bounds=[lb,up], method='interior-point', callback=None, options=None, x0=None)

However I am getting an error could you help me with that? thanks a lot!


Solution

  • You sholud define correctly the bounds for each variable in the same order as the coefficients. In this case, they’re between zero and some number:

    # Minimize = (0.035*x1) + (0.015*x2) + (0.025*x3)
    # x1+x2+x3=1.2
    # 0<=x1<=0.7
    # 0<=x2<=0.3
    # 0<=x3<=0.5
    
    c = [0.035, 0.015, 0.025] #objective function
    A_eq = [[1, 1, 1]]
    b = [1.2]
    x1_b = (0, 0.7)
    x2_b = (0, 0.3)
    x3_b = (0, 0.5)
    
    from scipy.optimize import linprog
    linprog(c, A_ub=None, b_ub=None, A_eq=A_eq, b_eq=b, bounds=[x1_b, x2_b,x3_b], method='interior-point', callback=None, options=None, x0=None)