Вы находитесь на странице: 1из 10

Introduction to Modeling System: AMPL

AMPL is an modeling language which can help us to formulate optimization problems by using sets, sums, etc. To solve the formulated optimization problems, solver software packages, which are included in AMPL, should be used. Which solver software package should be used depends on the type of the optimization problem (e.g., LP-problem, integer problem, nonlinear problem, etc.) to be solved. Example of commonly available solvers are CPLEX, OSL and MINOS. AMPL is available in DOS version, UNIX version and Windows version.

Modeling
Three different files are needed for solving an optimization problem with AMPL: Model file, input data file and command file. Usually, a model file has an extension .mod (or .txt), an input data file has an extension .dat and a command file has an extension .run (or .txt). The command file (e.g., testrun.run, or testrun.tex) specifies: which solver should be used name of the model file (e.g., testmod.txt) name of the input data file (e.g., testdat.txt) name of the result data file (e.g., testout.txt) and how the output data should be presented in the file

The following simple production planning problem is a Linear Programming problem and will be used to illustrate the modeling in AMPL. max z = 120xS + 190xL + 180xP subject to 0.30xS + 0.30xL + 0.30xP 800 0.25xS + 0.50xL + 0.45xP 800 0.30xS 570 0.60xL + 0.50xP 840 xS , xL , xP 0

(Cutting) (Forming) (Mounting S) (Mounting L and P)

One way to model the LP problem is to put all numeric values of the parameters of the model (i.e., objective function and constraints) directly into a model file. The model file, small.txt for the LP problem is shown as follows: #---------------------------------------# Model file: small.txt #---------------------------------------var xs >=0; # Variable definition var xl >=0; # Variable definition var xp >=0; # Variable definition

maximize z: subject to subject to subject to subject to

120*xs+190*xl+180*xp; #Objective function Cutting: 0.30*xs+0.30*xl+0.30*xp <= 800; Forming: 0.25*xs+0.50*xl+0.45*xp <= 800; Mounting_S: 0.30*xs <= 570; Mounting_LP: 0.60*xl+0.50*xp <= 840;

Observe that every constraint must have a name (e.g., Cutting). Observe also that every row is ended with ";" (semi-colon) and comments can be written after "#". The command file, smallrun.txt, is shown as follows: #---------------------------------------# Command file: smallrun.txt #---------------------------------------reset; option solver cplex; model small.txt; solve; display z; display xs; display xl; display xp; exit; The first command of the command file is "reset" which initializes the internal memory used by AMPL to zero. With the command "option solver cplex", one can select the solver cplex for solving the problem. The command "model small.txt" reads the model from the model file, small.txt. The solver program is started by the command "solve". The command "display" shows the results of the given variables on the screen. After execution of the program, the following results are shown: Z = 358000 xs = 1900 xl = 0 xp = 722.222 An alternative way to formulate a problem with AMPL is to give general parameters in a model file and use an input data file to specify numeric values of the parameters. Consider that the general LP problem can be written in the following form: Max

z c j x j
j 1
n

Subject to

a
j 1

ij

x j bi ,

i = 1,..,m j = 1, .., n

x j 0,

The model file, lp.txt in AMPL is #---------------------------------------# Model file: lp.txt #---------------------------------------param n; # Number of variables param m; # Number of constraints param c{1..n}; # Objective function coefficients param a{1..m,1..n}; # Constraint coefficients param b{1..m}; # Right-hand side var x{1..n} >= 0; # Variable definition maximize z: # Objective function sum {j in 1..n} c[j]*x[j]; subject to Constraints{i in 1..m}: # Constraints sum {j in 1..n} a[i,j]*x[j] <= b[i]; In the model file, n, m, c, a and b are parameters where n gives the number of variables and m the number of constraints. Parameters c, a and b define the cost vector, the constraint matrix and the right-hand side vector, respectively. In the brackets, the notation 1..n gives the set of all variables and the notation 1..m the set of all constraints. The var declaration declares the variable x and the summation sum {j in 1..n} sums all terms indexed by j. In the model file, the number of parameters and the actual values of the parameters are not defined. The advantage of this formulation is that the model looks the same for different sizes of problems and different input data. One just need to change the input data file in order to solve different problems. The LP problem which is formulated earlier in the model file small.txt can be solved by the model file lp.txt. We need to give the values of all parameters in an input data file lpdata.txt as follows: #---------------------------------------# Data file: lpdata.txt #---------------------------------------param n := 3; # Number of variables param m := 4; # Number of constraints param c := # Objective function coefficients 1 120 2 190 3 180; param a: 1 2 3 := # Constraint coefficients 1 0.3 0.3 0.3 2 0.25 0.5 0.45 3 0.3 0 0 4 0 0.6 0.5; param b := # Right-hand side 1 800 2 800 3 570 4 840;

