sas - Optmodel create data with 2 key-columns Optmodel -
i have vector x 3 sets [origin, destination, product] has value, , want create table has key columns origin , destination , every product column saying how transport.
so like:
origin dest. producta productb...
a.........ab.......10......0
a.........ac.......5.......4
i'm trying this, in optmodel:
create data ctrl3.transport [origin destination] = {fab, cities} {k in product} <col('product_'||k) = x[origin, destination, k]>; run;
but sas doesn't seem it
tldr;
the fix use {fi in fab, ci in cities}
instead of {fab,cities}
. use fi
, ci
instead of origin
, destination
. comments in code below explain further. more details after code.
code example
proc optmodel; set fab init /rdu/; set cities init /bcn mel/; set product init /planes trains automobiles/; num x{fab,cities,product} init [ /* bcn */ 3 4 5 /* mel */ 6 7 8]; put x[*]=; /* verify declarations */ /* efficient solution. in columns, use fi , ci, not origin , destination */ create data work.transport [origin destination] = {fi in fab, ci in cities} {k in product} <col('product_'||k) = x[fi, ci, k]> ; /* works because key-set set expression, confusingly, not same index set expression. difference important: version creates redundant set of arity 2. */ create data work.works_but_is_inneficient [origin destination] = ({fab, cities}) {k in product} <col('product_'||k) = x[origin, destination, k]> ; /* original approach. fails because when key-set index set expression, optmodel looks there dummy parameters. create data work.op [origin destination] = {fab, cities} {k in product} <col('product_'||k) = x[origin, destination, k]> ; */ quit;
details
for efficiency, optmodel has 2 similar types of expressions confuse advanced users:
- set expression, evaluates code , returns set
- index set expression, returns set of expressions.
you can associate dummy variables index set expression. in create data statement, when optmodel sees index set expression, uses dummy variables instead of names in square brackets. since in op there no such dummy variables, error.
Comments
Post a Comment