Building super-sets

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

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.

  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?

Hi alodder,

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

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_SetDefinition.html#UG_SetDefinition_ImplicitSetDefinition (the second example is pretty much the same as what I did here)

Best,
Lutz

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