使用cplex(python)解決限制背包(01背包)問題

問題描述:

對于01背包問題,簡歷數(shù)學(xué)模型,使用python的cplex模塊解決問題,找到最大解。
代碼實現(xiàn):

# -*- coding: utf-8 -*-
# The MIP problem solved in this example is:
#(2,6),(2,3),(6,5),(5,4),(4,6),::(2,1),(3,3),(6,9),(5,7),(4,5),::(2,2),(2,4.5),(6,7.5),(5.5,4。5),(7.4,8.6)
#   Maximize  6x1 + 3 x2 + 5 x3 + 4 x4 +6 x5+ x6 +3x7 + 9 x8+7 x9+5 x10+ 2 x11 +4.5 x12+7.5 x13 + 4.5x14 +8.6x15= 價值最大化
#   Subject to
#      (2,6),(2,3),(6,5),(5,4),(4,6),::(2,1),(3,3),(6,9),(5,7),(4,5),::(2,2),(2,4.5),(6,7.5),(5.5,4。5),(7.4,8.6)      重量小于背包的盛重量
#       2x1 + 2 x2 + 6 x3 + 5 x4 +4 x5+ 2 x6 +3x7 + 6 x8+7 x9+5 x10+ 2 x11 +2 x12+6 x13 + 5.5 x14 +7.4x15
#      -x2+ x5<=0                2號背包必須在五號背包之前放入
#   Bounds             對于各個背包要么取要么不取0 1二值問題

#        0 <= x1 <= 1
#        0 <= x2 <= 1
#        0 <= x3 <= 1
#        0 <= x4 <= 1
#        0 <= x5 <= 1
#        0 <= x6 <= 1
#        0 <= x7 <= 1
#        0 <= x8 <= 1
#        0 <= x9 <= 1
#        0 <= x10 <= 1
#        0 <= x11 <= 1
#        0 <= x12 <= 1
#        0 <= x13 <= 1
#        0 <= x14 <= 1
#        0 <= x15 <= 1
#   Integers
#       x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15

import cplex
from cplex.exceptions import CplexError
# (2,6),(2,3),(6,5),(5,4),(4,6),::(2,1),(3,3),(6,9),(5,7),(4,5),::(2,2),(2,4.5),(6,7.5),(5.5,4。5),(7.4,8.6)
# data common to all populateby functions
my_obj = [6.0, 3.0, 5.0, 4.0, 6.0, 1.0, 3.0, 9.0, 7.0, 5.0, 2.0, 4.5, 7.5, 4.5, 8.6]
my_ub = [1.0, 1.0, 1.0, 1.0, 1.0,1.0, 1.0, 1.0, 1.0, 1.0,1.0, 1.0, 1.0, 1.0, 1.0]
my_lb = [0.0, 0.0, 0.0, 0.0, 0.0,0.0, 0.0, 0.0, 0.0, 0.0,0.0, 0.0, 0.0, 0.0, 0.0]
my_ctype = "IIIIIIIIIIIIIII"     #表示參數(shù)的類型 c應(yīng)該表示數(shù)值,I表示整數(shù)
my_colnames = ["x1", "x2", "x3", "x4", "x5","x6", "x7", "x8", "x9", "x10","x11", "x12", "x13", "x14", "x15"]  #表示實驗的
my_rhs = [40,0,0]
my_rownames = ["r1", "r2","r3"]
my_sense = 'LLL'


def populatebyrow(prob):
  prob.objective.set_sense(prob.objective.sense.maximize)
  # 指要優(yōu)化的目標(biāo)函數(shù)是要求最大化
  prob.variables.add(obj=my_obj, lb=my_lb, ub=my_ub, types=my_ctype,
                     names=my_colnames)

  rows = [[["x1", "x2", "x3", "x4", "x5","x6", "x7", "x8", "x9", "x10","x11", "x12", "x13", "x14", "x15"], [2.0, 2.0, 6.0, 5.0, 4.0,2.0, 3.0, 6.0, 5.0, 4.0,2.0,2.0,6.0,5.5,7.4 ]],
          [["x2","x5"],[-1,1]],
          [["x7","x12"],[-1, 1]]
          ]
  # 上面是背包問題所要求的,后面是所要附加的約束條件,即要求2號背包必須在五號背包之前放入
  prob.linear_constraints.add(lin_expr=rows, senses=my_sense,
                              rhs=my_rhs, names=my_rownames)


try:
  my_prob = cplex.Cplex()
  handle = populatebyrow(my_prob)
  my_prob.solve()

except CplexError as exc:
  print(exc)

print()
# solution.get_status() returns an integer code
print("Solution status = ", my_prob.solution.get_status(), ":", end=' ')
# the following line prints the corresponding string
print(my_prob.solution.status[my_prob.solution.get_status()])
print("Solution value  = ", my_prob.solution.get_objective_value())

numcols = my_prob.variables.get_num()
numrows = my_prob.linear_constraints.get_num()

slack = my_prob.solution.get_linear_slacks()
x = my_prob.solution.get_values()

print('x: ')
print(x)

結(jié)果:

實驗結(jié)果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容