使用PuLP

时间:2018-04-25 20:05:09

标签: python-2.7 recursion pulp

我目前正在构建一个代码,可以将现有的混合整数线性编程画面矩阵转换为PuLP兼容模型。虽然此程序在一台计算机上运行,​​但由于另一台计算机上的最大递归深度错误,它一再失败。两台计算机使用的最大递归深度为1000.此代码复制的模型并不大,因为它是一个具有大约250个变量和90个约束的测试模型。

代码生成x变量和模型的目标函数,但在用于生成空约束的代码块的第一次迭代期间失败。模型的约束不返回值

 # Import PuLP modeler functions
from pulp import *

# base model
model = LpProblem("MASTRS_LP",LpMinimize)

# shape of existing tableau matrix
[m,n]=np.shape(A)

# positive X variables
xVar = {}
for i in range(1,n+1):
    if i-1 in e[:,1]:      # if i is in array holding indexes of integer variables
        xVar[i] = LpVariable(name='xNodeI_%s' % (i),lowBound=0, upBound=1,cat=LpInteger) #Integers
    else:
        xVar[i] = LpVariable(name='xRoute_%s' % (i),lowBound=0, upBound=None,cat=LpContinuous) # Routes

# Objective Function
model += lpSum([0]), "Sum of Total Supply Chain"
for i in range(1,n):
    model.objective.addterm(xVar[i+1], c[0,i])

# Constraints
for i in range(0,m):
    # Create empty constraint 
    if s[i]==1:
        model += lpSum([0]) <= b[i]
    if s[i]==0:
        model += lpSum([0]) == b[i]
    if s[i]==-1:
        model += lpSum([0]) >= b[i]

    # add variables to constraint
    for j in range(0,n):
        if A[i,j]!=0:
            model.constraints['_C'+str(i+1)].addterm(xVar[j+1], A[i,j])  

# delete existing transshipment variables
del A,b,s,c

# solve transshipment model
status = model.solve()

错误的第一行重复多次,

错误如下:

File "C:\Python27\ArcGIS10.3\lib\site-packages\pulp\pulp.py", line 735, in subInPlace
    self.subInPlace(e)
  File "C:\Python27\ArcGIS10.3\lib\site-packages\pulp\pulp.py", line 735, in subInPlace
    self.subInPlace(e)
  File "C:\Python27\ArcGIS10.3\lib\site-packages\pulp\pulp.py", line 733, in subInPlace
    or isinstance(other, Iterable)):
  File "C:\Python27\ArcGIS10.3\lib\abc.py", line 132, in __instancecheck__
    if subclass is not None and subclass in cls._abc_cache:
  File "C:\Python27\ArcGIS10.3\lib\_weakrefset.py", line 75, in __contains__
    return wr in self.data
RuntimeError: maximum recursion depth exceeded in cmp

1 个答案:

答案 0 :(得分:0)

如果我是你,我会改变生成约束和目标的方式

# Constraints
for i in range(0,m):
   # Create constraint lhs
   lhs = lpSum([A[i,j] * xVar[j+1] for j in range(0,n) if A[i,j]])
   if s[i]==1:
       model += lhs <= b[i]
   if s[i]==0:
       model += lhs == b[i]
   if s[i]==-1:
       model += lhs >= b[i]