The command file lprun.txt looks more or less the same as smallrun.txt. The difference is that an input data file must be given by the command data. The command file also shows how one can give a name of an output data file for writing results. This is done by typing "> lpout.txt" directly after the commands solve and display. #---------------------------------------# Command file: lprun.txt #---------------------------------------reset; option solver cplex; model lp.txt; data lpdata.txt; solve > lpout.txt; display x > lpout.txt; exit;

Syntax
Some of the most important commands in AMPL are described here. AMPL distinguishes upper-case letters from lower-case letters and therefore, for example, x and X are regarded as different variables. Notice that AMPL does not end a command or a declaration on a line break, and every command or declaration must be ended with a semi-colon (;).

var
All variables must be declared. Variables are declared by var. var var var var var var x; y binary; q integer; x{1..8}; x{1..8,1..20}; weight{Cars} # A floating-point variable # A binary variable # An integer variable # A vector of variables x[i], i=1..8 # A matrix of variables x[i,j] # A vector of variables weight[j], # where j belongs to a set Cars #(See syntax for set below) # A vector of variables y[j], # where y[j]>=0 and j belongs a set J

var y{J}>=0;

param
Parameters are declared with param. A constant value is assigned to a declared parameter as follows: param a; param C := 7; # Declare parameter a # Parameter C is declared and # assigned a constant value 7

Constant values are assigned to a declared vector of parameters as follows: param vec := 1 50 2 75 3 100; # The vector vec is declared # and assigned values vec[1]=50, vec[2]=75, vec[3]=100 Syntax for assigning constant values to a matrix of parameters: Param matr : 1 2 3 := 1 80 9 77 2 11 120 13; # matrix matr is assigned values matr[1,1]=80, # matr[1,2]=9, matr[1,3]=77, matr[2,1]=11 . . . Sometimes it is simpler to generate data in a transposed matrix. We can use (tr) to do this as follows: Param matr (tr): 1 80 2 9 3 77 1 11 120 13; 2 :=

If we have a 3-dimensional matrix with element mat3[i,j,k], we can assign values in every part of it. Suppose that we have a 2x2x2 matrix, we can assign values as follows: Param mat3 default 0 [*,*,1]: 1 2 := 1 80 . 2 . 44 [*,*,2]: 1 2 := 1 . 16 2 7 4; :=

The use of default 0 means that the elements marked by a point (.) should be assigned a default value 0.

maximize / minimize
Objective functions can be specified by using maximize or minimize. Notice that an objective function must be given a name. The name is followed by a colon (:) and then the objective function. maximize profit: sum {i in Units} c[i]*x[i];

subject to
A constraint or a group of constraints can be specified by subject to. Notice that a constraint or a group of constraints must be given a name. The name is followed by a colon (:) and then the constraint or the group of constraints. subject to constraint1: x + y = 7; subject to stock{i in Products}: Produced[i] + Availab[i] Sold[i] <= Stockcap[i]; subject to constraint3{i in N}: sum {j in 1..i} x[j] >= d[i];

set
A set of numerical values or a set of symbols can be declared by set. If the name of a set is followed by ordered, the defined ordering of the set is kept when the set is printed out . Set Set Set Set CARS := SAAB VOLVO BMW; OddNum := 1 3 5 7 9; NUMBER := 4 6 1 3; WEEKS ordered; # Symbols in the set WEEKS can be # given in a data file

sum
Summation of a number of variables is specified by sum in objective functions and constraints. Sum{i in CARS} Weight[i] # Summation of Weight for # all the cars in the set CARS sum{i in 1..20} (x[i]-y[i]) #Summation x[i]-y[i],i=1,..,20

