Null as default value for array?

412 views
Skip to first unread message

Stefan

unread,
Mar 7, 2010, 5:56:27 PM3/7/10
to AMPL Modeling Language
Hi all,

I was recently introduced to AMPL for a course in machine learning.
While trying to do some modeling for a midterm, I've recently come
across a problem I can't seem to work out: How do I fill a param array
with a default value of null?

I've seen references that when you create an array in the data
section, you can use the "-" character to represent "no value", as
seen in: http://www-personal.umich.edu/~murty/510/data.html. However,
when I try using this approach in the model section, I get errors.
Example:

var Weights{1..5, 1..5, 1..5} default .;

Is there anyway that I can set the value of an array as a non-numeric?
Is there a way to do an "array-of-arrays" in AMPL so that for each
"slice" in a given dimension, I can have resulting nxm matrices of
varying dimensions?

Thanks!

Paul

unread,
Mar 8, 2010, 11:24:33 AM3/8/10
to AMPL Modeling Language
On Mar 7, 5:56 pm, Stefan <stefan.louis.no...@gmail.com> wrote:
> I was recently introduced to AMPL for a course in machine learning.
> While trying to do some modeling for a midterm, I've recently come
> across a problem I can't seem to work out: How do I fill a param array
> with a default value of null?

You don't; you just don't give any initial or default value.


>
> I've seen references that when you create an array in the data
> section, you can use the "-" character to represent "no value", as
> seen in:http://www-personal.umich.edu/~murty/510/data.html.

I think you're misinterpreting that page. The single '-' character
used in data statements for sets (top half of the page) is not a null
value, it's a signal that the corresponding tuple is not part of the
set ('+' indicates it is part of the set). The '.' used later in the
page indicates no value specified, use the default if there is one (as
opposed to specifying a null value). The '--' in the defaultsym
statement later is a notational replacement for '.'.

> However,
> when I try using this approach in the model section, I get errors.
> Example:
>
> var Weights{1..5, 1..5, 1..5} default .;

That's because '.' is not a valid value.


>
> Is there anyway that I can set the value of an array as a non-numeric?

You can make it symbolic if the entire array is symbolic, but I don't
think that's what you want.

> Is there a way to do an "array-of-arrays" in AMPL so that for each
> "slice" in a given dimension, I can have resulting nxm matrices of
> varying dimensions?

Declare the array as rectangular but don't worry about specifying
values for index tuples that you are not going to use in the model.
Consider the following small nonsense model:

set FROM := 1..3;
set TO := 1..3;
param use {FROM} in TO; # use[i] tells me how many columns in row i
are meaningful
data;
param use := 1 3 2 2 3 1; # use[i] = 4 - i
model;
param a {FROM, TO} default 7; # all 9 cells are initialized to 1, but
3 won't be used
var X {FROM, TO} >= 0; # same story: 9 variables created 6 will be
used
minimize obj: X[1,1];
s.t. cons1 {i in FROM}: sum {j in TO : j <= use[i]} a[i,j]*X[i,j] <=
5; # indexing uses only X[i,j] where j <= use[i]
expand;
minimize obj:
X[1,1];

subject to cons1[1]:
7*X[1,1] + 7*X[1,2] + 7*X[1,3] <= 5;

subject to cons1[2]:
7*X[2,1] + 7*X[2,2] <= 5;

subject to cons1[3]:
7*X[3,1] <= 5;

Note that X[2, 3], X[3, 1] and X[3, 2] do not show up, nor do their
coefficients. There may be some modest waste of memory, but unless
you have a very large model that shouldn't be a problem. The key is
that AMPL will pay attention to parameter values (and variable initial
values) only when those parameters/variables are used in the model.

/Paul

Robert Fourer

unread,
Mar 9, 2010, 6:09:56 PM3/9/10
to am...@googlegroups.com, Stefan
There's some more to say about one of your questions:

Is there a way to do an "array-of-arrays" in AMPL so that
for each "slice" in a given dimension, I can have resulting
nxm matrices of varying dimensions?

You can do something like this by indexing your second and third dimensions
over sets that depend on your first dimension. Here's an example:

param K integer > 0;
param n {1..K} integer > 0;
param m {1..K} integer > 0;

var Weights {k in 1..K, 1..n[k], 1..m[k]};

The same kind of thing can be done with sets of strings, as in the following
example (steelT3.mod) from the AMPL book:

set PROD; # products
set AREA {PROD}; # market areas for each product
param T > 0; # number of weeks

param market {p in PROD, AREA[p], 1..T} >= 0;

var Sell {p in PROD, a in AREA[p], t in 1..T} >= 0, <= market[p,a,t];

maximize Total_Profit:
sum {p in PROD, t in 1..T}
(sum {a in AREA[p]} revenue[p,a,t]*Sell[p,a,t] - ...

One limitation is that the index for your fixed dimension has to come before
indices for varying dimensions.

Bob Fourer
4...@ampl.com

Reply all
Reply to author
Forward
0 new messages