NETWORK FLOWS AND COMBINATORIAL OPTIMIZATION

advertisement
ILOG CPLEX
 CPLEX is a product developed by ILOG to solve





LPs
MIPs
QPs
MIQPs
Network Flow problems.
 CPLEX technologies
 CPLEX callable library (for C)
 CPLEX interactive optimizer
 Concert Technology (for C++)
 Compatible platforms
 Windows
 Unix.
1
CPLEX interactive optimizer
 An interactive program to
 Load models in PC.
 Applying algorithms.
 Supports features like




Choosing different algorithms for problems.
Change the specifications in the problem.
Sensitivity analysis.
Re-optimizing the problem.
2
CPLEX interactive optimizer
 Locate cplex.exe in the folder
C:/ILOG/CPLEX70/bin/msvc6/stat_mda
 Make a new folder and copy cplex.exe into it.
 Run cplex.exe by clicking on it and you will see a DOS
window pop-up with CPLEX welcome message.
Welcome to CPLEX Linear Optimizer 8.0.0
with Mixed Integer & Barrier Solvers
Copyright (c) ILOG 1997-2002
CPLEX is a registered trademark of ILOG
Type 'help' for a list of available commands.
Type 'help' followed by a command name for more
information on commands.
CPLEX >
3
CPLEX interactive optimizer
 Type help on the command prompt to see the options.
CPLEX> help
add
baropt
change
display
enter
help
mipopt
netopt
optimize
primopt
quit
read
set
tranopt
write
xecute
add constraints to the problem
solve using barrier algorithm
change the problem
display problem, solution, or parameter
settings
enter a new problem
provide information on CPLEX commands
solve a mixed integer program
solve the problem using network method
solve the problem
solve using the primal method
leave CPLEX
read problem or basis information from a file
set parameters
solve using the dual method
write problem or solution info. to a file
execute a command from the OS
4
A simple example
Giapetto’s woodcarving manufactures two types of wooden
toys: soldiers and trains
Soldier
Train
Selling Price
$27
$21
Raw Material
required
Variable Cost
$10
$9
$14
$10
Finishing Labor
required
Carpenting labor
required
2 hrs
1 hr
1 hr
1 hr
Constraints:
100 finishing hour per week available
80 carpentry hours per week available
produce no more than 40 soldiers per week
Objective: maximize profit
5
Toys’ problem (cont…)
Linear Programming formulation:
Maximize z = 3x1 + 2x2
(Obj. Func.)
subject to
2x1 +
x1 +
x1
x1
x2
x2
x2
 0
 0
 100 (Finishing constraint)
 80 (Carpentry constraint)
 40 (Bound on soldiers)
(Sign restriction)
(Sign restriction)
6
Solving the toys’ problem (contd…)
 Enter the model.
CPLEX> enter
Enter name for problem: toys' problem
Enter new problem ['end' on a separate line terminates]:
Maximize
3x1+2x2
Subject to
2x1+x2 <=100
x1+x2 <=80
bounds
x1<=40
end
CPLEX>
7
Solving the toys’ problem (contd…)
 Optimizing
CPLEX> optimize
Tried aggregator 1 time.
No LP presolve or aggregator reductions.
Presolve time = 0.03 sec.
Iteration log . . .
Iteration: 1 Dual infeasibility =
Iteration: 3 Dual objective =
0.500000
180.000000
Dual - Optimal: Objective = 1.8000000000e+002
Solution time = 0.06 sec. Iterations = 3 (2)
CPLEX>
8
Display
 To see the results, type display on the command prompt
and it will show the options.
CPLEX> display
Display Options:
iis
display infeasibility diagnostics (IIS
constraints)
problem
display problem characteristics
sensitivity display sensitivity analysis
settings
display parameter settings
solution
display existing solution
Display what:
9
Displaying solution
 Type solution to see options you have,
