0

我正在尝试在 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()
4

2 回答 2

0

您可能在模型中存在错误。我会尝试以下方法:

  • 构建具有所有功能的最小模型。也就是说,首先构建您可以构建的最小模型(一个玩具示例),以便生成的模型可以是人类可读的。有一个或两个学生,只有两个位置,等等。一个模型,您可以手动计算解决方案。
  • 使用Pulp函数,创建一个包含模型的 LP 文件。
  • 使用其他求解器运行模型以检查您是否获得相同的不可行结果(例如,如果您处于非商业项目中,请尝试 SCIP)。
  • 阅读 lp 文件以检查/查找模型中的错误。
于 2017-05-12T17:46:58.123 回答
-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()
于 2017-11-08T08:32:19.500 回答