0

I have two sets.

  • S = Sources = {S1, S2, S3}
  • D = Desinations = {D1, D2, D3}

I have to minimize the total transportation cost subject to some constraints. I am using the pulp in Python. How can I introduce a variable such that I am allowing some specific route?

The condition is if cost $(S_i, D_j) >=$ 250 then 0 else 1.

allowed_route = []
for i in range(len(matrix)):

for j in matrix[i]:
    if j >= 250:
        allowed_route.append(0)
    else:
        allowed_route.append(1)
 np_array=np.asarray(allowed_route)
 allowed_route = np_array.reshape(6, 4) 
 allowed_route = np.array(P_allowed_PLF_cap).tolist()

In this way, I have defined the parameter but I am unable to introduce the variable.

2
  • You are describing a transportation model. It isn't clear whether you are stumped on how to declare pulp variables or on how to set up the problem as an LP. Regardless, here is a complete example: coin-or.org/PuLP/CaseStudies/a_transportation_problem.html Commented Jul 27, 2020 at 14:53
  • I am asking that how to declare a pulp variable where there are some conditions on the parameter. I know how to set up the problem as an LP. Thank you. Commented Jul 27, 2020 at 16:09

1 Answer 1

0

You should make a subset of your S X D variables that have the legal routes and use that. Here is an example:

In [4]: from pulp import *                                                      

In [5]: sources = {'S1', 'S2', 'S3'}                                            

In [6]: destinations = {'D1', 'D2', 'D3'}                                       

In [7]: legal_routes = ( ('S1', 'D2'), 
   ...:                  ('S2', 'D1') )                                         

In [8]: # note:  above is obviously infeasible...just shows idea...             

In [9]: route_select = LpVariable.dicts('route', legal_routes, 0, 1, LpBinary)

In reality, you probably have costs for each of those routes, so I would hold all of that in a dictionary and send the dictionary keys to the LpVariable.dicts call

The rest should look similar to the example I noted. If you are stuck, there are several other examples on this site if you search 'pulp transportation model' or 'pulp sparse constraints' & such.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.