Display what: solution
Display Solution Options:
basis
display a range of basic constraints or
variables
bestbound display the current MIP best bound
dual
display a set of solution dual values
kappa
display the condition number of the basis
matrix
objective display solution objective value
quality
display quality of solution
reduced
display a set of solution reduced costs
slacks
display a set of solution slack values
variables display a set of solution variable values
Display which part of the solution:
10
Displaying solution(contd…)
CPLEX> display solution variables
Display values of which variable(s): Variable Name
Solution Value
x1
20.000000
x2
60.000000
(Note:- To see all variables, just press “–” and press enter.
CPLEX> display solution objective
Dual - Optimal: Objective = 1.8000000000e+002
11
Viewing the model
 The model can be printed with following command
CPLEX> display problem all
Maximize
obj: 3 x1 + 2 x2
Subject To
c1: 2 x1 + x2 <= 100
c2: x1 + x2 <= 80
Bounds
0 <= x1 <= 40
All other variables are >= 0.
CPLEX>
12
Adding a constraint
 Command “add” can be used to add new constraints or
bounds.
CPLEX> add
Enter new constraints and bounds ['end' terminates]:
x1+2x2<=90
end
Problem addition successful.
CPLEX> display problem all
Maximize
obj: 3 x1 + 2 x2
Subject To
c1: 2 x1 + x2 <= 100
c2: x1 + x2 <= 80
c3: x1 + 2 x2 <= 90
Bounds
0 <= x1 <= 40
All other variables are >= 0.
CPLEX>
13
Change
 Using command “change” to change specifications of the
problem,
CPLEX> change
Change options:
Bounds change bounds on a variable
coefficient change a coefficient
delete
delete some part of the problem
name
change a constraint or variable name
objective change objective function value
problem change problem type
qpterm
change a quadratic objective term
rhs
change a right-hand side or network
supply/demand value
sense
change objective function or a constraint
sense
type
change variable type
Change to make:
14
Change
 An example of “change”
CPLEX> change delete constraints c3
Constraint 3 deleted.
CPLEX>
 Display the model to check,
CPLEX> display problem all
Maximize
obj: 3 x1 + 2 x2
Subject To
c1: 2 x1 + x2 <= 100
c2: x1 + x2 <= 80
Bounds
0 <= x1 <= 40
All other variables are >= 0.
CPLEX>
15
Saving and loading a model
 Model information can be solved to file using command
“write”.
 Use .lp extension files to save the model.
CPLEX> write mymodel.lp
Problem written to file 'mymodel.lp'.
 Use .txt extension to save the solution.
CPLEX> write mymodel.txt
Solution written to file 'mymodel.txt'.
 Model information can be loaded from the files using
command “read”.
 Use .lp extension files contains the model.
CPLEX> read mymodel.lp
Problem 'mymodel.lp' read.
Read time = 0.00 sec.
CPLEX>
 For more information use command help write or help
read.
16
Choosing an optimizer
 For optimizing, the default solver is dual-simplex.
 Other optimizers can be chosen by these commands,
 primopt
primal simplex optimizer
 tranopt
dual simplex optimizer
 netopt
network optimizer
(for problems with special structure of
a network flow problem)
 baropt
barrier optimizer
(uses interior point algorithm to solve
large scale problem)
17
Tips
 For large problems,
 write a small program in C/C++ which can read the data
from the data files and write the model to a text file in
the above-mentioned format.
 save the file with “.lp” extension.
 read the model from the file and solve.
 View a comprehensive introductory manual at
www.ise.ufl.edu\ilog\ILOG\CPLEX70\doc\getstart\onlin
edoc\index.html
and then choose “Chapter 2 Interactive Optimizer” from
the menu on the left.
 If still in doubt, bug the TA.
18
ILOG Concert 1.2
 A C++ library of classes and functions for
 Defining models.
 Applying algorithms.
 Supports algorithm for both constraint programming
and math programming (LP, MIP, QP, etc.).
 Can be integrated with rest of the application in the
program.
 The documentation for CPLEX 8.0 and CONCERT 1.2 can
be accessed by installing “Documentation” from the
installation CD.
19
An example
Linear Programming formulation:
Maximize z = 3x1 + 2x2
(Obj. Func.)
subject to
2x1 +
x1 +
x1
x1
x2
x2
x2
 0
 0
 100 (Finishing constraint)
 80 (Carpentry constraint)
 40 (Bound on soldiers)
(Sign restriction)
(Sign restriction)
20
An example (cont..)
 Indices
i= the item. Soldiers(i=1), Trains (i=2).
j= the resources. Finishing time (j=1), Carpentry time (j=2)
 Data
Profiti, Profit by selling a unit of item type i.
Laborij, Labor requirement of jth type for a unit of item type i.
Supplyj, Number of hours available for resource j.
 Decision variables
Xi
number of item i produced
 Constraints
 Maximum availability of fishing hours.
 Maximum availability of carpentry hours.
 Maximum production of soldiers.
 Objective
maximize profit
21
Basic steps
 Creating an environment.
 Building a model.
 Extracting a model for an algorithm.
 Solving the problem.
 Accessing results.
 Ending the program.
22
Complete program
#include <ilcplex/ilocplex.h>
ILOSTLBEGIN //ILOG standard template library
void main()
{
// creating an environment
IloEnv environment;
// creating a model
IloModel mymodel(environment);
// variable declaration
IloNumVarArray x(environment, 2, 0, IloInfinity,
ILOFLOAT);
23
Complete prgram(contd…)
//add the objective to the model
mymodel.add(IloMaximize(environment, 3*x[0]+2*x[1]));
//adding the constraints to the model
mymodel.add(2*x[0]+x[1]<=100); //finishing constraint.
mymodel.add(x[0]+x[1]<=80);
//carpentry constraint.
mymodel.add(x[0]<=40); //bound on number of soldiers.
// handing out the model to the algorithm
IloCplex mycplex(mymodel);
24
Complete program (contd..)
// solving the problem.
mycplex.solve();
// outcome of the solver.
cout << "Solution Status is " << mycplex.getStatus() <<
endl;
// the results
cout << " The objective value is " <<
mycplex.getObjValue() << endl;
cout << " The no of soldiers and trains produced are " <<
mycplex.getValues(x) <<endl;
// ending the program
environment.end();
}
25
Output.
Tried aggregator 1 time.
LP Presolve eliminated 2 rows and 1 columns.
Reduced LP has 1 rows, 2 columns, and 2 nonzeros.
Presolve time = 0.00 sec.
Iteration log . . .
Iteration: 1 Dual infeasibility =
0.000000
Solution Status is Optimal.
The objective value is 180.000
The no of soldiers and trains produced are [20, 60]
Press any key to continue
26
Creating expressions
 Alternatively, the constraints and the objective can be
added by making expressions for them and then adding
them to the model.
 Objective
mymodel.add(IloMaximize(environment, 3*x[0]+2*x[1]));
IloExpr obj(environment);
obj += 3*x[0];
obj+= 2*x[1];
mymodel.add(IloMaximize(environment,obj);
 Constraint
mymodel.add(2*x[0]+3*x[1]<=100);
IloExpr finishing_constraint(environment);
finishing_constraint += 2*x[0];
finishing_constraint += 3*x[1];
mymodel.add(finishing_constraint <= 100);
27
Multidimensional arrays
 To store data using CONCERT, we can use the predefined arrays,
IloNumArray array_name(environment, size_array)
 A 2-dimensional array is an array of arrays. So, it can be
defined as
typedef IloArray<IloNumArray> NumArray2dim;
 In toys problem, the data can be stored in array as,
IloNumArray Profit(environment,2);
IloNumArray Supply(environment,2);
NumArray2dim Labor(environment,2);
for(int i;i<2;i++)
Labor[i]= IloNumArray(environment,2);
28
Reading the data
 To read the data from the file data_file.txt
ifstream ifile("data_file.txt");
 The arrays can be directly read from the file by single
command,
ifile<<Profit<<Supply<<Labor;
29
Complete program
#include <ilcplex/ilocplex.h>
ILOSTLBEGIN //ILOG standard template library
void main()
{
// creating an environment
IloEnv environment;
// creating a model
IloModel mymodel(environment);
// declaring the data-structure to store data.
IloNumArray Profit(environment, 2);
IloNumArray Supply(environment, 2);
IloArray<IloNumArray> Labor(environment, 2);
for(int i = 0; i < 2; i++)
Labor[i] = IloNumArray(environment,2);
30
Complete program (contd…)
// reading the data from a file
ifstream ifile("data_file.txt");
ifile >> Profit >> Supply >> Labor;
ifile.close();
// variable declaration
IloNumVarArray x(environment, 2, 0, IloInfinity, ILOFLOAT);
//adding the objective to the model
IloExpr objective(environment);
for( i = 0; i < 2; i++)
objective += Profit[i]*x[i];
mymodel.add(IloMaximize(environment, objective));
31
Complete program (contd…)
//adding the constraints to the model
for(int j = 0; j < 2; j++)
{
IloExpr constraint(environment);
for(i = 0; i < 2; i++)
constraint += Labor[i][j]*x[i];
mymodel.add(constraint <= Supply[j]);
}
//bound on soldiers.
mymodel.add(x[0]<=40);
// handing over the model to the algorithm
IloCplex mycplex(mymodel);
// solving the problem
mycplex.solve();
32
Complete program (contd…)
// outcome of the solver.
mycplex.out() << "Solution Status is " <<
mycplex.getStatus() << endl;
// the results
cout << " The objective value is " <<
mycplex.getObjValue() << endl;
cout << " The no of soldiers produced are " <<
mycplex.getValue(x[0]) <<endl;
cout << " The no of trains produces are " <<
mycplex.getValue(x[1]) <<endl;
// releasing the memory
environment.end();
}
33
MIP
 The variables in a mixed-integer program are defined as,
// continuous variable declaration
IloNumVarArray x(environment, 2, 0, IloInfinity,
ILOFLOAT);
// integer variable declaration
IloNumVarArray x(environment, 2, 0, IloInfinity, ILOINT);
34
Selecting an optimizer
 The IloCplex treats all problem it solves as MIP problem.
The member functions to set the algorithm used at nodes
in branch and bound tree are;
void IloCplex::setRootAlgorithm (IloCplex::Algorithm alg);
void IloCplex::setNodeAlgorithm (IloCplex::Algorithm
alg);
 IloCplex::Dual (use the dual simplex algorithm.)
 IloCplex::Primal (use the primal simplex algorithm.)
 IloCplex::Barrier (use the barrier algorithm.)
35
Selecting the optimizer (contd…)
 IloCplex::BarrierPrimal (use the barrier algorithm
followed by the primal simplex algorithm)
 IloCplex::BarrierDual
(use the barrier algorithm
followed by the dual simplex algorithm )
 IloCplex::NetworkPrimal (use the network simplex
algorithm for the embedded network followed by the
primal simplex algorithm )
 IloCplex::NetworkDual (use the network simplex
algorithm for the embedded network followed by the
dual simplex algorithm)
 IloCplex::DualBarrier
(use the dual simplex algorithm
up to an iteration limit, then switch to barrier )
36
Setting CPLEX parameters
 Open url
www.ise.ufl.edu\ILOG\CPLEX70\doc\refman\onlinedoc\ind
ex.html and choose Appendix A from the menu on the
left
// -- set the problem solving time upper bound
cplex.setParam(IloCplex::TiLim,60);
// -- Turn on/off the simplex iteration on screen message
cplex.setParam(IloCplex::SimDisplay, 1);
// -- explicitly set the algorithm used to solve root
cplex.setRootAlgorithm(IloCplex::BarrierPrimal);
37
Setting CPLEX parameters (contd..)
 Parameters for MIP
//-- Turn on/off MIP node log on screen message
cplex.setParam(IloCplex::MIPDisplay, 1);
//-- Turn on/off MIP node log on screen message
cplex.setParam(IloCplex::MIPDisplay, 1);
//-- Setting optimality/feasibility as the driving objective.
cplex.setParam(IloCplex::MIPEmphasis,1);
//-- explicitly set the algorithm used to solve node
cplex.setNodeAlgorithm(IloCplex::BarrierPrimal);
//-- explicitly set the number of feasible solutions to be found
before
terminating
cplex.setParam(IloCplex::IntSolLim,2);
//-- explicitly declare to do Breadth/Depth/Best first search.
cplex.setParam(IloCplex::NodeSel ,2);
//-- explicitly set to apply Gomory cut aggressively
cplex.setParam(IloCplex::FracCuts,2);
38
Tips
 While solving an MIP, try different setting of the parameters
mentioned above.
 If the problem has the special structure of a network flow
problem, it can be solved 10-1000 times faster using networksimplex algorithm. Few things to watch out while solving a
network flow problem using CONCERT,
 Turn off preprocessing by setting,
cplex.setParam(IloCplex::PreInd,0);
as preporcessing might mess up with the special
structure of the network.
 Using CPLEX callable library, you can formulate the network
flow problem in a better way to take advantage of a datastructure developed exclusively for the problems of this
class. Refer
www.ise.ufl.edu\ILOG\CPLEX70\doc\userman\onlinedoc\inde
x.html , chapter 6 for more details.
39
Settings for CONCERT
1/ Start Microsoft Developer Studio.
The first step is to build the test.dsw project workspace.
2/ From the File menu, select New.
The New dialog box appears.
+ Select the "Projects" tab.
+ Fill in the project name (test)
+ Correct the location at which you want to have it
(<MYAPPDIR>)
+ Choose "Win32 console application"
+ Check "Create new workspace"
+ Click on "OK"
3/ From the Project menu, choose "Add to Project ... Files"
+ Select test.cpp or test.c as appropriate
+ Click on OK.
40
Settings for CONCERT
Next, you have to set some options so the project knows where to
find CPLEX and Concert include files and the CPLEX and Concert
libraries.
4/ From the Project menu, choose Settings
The Project Settings dialog box appears.
5/ In the Settings For pane, select test Win32 Release.
6/ From the tabs at the top of the dialog box, select the C/C++ type
of settings.
- With the General category tab:
- set Optimizations to Default.
- set Warning level to Level 3.
- With the Code generation tab:
- set Use runtime library to "Single Threaded".
- With the C/C++ Preprocessor selection:
- add the directory C:\ILOG\CPLEX80\include in the Additional
include
directories text box.
- If your application uses Concert Technology,
- with the C/C++ General selection:
- add IL_STD to the Preprocessor definitions text box
- with the C/C++ Preprocessor selection:
- add the directory C:\ILOG\CONCERT12\include in the
Additional include directories text box.
41
Settings for CONCERT
7/ From the tabs at the top of the dialog box, select the Link type of
settings.
- Add the file C:\ILOG\CPLEX80\lib\msvc6\stat_sta\cplex80.lib
in the Object/Modules text box.
- If your application uses Concert Technology, add the files
C:\ILOG\CPLEX80\lib\msvc6\stat_sta\ilocplex.lib
C:\ILOG\CONCERT12\lib\msvc6\stat_sta\concert.lib
- Add the file wsock32.lib in the Object/Modules text box if it is not
already there.
8/ Click on OK.
Finally you have to set test Win32 Release as the default project
configuration and build it.
9/ From the Build menu, select Set Active Configuration
- Select test Win32 Release.
- Click on OK.
10/ From the Build menu, select Build test.exe.
After completion of the compiling and linking process, the target is
created. The full path of the test.exe is <MYAPPDIR>\Release\test.exe
42
Download