Inner Assignment of Parameter

Problems with syntax of GAMS
Post Reply
GabrielYin
User
User
Posts: 72
Joined: 6 years ago
Location: Dallas, TX, USA
Contact:

Inner Assignment of Parameter

Post by GabrielYin »

Hi All,

I wonder if there is an efficient way to assign values for part of the parameter.

For instance, I have a parameter with 64 values controlled by m. Then I have another parameter with 12 values controlled by n. Now I want to put the latter parameter within the former one. Specifically, I want the part of the former parameter which starts from 13th value and ends with 24th value, to be exactly the latter one. I have an idea for that.

Code: Select all

Set      b       /1*64/
         m       /1*12/

Parameter        Former(b), Latter(m);

* Give Latter some values
loop(m,
         Latter(m) = ord(m);
);

scalar i /1/;

Loop(b$(ord(b)>=13 and ord(b)<=24),
         Former(b) = sum(m$(ord(m)=i), Latter(m));
         i = i+1;
);

display Former;
It can surely work, but it is weird that I created a non-index iterator "I". GAMS grammar for loop is a bit uncomfortable that I cannot use overall assignment like MATLAB: Former[13:24] = Latter. I wonder if there is more efficient way in GAMS to do this. By the way, to the best of my knowledge, GAMS has poor data manipulation functionality... I hope it can be improved in the future.

Best,
Gabriel
User avatar
dirkse
Moderator
Moderator
Posts: 214
Joined: 7 years ago
Location: Fairfax, VA

Re: Inner Assignment of Parameter

Post by dirkse »

Gabriel,

It seems you are trying to do Matlab programming in the GAMS language. That is possible, but as you have discovered, it's "wierd" and "uncomfortable", to use your words. I suggest you do things "the GAMS way". That means many things, but in this case it implies using the relational data model of GAMS: we refer to things by index names, but usually not by index positions. Parallel assignments and operations are usually preferred over loops. And we use subsets where appropriate.

At least some of this is detailed in the short GAMS Tutorial: you should have a look at that to get started.

https://www.gams.com/latest/docs/UG_Tutorial.html

Here's an example that (I hope) does what you wanted done in a GAMS way.

Code: Select all

sets
  i   / 'baltimore', 'annapolis', 'Washington', 'Easton', 'Oxford' /
  east(i) 'cities on the Eastern Shore'  / 'Easton', 'Oxford' /
  ;
parameters
  c(i) 
  d(east)
  ;
d(east) = ord(east);
c(east) = d(east);
Post Reply