Is it possible to iterate over tuples? How?

109 views
Skip to first unread message

Marco Bonelli

unread,
Apr 15, 2021, 8:30:38 AM4/15/21
to AMPL Modeling Language
I have a very simple example program, consisting of the following .dat file:

set points := A B C;
param weights := 
    A (1, 2, 1, 3)
    B (3, 1, 3)
    C (5,);

And the following .mod file:

set points;
param weights{points};
var x >= 0;
minimize obj: x;
subject to c1 {p in points}:
    x >= sum{w in weights[p]} w;

My intention here would be to use "weights" as a simple way to express a list of parameters for each point. Then, I would like to iterate over the parameters for each point and sum them up in my "c1" constraint. However, the last line of the mod file throws a syntax error complaining about that "weights[p]". I concur that you cannot iterate over a tuple, is that so?

Of course, this is a silly example since I could just define one value per point to avoid the summation, and of course this can be done differently. However, this is not the point. The reason for which I would like to express my data in such a way is that I would ideally like to have an arbitrary number of parameters per point upon which I can iterate over (regardless of the kind of operation I need to perform in the iteration). 

Would something like this be possible? How can I express my data in a compact way which allows me to define multiple parameters (with possibly repeated values) per point?

I would appreciate any kind of insight on this. Thank you very much!

AMPL Google Group

unread,
Apr 16, 2021, 10:35:28 AM4/16/21
to AMPL Modeling Language
The expression "{w in weights[p]}" requires weights[p] to be a set. You could define "set weights{points};" and then your constraint c1 would not give an error. But since a member can only appear once in each set, you would not be able to associate weights["A"] with "(1, 2, 1, 3)" or weights["B"] with "(3, 1, 3)". So for this situation you need to make weights a 2-dimensional parameter:

set points;
param n_weights {points} integer > 0;
param weight {p in points, 1..n_weights[p]};

var x >= 0;
minimize obj: x;
subject to c1 {p in points}:
   x >= sum {w in 1..n_weights[p]} weight[p,w];

There are various ways to give the data, for example:

param: points: n_weights :=
   A 4  B 3  C 1 ;

param weight:
       1   2   3   4 :=
   A   1   2   1   3
   B   3   1   3   .
   C   5   .   .   . ;


--
Robert Fourer
am...@googlegroups.com
{#HS:1484915997-103607#}

Marco Bonelli

unread,
Apr 16, 2021, 4:57:46 PM4/16/21
to am...@googlegroups.com
All clear! I did not know you could define a matrix like that "weight {p in points , 1..n_weights[p]}". Thank you very much.
--
You received this message because you are subscribed to a topic in the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ampl/OX4Skuxzwjo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ampl/reply-77152-1484915997-4311725652-1618583725-728031164%40helpscout.net.
Reply all
Reply to author
Forward
0 new messages