Augusto Dias
Augusto Dias

Reputation: 147

How can I define an array filled with a single value in AMPL

How can I define an unary or multidimensional array filled with one value in AMPL? Is there something like this?

param ARRAY {i in 1..1000} [i] := 20;

Should result in:

[20, 20, 20, ..., 20]

Upvotes: 1

Views: 562

Answers (1)

G_B
G_B

Reputation: 1573

You were almost there, but I'll throw in a couple of extra options:

param ARRAY{i in 1..1000} := 20;
# sets all values to 20

param ARRAY{i in 1..1000} default 20;
# sets all values to 20 unless otherwise specified 

param ARRAY{i in 1..1000};
for{i in 1..1000} {let ARRAY[i] = 20};
# iterates over the specified set.
# more useful if you want to do something like i^2 instead of a constant.

If you use the default 20 method, then display ARRAY; will only show the values that have been changed from default - it will look as if ARRAY is empty, but referencing specific elements will work OK.

Upvotes: 1

Related Questions