POLab
2017/10/08
【回到首頁】
● 有x、y、z三個活動想在同一天舉辦
● 場地總時間只有四個小時可使用
● 活動z的價值為活動x及y的兩倍
● 活動x與活動y至少要選一個舉辦
● 活動x需花費1小時
● 活動y需花費2小時
● 活動z需花費3小時
● 舉辦哪幾個活動可以使價值最大化?
from gurobipy import* #導入Gurobi函式庫
m=Model('mip1') # 建立一個新的model,並傳至m
x = m.addVar(vtype=GRB.BINARY, name="x") # m.addVar()加入變數
y = m.addVar(vtype=GRB.BINARY, name="y")
z = m.addVar(vtype=GRB.BINARY, name="z")
m.update() #更新此model
# m.setObjective()設置目標函數
m.setObjective(x + y + 2 * z, GRB.MAXIMIZE)
# m.addConstr()加入限制式
# Add constraint: x + 2 y + 3 z <= 4
m.addConstr(x + 2 * y + 3 * z <= 4, "c0")
# Add constraint: x + y >= 1
m.addConstr(x + y >= 1, "c1")
- 如果不想要顯示求解的過程,可在optimize()之前加入此程式碼:m.setParam('OutputFlag',0)
m.optimize() # m.optimize()求解
# 透過屬性varName、x顯示決策變數名字及值
for v in m.getVars():
print('%s %g' % (v.varName, v.x))
# 透過屬性objVal顯示最佳解
print('Obj: %g' % m.objVal)
Optimize a model with 2 rows, 3 columns and 5 nonzeros
Variable types: 0 continuous, 3 integer (3 binary)
Coefficient statistics:
Matrix range [1e+00, 3e+00]
Objective range [1e+00, 2e+00]
Bounds range [1e+00, 1e+00]
RHS range [1e+00, 4e+00]
Found heuristic solution: objective 2
Presolve removed 2 rows and 3 columns
Presolve time: 0.06s
Presolve: All rows and columns removed
Explored 0 nodes (0 simplex iterations) in 0.23 seconds
Thread count was 1 (of 4 available processors)
Solution count 2: 3 2
Optimal solution found (tolerance 1.00e-04)
Best objective 3.000000000000e+00, best bound 3.000000000000e+00, gap 0.0000%
x 1
y 0
z 1
Obj: 3