Lab 1 - Introduction to AMPL

Lab 1 - Introduction to AMPL

AMPL IDE is an environment to run the AMPL system

AMPL is a language to run mathematical optimization programs.

It solves them by means of solvers. They are the ones that do the work. AMPL just manages them giving them a language.

AMPL is thought to shorten the time of developing mathematical problems.

Knapsack problem

In the knapsack problem, you need to pack a set of items, with given values and sizes (such as weights or volumes), into a container with a maximum capacity . If the total size of the items exceeds the capacity, you can't pack them all.

Let

Schermata 2024-10-10 alle 17.20.15.png

We want to maximaze the value we steal.

maxxi=1ncixi

There are constrains (s.a):

i=1naixib0xiui,i=1,2,...,n

The more valuable pieces are the ones with higher ratio: ciai.

I can fill the pack of volume b completely if I accept item can be taken in pieces.

If I define what i is in a set called P, I can reformulate the problem as:

maxxi=1ncixi

with constrains

i=1naixib0xiui,iP

The problem can also be written in matrix form:

maxxcTx

with constrains

aTxb0xu

The .mod file

The mod file defines the structure of the problem. It defines what everything is: like P is a set, a is a parameter... It doesn't assign values.

Schermata 2024-10-10 alle 17.26.34.png

I define the parameters:

set P;
param a {j in P};
param b;
param c {j in P};
param u {j in P};

# I define a variable x
var X {j in P}

# I define the objective function
maxiaze beneficio: sum {j in P} c[j] * X[j]

# I define the constrains
subject to tiempo: sum {j in P} a[j] * X[j]<=b;
subject to Limites {j in P}: 0 <= X[j] <= u[j];

Capital letters matter. Names are case sensitive.

The .dat file

.dat files - contain the data that has to be compatible with the .mod file

It has to be consistent with what is in the .mod file.

Schermata 2024-10-10 alle 17.42.07.png

the aboves are two possible formats.

we define the set P with indexes "bandas" and "bobinas".

Then we assign values to the parameters.

a ha value 200 for "bandas" and 140 for "bobinas".

Execute

MsDoJ

open AMPL

AMPLE > get mod with command - ampl: model <fileName>.mod
AMPLE > get data with dat command - ampl: data <fileName>.dat
AMPLE > solve

We can work in 2 ways:

There are 4 solvers in our systems:

Understanding a model we don't know

If we're given a model of which we don't know the content, there are some commands that can help:

minCost example

minCost.mod

Translates a graph. G=(N,A)

AN×N

This is said as: set ARCOS within (CIUDADES cross CIUDADES);

For network flow problems we don't use var and subject to for the flow variables and constrains.
We use 2 new commands:

xij,(i,j)A

is written as: node Nodo {k in CIUDADES}: net_in = domanda[k]-oferta[k]
The part in the write tells that iN.

❗❗❗❗❗❗❗❗❗❗❗❗
❗❗❗ COMPLETARE ❗❗❗
❗❗❗❗❗❗❗❗❗❗❗❗