2023/3/19 上午11:50 HW2_110306073.
ipynb - Colaboratory
%pip install gurobipy>=10
import gurobipy as grb
import numpy as np
import math
import matplotlib.pyplot as plt
import scipy.stats
按兩下 (或按 Enter 鍵) 即可編輯
Question 1
# Declare and initialize model
mTables = grb.Model('Table')
#J Represent five decision vatiables respectively
J= 5
x = mTables.addVars([j for j in range(J)], vtype = grb.GRB.INTEGER, name = "x_1")
#Create amount constraints between modelA and their legs
mTables.addConstr(4*x[0] == x[2])
#Create amount constraints between modelB and their legs
mTables.addConstr(4*x[1] == x[3])
#Create amount constraints between modelAB and their tabletops
mTables.addConstr(x[0]+x[1] == x[4])
#Create amount constraints between tabletops and their legs
mTables.addConstr(x[2]+x[3] == 4*x[4])
#Create amount constraints of the available 500 feet of leg stock
mTables.addConstr(18*x[2]+30*x[3] <= 500*12)
#Create labor hours (capacity) constraints
mTables.addConstr(0.1*x[2]+0.15*x[3]+0.5*x[4]+0.3*x[0]+0.3*x[1] <= 80)
obj = (30 * x[0] + 45 * x[1])
mTables.setObjective(obj ,grb.GRB.MAXIMIZE)
mTables.update()
mTables.optimize()
print(mTables.Status == grb.GRB.OPTIMAL)
print(mTables.display())
#Print optimal decisions
for v in mTables.getVars():
print(v.VarName, v.X)
#Print optimal objective value
optobj=mTables.getObjective()
print(optobj.getValue())
Gurobi Optimizer version 10.0.1 build v10.0.1rc0 (linux64)
CPU model: Intel(R) Xeon(R) CPU @ 2.20GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 1 physical cores, 2 logical processors, using up to 2 threads
Optimize a model with 6 rows, 5 columns and 17 nonzeros
Model fingerprint: 0x4b039754
Variable types: 0 continuous, 5 integer (0 binary)
Coefficient statistics:
Matrix range [1e-01, 3e+01]
Objective range [3e+01, 4e+01]
Bounds range [0e+00, 0e+00]
RHS range [8e+01, 6e+03]
Found heuristic solution: objective -0.0000000
Presolve removed 4 rows and 3 columns
Presolve time: 0.00s
Presolved: 2 rows, 2 columns, 4 nonzeros
Variable types: 0 continuous, 2 integer (0 binary)
Found heuristic solution: objective 1995.0000000
Root relaxation: objective 2.333333e+03, 2 iterations, 0.00 seconds (0.00 work units)
Nodes | Current Node | Objective Bounds | Work
Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time
0 0 2333.33333 0 2 1995.00000 2333.33333 17.0% - 0s
H 0 0 2325.0000000 2333.33333 0.36% - 0s
0 0 2333.33333 0 2 2325.00000 2333.33333 0.36% - 0s
Explored 1 nodes (2 simplex iterations) in 0.03 seconds (0.00 work units)
Thread count was 2 (of 2 available processors)
Solution count 3: 2325 1995 -0
Optimal solution found (tolerance 1.00e-04)
Best objective 2.325000000000e+03, best bound 2.325000000000e+03, gap 0.0000%
True
Maximize
https://colab.research.google.com/drive/1PgTa_9nRm7RC6s_1WNFoSSOki8AR_YgB#scrollTo=BUp1Pj9S56xF&printMode=true 1/3
2023/3/19 上午11:50 HW2_110306073.ipynb - Colaboratory
30.0 x_1[0] + 45.0 x_1[1]
Subject To
R0: 4.0 x_1[0] + -1.0 x_1[2] = 0
R1: 4.0 x_1[1] + -1.0 x_1[3] = 0
R2: x_1[0] + x_1[1] + -1.0 x_1[4] = 0
R3: x_1[2] + x_1[3] + -4.0 x_1[4] = 0
R4: 18.0 x_1[2] + 30.0 x_1[3] <= 6000
R5: 0.3 x_1[0] + 0.3 x_1[1] + 0.1 x_1[2] + 0.15 x_1[3] + 0.5 x_1[4] <= 80
General Integers
['x_1[0]', 'x_1[1]', 'x_1[2]', 'x_1[3]', 'x_1[4]']
None
x_1[0] 28.0
x_1[1] 33.0
x_1[2] 112.0
x_1[3] 132.0
x_1[4] 61.0
2325.0
Question 2
# Declare and initialize model
mPlayers = grb.Model('Player')
#J Represent five decision vatiables respectively
J= 7
x = mPlayers.addVars([j for j in range(J)], vtype = grb.GRB.INTEGER, name = "x_1")
#Create constraints about bounds, so that x[i] can only be 0 or 1
mPlayers.addConstr(x[0]<=1)
mPlayers.addConstr(x[1]<=1)
mPlayers.addConstr(x[2]<=1)
mPlayers.addConstr(x[3]<=1)
mPlayers.addConstr(x[4]<=1)
mPlayers.addConstr(x[5]<=1)
mPlayers.addConstr(x[6]<=1)
#Create constraints about nonnegativitys, so that x[i] can only be 0 or 1
mPlayers.addConstr(x[0]>=0)
mPlayers.addConstr(x[1]>=0)
mPlayers.addConstr(x[2]>=0)
mPlayers.addConstr(x[3]>=0)
mPlayers.addConstr(x[4]>=0)
mPlayers.addConstr(x[5]>=0)
mPlayers.addConstr(x[6]>=0)
#Create constraints about the maximum amount of player that can play is five people
mPlayers.addConstr(x[0]+x[1]+x[2]+x[3]+x[4]+x[5]+x[6] == 5)
#Create at least four members need to play Guard constraint
mPlayers.addConstr(x[0]+x[2]+x[4]+x[6]>=4)
#Create at least two members need to play Forward constraint
mPlayers.addConstr(x[2]+x[3]+x[4]+x[5]+x[6] >=2)
#Create at least one member need to play Center constraint
mPlayers.addConstr(x[1]+x[3]+x[5]>=1)
#Create average ball-handling rate of the seven players had to be at least 2 constraint
mPlayers.addConstr(3*x[0]+2*x[1]+2*x[2]+1*x[3]+3*x[4]+3*x[5]+3*x[6] >= 10)
#Create average shooting rate of the seven players had to be at least 2 constraint
mPlayers.addConstr(3*x[0]+1*x[1]+3*x[2]+3*x[3]+3*x[4]+1*x[5]+2*x[6] >= 10)
#Create average rebounding rate of the seven players had to be at least 2 constraint
mPlayers.addConstr(1*x[0]+3*x[1]+2*x[2]+3*x[3]+3*x[4]+2*x[5]+2*x[6] >= 10)
#Create if player3 starts, player6 can't start constraint
mPlayers.addConstr(x[2]+x[5]<=1)
#Create if player1 starts, player4 and player5 must both start constraint
mPlayers.addConstr(2*x[0]-x[3]-x[4]<=0)
#Create either player2 or player3 must start constraint
mPlayers.addConstr(x[1]+x[2]>=1)
obj = (3*x[0]+2*x[1]+2*x[2]+1*x[3]+3*x[4]+3*x[5]+1*x[6])
mPlayers.setObjective(obj ,grb.GRB.MAXIMIZE)
mPlayers.update()
mPlayers.optimize()
print(mPlayers.Status == grb.GRB.OPTIMAL)
print(mPlayers.display())
#Print optimal decisions
for v in mPlayers.getVars():
print(v.VarName, v.X)
#Print optimal objective value
optobj=mPlayers.getObjective()
print(optobj.getValue())
Gurobi Optimizer version 10.0.1 build v10.0.1rc0 (linux64)
CPU model: Intel(R) Xeon(R) CPU @ 2.20GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 1 physical cores, 2 logical processors, using up to 2 threads
https://colab.research.google.com/drive/1PgTa_9nRm7RC6s_1WNFoSSOki8AR_YgB#scrollTo=BUp1Pj9S56xF&printMode=true 2/3
2023/3/19 上午11:50 HW2_110306073.ipynb - Colaboratory
Optimize a model with 24 rows, 7 columns and 61 nonzeros
Model fingerprint: 0x0a2c2874
Variable types: 0 continuous, 7 integer (0 binary)
Coefficient statistics:
Matrix range [1e+00, 3e+00]
Objective range [1e+00, 3e+00]
Bounds range [0e+00, 0e+00]
RHS range [1e+00, 1e+01]
Presolve removed 24 rows and 7 columns
Presolve time: 0.00s
Presolve: All rows and columns removed
Explored 0 nodes (0 simplex iterations) in 0.04 seconds (0.00 work units)
Thread count was 1 (of 2 available processors)
Solution count 1: 10
Optimal solution found (tolerance 1.00e-04)
Best objective 1.000000000000e+01, best bound 1.000000000000e+01, gap 0.0000%
True
Maximize
3.0 x_1[0] + 2.0 x_1[1] + 2.0 x_1[2] + x_1[3] + 3.0 x_1[4] + 3.0 x_1[5] + x_1[6]
Subject To
R0: x_1[0] <= 1
R1: x_1[1] <= 1
R2: x_1[2] <= 1
R3: x_1[3] <= 1
R4: x_1[4] <= 1
R5: x_1[5] <= 1
R6: x_1[6] <= 1
R7: x_1[0] >= 0
R8: x_1[1] >= 0
R9: x_1[2] >= 0
R10: x_1[3] >= 0
R11: x_1[4] >= 0
R12: x_1[5] >= 0
R13: x_1[6] >= 0
R14: x_1[0] + x_1[1] + x_1[2] + x_1[3] + x_1[4] + x_1[5] + x_1[6] = 5
R15: x_1[0] + x_1[2] + x_1[4] + x_1[6] >= 4
R16: x_1[2] + x_1[3] + x_1[4] + x_1[5] + x_1[6] >= 2
R17: x_1[1] + x_1[3] + x_1[5] >= 1
R18: 3.0 x_1[0] + 2.0 x_1[1] + 2.0 x_1[2] + x_1[3] + 3.0 x_1[4] + 3.0 x_1[5] + 3.0
x_1[6] >= 10
R19: 3.0 x_1[0] + x_1[1] + 3.0 x_1[2] + 3.0 x_1[3] + 3.0 x_1[4] + x_1[5] + 2.0 x_1[6]
>= 10
R20: x_1[0] + 3.0 x_1[1] + 2.0 x_1[2] + 3.0 x_1[3] + 3.0 x_1[4] + 2.0 x_1[5] + 2.0
x_1[6] >= 10
R21: x_1[2] + x_1[5] <= 1
R22: 2.0 x_1[0] + -1.0 x_1[3] + -1.0 x_1[4] <= 0
R23: x_1[1] + x_1[2] >= 1
General Integers
['x_1[0]', 'x_1[1]', 'x_1[2]', 'x_1[3]', 'x_1[4]', 'x_1[5]', 'x_1[6]']
None
Colab 付費產品 - 按這裡取消合約
check 0 秒 完成時間:上午11:50
https://colab.research.google.com/drive/1PgTa_9nRm7RC6s_1WNFoSSOki8AR_YgB#scrollTo=BUp1Pj9S56xF&printMode=true 3/3