Expanding Matrix Elements Depending on Values

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

Expanding Matrix Elements Depending on Values

Post by mmashraf12 »

Hi
Dear All,
I am facing issues in expanding matrix elements depending on the values. The detailed logic is given as follows:
if UNIT(j) Number of Units
/ 1 1
2 3
3 2
4 0
5 1 /

and

CAP(j) Capacities of Units
/ 1 200
2 450
3 500
4 1000
5 700 /

Then the result in the VERTICAL(c) should be the corresponding capacity CAP with the repetition according to the value in UNIT

VERTICAL(c)
1 200
2 450
3 450
4 450
5 500
6 500
7 1000

Thanks

The code is given as follows:

Code: Select all

Sets    j   /1*5/
        c   /1*7/;

Parameters
UNIT(j)     Number of Units
/   1   1
    2   3
    3   2
    4   0
    5   1   /
    
CAP(j)      Capacities of Units
/   1   200
    2   450
    3   500
    4   1000
    5   700 /
    
VERTICAL(c);
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: Expanding Matrix Elements Depending on Values

Post by abhosekar »

You can get this to work by using two loops in GAMS as follows:

Code: Select all

Sets    j   /1*5/
        c   /1*7/;

Parameters
UNIT(j)     Number of Units
/   1   1
    2   3
    3   2
    4   0
    5   1   /
    
CAP(j)      Capacities of Units
/   1   200
    2   450
    3   500
    4   1000
    5   700 /
    
VERTICAL(c);

alias(j, jj);
parameter cumsumj(j) cumulative summation;
cumsumj(j) = sum(jj$(ord(jj) <= ord(j)), unit(jj));

display cumsumj;

set csub(c);
csub(c) = yes;

loop(j,
loop(c$csub(c),
vertical(c)$((ord(c) le cumsumj(j))) = cap(j);
csub(c)$((ord(c) le cumsumj(j))) = no;
)

);
display vertical;

You can insert display statements anywhere you like to understand what's going on. If you don't know any of the keyword I have used here, please refer to the GAMS documentation.

One final note: Instead of using a parameter vertical(c), I would think of it as follows:
you have 5 types of equipment. Each type has a capacity and you have different number of units for each type. What you need is a map between the unit number and its type. In other words, a 2D set
map(c, j); It doesn't simplify the implementation but it will be much easier to use when you define the equations.

- Atharv
Post Reply