Multiplication of two subset of variables variables Topic is solved

Problems with modeling
Post Reply
Trb2
User
User
Posts: 2
Joined: 1 year ago

Multiplication of two subset of variables variables

Post by Trb2 »

Hello,

I have a problem with the set n (optimization variables), which can be devided in two sets a(n) and b(n) with equal cardinality. In my model, I want to minimize the sum of the product of the (ordered) elements of the two sets, i.e.

Code: Select all

 obj.. cost =e= sum(a, x(a)*x(b) ) . 
Obviously, this code is wrong because I use the set b in the equation, but I just want one scalar value for cost (and not one for evey b). I was thinking on something like

Code: Select all

 obj.. cost =e= sum(a, x(a)*x(a+3) )  
knowing that e.g.

Code: Select all

n /1*6/, a(n) /1*3/, b(n) /4*6/.  
But this is a very bad solution, and I was thinking that there has to be something else. Sorry for the (probably) trivial question, I am more used to Matlab :P
User avatar
bussieck
Moderator
Moderator
Posts: 1037
Joined: 7 years ago

Re: Multiplication of two subset of variables variables

Post by bussieck »

Hi, you need to establish a map that maps the first element of a to the first element of b, etc. If you have a map ab like this the algebra is simple: obj.. cost =e= sum(ab(a,b), x(a)*x(b) ). So question is how do you get such a map. There are many ways to do that in GAMS. There is some power syntax to do that at compile or execution time. But you can also use the ord operator for this. Here are some choices:

Code: Select all

set n /1*6/, a(n) /1*3/, b(n) /4*6/;

* Compile time
set ab(n,n) / #a:#b /;

* Execution time with matching operation
set ab2(n,n); option ab2(a:b);

* Execution time with ord
set ab3(n,n); ab3(a,b) = ord(a) = ord(b);

* Execution time with ord just based on n
alias (n,np)
set ab4(n,n); ab4(n,np) = ord(n) = ord(np)-(card(n)/2);
-Michael
Trb2
User
User
Posts: 2
Joined: 1 year ago

Re: Multiplication of two subset of variables variables

Post by Trb2 »

Thanks a lot! That solves my question. In the meantime, I also understood that an option is to just split the optimization variables x(n) in x1(m) and x2(m), where m has half cardinality compared to n. Then I can easily do

Code: Select all

obj.. cost =e= sum(m, x1(m)*x2(m))
and the problem is solved. Now i have two options to implement, I have to think about which one is more convenient for me. Thanks again!
Post Reply