I have a problem for which I don’t find a simple solution.
I have a multidimensional set s(i,j,k).
I would like to select randomly an element from this set.
I was thinking about using randomNum = uniformint(1,card(s)) to select a random number between 1 and the number of elements in s.
Then, I wanted to apply: sRand(i,j,k) $ (ord(s(i,j,k)) = randomNum) = yes;
to make sRand the subset that contains the single random element from s.
The problem is that GAMS does not allow to use “ord” on multidimensional set.
It looks strange to me because we can think of a “naturel” order of multimensional set such as this one:
1.1.1
1.1.2
1.2.1
1.2.2
2.1.1
2.1.2
etc…
In this case, we would have ord(1.2.1) = 3.
Anyway, because I cannot use “ord” on this multidimensional set, I have no idea how to pick up randomly an element from this set.
Note that you need to use random.seed() if you want the Python code to be reproducible. Otherwise Python will pick a different element every time you execute the program.
I like Clemens’ Python approach. But if that’s not your style, you can construct the function you wanted - what you thought ord() would give you - in a parameter, and use that:
sets
i / i1, i2 /
j / j1 * j3 /
ij(i,j) / i1.(j1,j2), i2.(j2,j3) /
rnd(i,j)
;
scalars n, r;
parameter pos(i,j) 'position in 1..card(ij)';
n = 0;
loop{ij(i,j),
n = n + 1;
pos(i,j) = n;
};
abort$[n <> card(ij)] 'bogus pos computation', n, ij;
execseed = 07041776;
r = uniformint(1,card(ij));
rnd(ij)$[pos(ij) = r] = yes;
I thought about the answer 1) from Clemens but, unfortunately, my set s does not contain all the possible combination of the sets i, j, and k.
I don’t have 1.1.1, 1.1.2, 1.2.1, 1.2.2, 2.1.1, 2.1.2 …
But rather something like 1.1.2, 1.2.1, 2.1.1…
Because of this, using the method 1) may select a combination of (i,j,k) that does not belong to s(i,j,k).
The python approach and the approach from dirkse are exactly what I was looking for though, thank you!