简体   繁体   English

如何在 Python 的纸浆环境中引入变量

[英]How to introduce a variable in pulp environment of Python

I have two sets.我有两套。

  • S = Sources = {S1, S2, S3} S = 来源 = {S1, S2, S3}
  • D = Desinations = {D1, D2, D3} D = 目的地 = {D1, D2, D3}

I have to minimize the total transportation cost subject to some constraints.我必须在某些限制条件下最小化总运输成本。 I am using the pulp in Python.我正在使用 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 .条件是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.您应该制作具有合法路由并使用它的 SXD 变量的子集。 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实际上,您可能对每条路线都有成本,所以我会将所有这些都保存在字典中并将字典键发送到 LpVariable.dicts 调用

The rest should look similar to the example I noted. rest 应该与我提到的示例相似。 If you are stuck, there are several other examples on this site if you search 'pulp transportation model' or 'pulp sparse constraints' & such.如果您遇到困难,如果您搜索“纸浆运输模型”或“纸浆稀疏约束”等,本网站上还有其他几个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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