Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 40

AMPL INTRODUCTION

 Mathematical programming is a technique for solving certain


kinds of problems notably maximizing profits and minimizing
costs subject to constraints on resources, capacities, supplies,
demands.
 AMPL is a language for specifying such optimization problems
 Before starting to write the problem into the AMPL we need to
convert the given problem into linear model, so that we can clear
idea about the implementation of the problem.
 AMPL program (model file) consists following things
 sets, like the products
 parameters, like the production and profit rates
 variables, whose values the solver is to determine
 constraints that the solution must satisfy.
 an objective, to be maximized or minimized
 Data file consists actual values for sets and parameters
AMPL: Model and Data
 In AMPL, model and data can be kept
separated
 Model file saves with extension .mod and
data file with extension .dat.
 Useful when you have several instances
of the same
 Data section starts with command data;

 In data sections, we assign values to


parameters;
AMPL: Sets and indices
 Sets can be referred to with indices.
 Useful to specify an element of a
vector parameter/variable.
 Example:
– param n;
– set S = 1..n;
– param lb {S};
– param ub {S};
– var x {i in S} >= lb[i] <= ub[i];
AMPL: Parameters
 Parameterscan be defined with
param. They are an input to the
problem.
– param pi = 3.14159265358979323846;
– param Vol = 20; # volume of each tin
can
– param price_per_gallon; # Can set it
later!
AMPL: Variables
 The command var specifies a
variable of the problem, its bounds,
its type, and (possibly) an initial
value.
– var x1 >= 0 <= 4;
– var numtrucks >= 3 integer default 5;
– var buyObj1 binary;
AMPL: Constraints
 Constraints are preceded by a name
and a “:”
 myconstr1: x1 + 3*x2 + x3 <= 4;
 c2: numtrucks >= ntr_AZ + ntr_NY +
ntr_PA;
AMPL: Objective function
 Preceded
by either minimize or
maximize, name of the objective,
and “:”

 minimizexpense: 10*numtrucks +
3*numcars;
 maximize distance: x1 + 2*cos(x3);
Some more important things in AMPL
 The semicolon: all commands must end with
one;
 Comments: # this won’t be interpreted
 To solve a problem, choose a solver:
 CPLEX: for linear and integer programming

 MINOS: for nonlinear programming

 With the command option solver cplex;


 Guess what the reset; command does?
AMPL: Set operations
 LetA, B, and U be sets
 AMPL allows simple set operations:

– Union: U = A union B
– Intersection: U = A inter B
– Cross Product: U = { A , B }
 Theseoperations become useful or
necessary in many problems
Advantage of Set Operations
 Suppose we have 3 sets of products,
– CANDY
– TOYS
– GAMES

 In our model we may need to declare the


following parameters for each:
– Price
– Supply
– Demand
Naïve Setup
– Param Price_C {CANDY};
– Param Price_T {TOYS};
– Param Price_G {GAMES};
– Param Supply_C {CANDY};
– Param Supply_T {TOYS};
– Param Supply_G {GAMES};
– Param Demand_C {CANDY};
– Param Demand_T {TOYS};
– Param Demand_G {GAMES};
Using Set Operation Union

Param Price {CANDY union TOYS union GAMES};


Param Supply {CANDY union TOYS union GAMES};
Param Demand {CANDY union TOYS union GAMES};
Even Better…

Set PRODUCTS =
CANDY union TOYS union
GAMES;

Param Price {PRODUCTS};


Param Supply {PRODUCTS};
Param Demand {PRODUCTS};
Compound Sets
 Suppose you have a set called
PRODUCTS of products to sell at a
store
 Consider that instead of one store
you have three stores in different
parts of the country
 Each store has a different level of
each paramater
One Solution
 Instead
of using the set PRODUCTS,
make three different sets:
– PRODUCTS_1
– PRODUCTS_2
– PRODUCTS_3
 Leteach of the three numbers
represent one of your store locations
One Solution (cont…)
 Next, define each parameter for each
set of products:
– Param Price {PRODUCTS_1};
– Param Supply {PRODUCTS_1};
– Param Demand {PRODUCTS_1};
– Param Price {PRODUCTS_2};
– Param Supply {PRODUCTS_2};
–…
Easier Solution
For a better solution use compound
sets:

– Param Price {PRODUCTS, STORES};


– Param Supply {PRODUCTS, STORES};
– Param Demand {PRODUCTS, STORES};
Structure of {PRODUCTS,
STORES}
 Suppose
– PRODUCTS := oreos jenga lazertag ;
– STORES := 1 2 3 ;
 Then {PRODUCTS, STORES} is the set of
all combinations of products with stores:

(oreos,1) (oreos,2) (oreos,3)


