Hi, I’m a very new beginner to AMPL and I’m wondering how to define a vector/matrix in AMPL. What I have learned is to use:
param p{N} :=
1 xx(some constants)
2 xx
3 xx;
…
for defining a vector in the data file and
param a{M,N}:=
1 2 3 …
1
2
3
…;
for defining a matrix in the data file.
However when I try to data ; it always says p is not a param.
Can someone tell me how to solve this?
Also is it possible to display/show a vector/matrix in AMPL? If so, how can I get that?
Thanks!
First you have to set up an optimization model that defines the sets and parameters that you are using:
set M;
set N;
param p {N};
param a {M,N};
Then, separately, you can specify data for a particular instance of your model:
set M := 1 2 3 4 5 ;
set N := 1 2 3 ;
param p :=
1 25
2 35
3 55 ;
param a: 1 2 3 :=
1 3 4 5
2 5 7 8
3 2 9 8
... ;
The message “p is not a param” occurs when you try to read the data into AMPL before you have read in the model. If this model-data separation is not familiar to you, take a look at the introduction in Chapter 1 of the AMPL book.
Hi Robert,
Thanks for your explanation! I have understood all.
Duzhou