Why Python import gurobipy MIP import scop 2
Why Python? • モジュールを読み込め ば何でもできる! • 最適化もできる! import gurobipy (MIP) import scop 2 (CP) • import optseq 2 (Scheduling) • 空を飛ぶことも!? import antigravity ? http: //xkcd. com/353 / http: //www. logopt. com/ mikiokubo/に 20分の入門ビデオ
How to solve hard optimization problems quickly by Python Metaheuristics MIP solver Gurobi Developed by: Zonghao Gu, Edward Rothberg, Robert Bixby Free academic license + Python Interface Python Source Codes Developed by: Me & J. Pedroso VS. ? +
Pythonでの実装(2) for j in range(n): y[j]=model. add. Var(obj=0, vtype=“B”, name="y"+str(j)) for i in range(n): x[i, j]=model. add. Var(obj=cost[i, j], vtype=“B”, name="x"+str(i)+str(j)) model. update()
Pythonでの実装(3) for i in range(n): L=Lin. Expr() #線形表現オブジェクト L を空で作成 for j in range(n): L. add. Terms(1, x[i, j]) #線形表現オブジェクトに項を追加 model. add. Constr(lhs=L, sense= " = ", rhs=1, name="Demand"+str(i)) 線形表現(Linear Express) クラス Lin. Expr
Pythonでの実装(5) import networkx as NX #network. Xモジュールの読み込み import matplotlib. pyplot as P #描画の準備 P. ion() G = NX. Graph() #グラフオブジェクトGの生成 G. add_nodes_from(range(n)) #点の追加 for (i, j) in edge: #グラフに枝を追加 G. add_edge(i, j) NX. draw(G) #描画
弱い定式化での結果 n=200, k=20 Optimize a model with 401 Rows, 40200 Columns and 80400 Non. Zeros 中略 Explored 1445 nodes (63581 simplex iterations) in 67. 08 seconds Thread count was 2 (of 2 available processors) Optimal solution found (tolerance 1. 00 e-04) Best objective 1. 0180195861 e+01, best bound 1. 0179189780 e+01, gap 0. 0099% Opt. value= 10. 1801958607
強い定式化での結果 Optimize a model with 40401 Rows, 40200 Columns and 160400 Non. Zeros 中略 Explored 0 nodes (1697 simplex iterations) in 3. 33 seconds (分枝しないで終了!) Thread count was 2 (of 2 available processors) Optimal solution found (tolerance 1. 00 e-04) Best objective 1. 0180195861 e+01, best bound 1. 0180195861 e+01, gap 0. 0% Opt. value= 10. 1801958607
k-center問題 • min-max型の施設配置問題 • 施設は顧客上から選択 • Euclid距離,x, y座標は一様ランダム Which is the solution of the k-center problem?
Miller-Tucker-Zemlinの定式化
結果 Optimize a model with 6480 Rows, 6400 Columns and 37762 Non. Zeros 中略 Cutting planes: Gomory: 62 Implied bound: 470 MIR: 299 Zero half: 34 Explored 125799 nodes (2799697 simplex iterations) in 359. 01 seconds Optimal solution found (tolerance 1. 00 e-04) Best objective 7. 4532855108 e+00, best bound 7. 4525704995 e+00, gap 0. 0096% Opt. value= 7. 45328551084
Pythonによる記述(主問題) K = len(t) t: 初期パターンのリスト master = Model("MP") x = {} for k in range(K): x[k] = master. add. Var(obj=1, vtype="I", name="x[%d]"%k) master. update() orders={} for i in range(m): coef = [t[k][i] for k in range(K) if t[k][i] > 0] var = [x[k] for k in range(K) if t[k][i] > 0] orders[i] = master. add. Constr(Lin. Expr(coef, var), ">", q[i], name="Order[%d]"%i) master. update()
Pythonによる記述(列生成) while 1: knapsack. update() iter += 1 knapsack. optimize() relax = master. relax() if knapsack. Obj. Val < 1+EPS: relax. optimize() break pi = [c. Pi for c in relax. get. Constrs()] col = Column() knapsack = Model("KP") for i in range(m): knapsack. Model. Sense=-1 if t[K][i] > 0: y = {} for i in range(m): y[i] = knapsack. add. Var(obj=pi[i], ub=q[i], vtype="I", name="y[%d]"%i) knapsack. update() L = Lin. Expr(w, [y[i] for i in range(m)]) knapsack. add. Constr(L, "<", B, ame="width") col. add. Terms(t[K][i], orders[i]) x[K] = master. add. Var(obj=1, vtype="I", name="x[%d]"%K, column=col) master. update() K += 1 master. optimize()
Pythonでの実装(1) from gurobipy import * model=Model("gcp") x={} y={} for i in range(n): for k in range(K): x[i, k]=model. add. Var(obj=0, vtype="B", name="x"+str(i)+str(k)) for k in range(K): y[k]=model. add. Var(obj=1, vtype=“B”, name="y"+str(k)) model. update()
Pythonでの実装(2) for i in range(n): L=Lin. Expr() for k in range(K): L. add. Terms(1, x[i, k]) model. add. Constr(lhs=L, sense= " = ", rhs=1, name="const"+str(i)) 中略 model. optimize() print "Opt. value=", model. Obj. Val for v in model. get. Vars(): if v. X>0. 001: print v. Var. Name, v. X
上界と下界の変化(原定式化) 点数 n=40,彩色数上限 Kmax=10 Optimize a model with 3820 Rows, 410 Columns and 11740 Non. Zeros Explored 17149 nodes (3425130 simplex iterations) in 1321. 63 seconds
上界と下界の変化(対称性除去) Optimize a model with 3829 Rows, 410 Columns and 11758 Non. Zeros Explored 4399 nodes (1013290 simplex iterations) in 384. 53 seconds MIPFocus=2(最適性保証優先) で 67秒,MIPFocus=3(下界優先) で 70秒
上界と下界の変化(+SOS) Optimize a model with 3829 Rows, 410 Columns and 11758 Non. Zeros. Explored 109 nodes (58792 simplex iterations) in 22. 02 seconds MIPFocus=2(最適性保証優先) で 65秒,MIPFocus=3(下界優先) で 126秒
Fixed-K Approach +Binary Search 彩色数 Kの上界 UB,下界 LB while UB – LB >1: K= [ (UB+LB)/2 ] [ ] : Gaussの記号 if Fixd-K定式化の最適値が 0 then UB = K else LB = K
Improved Formulation Fixed-K Approach +Binary Search
Gurobi Objects GRBError add. Variable Model add. SOS add. Constr SOS Callbacks Column Constraint Lin. Expr Quad. Expr
Model Object = Model(“name of model”) Methods • Add. Var • Add. Constr • optimize • update • get. Vars • relax. . . Attributes • • • . . . Params Obj. Val Model. Sense Runtime Status Sol. Count
Prams (Parameters to control the optimizer) • • • Time. Limit: limit of the computational time MIPGap:upper bound of the relative error MIPFocus:MIP search strategy Solution. Number:number of solution LPMethod:solution method of linear optimization Output. Flag: 0=Off,1=On. . . E. g. , : Model. Object. Params. Time. Limit=10
SCOP Objects add. Variable(s) Model add. Constraint Variable Linear Quadratic Alldiff 詳細については http: //www. logopt. com/scop/
Model Object = Model(“name of model”) Methods • Add. Variable(s) • Add. Constraint • optimize Attributes Params • variables • constraints •
Prams (Parameters to control the optimizer) • • Time. Limit: limit of the computational time Target:target penalty value Random. Seed: random seed number Output. Flag: 0=Off,1=On
Opt. Seq Objects add. Activity Model Attribute add. Mode add. Resource add. Temporal Mode Resource Temporal 詳細については http: //www. logopt. com/Opt. Seq/
Model Object = Model(“name of model”) Methods • • • Add. Activity Add. Resource Add. Temporal optimize write Attributes • • • Params activities modes resources temporals
Prams (Parameters to control the optimizer) • Time. Limit: limit of the computational time • Makespan: True= makespan minimization, False=weighted tardiness minimization • Random. Seed: random seed number • Output. Flag: 0=Off,1=On
- Slides: 74