Graph Optimization - Lab 1 ORLAB - Operations Research Laboratory Stefano Gualandi October 24, 2011 Stefano Gualandi Graph Optimization - Lab 1
Graph Optimization - Lab 1ORLAB - Operations Research Laboratory
Stefano Gualandi
October 24, 2011
Stefano Gualandi Graph Optimization - Lab 1
What you do during lab sessions
Algorithms Solution(s)
Modeling Interpretation
Math Model
Data
Solver
Problem Strategy
Modeling Interpretation
Check this out: http://challenge.roadef.org/2012/en/
Stefano Gualandi Graph Optimization - Lab 1
What you do during lab sessions
Algorithms Solution(s)
Modeling Interpretation
Math Model
Data
Solver
Problem Strategy
Modeling Interpretation
Check this out: http://challenge.roadef.org/2012/en/
Stefano Gualandi Graph Optimization - Lab 1
WARNINGS
I Warning 1: Lab sessions complement exercise sessions
I Warning 2: read carefully the error messages!I Common pitfalls:
I unbounded variablesI missing constraintsI missunderstood constraintsI wrong quantification in the summation of constraintsI wrong objective function
Stefano Gualandi Graph Optimization - Lab 1
A short introduction to AMPL
In AMPL, models, data, and scripts are written in three separatetext (ASCII) files:
I FILENAME.mod: it contains the model of the problem, i.e.,variables, constraints, and objective function;
I FILENAME.dat: it contains the data specifing an instance ofthe problem, i.e., the coefficients of the constraints andobjective function
I FILENAME.run: it contains script commands that typicallyselect the solver, set parameters, solve the problem, anddisplay the solution in a format of your choice. It can be usedto implement basic algorithms.
As editor, you can use Notepad, Emacs (with syntax highlight), orGusek (based on SciiTech).
Stefano Gualandi Graph Optimization - Lab 1
AMPL keywords
I set: defines set of elements
I param: defines constant parameters like constants, vectors, ormatrices
I var: defines continuous variables. They can be defined alsoas
I integerI binary
I sum: defines a linear sum
I subject to: defines a family of constraints
I minimize or maximize: defines the objective function
Check this out: http://www.ampl.com/FAQ/index.html
Stefano Gualandi Graph Optimization - Lab 1
AMPL support for scripting
I let ...: change the value of a set or parameter
I for {set expression} ...: iterate over the elements of aset
I if <condition> then ... else ...: check conditional
I repeat { ... } while (condition): loop while a givencondition is verifies
I repeat { ... } until (condition): loop until a givencondition is verifies
I Uniform(a,b): draw a random number in the interval [a...b]
I Uniform01(): draw a random number in the interval [0...1]
Check this out: http://www.ampl.com/NEW/newlang.html
Stefano Gualandi Graph Optimization - Lab 1
Invoking AMPL commands
You have two options:
1. Use the AMPL shell, and type the following commands (goodfor learning the basic steps):
I ampl: option solver cplex;I ampl: model YOUR PATH/YOUR FILE.mod;I ampl: data YOUR PATH/YOUR FILE.dat;I ampl: solve;I ampl: display x;
2. Use the script command file by DOS or LINUX shell (good forrapid math modeling)
I % ampl YOUR FILE.mod YOUR FILE.dat YOUR FILE.run;
Stefano Gualandi Graph Optimization - Lab 1
Example 1: Cabled Networks
Stefano Gualandi Graph Optimization - Lab 1
Example 2: Wireless Networks
https://nodes.wlan-si.net/network/topology/Stefano Gualandi Graph Optimization - Lab 1
Dealing with Directed Graphs
param n > 0; # Number of nodes
set N := 1..n; # Set of nodes
set A within N cross N; # Set of arcs
# Forward and Backward star of each node
set FS {i in N} := setof {j in N: (i,j) in A} (i,j);
set BS {i in N} := setof {j in N: (j,i) in A} (j,i);
# In and Out degree of each vertex
param in_degree {i in N} := card(BS[i]);
param out_degree {i in N} := card(FS[i]);
# Example of node weight vector
param w {N} default 1;
# Example of arc capacity vector
param u {A} default 1;
Stefano Gualandi Graph Optimization - Lab 1
Graph examples
data;
param n := 5;
param: A : u :=1 2 21 3 44 3 22 3 55 1 32 5 2;
Stefano Gualandi Graph Optimization - Lab 1
Print a Directed Graph
printf "\nFORWARD STARS:\n";
for {i in N} {
printf "node %d: ", i;
for {(j,l) in FS[i]}
printf "(%d,%d)[u=%d] ", j, l, u[j,l];
printf "\n";
}
printf "\nBACKWARD STARS:\n";
for {i in N} {
printf "node %d: ", i;
for {(j,l) in BS[i]}
printf "(%d,%d)[u=%d] ", j, l, u[j,l];
printf "\n";
}
printf "\nDEGREES \n";
display out_degree, in_degree;
Command line:
ampl directed-graphs.mod digraph-1.dat print-digraph.run
Stefano Gualandi Graph Optimization - Lab 1
Warm up (or Weak up!)
Start with the exercise 1 on the handouts
Stefano Gualandi Graph Optimization - Lab 1
Exercise 3: kruskal.run
repeat {
let MinCostEdges := {(i,j) in F: c[i,j] == min{(u,v) in F} c[u,v]};
let F := F diff MinCostEdges;
for {(i,j) in MinCostEdges} {
if cc[i] == 0 then {
if cc[j] == 0 then { # Case 1: neither vertex is in the tree.
let cc[i] := max{v in N} cc[v] + 1;
let cc[j] := cc[i]; let T := T union {(i,j)};
} else { # Case 2: j is in the tree, but i isn’t.
let cc[i] := cc[j]; let T := T union {(i,j)};
}
} else {
if cc[j] == 0 then { # Case 3: i is in the tree, but j isn’t.
let cc[j] := cc[i]; let T := T union {(i,j)};
} else if cc[i] <> cc[j] then { # Case 4: i and j are in different connected components
for {v in N: cc[v] == cc[j]} let cc[v] := cc[i];
let T := T union {(i,j)};
} else printf "Adding edge (%d,%d) would create a cycle.\n\n",i,j;
}
}
} while (card(T) < n - 1);
printf "A minimum MST has cost %d.\n",sum{(i,j) in T} c[i,j];
Stefano Gualandi Graph Optimization - Lab 1
Challenge 2: FarmVille
Stefano Gualandi Graph Optimization - Lab 1
Challenge 2: FarmVille
You begin the game with a 12 by 12 piece of land. That gives you144 one by one squares in which to plant crops. To simplify theexercise, we have the following assumptions
1. Restrict my attention to the four crops initially available(strawberries, eggplant, wheat, and soybeans);
2. Assume crops will be harvested as soon as they are ripe (thatis, ignore wilting);
3. Disregard the random bonuses and gifts that show up everynow and then.
The cost column includes the cost of plowing the land (15 coins)plus the cost of purchasing the seeds. Because the harvest times ofall four crops are multiples of four hours, we can think in terms of4-hour periods.
Stefano Gualandi Graph Optimization - Lab 1
Challenge 2: FarmVille
You initially have 200 coins, one ripe and one half-grown square ofstrawberries, one ripe and one half-grown square of eggplant. Byharvesting the two ripe squares, your fortune grows to 323 coins.We also go ahead and clean (delete) the two half-grown squares sothat we can begin with a clean slate.
Question: What is the largest amount of cash one can obtain in 6days? (need to rest on the 7th day).
Stefano Gualandi Graph Optimization - Lab 1
Links and email
I Lab sessions web site:http:
//www-dimat.unipv.it/~gualandi/graph-optimization/
I E-mail:stefano.gualandi [at] unipv.it
Stefano Gualandi Graph Optimization - Lab 1