# [AMPL 24631] new set

Hi everyone!
Starting from an empty set (set X), is it possible to insert the values of another one (set Z ) inside it, based on an if condition?
For example, I have an initial set of 20 planes, and I want to create a new set of 10 planes, taking the 10 planes that have a greater value of any parameter t, relative to the set of 20 planes.
How is it possible to write this in ampl?

Sure, if you have defined

``````set Z = 1..20; # planes
set X;

param value {Z} = Uniform(7,23);
``````

then you can start X as the empty set and add all members that satisfy some condition. Here’s a simple example:

``````let X := {};
for {i in Z} {
if value[i] >= 17 then
let X := X union {i};
};

display {i in X} value[i];
``````

But do you actually want to create a set of the 10 planes that have the highest value? If so, that would require a different approach, and you can write back to ask for more help with it.

Thank tou so much! Yes my task is to create a new set of 10 planes, or 50% of the initial number of planes (20 in our case), considering the 10 planes with the highest value of parameter “value”. Another idea could be to choose the 10 planes randomly, using a specific function, if it’s possible.
Thank you for your help again

The attached file setofClargest.run shows a way that you can extract the members of a set (PLANES) that correspond to the C largest values of a parameter (value). Starting from PLANES, it computes the following sets, with the last one containing the planes having the C best values (where C = 4 in this case):

`set PLANES := a b c d e f g;`

`set valsByPlane := 7 2 5 4 11 6 9;`
`set valsSorted := 11 9 7 6 5 4 2;`
`set planesByVal := e g a f c d b;`

`set PLANESCbest := e g a f;`

To choose the planes randomly, just set the values randomly instead of putting them in the data:

`param value {PLANES} = Uniform01();`

This only works if all the values are different, however. If some planes might have the same value, then you need to perturb the members of valsByPlane and valsSorted by different small amounts, as shown in setofClargestdupl.run.

setofClargest.run (524 Bytes)

setofClargestdupl.run (568 Bytes)

Thank you so much Robert!
Ok now i have the set PLANESCbest, formed by the 4 planes with the highest values of ‘value’. Then i want to fix the value of a hypothetical binary variable y, belonging to my model, to 1, with y depending on the new set PLANESCbest ( var y{i in PLANESCbest, j in PLANESCbest: i!=j} binary ). Is it possible to do this iteratively? For example i would like :

for {i in PLANESCbest, j in PLANESCbest: i!=j and T[i]<T[j]}
{
fix y[i,j]:=1;
fix y[j,i]:=0;
}

now PLANESCbest is not formed by the 4 highest value of ‘value’, but is formed from the fifth to the eighth highest value.

for {i in PLANESCbest, j in PLANESCbest: i!=j and T[i]<T[j]}
{
fix y[i,j]:=1;
fix y[j,i]:=0;
}

now from the ninth to the twelfth highest value, and so on up to the twentieth plane.