ilog cplex

25
1 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.

Upload: aya

Post on 17-Jan-2016

129 views

Category:

Documents


1 download

DESCRIPTION

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. CPLEX interactive optimizer. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ILOG CPLEX

1

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.

Page 2: ILOG CPLEX

2

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.

Page 3: ILOG CPLEX

3

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

Page 4: ILOG CPLEX

4

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.

Page 5: ILOG CPLEX

5

Page 6: ILOG CPLEX

6

CPLEX interactive optimizer (help)

Type help on the command prompt to see the options.

CPLEX> helpadd add constraints to the problembaropt solve using barrier algorithmchange change the problemdisplay display problem, solution, or parameter

settingsenter enter a new problemhelp provide information on CPLEX commandsmipopt solve a mixed integer programnetopt solve the problem using network methodoptimize solve the problem (default is dual-simplex)primopt solve using the primal methodquit leave CPLEXread read problem or basis information from a fileset set parameterstranopt solve using the dual methodwrite write problem or solution info. to a filexecute execute a command from the OS

Page 7: ILOG CPLEX

7

Entering a problem from the keyboard

CPLEX> enterEnter name for problem: exampleEnter new problem ['end' on a separate line terminates]:max 4x+6ysubject tox+y<5End

CPLEX> optimizeTried 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+01Solution time = 0.00 sec. Iterations = 0 (0)

Page 8: ILOG CPLEX

8

CPLEX> display

Display Options:iis display infeasibility diagnostics (IIS constraints)problem display problem characteristicssensitivity display sensitivity analysissettings display parameter settingssolution display existing solutionDisplay what: solutionDisplay Solution Options:basis display a range of basic constraints or variablesbestbound display the current MIP best bounddual display a set of solution dual valueskappa display the condition number of the basis matrixobjective display solution objective valueqcslacks display a set of solution quadratic constraint slack valuesquality display quality of solutionreduced display a set of solution reduced costsslacks display a set of solution slack valuesvariables display a set of solution variable values

Display which part of the solution: varDisplay values of which variable(s): -Variable Name Solution Valuey 5.000000All other variables in the range 1-2 are zero.

Page 9: ILOG CPLEX

9

Reading from the input file

Example input file:max 4x+6ysubject tox+y<5End

Save it as example.lp under your directory, let us say your directory is called “6417example”

Page 10: ILOG CPLEX

10

CPLEX> read 6417example/example.lpProblem '6417example/example.lp' read.Read time = 0.00 sec.CPLEX> optimizeTried 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+01Solution time = 0.00 sec. Iterations = 0 (0)

CPLEX> display solution variables – (shows all optimal variable values)

Page 11: ILOG CPLEX

11

CPLEX> display problem all

Maximizeobj: 4 x + 6 ySubject To c1: x + y <= 5Bounds All variables are >= 0.CPLEX> addEnter new constraints and bounds ['end' terminates]:x+y<8endProblem addition successful.

Page 12: ILOG CPLEX

12

Binary or integer variables

Before end we need to write

Binariesxyint ztEnd

Page 13: ILOG CPLEX

13

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)

Page 14: ILOG CPLEX

14

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

Page 15: ILOG CPLEX

15

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.

Page 16: ILOG CPLEX

16

An example

Linear Programming formulation:

min

,...,

,...,

,

ij iji j

ij ji

ij ij

ij

cx

x D j

x S i

x forall i j

10 10

1 1

10

1

10

1

1 10

1 10

0

Page 17: ILOG CPLEX

17

Basic steps

Creating an environment.

Building a model.

Extracting a model for an algorithm.

Solving the problem.

Accessing results.

Ending the program.

Page 18: ILOG CPLEX

18

Creating an environment and a model

#include <ilcplex/ilocplex.h>ILOSTLBEGIN // ILOG standard template libraryvoid main(){

// creating an environmentIloEnv env1; // name of the environment is env1// creating a modelIloModel 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.

Page 19: ILOG CPLEX

19

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 arrayfor(int i=0;i<10;i++)x[i]= IloNumVarArray(env1,10);

//x is a matrix x[i][j]…10*10

Page 20: ILOG CPLEX

20

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);

Page 21: ILOG CPLEX

21

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();

1 3 3 4 5 5 5 5 5 5 3 4 5 5 5 5 5 5 6 6........ . . . . . . . .

Page 22: ILOG CPLEX

22

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]);}

Page 23: ILOG CPLEX

23

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]);}

Page 24: ILOG CPLEX

24

Solving and output

// handing over the model to the algorithmIloCplex mycplex(mymodel); // solving the problemmycplex.solve();mycplex.out() << "Solution Status is " << mycplex.getStatus()

<< endl;// the resultscout << " 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 memoryenv1.end();

Page 25: ILOG CPLEX

25

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