display
The command display is used in command file for showing the solution of a problem. Display x; # Display x on screen Display x > fileout.txt;# Print out x to a file Display con.dual; # Display dual variables of # constraint con Display con.slack; # Display slack variables of # constraint con Display x.rc; # Display reduced cost for x If one wants to control the display or printing, one can use a number of option commands: option option option option omit_zero_cols; # Display non-zero columns omit_zero_rows; # Display non-zero rows display_eps .0001; # Define the limit for non-zero solution_round 6; # Number of decimals in a solution

Other commands
The following commands can be used when CPLEX is used as a solver. If one solves large integer problems, it is sometimes necessary to set tolerance and time requirements. option cplex_options time 360; # break after 360 seconds option cplex_options timing 1; # show CPU time option cplex_options mipgap 0.0001; # specify tolerance # in B&B tree If one wants to use some of the above commands together, one can use the following command. It is only valid for CPLEX-options. option cplex_options time 360 timing 1 mipgap 0.0001; If one wants to have information on the size of a model, for example, the number of variables and number of constraints, one can use option show_stats 1; # 1: set the option of display info # 0: remove the option of display info If one has an integer problem and wishes to solve the problem with LP-relaxation, one can use option relax_integrality 1; # 1: solve LP-relaxation # 0: solve integer problem

If one wants to describe cardinality, for example, how many variables x which is 1 in a set COLS, one can use card{j in COLS: x[j]=1}; In many solvers, there is an algorithm which tries to reduce the number of constraints and variables of a problem before it is solved by, for instance, CPLEX. If one wants to perform sensitivity analysis, it is important to disable this algorithm since certain constraints may be removed by the algorithm. This can be done by option presolve 0;

Sensitivity analysis
If one wants to perform sensitivity analysis, one would like to display or print out as much information as possible. In AMPL, there are a number of useful commands for this task. The following is an example of a command file for all types of models. #-------------------------------------# Command file: ogrun.txt #-------------------------------------reset; # Initialize memory to 0 option solver cplex; # Selection of the solver cplex option presolver 0; # Do not use preprocessing option cplex_options sensitivity; # Sensitivity analysis model og.txt; # Read the model file data ogdata.txt; # Read the data file solve; # Solve the problem display _objname, _obj > ogout.txt # Print out the value of the objective function display _varname, _var, _var.rc > og.res # Print out names, values and reduced costs display _varname, _var.down, _var.current, _var.up > ogout.txt # Print out names, objective function coefficients # together with what can be changed without causing # a base change # down = lower limit, up = upper limit display _conname, _con.slack, _con.dual > ogout.txt # Print out constraint name, slack and shadow price display _conname,_con.down,_con.current,_con.up >ogout.txt # Print out constraint name, right-hand side coefficient # together with what can be changed without causing # a base change

Transportation problem
AMPL is very useful for modeling and solving structured problems. Here, we study a transportation problem to illustrate how to use AMPL. The following transportation problem is formulated to show how to define sets and perform summation over all indexes in a set. min z cij xij
iI jJ

Subject to

x
iI

jJ

ij

Si,

iI jJ

(Supply) (Demand)

ij

D j,

i I , j J xij 0, The general model for a transportation problem can be written as follows. #---------------------------# Model file: trp.txt #---------------------------set I; # source set J; # sink param S{I} >=0; # Supply param D{J} >=0; # Demand param c{I,J} >=0; # Cost var x{I,J} >= 0; minimize z: sum{i in I, j in J} c[i,j]*x[i,j]; subject to supply {i in I}: sum{j in J} x[i,j] <= S[i]; subject to demand {j in J}: sum{i in I} x[i,j] = D[j]; In the data file for this example, one can write the following.
#---------------------------# Data file: trpdata.txt #---------------------------set I := F1 F2 F3 F4; # Define source set J := K1 K2 K3 K4 K5; # Define sink param S := # supply, values of S[i] F1 30 F2 40 F3 30 F4 40; param D := # demand, values of D[j] K1 20 K2 30 K3 15 K4 25 K5 20; param c: # Cost, values of c[i,j] K1 K2 K3 K4 K5 := F1 2.8 2.55 3.25 4.3 4.35 F2 4.3 3.15 2.55 3.3 3.5 F3 3 3.3 2.9 4.3 3.4 F4 5.2 4.45 3.5 3.75 2.45;

10

Вам также может понравиться