Resources

Example Code from Details Section (lsod01)

/**********************************************************************/
/*                                                                    */
/*               S A S   S A M P L E   L I B R A R Y                  */
/*                                                                    */
/*    NAME: lsod01                                                    */
/*   TITLE: Example Code from Details Section (lsod01)                */
/* PRODUCT: OR                                                        */
/*  SYSTEM: ALL                                                       */
/*    KEYS: OR                                                        */
/*   PROCS: FCMP, OPTLSO                                              */
/*    DATA:                                                           */
/*                                                                    */
/* SUPPORT:                             UPDATE:                       */
/*     REF:                                                           */
/*    MISC: Various code from the details section of the PROC OPTLSO  */
/*          chapter of the Local Search Optimization book.            */
/*          This code doesn't make sense all together, but is         */
/*          included in this sample file as a place to easiliy copy   */
/*          and paste parts of the code and build upon it.            */
/*                                                                    */
/**********************************************************************/

/**********************************************************************/
/*                                                                    */
/*  Sample code found in Details:Variable Data Set section            */
/*                                                                    */
/**********************************************************************/

data vardata;
   input _id_ $ _lb_ _ub_ _type_ $;
   datalines;
x1  0 1000  C
x2  0 0.001 C
x3  0 4     I
;

data vardata;
   input _id_ $ _lb_ _ub_ _type_ $ _scale_;
   datalines;
x1  0 1000  C  1000
x2  0 0.001 C  0.5
x3  0 4     I  2
;

/**********************************************************************/
/*                                                                    */
/*  Sample code found in Details:Describing the Objective Function    */
/*  section                                                           */
/*                                                                    */
/**********************************************************************/

proc fcmp outlib=sasuser.myfuncs.mypkg;
   function sixhump(x1,x2);
      return ((4 - 2.1*x1**2 + x1**4/3)*x1**2
              + x1*x2 + (-4 + 4*x2**2)*x2**2);
   endsub;
run;

data objdata;
   input _id_ $ _function_ $ _sense_ $;
   datalines;
f   sixhump     min
;

data objdata;
   length _function_ $10;
   input _id_ $ _function_ $ _sense_ $;
   datalines;
f1   sixhump1      .
f2   sixhump2      .
f3   sixhumpNew    min
;
proc fcmp outlib=sasuser.myfuncs.mypkg;
   function sixhump1(x1,x2);
      return (4 - 2.1*x1**2 + x1**4/3);
   endsub;
   function sixhump2(x1,x2);
      return (-4 + 4*x2**2);
   endsub;
   function sixhumpNew(x1,x2,f1,f2);
      return (f1*x1**2 + x1*x2 + f2*x2**2);
   endsub;
run;

data Abdata;
   input _id_ $ a1 a2 a3 a4 a5 b;
   datalines;
row1  1  2  3  4  5  6
row2  7  8  9  10 11 12
row3  13 14 15 16 17 18
;

proc fcmp outlib=sasuser.myfuncs.mypkg;
   function axbi(x1,x2,x3,x4,x5,a1,a2,a3,a4,a5,b);
      array x[5];
      array a[5];
      di = -b;
      do j=1 to 5;
         di = di + a[j]*x[j];
      end;
      return (di*di);
   endsub;

   function onenorm(x1,x2,x3,x4,x5);
      array x[5];
      f2 = 0;
      do j=1 to 5;
         f2 = f2 + abs(x[j]);
      end;
      return (f2);
   endsub;

   function combine(f1, f2);
      return (sqrt(f1)+f2);
   endsub;
run;

data lsqobj1;
   input _id_ $ _function_$ _sense_ $ _dataset_ $;
   datalines;
f1        axbi         .      Abdata
f2        onenorm      .      .
f         combine      min    .
;

data xvar;
   input _id_ $ @@;
   datalines;
   x1 x2 x3 x4 x5
   ;

options cmplib=sasuser.myfuncs;
proc optlso
   variables = xvar
   objective = lsqobj1;
run;

proc fcmp outlib=sasuser.myfuncs.mypkg;
   function combine2(x1,x2,x3,x4,x5, f1);
      array x[5];
      f2 = 0;
      do j=1 to 5;
         f2 = f2 + abs(x[j]);
      end;
      return (sqrt(f1)+f2);
   endsub;
run;

data lsqobj2;
   input _id_ $ _function_$ _sense_ $ _dataset_ $;
   datalines;
f1        axbi         .         Abdata
f         combine2     min       .
;

options cmplib=sasuser.myfuncs;
proc optlso
   variables = xvar
   objective = lsqobj2;
run;

/**********************************************************************/
/*                                                                    */
/*  Sample code found in Details:Describing Linear Constraints        */
/*  section                                                           */
/*                                                                    */
/**********************************************************************/

data lcondata;
   input _id_ $ _lb_ x1-x3 _ub_;
   datalines;
a1 -1    2 0 -4 15
a2  1   -3 7  0 13
a3  11   1 1  1 11
;

/**********************************************************************/
/*                                                                    */
/*  Sample code found in Details:Describing Nonlinear Constraints     */
/*  section                                                           */
/*                                                                    */
/**********************************************************************/

proc fcmp outlib=sasuser.myfuncs.mypkg;
   function con1(x1, x2, x3);
      return (x1*x2*x3 + sin(x2));
   endsub;
   function con2(x1, x2, x3);
      return (x1*x2 + x1*x3 + x3*x3);
   endsub;
run;

data condata;
   input _id_ $ _lb_ _ub_;
   datalines;
con1 -1 1
con2  1 1
;

/**********************************************************************/
/*                                                                    */
/*  Sample code found in Details:Specifying and Returning Trial       */
/*  Points section                                                    */
/*                                                                    */
/**********************************************************************/

data popin;
   low = -5.0;
   upp = 10.0;
   numpoints = 30;
   dim = 5;
   do _sol_=1 to numpoints;
      do i=1 to dim;
         _id_= compress("x" || put(i, 4.0));
         _value_ = low + (upp-low)*ranuni(2);
         output;
      end;
   end;
   keep _sol_ _id_ _value_;
run;