## Loops over sets and conditions

Problems with modeling
GAMSisGREAT
User Posts: 11
Joined: 2 years ago

### Loop and parameters

I have the following problem. I loop through a set, use this to define a new set and its complement, and inside the loop I call some parameters that depend on these sets. However, I can't make these parameters to exclusively be defined over the sets. Here is an example below. What you see in the last couple of lines is:
---- 25 PARAMETER test1 set of j
A 1.000, B 1.000, C 1.000
---- 25 PARAMETER test2 set of not j
A 2.000, B 2.000, C 2.000

Instead I want to see something like this:
---- 25 PARAMETER test1 set of j
C 1.000
---- 25 PARAMETER test2 set of not j
A 2.000, B 2.000

Why are the test2 not in the complement set of test1? When I display the sets in the loop it looks like they are complementary, but then this does not hold for the parameters test1 and test2 any longer. Thanks for any help!

INPUT:
Sets
\$onempty
k / A, B, C /
j(k) / /
notj(k) / /
;

Alias (k, kk) ;
Alias (j, jj) ;
Alias (notj, jnotj) ;

Parameters
test1 "set of j"
test2 "set of not j"
;

loop(kk,
j(kk)=yes;
notj(k)=not j(k);
display j, notj;
test1(j) = 1;
test2(notj) = 2;
display test1, test2;
j(kk)=no;
notj(k)=no;
display j, notj;
);

OUTPUT:
---- 22 SET j
A
---- 22 SET notj
B, C
---- 25 PARAMETER test1 set of j
A 1.000
---- 25 PARAMETER test2 set of not j
B 2.000, C 2.000
---- 28 SET j
( EMPTY )
---- 28 SET notj
( EMPTY )
---- 22 SET j
B
---- 22 SET notj
A, C
---- 25 PARAMETER test1 set of j
A 1.000, B 1.000
---- 25 PARAMETER test2 set of not j
A 2.000, B 2.000, C 2.000
---- 28 SET j
( EMPTY )
---- 28 SET notj
( EMPTY )
---- 22 SET j
C
---- 22 SET notj
A, B
---- 25 PARAMETER test1 set of j
A 1.000, B 1.000, C 1.000
---- 25 PARAMETER test2 set of not j
A 2.000, B 2.000, C 2.000
---- 28 SET j
( EMPTY )
---- 28 SET notj
( EMPTY )

Renger
Posts: 588
Joined: 4 years ago

### Re: Loop and parameters

Hi
This only works if you change the code as follows:

Code: Select all

``````loop(kk,
j(kk) = yes;
notj(k) = not j(k);
display j, notj;
test1(j) = 1;
test2(notj) = 2;
display test1, test2;
j(kk) = no;
notj(k) = no;
[color=#FF0000]    test1(kk) = 0;
test2(k) = 0;[/color]
display j, notj;
);
``````
Otherwise the parameters test1 and test2 will keep their values. You could also drop one of the test parameter and initialize an overall parameter test with 2, and then set test(j) = 1.

This will give you, however, only the combinations of 1 element and the rest of the set. If you want to have all combinations of a bigger set, I probably would use R (taken from http://rsnippets.blogspot.ch/2012/04/ge ... f-set.html:

Code: Select all

``````all.subsets <- function(set) {
n <- length(set)
bin <- vector(mode = "list", length = n)
for (i in 1L:n) {
bin[[i]] <- rep.int(c(rep.int(F, 2L ^ (i - 1L)),
rep.int(T, 2L ^ (i - 1L))),
2L ^ (n - i))
}
apply(do.call(cbind, bin), 1L, function(x) { set[x] } )
}

set <- c("A","B", "C","D")
all.subsets(set = set)``````
Which gives you:

Code: Select all

``````[]
character(0)

[]
 "A"

[]
 "B"

[]
 "A" "B"

[]
 "C"

[]
 "A" "C"

[]
 "B" "C"

[]
 "A" "B" "C"

[]
 "D"

[]
 "A" "D"

[]
 "B" "D"

[]
 "A" "B" "D"

[]
 "C" "D"

[]
 "A" "C" "D"

[]
 "B" "C" "D"

[]
 "A" "B" "C" "D"``````
This could be used to generate a data frame with all the test1 and test2 parameters and imported in Gams using gdxrrw.
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist

GAMSisGREAT
User Posts: 11
Joined: 2 years ago

### Re: Loop and parameters

Would you have any idea how to write this in GAMS if the order of the items in a set do not matter? My thought was to use two loops together with a circular operator, but I just have no idea how to write this circular operator properly in order to get only the upper triangular matrix of all potential combinations (meaning the order doesn't matter). Thanks so much for your help!!
Renger wrote:
2 years ago

If you want to have all combinations of a bigger set, I probably would use R (taken from http://rsnippets.blogspot.ch/2012/04/ge ... f-set.html:

Code: Select all

``````all.subsets <- function(set) {
n <- length(set)
bin <- vector(mode = "list", length = n)
for (i in 1L:n) {
bin[[i]] <- rep.int(c(rep.int(F, 2L ^ (i - 1L)),
rep.int(T, 2L ^ (i - 1L))),
2L ^ (n - i))
}
apply(do.call(cbind, bin), 1L, function(x) { set[x] } )
}

set <- c("A","B", "C","D")
all.subsets(set = set)``````
Which gives you:

Code: Select all

``````[]
character(0)

[]
 "A"

[]
 "B"

[]
 "A" "B"

[]
 "C"

[]
 "A" "C"

[]
 "B" "C"

[]
 "A" "B" "C"

[]
 "D"

[]
 "A" "D"

[]
 "B" "D"

[]
 "A" "B" "D"

[]
 "C" "D"

[]
 "A" "C" "D"

[]
 "B" "C" "D"

[]
 "A" "B" "C" "D"``````
This could be used to generate a data frame with all the test1 and test2 parameters and imported in Gams using gdxrrw.
Cheers
Renger

Renger
Posts: 588
Joined: 4 years ago

### Re: Loops over sets and conditions

No, I don't have an idea for that. But, I must say, that if things get too difficult in R, I often use Excel or R to do this kind of stuff (RisGreat...).
Cheers
Renger
PS. I moved the topic to a separate topic (I am the administrator too). The only thing is that the title remained the same. Sorry
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist