Loops over sets and conditions

Problems with modeling
Post Reply
GAMSisGREAT
User
User
Posts: 11
Joined: 6 months ago

Loop and parameters

Post by GAMSisGREAT » 6 months ago

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 )

User avatar
Renger
Posts: 150
Joined: 1 year ago

Re: Loop and parameters

Post by Renger » 6 months ago

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

[[1]]
character(0)

[[2]]
[1] "A"

[[3]]
[1] "B"

[[4]]
[1] "A" "B"

[[5]]
[1] "C"

[[6]]
[1] "A" "C"

[[7]]
[1] "B" "C"

[[8]]
[1] "A" "B" "C"

[[9]]
[1] "D"

[[10]]
[1] "A" "D"

[[11]]
[1] "B" "D"

[[12]]
[1] "A" "B" "D"

[[13]]
[1] "C" "D"

[[14]]
[1] "A" "C" "D"

[[15]]
[1] "B" "C" "D"

[[16]]
[1] "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

GAMSisGREAT
User
User
Posts: 11
Joined: 6 months ago

Re: Loop and parameters

Post by GAMSisGREAT » 6 months ago

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:
6 months 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

[[1]]
character(0)

[[2]]
[1] "A"

[[3]]
[1] "B"

[[4]]
[1] "A" "B"

[[5]]
[1] "C"

[[6]]
[1] "A" "C"

[[7]]
[1] "B" "C"

[[8]]
[1] "A" "B" "C"

[[9]]
[1] "D"

[[10]]
[1] "A" "D"

[[11]]
[1] "B" "D"

[[12]]
[1] "A" "B" "D"

[[13]]
[1] "C" "D"

[[14]]
[1] "A" "C" "D"

[[15]]
[1] "B" "C" "D"

[[16]]
[1] "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

User avatar
Renger
Posts: 150
Joined: 1 year ago

Re: Loops over sets and conditions

Post by Renger » 6 months ago

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

Post Reply