Building super-sets

Problems with syntax of GAMS
Post Reply
alodder
User
User
Posts: 2
Joined: 2 months ago

Building super-sets

Post by alodder » 2 months ago

I'm trying to build a super-set so i can collect parameters / variables about items that come from different sets.

Code: Select all


Sets
fruit / apple, orange /
veggie / carrot,  tomato /
;
Parameters
FruitCost /
  apple    .99
  orange   .89
/
VeggieCost /
  tomato    .63
  carrot     .83
/;


Set food / #fruit #veggie /;

Parameter FoodCost(food);
FoodCost(fruit) = FruitCost(fruit);
FoodCost(veggie) = FruitCost(veggie);

display FoodCost;

However, when I run this i get a domain violation, which is essentially that allthought my 'food' set contains all the elements of the 'fruit' set, the 'fruit' set is not a subset of 'food' and so it isn't a legal assignment.

Code: Select all

  68  FoodCost(fruit) = FruitCost(fruit);
****                $171
  69  FoodCost(veggie) = FruitCost(veggie);
****                 $171

G e n e r a l   A l g e b r a i c   M o d e l i n g   S y s t e m
Error Messages


171  Domain violation for set

**** 2 ERROR(S)   0 WARNING(S)
How do I make 'food', 'fruit' and 'veggie' compatible so I can aggregate parameters and variables across sets?

Lutz
User
User
Posts: 26
Joined: 2 years ago

Re: Building super-sets

Post by Lutz » 2 months ago

Hi alodder,

This is an excellent example for the use of the new GAMS feature called "implicit set definition":

Code: Select all

Sets
food
fruit(food<) / apple, orange /
$onMulti
veggie(food<) / carrot,  tomato /
;

Parameters
FruitCost(fruit) /
  apple    .99
  orange   .89
/
VeggieCost(veggie) /
  tomato    .63
  carrot     .83
/;


Parameter FoodCost(food);
FoodCost(fruit) = FruitCost(fruit);
FoodCost(veggie) = VeggieCost(veggie);

display FoodCost;
This was introduced with the latest GAMS version 26.1. More about this can be read here: https://www.gams.com/latest/docs/UG_Set ... Definition (the second example is pretty much the same as what I did here)

Best,
Lutz

alodder
User
User
Posts: 2
Joined: 2 months ago

Re: Building super-sets

Post by alodder » 2 months ago

Thank you Lutz! This will work well for my use-case.

Post Reply