(jenga,1) (jenga,2) (jenga,3)
(lazertag,1) (lazertag,2) (lazertag,3)
Specifying Data
 In
your .dat file, your declaration of
Demand could look like this:

param Demand: 1 2 3 :=
oreos 10 20 30
jenga 30 33 42
lazertag 40 30 22 ;
AMPL: Set indexing
 Indexing of a one dimensional set

sum {i in PRODUCTS} cost[i]*make[i];

 Indexing is similar in compound sets


 One can say

sum { (i,j) in {PRODUCTS, STORES}} cost[i]*make[i,j];


or
sum { i in PRODUCTS, j in STORES} cost[i]*make[i,j];
When do we need indexing?
 We may declare a parameter with or
without giving index values:

param Demand { PRODUCTS, STORES };

or

param Demand { i in PRODUCTS, j in STORES };


AMPL: Transpose
 Recall an earlier example:

Param Demand: 1 2 3 :=
oreos 10 20 30
jenga 30 33 42
lazertag 40 30 22 ;
How to Transpose

 Data in Transposed form

Param Demand (tr): oreos jenga


lazertag :=
1 10 30 40
2 20 33 30
3 30 42 22 ;
Why Transpose
 Not Necessary, but can help with data
management

Param Demand (tr): oreos jenga lazertag


:=
1 10 30 40
2 20 33 30
3 30 42 22 ;
AMPL: Omitted Data Entry
Example: Consider the following data

Param: Cost Supply Demand


:=
oreos 10 20 30
jenga 30 33 42
lazertag 40 30 22 ;
Omitted Data Entry
Suppose in addition to the data
specified in the previous table, you
have an additional parameter such
as:

Param Calories {FOOD};

This parameter would apply to oreos,


but not to jenga or lazertag.
Example
Param: Cost Supply Demand :=
oreos 10 20 30
jenga 30 33 42
lazertag 40 30 22 ;

Param: Calories :=
oreos 100 ;
Example

Param: Cost Supply Demand Calories:=


oreos 10 20 30 100
jenga 30 33 42 .
lazertag 40 30 22 . ;

We can use “.” to represent omitted data


AMPL: Operation with sets
 InLinear and Integer Programming,
we’ll see very often the notation
∑(i=1 ..n) aixi.
– How can that be expressed in AMPL?
 param n;
 set S = 1..n;

 param a {S};

 var x {S};

 minimize linFun: sum {i in S} a [i] * x[i];


AMPL: A two-variable linear program
 Tons per hour : Bands 200
Coils 140
Profit per ton : Bands $25
Coils $30
Maximum tons : Bands 6000
Coils 4000
If 40 hours of production time are available, how
many tons of bands and how many tons of coils
should be produced to bring in the greatest total
profit?
The two-variable linear program in
AMPL
 Var X;
Var Y;
maximize Profit : 25*X+30*Y;
subject to Time :
(1/200)*X+(1/140)*Y<=40;
subject to X_limit : 0<=X<=6000;
subject to Y_limit : 0<=Y<=4000;

 The file – call it prod0.mod


AMPL: Executing commands
 Solve prod0.mod
ampl : option solver cplex;
ampl : model prod0.mod;
ampl : solve;
IBM ILOG CPLEX Optimization Studio Preview Edition good for 82 more days.
The CPLEX Optimizers will solve problems up to 500 variables and 500 constraints
.
CPLEX 12.2.0.0: LP Presolve eliminated 1 rows and 2 columns.
All rows and columns eliminated.
optimal solution; objective 188571.4286
0 dual simplex iterations (0 in phase I)

ampl : display X, Y;
X=6000
Y=1400

ampl : quit;
The two-variable linear program in
AMPL
 Each variable is named in a var statement
 Each constraint by a statement that begins
with subject to and a name like X_limit or
Time for the constraint
 Multiplication requires an explicit *
operator
 ≦ relation is written <=
A linear programming model
 Figure 1-1 shows the production problem in
algebraic notation
AMPL: Improved model
AMPL: Data file
Features of AMPL
AMPL supports wide range of problem types
 Linear Programming
 Mixed Integer Linear Programming
 Quadratic Programming
 Mixed Integer Quadratic Programming
 Quadratic Constrained Programming
 Mixed Integer Quadratic Constrained
Programming
- Millions of constraints and variables
Advantages
 Syntax is almost similar to the
mathematical notation of optimization
problems.
 This allows for a very concise and readable
definition of problems in the domain of
optimization.
AMPL References

 Refer to AMPL web site, www.ampl.com,


for more up to date information and
resources
 For GUI

– http://www.ampl.com/GUI/expermt.html
Thank you.

Any Questions ?

Email: manoj.imm@gmail.com
harishreddygavin@gmail.com

You might also like