Monday, December 3, 2012

GAMS question

A question was posed at https://groups.google.com/forum/?fromgroups=#!topic/gamsworld/0qYEcVd9x8I:

set    j  /n1*n5/,
       k  /n5/;

variable pi(j);

scalar ab;
ab=sum (k, sum(j$(ord(j)=ord(k)), pi.L(j) )  );

The user want to pick the 5-th element. This implementation clearly does not work. Some valid answers were given:

Ab= sum(j$sameas(j,“n5“), pi.L(j) )  ;
or
ab=sum (k, sum(j$sameas(j,k), pi.L(j) )  );

However I would say this is not really good modeling practice. The best approach IMHO would be:

set   j     /n1*n5/,
      k(j)  /n5/;

variable pi(j);

scalar ab;
ab=sum(k, pi.L(k));

There are two advantages of this approach:

  • The set k is now domain checked against j; no elements outside j can be specified here without alarm bells going off. Domain checking is very important when developing large, complex models.
  • We can substantially simplify the assignment to ab and increase readability. The expression conveys immediately what it is supposed to do.

Setting up proper subsets will help you down the road.

No comments:

Post a Comment