How to manipulate specific elements in a set?

891 views
Skip to first unread message

Martin Smith

unread,
May 8, 2014, 7:44:42 PM5/8/14
to am...@googlegroups.com, sup...@ampl.com

I want to do a bubble sort on a two-dimensional set, but to do so means swapping the index position of elements of the set and that means referencing specific members of the set, which is what I’m having problems with. To illustrate the issues consider the following.

 

ampl: delete B;

ampl: delete A;

ampl: set A;

ampl: set B {A};

ampl: let A := {1,2};

ampl: let B[1] := {3,4};

ampl: let B[2] := {5,6};

ampl: display B;

set B[1] := 3 4;

set B[2] := 5 6;

 

How do you refer to item B[2,4] or 6? Do we have to define these as ordered sets? Even then I’m unclear how to assign a specific member a value in an ordered set. In this case we have:

 

ampl: set A ordered;

ampl: set B {A} ordered;;

ampl: let A := {1,2};

ampl: let B[1] := {3,4};

ampl: let B[2] := {5,6};

ampl: display member(2,B[2]);

member(2,  B[2]) = 6

 

But I still cannot specifically change this items value using any of these function that return values from ordered sets:

 

ampl: let member(2,B[2]) := 7;

 

syntax error

context:  let  >>> member( <<< 2,B[2]) := 7;

 

I expect that I’m missing something here, but if not how would one implement, say, a bubble sort on a set (as opposed to parameter vectors) where I have to reassign the value of one element in the set to the position of another element?

 

Thanks in Advance,

 

Martin

 

Martin Smith
Managing Director
Mine Smith Pty Ltd

Phone:
+61 (4) 0395 5332


Email: msm...@minesmith.com.au

 

fbahr

unread,
May 9, 2014, 11:59:29 AM5/9/14
to am...@googlegroups.com, sup...@ampl.com
IINM, sets are immutable.

(Well, sort of... sets are dynamic in the way that parametrized definitions get updated when(ever) parameter values change, e. g.:

ampl: set ALPHABET ordered = {'A', 'B', 'C', 'D', 'E'};
ampl: param IDX {i in 1 .. card(ALPHABET)} default i;
ampl: set MY_SET = setof {i in 1 .. 3} member(IDX[i], ALPHABET);
ampl: display MY_SET;
set MY_SET := A B C;
ampl: let {i in 0 .. 2} IDX[i+1] := 5 - i;
ampl: display MY_SET;
set MY_SET := E D C;


In this way you can actually manipulate sets to take arbitrary values... but: working with params directly might give you less headaches.)

--fbahr
Reply all
Reply to author
Forward
0 new messages