我正在尝试使用python中的COIN-OR来解决线性编程问题。 我已完成所有工作,但似乎有一个我无法识别的错误。 很多时候,根据我试图强制施加的约束,解决方案可能不可行或不正确。 值,二进制变量也是不正确的,即~0.1 ^ 10或~0.999 .....
请帮我找出错误或尝试指导解决问题。
我正在附上
Model1主类接受输入并创建一个新的输出文件,然后PanelTwo类方法构建Distance矩阵和Njg矩阵。
Model1类的构造函数方法中似乎存在错误,该类构建了LP并解决了LP
声明变量和约束的代码是:
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()
答案 0 :(得分:0)
你可能在模型中有bug。我会尝试以下方法:
Pulp
功能,创建包含模型的LP文件。答案 1 :(得分:-1)
import wx
import wx.xrc
class WelcomeScreen(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=u"Stock Cutting", pos=wx.DefaultPosition,
size=wx.Size(448, 395), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
self.Bind(wx.EVT_CLOSE, self.OnClose)
# prevent resizing of the window
self.SetSizeHintsSz(wx.Size(448, 394), wx.Size(448, 395))
bSizer1 = wx.BoxSizer(wx.VERTICAL)
#create image object using the jpg file stored in the root directory
self.m_bitmap1 = wx.StaticBitmap(self, wx.ID_ANY, wx.Bitmap(u"test.jpg", wx.BITMAP_TYPE_ANY),
wx.DefaultPosition, wx.DefaultSize, 0)
bSizer1.Add(self.m_bitmap1, 0, wx.ALL, 5)
self.m_button1 = wx.Button(self, wx.ID_ANY, u"START", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_button1.SetFont(wx.Font(15, 71, 90, 92, False, wx.EmptyString))
self.m_button1.Bind(wx.EVT_BUTTON, self.next, self.m_button1)
bSizer1.Add(self.m_button1, 1, wx.ALL | wx.EXPAND, 5)
self.SetSizer(bSizer1)
self.Layout()
self.Centre(wx.BOTH)
self.Show(True)
def OnClose(self, event):
dlg = wx.MessageDialog(self,
"Do you really want to close this application?",
"Confirm Exit", wx.OK | wx.CANCEL | wx.ICON_QUESTION)
result = dlg.ShowModal()
dlg.Destroy()
if result == wx.ID_OK:
self.Destroy()
# function to go to the next screen to enter the data
def next(self, event):
self.Show(False)
# start = home.HomeScreen(None, prob)
# main method to start the application
if __name__ == "__main__":
app = wx.App()
WelcomeScreen = WelcomeScreen(None)
app.MainLoop()