First program difference between two arrays

Hi,
I’m new to AMPL and I’m trying to set a variable/param contains a difference betweend 2 arrays:
File .mod
set C;
param Cost{C};
param Ric{C};

param Net{C};

file .dat
data;

set C := g1 g2 g3 g4 g5;
param Ric :=
g1 5
g2 7
g3 9
g4 9
g5 8;

param Cost :=
g1 1
g2 3
g3 1
g4 4
g5 2;

param Net := {i in C} : Ric[i]-Cost[i];

It doesn’t work.
How can I fix it?

Thanks in advance

1 Like

Hi eros,

One way to declare Net correctly is:

param Net {i in C} := Ric[i]-Cost[i];

However, you should not put a declaration like that in the data file (as you would be declaring it twice, in the model and in the data file). As Net does not contain data that you are going to change, you could just keep it in the .mod file.

.mod file

set C;
param Cost{C};
param Ric{C};
param Net {i in C} := Ric[i]-Cost[i];

.dat file

data;

set C := g1 g2 g3 g4 g5;
param Ric :=
g1 5
g2 7
g3 9
g4 9
g5 8;

param Cost :=
g1 1
g2 3
g3 1
g4 4
g5 2;

After loading the model and data files, try the display command and you should get:

ampl: model file.mod;
ampl: data file.dat;
ampl: display Ric, Cost, Net;          
:  Ric Cost Net    :=
g1   5   1    4
g2   7   3    4
g3   9   1    8
g4   9   4    5
g5   8   2    6
;

Hope it helps!

2 Likes

It works! Thank you so much!

1 Like