How to define a parameter x(t)=an element from a set

503 views
Skip to first unread message

charis ntakolia

unread,
Apr 7, 2016, 6:46:26 AM4/7/16
to AMPL Modeling Language
In my code at data file I have defined a set of nodes
 set NODES := n0 n1 n2 n3 n4 n5 n6;

and the set of airplanes
set AIR := k1 k2 k3 k4;

that represents the airports and I have a parameter depar(k) that shows for each airplane k from which airport it departs.
so I wrote
param: depar :=
       k1    n1
       k2    n3
       k3    n2
       k4    n5;

However I got a syntax error.
Which is the right way to write define this parameter?

I attach the whole files for the example with 4 airplanes

Thank you for your help in advance.
4planes.dat
4planes.mod

DINESH BABU

unread,
Apr 7, 2016, 9:06:42 AM4/7/16
to AMPL Modeling Language
Hi Charis,

My name is Dinesh. I looked your code and I am also getting error in using Index and subscripts of code. I don't know how to solve. Could you look my post "https://groups.google.com/forum/#!topic/ampl/OCh2YlxbbHk" and tell me your suggestion?

Robert Fourer

unread,
Apr 7, 2016, 10:21:00 PM4/7/16
to am...@googlegroups.com
By default AMPL assumes that parameters take numerical values. If you want a parameter to be able to take string values like "n0", "n1", etc., then you need to add "symbolic" to its definition. For example in your model you could write:

param depar {AIR} symbolic in NODES;

Then your data statements will be accepted.

Bob Fourer

=======

char...@gmail.com

unread,
Apr 8, 2016, 8:26:30 AM4/8/16
to AMPL Modeling Language, 4...@ampl.com
Dear Fourer,
thank you for your fast respond. Now the error that I get is:
ampl: reset; model 4planes.mod; data 4planes.dat; option solver cplex; solve;

4planes.mod, line 82 (offset 2482):
 syntax error
No constraints or objectives.
ampl:

the line 82 is about the objective function:
#OBJECTIVE FUNCTION
# the objective function minimizes the total cost of airborne and ground-holding delays,
# speed deviations and cancellation costs
var AGDelays =
 sum {k in AIRPLANES, t in PERIODS} 
  (fuelcost * ((t - ta[k]) * f[k,arr[k],n0,t] - (t - td[k]) * f[k,n0,depar[k],t]) (line 82)
   +
   ghcost * (t - td[k]) * f[k,n0,depar[k],t]);
  
var SpeedDevs =
 sum { k in AIRPLANES, (i,j) in ARCS}
  (addfcost * abs(r[k,i,j] * Tpr[k] - sum{t in PERIODS} f[k,i,j,t]));
  
var Cancels =
 sum { k in AIRPLANES}
   cancost * (1 - r[k,n0,depar[k]]);
  
minimize Total_Cost:
 AGDelays + SpeedDevs + Cancels;

 Now I will show you how I have defined the above parameters and variables used in these expressions:

# the set of time periods obtained discretizing the considered time horizon
set PERIODS := 1..24; does this mean that the elements of this set are integers? because in line 82 as you saw I subtract t-ta[k]

# scheduled departure time of airplane k
param td {AIRPLANES} > 0 integer;

# duration of flight of aiplane k
param dur{AIRPLANES} > 0 integer;

# the estimated arrival time of airplane k to their destination
param ta {k in AIRPLANES} = td[k] + dur[k] integer; (what I actually want to do here is to find the arrival time by adding the duration of the flight to the departure time)

I also attach the files in case you want to check something else in the code.


 
4planes.dat
4planes.mod

Robert Fourer

unread,
Apr 8, 2016, 11:37:08 PM4/8/16
to am...@googlegroups.com
Since param fuelcost is indexed over {AIRPLANES}, you need to multiply by fuelcost[k] in the expression for AGDelays. There are similar errors with several other parameters.

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of char...@gmail.com
Sent: Friday, April 8, 2016 5:37 AM
To: AMPL Modeling Language

char...@gmail.com

unread,
Apr 9, 2016, 7:49:22 AM4/9/16
to AMPL Modeling Language, 4...@ampl.com
Dear Bod,
thank you for the notice, I was so sure that I have written it correctly that I couldn't see the missing [k].

I corrected it and now I get many errors.
ampl: reset; model 4planes.mod; data 4planes.dat; option solver cplex; solve;

4planes.mod, line 82 (offset 2502):
 n0 is undefined (how it can be undefined I have defined it @data file)

4planes.mod, line 82 (offset 2535):
 n0 is undefined

4planes.mod, line 84 (offset 2593):
 n0 is undefined

4planes.mod, line 92 (offset 2818):
 n0 is undefined

4planes.mod, line 106 (offset 3456):
 n0 is undefined

4planes.mod, line 112 (offset 3750):
 n0 is undefined

4planes.mod, line 118 (offset 4022):
 k is undefined

