ILOG CPLEX l CPLEX is a product developed

  • Slides: 42
Download presentation
ILOG CPLEX l CPLEX is a product developed by ILOG to solve Ø Ø

ILOG CPLEX l CPLEX is a product developed by ILOG to solve Ø Ø Ø LPs MIPs QPs MIQPs Network Flow problems. l CPLEX technologies Ø CPLEX callable library (for C) Ø CPLEX interactive optimizer Ø Concert Technology (for C++) l Compatible platforms Ø Windows Ø Unix. 1

CPLEX interactive optimizer l An interactive program to Ø Load models in PC. Ø

CPLEX interactive optimizer l An interactive program to Ø Load models in PC. Ø Applying algorithms. l Supports features like Ø Ø Choosing different algorithms for problems. Change the specifications in the problem. Sensitivity analysis. Re-optimizing the problem. 2

CPLEX interactive optimizer l Locate cplex. exe in the folder C: /ILOG/CPLEX 70/bin/msvc 6/stat_mda

CPLEX interactive optimizer l Locate cplex. exe in the folder C: /ILOG/CPLEX 70/bin/msvc 6/stat_mda l Make a new folder and copy cplex. exe into it. l 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 l Type help on the command prompt to see the options.

CPLEX interactive optimizer l 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 l. Giapetto’s woodcarving manufactures two types of wooden toys: soldiers

A simple example l. Giapetto’s woodcarving manufactures two types of wooden toys: soldiers and trains Constraints: u 100 finishing hour per week available u 80 carpentry hours per week available uproduce no more than 40 soldiers per week l. Objective: maximize profit 5

Toys’ problem (cont…) Linear Programming formulation: Maximize z = 3 x 1 + 2

Toys’ problem (cont…) Linear Programming formulation: Maximize z = 3 x 1 + 2 x 2 (Obj. Func. ) subject to 2 x 1 + x 2 x 1 0 x 2 0 100 (Finishing constraint) 80 (Carpentry constraint) 40 (Bound on soldiers) (Sign restriction) 6

Solving the toys’ problem (contd…) l Enter the model. CPLEX> enter Enter name for

Solving the toys’ problem (contd…) l Enter the model. CPLEX> enter Enter name for problem: toys' problem Enter new problem ['end' on a separate line terminates]: Maximize 3 x 1+2 x 2 Subject to 2 x 1+x 2 <=100 x 1+x 2 <=80 bounds x 1<=40 end CPLEX> 7

Solving the toys’ problem (contd…) l Optimizing CPLEX> optimize Tried aggregator 1 time. No

Solving the toys’ problem (contd…) l Optimizing CPLEX> optimize Tried aggregator 1 time. No LP presolve or aggregator reductions. Presolve time = 0. 03 sec. Iteration log. . . Iteration: 1 Dual infeasibility = 0. 500000 Iteration: 3 Dual objective = 180. 000000 Dual - Optimal: Objective = 1. 800000 e+002 Solution time = 0. 06 sec. Iterations = 3 (2) CPLEX> 8

Display l To see the results, type display on the command prompt and it

Display l 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 l Type solution to see options you have, Display what: solution Display

Displaying solution l 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

