Set 2

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
Tutorial for CPLEX interactive optimizer in Unix
To connect the iseunix machine you can use SSH Secure Shell, which you
can download a trial version from
http://ftp.ssh.com/pub/ssh/SSHSecureShellClient-3.2.9.exe
3
1. Use SSH (Secure shell) to connect to iseunix machine.
When you hit the connect button it will ask the password,
which is: tempaccess6417, it is good until November 22.
2. Create a folder under your name and run your programs
under this directory. You can do that by opening the file
transfer window, by clicking the yellow folder with blue dots
on it.
3. Type cplex and press enter.
4
5
CPLEX interactive optimizer (help)
 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 (default is dual-simplex)
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
6
Entering a problem from the keyboard
CPLEX> enter
Enter name for problem: example
Enter new problem ['end' on a separate line terminates]:
max 4x+6y
subject to
x+y<5
End
CPLEX> optimize
Tried aggregator 1 time.
LP Presolve eliminated 1 rows and 2 columns.
All rows and columns eliminated.
Presolve time = 0.00 sec.
Dual simplex - Optimal: Objective = 3.0000000000e+01
Solution time = 0.00 sec. Iterations = 0 (0)
7
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: 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
qcslacks
display a set of solution quadratic constraint slack values
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: var
Display values of which variable(s): Variable Name
Solution Value
y
5.000000
All other variables in the range 1-2 are zero.
8
Reading from the input file
 Example input file:
max 4x+6y
subject to
x+y<5
End
 Save it as example.lp under your directory, let us say your
directory is called “6417example”
9
CPLEX> read 6417example/example.lp
Problem '6417example/example.lp' read.
Read time = 0.00 sec.
CPLEX> optimize
Tried aggregator 1 time.
LP Presolve eliminated 1 rows and 2 columns.
All rows and columns eliminated.
Presolve time = 0.00 sec.
Dual simplex - Optimal: Objective = 3.0000000000e+01
Solution time = 0.00 sec. Iterations = 0 (0)
CPLEX> display solution variables –
(shows all optimal variable values)
10
 CPLEX> display problem all
Maximize
obj: 4 x + 6 y
Subject To
c1: x + y <= 5
Bounds
All variables are >= 0.
CPLEX> add
Enter new constraints and bounds ['end' terminates]:
x+y<8
end
Problem addition successful.
11
Binary or integer variables
 Before end we need to write
Binaries
x
y
int
z
t
End
12
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)
13
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 about cplex and concert technologies
14
ILOG Concert 10
 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.
15
An example
Linear Programming formulation:
min
10 10
 c ij x ij
10
i 1 j 1
 x ij  D j
i 1
j  1,..., 10
10
 x ij  Si i  1,...,10
j 1
x ij  0
for all i , j
16
Basic steps
 Creating an environment.
 Building a model.
 Extracting a model for an algorithm.
 Solving the problem.
 Accessing results.
 Ending the program.
17
Creating an environment and a model
#include <ilcplex/ilocplex.h>
ILOSTLBEGIN
// ILOG standard template library
void main()
{
// creating an environment
IloEnv env1; // name of the environment is env1
// creating a model
IloModel mymodel(env1);
//name of the model is mymodel in env1.
We will fill out this part…
}
Note: The line ILOSTLBEGIN is a macro that is needed for portability.
Microsoft Visual C++ code varies, depending on whether you use the
STL or not. It allows you to switch between both types of code without
the need to otherwise change your source code.
18
Variable declaration
IloNumVarArray x(env1, 2, 0, IloInfinity, ILOFLOAT);
// float variable array, has two elements x1 and x2
typedef IloArray<IloNumVarArray> NumVarMatrix;
\\ define two dimensional variable array called NumVarMatrix
NumVarMatrix x(env1,10);
\\ define x as a two dimensional variable array
for(int i=0;i<10;i++)
x[i]= IloNumVarArray(env1,10);
//x is a matrix x[i][j]…10*10
19
Parameters
To store data using CONCERT, we can use the pre-defined
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;
IloNumArray Demand(env1,10);
IloNumArray Supply(env1,10);
NumArray2dim cost(env1,10);
for(int i=0;i<10;i++)
cost[i]= IloNumArray(env1,10);
20
reading input file
ifstream file(“transport.txt");
if (!file) cerr << "Error" << endl;
for (int k=0;k<10;k++){
for (int j=0;j<10;j++){
file >> cost[k][j];
}
}
file.close();
1334555555
3455555566
.
.
.
.
.
.
.
.........
21
Expressions
IloExpr objective(env1);
for(IloInt i = 0; i < 10; i++)
for(IloInt j= 0; j < 10; j++)
objective += cost[i][j]*x[i][j];
mymodel.add(IloMinimize(env1, objective));
for(IloInt j=0;j<10;j++)
{
IloExpr r(env1);
for (IloInt i=0;i<10;i++)
{
r+=x[i][j];
}
mymodel.add(r>demand[j]);
}
22
Expressions contd.
for(IloInt i=0;i<10;i++)
{
IloExpr r(env1);
for (IloInt j=0;j<10;j++)
{
r+=x[i][j];
}
mymodel.add(r<supply[i]);
}
23
Solving and output
// handing over the model to the algorithm
IloCplex mycplex(mymodel);
// solving the problem
mycplex.solve();
mycplex.out() << "Solution Status is " << mycplex.getStatus()
<< endl;
// the results
cout << " The objective value is " << mycplex.getObjValue() <<
endl;
cout << " x[0][0]= " << mycplex.getValue(x[0][0])<<endl;
cout << " x[1][4]= " << mycplex.getValue(x[1][4]) <<endl;
// releasing the memory
env1.end();
24
Running example.cpp in unix
 go to the directory of the example.cpp
 g++ -c -O -fPIC -fexceptions -DNDEBUG -DIL_STD
-I/usr/local/cplex90/include -I/usr/local/concert20/include
./example.cpp –o example.o
 It will create output file called example.o
 g++ -O -fPIC -fexceptions -DNDEBUG -DIL_STD
-I/usr/local/cplex90/include -I/usr/local/concert20/include
example.o -o example
-L/usr/local/cplex90/lib/i86_linux2_glibc2.3_gcc3.2/static_pic
-lilocplex -lcplex
-L/usr/local/concert20/lib/i86_linux2_glibc2.3_gcc3.2/static_pic
-lconcert -lm –lpthread
 Convert it to executable example
 To run it type ./example
25
Download