纸浆不产生变量的二进制值

时间:2019-05-15 07:54:19

标签: python pulp

我是纸浆新手,正在尝试建立一个线性程序。有许多变量是二进制的,并按照以下示例进行设置:

编辑:更完整的代码 谢谢kabdulla,更完整的代码如下:

 # Problem creation
    prob = LpProblem("scheduling", LpMaximize)

    varX = {str(i) + "-" + str(j): LpVariable("X_" + str(i) + "-" + str(j), cat=LpBinary) for i in
            range(numProjects) for j in range(numTimePeriods)} #project finish
    varY = {str(i) + "-" + str(j): LpVariable("Y_" + str(i) + "-" + str(j), cat=LpBinary) for i in
            range(numProjects) for j in range(numTimePeriods)} #project is in progress
    varZ = {str(i) + "-" + str(k) : LpVariable("Z_" + str(i) + "-" + str(k), cat=LpBinary) for i in range(numProjects) for k in range(numSuperPeriods)}
    varW = {str(i) + "-" + str(q) + "-" + str(j) : LpVariable("W_" + str(i) + "-" + str(q) + "-" + str(j) ,0,1, cat=LpBinary) for i in range(numProjects) for q in range(self.projects[i].duration) for j in range(numTimePeriods)}
    varV = {str(i) : LpVariable("V_" + str(i),0,1, cat=LpBinary) for i in range(numProjects)} #project is selected

  # Objective Function

  prob += sum([varV[str(i)] * self.projects[i].getBenefitPoints()  for i in range(numProjects)]), "Maximize the benefit points"

 #Constraints

    for i in range(numProjects):
        for j in range(numTimePeriods):
           # prob += varY[str(i) + "-" + str(j)] == varY[str(i) + "-" + str(j)], "Const: Must binary pgt " + str(i) + " at time period " + str(j)
           #  prob += pow([varY[str(i) + "-" + str(j)] for j in range(numTimePeriods)],1) == 1, "Const: Must binary pgt " + str(
           #      i) + " at time period " + str(j)
            prob += len([varY[str(i) + "-" + str(j)] for j in range(numTimePeriods)])==self.projects[i].duration, "Const: Must binary pgt " + str(i) + " at time period " + str(j)
      #various other constraints....

  prob.solve()

但是,在运行模型时,结果通常是varY的值为十进制,如输出示例所示:

 Y values:  [('0-0', 0.71428571), ('0-1', 0.71428571), ('0-2', 0.71428571), ('0-3', 0.71428571), ('0-4', 0.71428571), ('0-5', 0.71428571), ('0-6', 0.71428571),.....

我尝试了不同的方法来输入约束,但是它似乎从未可靠地产生二进制变量。任何帮助,不胜感激!

0 个答案:

没有答案