1
votes

Using pulp and python I am trying to solve a water balance(similar to the classic transportation problem) linear programming problem in the form of:

minimize c'x subject to:

Ax=b

lb<=x<=ub

where A is a (10x18) matrix, c(1x18) and b(10x1) are vectors and U (18x2) is a two-column matrix that includes lb and ub respectively.

import pulp as p
import pandas as pd
import numpy as np

A=pd.read_csv(r"C:\...\A.csv",delimiter=',',dtype='int8',header=None)
A=A.to_numpy()

c=pd.read_csv(r"C:\...\c.csv",delimiter=',',dtype='float64',header=None)
c=c.to_numpy()

U=pd.read_csv(r"C:\...\U.csv",delimiter=',',dtype='float64',header=None)

b=pd.read_csv(r"C:\...\b.csv",delimiter=',',dtype='float64',header=None)
b=b.to_numpy()

# Create a LP Minimization problem 
Lp_prob = p.LpProblem('Problem', p.LpMinimize)  

# Create problem Variables  
x = p.LpVariable("x", lowBound = U[0], upBound = U[1])   # Create a variable x

# Objective Function
c=np.transpose(c)
Lp_prob+= p.lpSum(c*x)

# Constraints: 
Lp_prob += p.lpSum(A*x) == b

# Display the problem 
print(Lp_prob) 

status = Lp_prob.solve()   # Solver 
print(p.LpStatus[status])   # The solution status 

# Printing the final solution 
print(p.value(x), p.value(Lp_prob.objective)) 

Question When I try to run the project, it displays the message

"File "C:\Users\stavroula\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1555, in nonzero self.class.name

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

Why does it happen?

The problem can be solved successfully using the scipy.optimize.linprog() routine.

Thanks in advance!!

1

1 Answers

0
votes

PuLP does not use matrix notation. So A*x becomes something like (in pseudo-code notation):

sum_j A[i,j]*x[j]

If you want to use matrix notation, have a look at CVXPY.