[AMPL 1824] Compiling Sets

View: New views
2 Messages — Rating Filter:   Alert me  

[AMPL 1824] Compiling Sets

by patrick-131 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Suppose I have a group of 10 facilities and I want to create a set
that contains all possible pairs of facilities but I want to do it in
such a way that I can retrieve from the place in the set the two
facilities in each component of the set.  For instance, if I only have
three facilites, I could create the set K where

K(1) = (1,2)
K(2) = (1,3)
K(3) = (2,1)
K(4) = (2,3)
K(5) = (3,1)
K(6) = (3,2)

Then I can retrive the first and second facility simply by knowing the
place number in the set K.  However, I don't know how to code this in
AMPL!  If it is easier if the duplicates are included - i.e. (1,1) -
then that wouldn't be a big deal.

Jonathan

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---


[AMPL 1825] Re: Compiling Sets

by Paul A. Rubin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message




On Jun 11, 2:24 pm, patr...@... wrote:

> Suppose I have a group of 10 facilities and I want to create a set
> that contains all possible pairs of facilities but I want to do it in
> such a way that I can retrieve from the place in the set the two
> facilities in each component of the set.  For instance, if I only have
> three facilites, I could create the set K where
>
> K(1) = (1,2)
> K(2) = (1,3)
> K(3) = (2,1)
> K(4) = (2,3)
> K(5) = (3,1)
> K(6) = (3,2)
>
> Then I can retrive the first and second facility simply by knowing the
> place number in the set K.  However, I don't know how to code this in
> AMPL!  If it is easier if the duplicates are included - i.e. (1,1) -
> then that wouldn't be a big deal.

I don't think you can create a set to do this.  To retrieve a set
entry by it's position in the set, the set must be ordered, and AMPL
does not allow ordering of sets with dimension > 1.  What you can do
is put the first and second entries of the pairs in separate parameter
vectors, as in the following:

set FACILITY := 1 .. 5;
set PAIRS := 1 .. card(FACILITY)*(card(FACILITY) - 1);
param first {PAIRS} in FACILITY;
param second {PAIRS} in FACILITY;
param n default 1;
for {(f, g) in FACILITY cross FACILITY : f <> g} {
  let first[n] := f;
  let second[n] := g;
  let n := n + 1;
}

The k-th pair will be (first[k], second[k]).

Another possibility, since you are willing to work around reflective
pairs (i, i), is to use a formula.  This works if your facilities are
indexed 1 .. N.  Start with

param nFacilities integer > 0;
set FACILITY := 1 .. nFacilities;  // omit if you don't need it
somewhere else
set PAIRS := 1 .. nFacilities*nFacilities;
// ... supply a value for nFacilities
display {p in PAIRS : (p - 1) div nFacilities <> (p - 1) mod
nFacilities} ((p - 1) div nFacilities, (p - 1) mod nFacilities);

For p in PAIRS, the first entry in pair p is (p - 1) div nFacilities
and the second is (p - 1) mod nFacilities.

If you prefer this approach but want to use more general symbols for
your facilities, you can define set FACILITY as ordered and then refer
to a pair using member((p - 1) div card(FACILITY), FACILITY) and
member((p - 1) mod card(FACILITY), FACILITY).

/Paul

/Paul

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

LightInTheBox - Buy quality products at wholesale price!