问题
Is there a way to simplify declaring a tuple of parameters in AMPL
? For example, if a three-dimensional point was needed as a parameter, what I'm doing is declaring it as
param Point {{'a', 'b', 'c'}};
and then accessing it through P['a']
, P['b']
, and P['c']
. The problem with this is, first, that it's ugly. To specify the point (1, 2, 3)
in the data file you would have to write
param Point := a 1 b 2 c 3;
. I tried using the ordered
keyword—i.e. param Point {{'a', 'b', 'c'} ordered};
—so that the label was implied by the order—i.e. param Point := 1 2 3
—, but that's not allowed inline, and having to define a separate "dummy" set is even uglier. I tried searching through the AMPL book, but found nothing for this specific purpose.
The other alternative is even worse:
param a;
param b;
param c;
It seems very stupid that there isn't a special syntax/shortcut for this.
Also note that I don't want to declare a set; I want an actual tuple parameter, of fixed length (specified through the model), whose entries are to be used as numerical parameters, and not labels.
So, is there a good way to do this?
回答1:
If I've understood correctly, what you're looking for here is a way to input values for an AMPL tuple (indexed over some set S) without having to include the names of the indices (members of S) alongside the values - i.e. requiring that the index for each value is deduced from the order in which the values are provided.
I'm not aware of a simple way to do this. In AMPL, indexed numeric parameters (and variables) work in a similar way to e.g. a Python dict()
: elements are referenced by key (index value), not by position. Because of this, all the methods of data input that I've seen require giving the indices alongside the values.
Given that AMPL does support ordered sets, I presume it would be possible to provide an input mode that allows for indices to be implied by position instead of stated explicitly. However, I'm not aware of this existing. It might be that it simply isn't a priority, or it might be a deliberate design decision to improve code robustness.
I suspect position-dependent inputs might be a bit more fragile for many of the problems AMPL is intended to handle; they certainly would be for the ones I've used it for. Often I want to reuse the same model with small changes to the index sets, in which case explicitly indexing the inputs helps prevent assigning the wrong values to the wrong places.
来源:https://stackoverflow.com/questions/53988808/declaring-a-tuple-of-parameters-in-ampl