« Return to Thread: [AMPL 2631] Arbitrary Cartesian Products

[AMPL 2645] Re: Arbitrary Cartesian Products

by Robert Fourer-2 :: Rate this Message:

Reply to Author | View in Thread



You need a for-loop to do this.  For example when L is 2,

   param r {i in 1..N, j in 1..L};
   param j;

   let j := 0;
   for {(r1,r2) in PivotalVotes} {
      let j := j + 1;
      let r[j,1] := r1;
      let r[j,2] := r2;
   }

When L is 3 you have to expand the loop,

   for {(r1,r2,r3) in PivotalVotes} {
      let j := j + 1;
      let r[j,1] := r1;
      let r[j,2] := r2;
      let r[j,3] := r3;
   }

There's no way to write a general loop that handles every possible value of
L, as that would again involve working with a set (PivotalVotes) and with
tuples whose dimension depends on a data value.

Bob Fourer
4er@...


> -----Original Message-----
> From: guilherme.freitas@... [mailto:guilherme.freitas@...] On
> Behalf Of Guilherme P. de Freitas
> Sent: Sunday, July 12, 2009 7:21 PM
> To: 4er@...
> Cc: ampl@...
> Subject: Re: [AMPL 2631] Arbitrary Cartesian Products
>
> I guess the short question is: if 'a' is a tuple, how do I specify the
> j-th element of 'a'?
>
> On Sun, Jul 12, 2009 at 5:11 PM, Guilherme P. de
> Freitas<guilherme@...> wrote:
> > It turns out that if I rewrite that 'PivotalVotes' set by hand, then I
> > have to create a "number-of-parties-agnostic" representation of it for
> > later use, otherwise I will also have to rewrite almost everything
> > that follows the definition of the set "PivotalVotes" for every
> > different problem. I am trying to do what Robert Fourer suggested,
> > creating a parameter 'r' indexed by {1..N, 1..L} where N is the total
> > number of qualifying tuples (r1, ..., rL) and L is the number of
> > parties, and 'r[i, j]' should be the value of 'rj' (written by hand in
> > the definition of PivotalVotes, note that 'rj' is different from
> > 'r[j]') in the 'i-th' qualifying tuple. So I am trying this (example
> > with L=2 parties):
> >
> > \\--- begin code ---
> >
> > set Parties ordered;   # Set of political parties
> > param L = card(Parties);   # Number of parties
> > param n {Parties} integer > 0;   # number of legislators in each party
> > param n_total = sum {party in Parties} n[party];    # total number of
> > legislators
> > param q;     # fraction of votes necessary for legislation to pass
> >
> > set auxC = {0..n[member(1, Parties)], 0..n[member(2, Parties)]};
> >
> > set PivotalVotes = {(r1, r2) in auxC : r1 + r2 = floor(n_total * q)}
> > ordered;  # combinations of votes such that one more vote passes the
> > legislation
> >
> > param N = card(PivotalVotes);
> >
> > param r {i in 1..N, j in 1..L} = member(i, PivotalVotes)[j]
> >
> > --- end code ---//
> >
> > But the problem is that "member(i, PivotalVotes)[j]" is illegal
> > syntax. But I need a way to select the l-th member of a tuple.
> > Otherwise this code will be too messy.
> >
> > Thanks in advance,
> >
> > Guilherme
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To post to this group, send email to ampl@...
To unsubscribe from this group, send email to ampl+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/ampl?hl=en
-~----------~----~----~----~------~----~------~--~---

 « Return to Thread: [AMPL 2631] Arbitrary Cartesian Products