# Or seminar2011final

• View
787

1

Embed Size (px)

Transcript

• Python

• Why Python? import gurobipy (MIP) import scop2 (CP)import optseq2 (Scheduling) import antigravity ?http://xkcd.com/353/http://www.logopt.com/ mikiokubo/20

• Python30Windows, Mac, Linux)

• Python 1 A=[] A.append(5), a=A.pop(), A.remove(6), A.sort() D= { } Hash D[(1,2)] =6, D[Hello]= for i in [1,2,5,6]: for key in D: print i*2 print key,D[key] => 2,4,10,12 =>

• How to solve hard optimization problems quickly by Python Metaheuristics Python Source Codes Developed by: Me & J. Pedro PedrosoMIP solver Gurobi Developed by: Zonghao Gu, Edward Rothberg Robert Bixby VS. ? +Free academic license + Python Interface

• MIP, CP, Scheduling Solvers(MIP): Gurobi(CP): SCOP (Solver for COnstraint Programming): OptSeq II ( -Python -

• Mixed Integer Programming (MIP)Variables x : 0-1Binary Constraints) or () minimize cx+xQx () subject to Ax=b ()

• Gurobi (1) model = Model("Wine Blending")

• Gurobi (3) model.setObjective(15*x1 + 18*x2 + 30*x3, GRB.MAXIMIZE) model.addConstr(2*x1 + x2 + x3
• x=[] for i in range(1,4): var=model.addVar(name=x[%s]%i) x.append(var) x1 + x2 + x3

• (2) 2 x1 + x2 + x3
• (1)Blends, Profit = multidict({"Dry":15, "Medium":18, "Sweet":30}) => Blends=["Dry", "Medium, "Sweet] List of Keys Profit[Dry]=15, Profit[Medium]=18, ...

Grapes, Inventory = multidict({"Alfrocheiro":60, "Baga":60, "Castelao":30})

Use = { ("Alfrocheiro","Dry"):2, ("Alfrocheiro","Medium"):1, ("Alfrocheiro","Sweet"):1, ("Baga","Dry"):1, .... }

• (2)x = {}for j in Blends: x[j] = model.addVar(vtype="C", name="x[%s]"%j)model.update()

model.setObjective(quicksum(Profit[j]*x[j] for j in Blends), GRB.MAXIMIZE) for i in Grapes: model.addConstr(quicksum(Use[i,j]*x[j] for j in Blends)

• k-medianmin-sum n=200 k=20 Euclid

• Python1from gurobipy import * #gurobipy

# k-mediandef solve(n,k,cost): model=Model(median) # y={} # x={}Hanako, (1,2)127cm

• Python for i in range(n): L=LinExpr() # L for j in range(n): L.addTerms(1,x[i,j]) #model.addConstr(lhs=L,sense= " = ", rhs=1,name="Demand"+str(i)) Linear Express LinExpr

• Python model.optimize() # print Opt.value=,model.ObjVal # edge=[] #(i,j) for (i,j) in x: #x if x[i,j].X==1: #x[i,j]1 edge.append((i,j)) #edge(i,j) return edge

• Python import networkx as NX #networkX 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=20Optimize a model with 401 Rows, 40200 Columns and 80400 NonZerosExplored 1445 nodes (63581 simplex iterations) in 67.08 secondsThread count was 2 (of 2 available processors)Optimal solution found (tolerance 1.00e-04)Best objective 1.0180195861e+01, best bound 1.0179189780e+01, gap 0.0099%Opt.value= 10.1801958607

• Graph1

15.515080

15.515083.85187

15.515086.71677

15.515087.94031

15.515088.60712

15.515089.04178

15.515089.26948

13.60334659.26948

13.603359.26948

12.33572199.26996

11.11741879.26996

11.02587419.26996

11.025879.26996

10.87896749.26996

10.53059489.26996

10.37303719.26996

10.34984789.26996

10.340229.26996

10.31090499.26996

10.30605879.26996

10.23308469.26996

10.19818799.27148

10.198199.27148

10.18150379.272

10.18159.3406

10.18019599.4862

10.18029.48743

10.18029.52669

10.18029.57665

10.18029.61687

10.18029.71297

10.18029.88786

10.18029.97542

10.180210.13542

UB

LB

CPU

Obj. Func. Value

flp1

CPUUBLB

115.515080

115.515083.85187

115.515086.71677

215.515087.94031

215.515088.60712

215.515089.04178

315.515089.26948

413.60334659.26948

513.603359.26948

712.33572199.26996

811.11741879.26996

811.02587419.26996

1011.025879.26996

1010.87896749.26996

1110.53059489.26996

1210.37303719.26996

1210.34984789.26996

1310.340229.26996

1510.31090499.26996

1610.30605879.26996

1710.23308469.26996

1910.19818799.27148

2010.198199.27148

2110.18150379.272

2510.18159.3406

2910.18019599.4862

3010.18029.48743

3510.18029.52669

4010.18029.57665

4510.18029.61687

5010.18029.71297

5510.18029.88786

6010.18029.97542

6510.180210.13542

• Optimize a model with 40401 Rows, 40200 Columns and 160400 NonZerosExplored 0 nodes (1697 simplex iterations) in 3.33 seconds (Thread count was 2 (of 2 available processors)Optimal solution found (tolerance 1.00e-04)Best objective 1.0180195861e+01, best bound 1.0180195861e+01, gap 0.0%Opt.value= 10.1801958607

• Big M

• k-centermin-maxEuclid

Which is the solution of the k-center problem?

• 10010k-median200203

Graph1

1.07470.00349

1.06148950.00349

0.46249920.00349

0.46250.00657

0.46250.01183

0.46179860.01183

0.45961220.01183

0.45098940.01183

0.450990.01468

0.44904520.01468

0.44364180.01468

0.44187840.01468

0.441880.01938

0.44041080.01938

0.440410.01944

0.440410.01945

0.440410.01945

0.43988110.01945

0.43925120.01945

0.37592470.01945

0.375920.0215

0.375920.02157

0.375920.02164

0.375920.0217

0.37105540.0217

0.371060.02196

0.371060.02199

0.33487390.02203

0.334870.02427

0.334870.02443

0.33444690.02466

0.334450.02466

0.31503010.02474

0.30596540.02474

0.305970.0274

0.305970.02785

0.305970.0283

0.30356620.02832

0.27627990.02832

0.276280.03239

0.276280.03308

0.26666720.03561

0.266670.04051

0.24569730.0432

0.24570.05132

0.24083990.05306

0.23760760.05306

0.22966350.05306

0.22053250.05306

0.21949740.08125

0.21696420.13876

0.21534090.16999

0.21086650.16999

0.2090260.19544

0.20579120.19853

0.20511330.19853

0.20510.2038

UB

LB

CPU Time

Obj. Fun. Value

k-center

CPUUBLB

241.07470.00349

321.06148950.00349

330.46249920.00349

980.46250.00657

1210.46250.01183

1230.46179860.01183

1230.45961220.01183

1230.45098940.01183

1270.450990.01468

1270.44904520.01468

1290.44364180.01468

1290.44187840.01468

1420.441880.01938

1430.44041080.01938

1450.440410.01944

1580.440410.01945

1750.440410.01945

1760.43988110.01945

1770.43925120.01945

2350.37592470.01945

2570.375920.0215

2600.375920.02157

2660.375920.02164

2700.375920.0217

2700.37105540.0217

2770.371060.02196

2800.371060.02199

2830.33487390.02203

2850.334870.02427

2900.334870.02443

2940.33444690.02466

2960.334450.02466

2980.31503010.02474

2980.30596540.02474

3010.305970.0274

3050.305970.02785

3100.305970.0283

3100.30356620.02832

3130.27627990.02832

3150.276280.03239

3270.276280.03308

3270.26666720.03561

3330.266670.04051

3330.24569730.0432

3370.24570.05132

3370.24083990.05306

3370.23760760.05306

3370.22966350.05306

3400.22053250.05306

3400.21949740.08125

3440.21696420.13876

3440.21534090.16999

3460.21086650.16999

3470.2090260.19544

3470.20579120.19853

3470.20511330.19853

3500.20510.2038

• k-1

• k-+Binary Search UB LB while UB LB >: = (UB+LB)/2 if k- 0 then UB = else LB =

• Min-maxMIP Job shop =>k-min-maxmin-max

• 85,900

• Miller-Tucker-Zemlin

• Euclid TSP

Out of Memory!

Graph1

38.107396.81231

9.30761346.81231

7.91714816.81231

7.917156.86138

7.917156.8639

7.917156.8639

7.917156.8639

7.917156.8699

7.638796.87545

7.62651056.92177

7.62623196.92177

7.62382816.92177

7.61154866.92177

7.59727866.92177

7.5849996.92177

7.49333756.92177

7.47814856.92177

7.478156.92504

7.478156.92737

7.478156.92737

7.478156.95801

7.478157.12981

7.478157.14675

7.478157.15378

7.478157.1