4planes.mod, line 123 (offset 4228):
 n0 is undefined

4planes.mod, line 123 (offset 4249):
 t is undefined

4planes.mod, line 123 (offset 4249):
 n0 is undefined

Bailing out after 10 warnings.
Error executing "solve" command:
error processing constraint Arc_Capacity[1,'n1','n2']:
 invalid subscript f['k','n2','n1',1]

maybe there is a problem with the way I have defined my decision variables
#VARIABLES
# artificial variable that equals 1 if airplane k has travelled arc(i,j), otherwise is 0
var r{k in AIRPLANES, i in NODES, j in NODES : (i,j) in ARCS} binary;

# decision variable that equals 1 if airplane k has travelled arc(i,j) at time t, otherwise is 0
var f{k in AIRPLANES, i in NODES, j in NODES, t in PERIODS : (i,j) in ARCS} binary;


4planes.dat
4planes.mod

Robert Fourer

unread,
Apr 10, 2016, 3:07:48 PM4/10/16
to am...@googlegroups.com
When you write an expression like f[k,arr[k],n0,t] in the model (in the definition of var AGDelays), AMPL interprets n0 as referring to a param named n0. So, since your model doesn't define a param named n0, the message "n0 is undefined" appears.

If instead you want to refer in the model to the string n0 (one of the members of set NODES) then you need to put it in quotes in the model to show AMPL that it is a string. For example you could write f[k,arr[k],"n0",t]. (You can also write "n0" in the .dat file, but the simpler syntax of .dat files allows us to makes the quotes optional and save you the trouble of entering all of those " symbols.

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of char...@gmail.com
Sent: Saturday, April 9, 2016 5:59 AM
To: AMPL Modeling Language
Cc: 4...@ampl.com

char...@gmail.com

unread,
Apr 11, 2016, 7:17:46 AM4/11/16
to AMPL Modeling Language, 4...@ampl.com
Dear Bob,
thank you for your valuable help.
I have already made some more changes and now the only error I get is the following:

ampl:  reset; model 4planes.mod; data 4planes.dat; option solver cplex; solve;

Error executing "solve" command:
error processing constraint Arc_Capacity[1,'n22','n29']:
 invalid subscript f['k1','n29','n22',1]

I don't know how I get this error because the code is:
# Arc capacity constraint: it ensures that the number of airplane traverses arc (i,j) at time period t
# will not exceed its capacity for that period.

subject to Arc_Capacity {t in PERIODS, (i,j) in ARCS}:
 sum{k in AIRPLANES} (f[k,i,j,t] + f[k,j,i,t]) <= C;

so how it choses arcs (n22, n29) or (n29, n22) since I haven't defined them in my data file as elements of ARCS.
I also tried to write
subject to Arc_Capacity {t in PERIODS, i in NODES, j in NODES: (i,j) in ARCS}:
 sum{k in AIRPLANES} (f[k,i,j,t] + f[k,j,i,t]) <= C;
but I got the same error.
4planes.dat
4planes.mod

char...@gmail.com

unread,
Apr 11, 2016, 7:17:46 AM4/11/16
to AMPL Modeling Language, 4...@ampl.com
Dear Bob,
thank you once again. Your help is valuable.
Now I did the changes with "no" and there is no problem with this part of the code.
However I run it again and I get the following errors:

ampl:  reset; model 4planes.mod; data 4planes.dat; option solver cplex; solve;

4planes.mod, line 118 (offset 4034):
 k is undefined ( How k is undefined since I wrote in my code:
# Arc capacity constraint: it ensures that the number of airplane traverses arc (i,j) at time period t
# will not exceed its capacity for that period.

subject to Arc_Capacity {t in PERIODS, (i,j) in ARCS}:
 sum{k in AIRPLANES} f[k,i,j,t] + f[k,j,i,t] <= C;)


the same error for t, and u that defined in sum bracket.

4planes.mod, line 123 (offset 4278):
 t is undefined

4planes.mod, line 146 (offset 5527):
 u is undefined
4planes.dat
4planes.mod

char...@gmail.com

unread,
Apr 11, 2016, 10:59:55 PM4/11/16
to AMPL Modeling Language, 4...@ampl.com, char...@gmail.com
Dear Bob,
I managed to make it run without errors but now I got the message:
ampl:  reset; model 4planes.mod; data 4planes.dat; option solver cplex; solve;
Sorry, the student edition of AMPL is limited to 300 variables
and 300 constraints and objectives (after presolve) for nonlinear
problems.  You have 9180 variables, 5687 constraints, and 1 objective.


Does this mean that my code is ok but I just have to buy a license?
Because I would be happy to do that if I knew that I don't have problems with the code.
4planes.dat
4planes.mod

Victor Zverovich

unread,
Apr 12, 2016, 7:52:04 AM4/12/16
to am...@googlegroups.com
Yes, your model was translated correctly but the demo (aka student) version is limited in the number of variables and constraints. You can either buy a license or request a trial to overcome this limitation.

HTH,
Victor

--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at https://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.

char...@gmail.com

unread,
Apr 19, 2016, 12:41:42 PM4/19/16
to AMPL Modeling Language
Dear Victor and Bob thank you for your help.
I would also like to ask two questions:
1st I want to define a set for permitted velocities of airplanes (set V) but it will be depended to the type of aircraft (which means I need to have a set V[aircraft_type].
I have defined it by using the same philosophy as with parameters:
set V:=
 type1 650 750 850
 type2 600 700 800
 type3 550 650 750
 type4 500 600 700;

which means that the type1 of aircraft have velocities 650, 750 and 850 and etc. for type 2, 3 and 4.

2nd. Is there a command or expression for choosing randomly an element from a set.I want now to choose randomly a velocity from the permitted velocities for a certain type of aircraft as the initial one.

so I want a parameter param initvel {k in AIRPLANES} = random[V[ktype[k]]]
I think something like that.
With the command ktype[k] we get the type of k airplane. So I want randomly to choose a velocity from the permitted values for the type of k aircraft.

Thank you in advance.



 

Robert Fourer

unread,
Apr 20, 2016, 10:15:36 AM4/20/16
to am...@googlegroups.com
You can define an indexed collection of sets V by writing

set aircraft_type;
set V {aircraft_type};

Then V[i] will be a set for each i in aircraft_type, and you can set its members to be the velocities. See section 6.5 of the AMPL book (http://ampl.com/BOOK/CHAPTERS/09-sets2.pdf#page=10) for a description of how to do this and how to write the data.

To select a random member from a set, you can declare it to be ordered,

set V {aircraft_type} ordered;

and then apply the "member" built-in function to a random integer to select a random member:

param initvel {k in AIRPLANES} =
member (ceil(Uniform(0,card(V[ktype[k]]))), V[ktype[k]]);

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of char...@gmail.com
Sent: Tuesday, April 19, 2016 11:27 AM
To: AMPL Modeling Language

Charis Ntakolia

unread,
May 30, 2016, 10:27:07 AM5/30/16
to Robert Fourer, am...@googlegroups.com

Dear Bob,

Thank you for your help.

Now I am having a problem with sets.

 

Lets say I have a set

# the set of admissible flight levels for each type of airplane in (*10^3 Feet)

set Z{TYPE} ordered;

 

in data file

# the set of admissible flight levels for each type of airplane in (*10^3 Feet)

set Z[type1] := 30 35 40;

set Z[type2] := 25 30 35;

set Z[type3] := 20 25 30;

set Z[type4] := 15 20 25;

 

the set of airplanes

set AIRPLANES;

 

and the parameter

# The type of each airplane

param ktype {AIRPLANES} symbolic in TYPE;

 

and I have plane1 and plane2 and I want to define their intersections in flight levels Z to be not empty.

So I write

Subject to Constraint {plane1 in AIRPLANES, plane2 in AIRPLANES, z in Z[ktype[plane1]] inter Z[ktype[plane2]]:

plane1 <> plane2 and Z[ktype[plane1]] inter Z[ktype[plane2]] <> {}}:

….then I write the expression of constraint.

 

But I get a syntax error.

 

Could you please let me know the problem. I have realized that the problem must be in this expression <> {}

However in your book you say that the empty set is symbolized as {}.

 

Thank you in advance,

Best,

Charis

 

 

 

Από: Robert Fourer
Αποστολή: Τετάρτη, 20 Απριλίου 2016 5:15 μμ
Προς: am...@googlegroups.com
Θέμα: RE: [AMPL 11852] How to define a parameter x(t)=an element from a set

--

You received this message because you are subscribed to a topic in the Google Groups "AMPL Modeling Language" group.

To unsubscribe from this topic, visit https://groups.google.com/d/topic/ampl/bq-cun75dxs/unsubscribe.

To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.

Robert Fourer

unread,
May 31, 2016, 12:09:37 AM5/31/16
to am...@googlegroups.com
You are running into the problem that <> does not work with sets. But instead of "Z[ktype[plane1]] inter Z[ktype[plane2]] <> {}" you can write "card(Z[ktype[plane1]] inter Z[ktype[plane2]]) > 0" where card is the cardinality function that gives the number of members in a set.

In your particular example, however, it should suffice to write

subject to Constraint
{plane1 in AIRPLANES, plane2 in AIRPLANES,
z in Z[ktype[plane1]] inter Z[ktype[plane2]]: plane1 <> plane2}: ...

This is because if Z[ktype[plane1]] inter Z[ktype[plane2] is empty then the indexing set of the constraint is empty; and when the indexing set is empty, no constraints are generated.

Bob Fourer
am...@googlegroups.com

=======
Reply all
Reply to author
Forward
0 new messages