简体   繁体   中英

How to introduce a variable in pulp environment of Python

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.

You should make a subset of your SXD 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM