我正在尝试在 python 中使用 COIN-OR 解决线性规划问题。我已经完成了所有工作,但似乎有一个我无法识别的错误。很多时候,根据我试图强制的约束,解决方案要么不可行要么不正确。二进制变量的值也不正确,即〜0.1 ^ 10或〜0.999.....
请帮助我找到错误或尝试指导解决问题。
我附上
- zip 文件(代码 + 来自 excel 的输入数据文件)
- 单词文档。对于数学公式
Model1 主类接受输入并创建一个新的输出文件,然后 PanelTwo 类方法构建距离矩阵和 Njg 矩阵。
构建LP并解决LP的Model1类的构造方法似乎有错误
声明变量和约束的代码是:
M = 100000 * prob.schoolNum
sModel = []
for i in range(prob.schoolNum):
sModel.append(i)
gModel = []
for i in xrange(prob.gradeNum):
gModel.append(i)
Beta = []
for i in xrange(prob.schoolNum):
temp = 0
for j in xrange(prob.gradeNum):
temp = temp + prob.Njg[i][j]
if temp < prob.Nmax:
Beta.append(0)
else:
Beta.append(1)
# x = students of grade g transfer from school i to j
x = LpVariable.matrix("x_igj_", (sModel, gModel, sModel), 0, 1, LpBinary)
y = LpVariable.matrix("status of school", (sModel), 0, 1, LpBinary)
# o = resulting students in grade in school
o = LpVariable.matrix("o", (sModel, gModel), 0, None, LpInteger)
# oHelper = summation of o for all g rades
oHelper = LpVariable.matrix("oH", (gModel), 0, None, LpInteger)
#Njg_Helper = Total students in a particular school
Njg_helper = LpVariable.matrix("NH", (sModel), 0, None, LpInteger)
formulation = LpProblem("School Consolidation Model", LpMinimize)
formulation += lpSum(((prob.Njg[i][g] * x[i][g][j] for j in sModel) for g in gModel) for i in sModel)
for i in sModel:
for j in sModel:
for g in gModel:
formulation += x[i][g][j] * prob.D[i][j] <= prob.d1
for i in sModel:
for j in sModel:
for g in gModel:
formulation += x[i][g][j] <= y[j]
for i in sModel:
for g in gModel:
formulation += lpSum(x[i][g][j] for j in sModel) <= 1 - y[i]
for j in sModel:
formulation += ((lpSum(prob.Njg[j][g] for g in gModel) - prob.Nmax) * (1 - y[j])) <= 0
for i in sModel:
for j in sModel:
if i != j and Beta[i] * Beta[j] != 1:
formulation += (prob.D[i][j] - prob.d2) >= (y[i] + y[j] - 2) * M
for g in gModel:
formulation += lpSum(o[j][g] for j in sModel) == oHelper[g]
formulation += lpSum(prob.Njg[i][g] for i in sModel) == oHelper[g]
for j1 in sModel:
formulation += lpSum(prob.Njg[i1][g]*x[i1][g][j1] for i1 in sModel) == o[j1][g]-prob.Njg[j1][g]*y[j1]
formulation.solve()