set default value for param read from a file

21 views
Skip to first unread message

aymi

unread,
Oct 14, 2016, 1:18:16 PM10/14/16
to AMPL Modeling Language
Hello everyone!

I've defined a param  G{A,B} (where A,B are sets of integers) in a file. For example for A={0,...,5}, B={6,...,11} my G might look as follows:

data;
param G :=
0 11 9
0 8 10
0 10 19
1 11 4
1 8 2
1 10 14
3 11 8
3 8 12
3 10 6
;

Now, I'd like to set G[a,b]=-1 for all other a in A and b in B.

I've already tried to declare G as param G{A,B} default -1; in my .mod file as well as

data;
param G  default -1 :=
...

and

data;
param G{A,B} default -1 :=
...

in my .dat file, but all 3 attempts failed.


What would be the correct syntax to achieve the desired result?

Thanks for your help!

Robert Fourer

unread,
Oct 14, 2016, 8:13:09 PM10/14/16
to am...@googlegroups.com
This is correct:

set A = 0..5;
set B = 6..11;
param G {A,B} default -1;

Alternatively it is also correct to define "param G {A,B};" and then specify "param G default -1 := ..." in the data file. Either way, if you display G right after you read it in, you will only see the non-default values:

ampl: display G;
G :=
0 8 10
0 10 19
0 11 9
1 8 2
1 10 14
1 11 4
3 8 12
3 10 6
3 11 8
;

The other values for G remain undefined until you give a command that forces the default values to be assigned. So for example if you explicitly request a display of all G[i,j] values then you will see the -1 defaults:

ampl: display {i in A, j in B} G[i,j];
G[i,j] [*,*]
: 6 7 8 9 10 11 :=
0 -1 -1 10 -1 19 9
1 -1 -1 2 -1 14 4
2 -1 -1 -1 -1 -1 -1
3 -1 -1 12 -1 6 8
4 -1 -1 -1 -1 -1 -1
5 -1 -1 -1 -1 -1 -1
;

A "solve;" will always force the default values to be assigned.

Bob Fourer
am...@googlegroups.com

=======

yoshla...@gmail.com

unread,
Oct 16, 2016, 8:41:01 PM10/16/16
to AMPL Modeling Language, yoshla...@gmail.com
Unfortunately, this doesn"t work. I get the following error message:

error processing objective weight:
        invalid subscript G[0,6]
while solving, if I write "param G{A,B} default -1;" in my .mod file.

The some problem occures if I write "default -1" behind G{A,B} in the data file!

Is there any other way to avoid this error?



Robert Fourer

unread,
Oct 17, 2016, 10:23:38 AM10/17/16
to am...@googlegroups.com
This error occurs if 0 is not in A or if 6 is not in B, so you should check your model and data to be sure that 0 really is a member of A and that 6 really is a member of B. If you need more help, post all of your model and data statements that are relevant to A, B, G, and objective weight.

Bob Fourer
am...@googlegroups.com

=======

yoshla...@gmail.com

unread,
Oct 17, 2016, 11:26:05 AM10/17/16
to AMPL Modeling Language, yoshla...@gmail.com
This seems to be the problem indeed. Some error occured while generating the data for A and B, so I will fix it and hopefully things will work then!

Thanks for your help :)
Reply all
Reply to author
Forward
0 new messages