Building super-sets Topic is solved

Problems with syntax of GAMS
Post Reply
alodder

Building super-sets

Post by alodder »

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: 59
Joined: 7 years ago

Re: Building super-sets

Post by Lutz »

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

Re: Building super-sets

Post by alodder »

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