Displaying solution(contd…) CPLEX> display solution variables Display values of which variable(s): Variable Name Solution Value x 1 20. 000000 x 2 60. 000000 (Note: - To see all variables, just press “–” and press enter. CPLEX> display solution objective Dual - Optimal: Objective = 1. 800000 e+002 11

Viewing the model l The model can be printed with following command CPLEX> display

Viewing the model l The model can be printed with following command CPLEX> display problem all Maximize obj: 3 x 1 + 2 x 2 Subject To c 1: 2 x 1 + x 2 <= 100 c 2: x 1 + x 2 <= 80 Bounds 0 <= x 1 <= 40 All other variables are >= 0. CPLEX> 12

Adding a constraint l Command “add” can be used to add new constraints or

Adding a constraint l Command “add” can be used to add new constraints or bounds. CPLEX> add Enter new constraints and bounds ['end' terminates]: x 1+2 x 2<=90 end Problem addition successful. CPLEX> display problem all Maximize obj: 3 x 1 + 2 x 2 Subject To c 1: 2 x 1 + x 2 <= 100 c 2: x 1 + x 2 <= 80 c 3: x 1 + 2 x 2 <= 90 Bounds 0 <= x 1 <= 40 All other variables are >= 0. CPLEX> 13

Change l Using command “change” to change specifications of the problem, CPLEX> change Change

Change l Using command “change” to change specifications of the problem, CPLEX> change Change options: Bounds change bounds on a variable coefficient change a coefficient 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 l An example of “change” CPLEX> change delete constraints c 3 Constraint 3

Change l An example of “change” CPLEX> change delete constraints c 3 Constraint 3 deleted. CPLEX> l Display the model to check, CPLEX> display problem all Maximize obj: 3 x 1 + 2 x 2 Subject To c 1: 2 x 1 + x 2 <= 100 c 2: x 1 + x 2 <= 80 Bounds 0 <= x 1 <= 40 All other variables are >= 0. CPLEX> 15

Saving and loading a model l Model information can be solved to file using

Saving and loading a model l 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'. l 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> l For more information use command help write or help read. 16

Choosing an optimizer l For optimizing, the default solver is dual-simplex. l Other optimizers

Choosing an optimizer l For optimizing, the default solver is dual-simplex. l 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 l For large problems, Ø write a small program in C/C++ which can

Tips l 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. l View a comprehensive introductory manual at www. ise. ufl. eduilogILOGCPLEX 70docgetstartonlin edocindex. html and then choose “Chapter 2 Interactive Optimizer” from the menu on the left. l If still in doubt, bug the TA. 18

ILOG Concert 1. 2 l A C++ library of classes and functions for Ø

ILOG Concert 1. 2 l A C++ library of classes and functions for Ø Defining models. Ø Applying algorithms. l Supports algorithm for both constraint programming and math programming (LP, MIP, QP, etc. ). l Can be integrated with rest of the application in the program. l 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 = 3 x 1 + 2 x

An example Linear Programming formulation: Maximize z = 3 x 1 + 2 x 2 (Obj. Func. ) subject to 2 x 1 + x 2 x 1 0 x 2 0 100 (Finishing constraint) 80 (Carpentry constraint) 40 (Bound on soldiers) (Sign restriction) 20

An example (cont. . ) l Indices i= the item. Soldiers(i=1), Trains (i=2). j=

An example (cont. . ) l Indices i= the item. Soldiers(i=1), Trains (i=2). j= the resources. Finishing time (j=1), Carpentry time (j=2) l 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. l Decision variables Xi number of item i produced l Constraints u Maximum availability of fishing hours. u Maximum availability of carpentry hours. u Maximum production of soldiers. l Objective maximize profit 21

Basic steps l Creating an environment. l Building a model. l Extracting a model

Basic steps l Creating an environment. l Building a model. l Extracting a model for an algorithm. l Solving the problem. l Accessing results. l Ending the program. 22

Complete program #include <ilcplex/ilocplex. h> ILOSTLBEGIN //ILOG standard template library void main() { //

Complete program #include <ilcplex/ilocplex. h> ILOSTLBEGIN //ILOG standard template library void main() { // creating an environment Ilo. Env environment; // creating a model Ilo. Model mymodel(environment); // variable declaration Ilo. Num. Var. Array x(environment, 2, 0, Ilo. Infinity, ILOFLOAT); 23

Complete prgram(contd…) //add the objective to the model mymodel. add(Ilo. Maximize(environment, 3*x[0]+2*x[1])); //adding the

Complete prgram(contd…) //add the objective to the model mymodel. add(Ilo. Maximize(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 Ilo. Cplex mycplex(mymodel); 24

Complete program (contd. . ) // solving the problem. mycplex. solve(); // outcome of

Complete program (contd. . ) // solving the problem. mycplex. solve(); // outcome of the solver. cout << "Solution Status is " << mycplex. get. Status() << endl; // the results cout << " The objective value is " << mycplex. get. Obj. Value() << endl; cout << " The no of soldiers and trains produced are " << mycplex. get. Values(x) <<endl; // ending the program environment. end(); } 25

Output. Tried aggregator 1 time. LP Presolve eliminated 2 rows and 1 columns. Reduced

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 l Alternatively, the constraints and the objective can be added by making

Creating expressions l Alternatively, the constraints and the objective can be added by making expressions for them and then adding them to the model. l Objective mymodel. add(Ilo. Maximize(environment, 3*x[0]+2*x[1])); Ilo. Expr obj(environment); obj += 3*x[0]; obj+= 2*x[1]; mymodel. add(Ilo. Maximize(environment, obj); l Constraint mymodel. add(2*x[0]+3*x[1]<=100); Ilo. Expr finishing_constraint(environment); finishing_constraint += 2*x[0]; finishing_constraint += 3*x[1]; mymodel. add(finishing_constraint <= 100); 27

Multidimensional arrays l To store data using CONCERT, we can use the predefined arrays,

Multidimensional arrays l To store data using CONCERT, we can use the predefined arrays, Ilo. Num. Array array_name(environment, size_array) l A 2 -dimensional array is an array of arrays. So, it can be defined as typedef Ilo. Array<Ilo. Num. Array> Num. Array 2 dim; l In toys problem, the data can be stored in array as, Ilo. Num. Array Profit(environment, 2); Ilo. Num. Array Supply(environment, 2); Num. Array 2 dim Labor(environment, 2); for(int i; i<2; i++) Labor[i]= Ilo. Num. Array(environment, 2); 28

Reading the data l To read the data from the file data_file. txt ifstream

Reading the data l To read the data from the file data_file. txt ifstream ifile("data_file. txt"); l 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() { //

Complete program #include <ilcplex/ilocplex. h> ILOSTLBEGIN //ILOG standard template library void main() { // creating an environment Ilo. Env environment; // creating a model Ilo. Model mymodel(environment); // declaring the data-structure to store data. Ilo. Num. Array Profit(environment, 2); Ilo. Num. Array Supply(environment, 2); Ilo. Array<Ilo. Num. Array> Labor(environment, 2); for(int i = 0; i < 2; i++) Labor[i] = Ilo. Num. Array(environment, 2); 30

Complete program (contd…) // reading the data from a file ifstream ifile("data_file. txt"); ifile

Complete program (contd…) // reading the data from a file ifstream ifile("data_file. txt"); ifile >> Profit >> Supply >> Labor; ifile. close(); // variable declaration Ilo. Num. Var. Array x(environment, 2, 0, Ilo. Infinity, ILOFLOAT); //adding the objective to the model Ilo. Expr objective(environment); for( i = 0; i < 2; i++) objective += Profit[i]*x[i]; mymodel. add(Ilo. Maximize(environment, objective)); 31

Complete program (contd…) //adding the constraints to the model for(int j = 0; j

Complete program (contd…) //adding the constraints to the model for(int j = 0; j < 2; j++) { Ilo. Expr 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 Ilo. Cplex mycplex(mymodel); // solving the problem mycplex. solve(); 32

Complete program (contd…) // outcome of the solver. mycplex. out() << "Solution Status is

Complete program (contd…) // outcome of the solver. mycplex. out() << "Solution Status is " << mycplex. get. Status() << endl; // the results cout << " The objective value is " << mycplex. get. Obj. Value() << endl; cout << " The no of soldiers produced are " << mycplex. get. Value(x[0]) <<endl; cout << " The no of trains produces are " << mycplex. get. Value(x[1]) <<endl; } // releasing the memory environment. end(); 33

MIP l The variables in a mixed-integer program are defined as, // continuous variable

MIP l The variables in a mixed-integer program are defined as, // continuous variable declaration Ilo. Num. Var. Array x(environment, 2, 0, Ilo. Infinity, ILOFLOAT); // integer variable declaration Ilo. Num. Var. Array x(environment, 2, 0, Ilo. Infinity, ILOINT); 34

Selecting an optimizer l The Ilo. Cplex treats all problem it solves as MIP

Selecting an optimizer l The Ilo. Cplex 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 Ilo. Cplex: : set. Root. Algorithm (Ilo. Cplex: : Algorithm alg); void Ilo. Cplex: : set. Node. Algorithm (Ilo. Cplex: : Algorithm alg); Ø Ilo. Cplex: : Dual (use the dual simplex algorithm. ) Ø Ilo. Cplex: : Primal (use the primal simplex algorithm. ) Ø Ilo. Cplex: : Barrier (use the barrier algorithm. ) 35

Selecting the optimizer (contd…) Ø Ilo. Cplex: : Barrier. Primal (use the barrier algorithm

Selecting the optimizer (contd…) Ø Ilo. Cplex: : Barrier. Primal (use the barrier algorithm followed by the primal simplex algorithm) Ø Ilo. Cplex: : Barrier. Dual (use the barrier algorithm followed by the dual simplex algorithm ) Ø Ilo. Cplex: : Network. Primal (use the network simplex algorithm for the embedded network followed by the primal simplex algorithm ) Ø Ilo. Cplex: : Network. Dual (use the network simplex algorithm for the embedded network followed by the dual simplex algorithm) Ø Ilo. Cplex: : Dual. Barrier (use the dual simplex algorithm up to an iteration limit, then switch to barrier ) 36

Setting CPLEX parameters l Open url www. ise. ufl. eduILOGCPLEX 70docrefmanonlinedocin dex. html and

Setting CPLEX parameters l Open url www. ise. ufl. eduILOGCPLEX 70docrefmanonlinedocin dex. html and choose Appendix A from the menu on the left // -- set the problem solving time upper bound cplex. set. Param(Ilo. Cplex: : Ti. Lim, 60); // -- Turn on/off the simplex iteration on screen message cplex. set. Param(Ilo. Cplex: : Sim. Display, 1); // -- explicitly set the algorithm used to solve root cplex. set. Root. Algorithm(Ilo. Cplex: : Barrier. Primal); 37

Setting CPLEX parameters (contd. . ) l Parameters for MIP //-- Turn on/off MIP

Setting CPLEX parameters (contd. . ) l Parameters for MIP //-- Turn on/off MIP node log on screen message cplex. set. Param(Ilo. Cplex: : MIPDisplay, 1); //-- Setting optimality/feasibility as the driving objective. cplex. set. Param(Ilo. Cplex: : MIPEmphasis, 1); //-- explicitly set the algorithm used to solve node cplex. set. Node. Algorithm(Ilo. Cplex: : Barrier. Primal); //-- explicitly set the number of feasible solutions to be found before terminating cplex. set. Param(Ilo. Cplex: : Int. Sol. Lim, 2); //-- explicitly declare to do Breadth/Depth/Best first search. cplex. set. Param(Ilo. Cplex: : Node. Sel , 2); //-- explicitly set to apply Gomory cut aggressively cplex. set. Param(Ilo. Cplex: : Frac. Cuts, 2); 38

Tips l While solving an MIP, try different setting of the parameters mentioned above.

Tips l 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. set. Param(Ilo. Cplex: : Pre. Ind, 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. eduILOGCPLEX 70docusermanonlinedocinde x. html , chapter 6 for more details. 39

Settings for CONCERT 1/ Start Microsoft Developer Studio. The first step is to build

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 "Win 32 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

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 Win 32 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: ILOGCPLEX 80include 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: ILOGCONCERT 12include in the Additional include directories text box. 41

Settings for CONCERT 7/ From the tabs at the top of the dialog box,

Settings for CONCERT 7/ From the tabs at the top of the dialog box, select the Link type of settings. - Add the file C: ILOGCPLEX 80libmsvc 6stat_stacplex 80. lib in the Object/Modules text box. - If your application uses Concert Technology, add the files C: ILOGCPLEX 80libmsvc 6stat_stailocplex. lib C: ILOGCONCERT 12libmsvc 6stat_staconcert. lib - Add the file wsock 32. lib in the Object/Modules text box if it is not already there. 8/ Click on OK. Finally you have to set test Win 32 Release as the default project configuration and build it. 9/ From the Build menu, select Set Active Configuration - Select test Win 32 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>Releasetest. exe 42