OPL: a modellng language Carlo Mannno (from OPL reference manual) Unversty of Oslo, INF-MAT60 - Autumn 00 (Mathematcal optmzaton)
ILOG Optmzaton Programmng Language OPL s an Optmzaton Programmng Language Easy to generate and solve an lp models (program) It also provdes all features of a standard programmng language We wll use the IDE user nterface An OPL model conssts of: a sequence of declaratons optonal preprocessng nstructons the model/problem defnton optonal postprocessng nstructons Model are stored n fles wth etenson.mod
Data types, constants and varables Defnng a model: Frst you need to defne varables and constants (names and types) Basc types: float, nt, strng. E. (constants) nt = ; float+ k =.7; strng s = Optmzaton ; Varables are ntroduced by dvar dvar float+ ; (ratonal non negatve varable) dvar nt y; (nteger varable)
A smple model LP models translate naturally to OPL models LP model OPL model ma 0g s. t. g c g c g g, c R 8 0c dvar float g; dvar float c; mamze 0*g + 0*c; subject to { d: g + c <=; d: *g + *c <= 8; d: g <= ; OBS: you can assocate a name wth every constrant by puttng t before the constrant, followed by colon : Constrant name = correspondng dual varable (name)
Usng OPL nterface (IDE) To solve models you need (to create) a project. Projects can contan several models (and nstances). To etract a specfc model (and a specfc nstance) you need to defne a confguraton. A project can thus contan several confguratons, each contanng a specfc par model nstance. Once you create a new confguraton, the correspondng model can be mported by drag-and-drop
Usng OPL nterface (IDE) Eample: Project Name: Frst eample 00 The project contans only one model (smple-model.mod) The project contans only one confguraton (Confguraton) confguraton model Confguraton contans model smple-model.mod
Ranges and arrays Ranges of ntegers are defned as range Rows =..; nt n = ; range Cols =..n; Ranges are used to defne arrays range nodes =..; range edges =..7; float weght[nodes] = [,, 6., 7,.]; float A[nodes][edges] = [ ]; [, 0, 0,, 0, 0, -] [-, 0, 0, 0,, 0, ]... Ranges are also used n summatons and loops forall ( n tems)
Eample: ranges and arrays ma s. t. n n w c {0, n k nt n = ; range Items =..n; nt w[items] = [,,,, ]; nt c[items] = [, 8,, 7, 9]; nt k = 8; dvar nt [Items] n 0..; c 8 7 9 w mamze sum( n Items) c[]*[]; subject to { knap: sum( n Items) w[]*[] <= k; Ranges are used n summatons and loops forall ( n tems)
Separatng models and data A model s somethng dfferent from one of ts nstances The lnear program for the shortest path problem has a structure whch s the same for any graph and any weght functon. We only need one model, and then we can apply the model to any nstance of the shortest path problem OPL allows to mantan a strct separaton between the model and ts nstances. Models are stored n.mod fles Instances are stored n yyy.dat fles In the standard IDE nterface, can be dfferent from yyy The Confguraton matches the model wth the nstance.
Separatng models and data ma s. t. n n c w {0, n k Knapsack.mod nt n = ; range Items =..n; nt w[items] = ; nt c[items] = ; nt k = ; dvar nt [Items] n 0..; mamze sum( n Items) c[]*[]; subject to { knap: sum( n Items) w[]*[] <= k; n c 8 7 9 w k 8 Knapsack.dat n = ; w = [,,,, ]; c= [, 8,, 7, 9]; k = 8; Knapsack.dat n = 6; w = [,,,,, ]; c= [, 8,, 7, 9, ]; k = 8;
OPL IDE Eample: Two projects Frst eample 00 knapsack_carlo knapsack_carlo contans one model (knapsack.mod) two nstances (knapsack.dat, knapsack.dat) two confguratons (confguraton, confguraton) Confguraton contans model knapsack.mod and nstance knapsack.dat Confguraton contans model knapsack.mod and nstance knapsack.dat
s-t path problem mn uv A c uv uv - = - s us ut uv u s - D(s) - D(t) - uv - D(v) ut - 0 R A su tu vu + s D(s) u = + tu D(t) + vu = 0 D(v) v t V-{s,t s - G = (V,A) t mn c T A =b 0 s t s s s t t 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 c b 0 0 0
s-t path problem mn c T A =b 0 nt n =...; // number of nodes nt m =...; // number of edges range nodes =..n; s t s s s t t 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 c b 0 0 0 range edges =..m; nt A[nodes][edges] =...; nt b[nodes] =...; nt c[edges] =...; dvar float+ [edges]; n = ; m = 7; b = [-, 0, 0, 0, ]; c = [,, -,,, -, ]; mnmze sum(j n edges) c[j]*[j]; subject to { forall ( n nodes) y: sum(j n edges) A[][j]*[j] == b[]; A = [ [-,-,-, 0, 0, 0, 0] [, 0, 0,-,, 0, 0] [0,, 0, 0,-,, 0] [0, 0,, 0, 0,-,-] [0, 0, 0,, 0, 0, ]];
Aggegatng data: Tuples Several related data can be clustered together n tuples tuple Pont { float ; float y; Pont P = <,>; nt a = P.; Int b = P.y; {Pont ponts = { <,>, <,>, <.,.>;
Eplotng sparsty: tuples ma y t - y s y v - y u c uv for all uv A nt Nvert =...; range Verts =..Nvert; nt source = ; nt snk = ; tuple arc { nt u; nt v; float w; {arc Arcs =...; dvar float y[verts]; mamze y[snk] - y[source]; subject to { forall(e n Arcs) y[e.v] - y[e.u] <= e.w; Nvert = ; source = ; snk = ; Arcs = { < >, < >, < >, < ->, < >, < >, < >, ; - G = (V,A)
Epresson Condtonal epressons can be used n loops and n summatons. nt Nvert =...; range Verts =..Nvert; nt source =...; nt snk =...; tuple arc { nt u; nt v; float w; {arc A =...; dvar float+ [A]; mn uv A c uv uv us - - + su = - s D(s) su D(s) us ut uv - D(t) - uv - D(v) ut - 0 R A tu vu = + tu D(t) + vu = 0 D(v) v t V-{s,t mnmze sum (e n A) e.w * [e]; subject to { forall (z n Verts : z!= source && z!= snk) z: sum (e n A: e.v == z) [e] - sum (e n A: e.u == z) [e] == 0 ; s: sum (e n A: e.v == source) [e] - sum (e n A: e.u == source) [e] == -; t: sum (e n A: e.v == snk) [e] - sum (e n A: e.u == snk) [e] == ;