Resources

Expression Extension Examples (omod0d)

/***************************************************************/
/*                                                             */
/*          S A S   S A M P L E   L I B R A R Y                */
/*                                                             */
/*    NAME: omod0d                                             */
/*   TITLE: Expression Extension Examples (omod0d)             */
/* PRODUCT: OR                                                 */
/*  SYSTEM: ALL                                                */
/*    KEYS: OR                                                 */
/*   PROCS: OPTMODEL                                           */
/*    DATA:                                                    */
/*                                                             */
/* SUPPORT:                             UPDATE:                */
/*     REF:                                                    */
/*    MISC: Examples from the Expression Extensions section of */
/*          the OPTMODEL chapter of Mathematical Programming.  */
/*                                                             */
/***************************************************************/

/*  AND Aggregation Expression  */
proc optmodel;
   put (and{i in 1..5} i < 10); /* returns 1 */
   put (and{i in 1..5} i NE 3); /* returns 0 */
quit;

/*  CARD Function  */
proc optmodel;
   put (card(1..3));
quit;

/*  CROSS Expression  */
proc optmodel;
   set s1 = 1..2;
   set<string> s2 = {'a', 'b'};
   set<number, string> s3=s1 cross s2;
   put 's3 is ' s3;
   set<number, string, number> s4 = s3 cross 4..5;
   put 's4 is ' s4;
quit;

/*  IN Expression  */
proc optmodel;
   set s = 1..10;
   put (5 in s);         /* outputs 1 */
   put (-1 not in s);    /* outputs 1 */
   set<num, str> t = {<1,'a'>, <2,'b'>, <2,'c'>};
   put (<2, 'b'> in t);   /* outputs 1 */
   put (<1, 'b'> in t);  /* outputs 0 */
quit;

/*  Index Set Expression  */
proc optmodel;
   put ({i in 1..5 : i NE 3}); /* outputs {1,2,4,5} */
quit;

/*  INTER Expression  */

proc optmodel;
   put ({1,3} inter {2,3}); /* outputs {3} */
quit;

/*  INTER Aggregation Expression  */

proc optmodel;
   put (inter{i in 1..3} i..i+3); /* outputs {3,4} */
quit;

/*  MAX Aggregation Expression  */

proc optmodel;
   put (max{i in 2..5} 1/i);
quit;

/*  MIN aggregation Expression  */

proc optmodel;
   put (min{i in 2..5} 1/i);
quit;

/*  OR Aggregation Expression  */

proc optmodel;
   put (or{i in 1..5} i = 2); /* returns 1 */
   put (or{i in 1..5} i = 7); /* returns 0 */
quit;

/*  PROD Aggregation Expression  */

proc optmodel;
   number n = 5;
   put (prod{i in 1..n} i); /* outputs 120 */
quit;

/*  RANGE Expression  */

proc optmodel;
   put (10..30 by 7); /* outputs {10,17,24} */
quit;

/*  Set Constructor Expression  */

proc optmodel;
   put ({1,2,3,2}); /* outputs {1,2,3} */
quit;

proc optmodel;
   number m = 3, n = 4;
   var x{1..4} init 1;
   string y = 'c';
   put ({<'a', x[3]>, <'b', m>, <y, m/n>});
quit;

/*  SETOF Aggregation Expression  */

proc optmodel;
   put (setof{i in 1..3}<i, i*i, i**3>);
quit;

/*  SLICE Expression  */

proc optmodel;
   put (slice(<1,*>, {<1,3>, <1,0>, <3,1>}));
   put (slice(<*,2,*>, {<1,2,3>, <2,4,3>, <2,2,5>}));
quit;

proc optmodel;
   set<str,str> dep = {<'B','A'>, <'C','B'>, <'D','C'>};
   set<str,str> cl;
   set<str> cn;
   cl = dep;
   cn =  (setof{<i,j> in dep} i) inter (setof{<i,j> in dep} j);
   for {node in cn}
       cl = cl union (slice(<*,node>,cl) cross slice(<node,*>,cl));
   put cl;
quit;

/*  SUM Expression   */

proc optmodel;
   put (sum {i in 1..10} i);  /* outputs 55 */
quit;

/*  SYMDIFF Expression  */

proc optmodel;
   put ({1,3} symdiff {2,3}); /* outputs {1,2} */
quit;

/*  Tuple Expression  */

proc optmodel;
   put (<1,2,3> in setof{i in 1..2}<i,i+1,i+2>);
   put ({<1,'a'>, <2,'b'>} cross {<3,'c'>, <4,'d'>});
quit;

/*  UNION Expression  */

proc optmodel;
   put ({1,3} union {2,3}); /* outputs {1,3,2} */
quit;

/*  UNION Aggregation Expression  */

proc optmodel;
   put (union{i in 1..3} i..i+3); /* outputs {1,2,3,4,5,6} */
quit;

/*  WITHIN Expression  */

proc optmodel;
   put ({1,3} within {2,3});     /* outputs 0 */
   put ({1,3} not within {2,3}); /* outputs 1 */
   put ({1,3} within {1,2,3});   /* outputs 1 */
quit;