Getting Started Example (lsog01)

/**********************************************************************/
/*                                                                    */
/*               S A S   S A M P L E   L I B R A R Y                  */
/*                                                                    */
/*    NAME: lsog01                                                    */
/*   TITLE: Getting Started Example (lsog01)                          */
/* PRODUCT: OR                                                        */
/*  SYSTEM: ALL                                                       */
/*    KEYS: OR                                                        */
/*   PROCS: FCMP, OPTLSO                                              */
/*    DATA:                                                           */
/*                                                                    */
/* SUPPORT:                             UPDATE:                       */
/*     REF:                                                           */
/*    MISC: Getting Started example from the OPTLSO chapter of the    */
/*          Local Search Optimization book.                           */
/*                                                                    */
/**********************************************************************/
title 'PROC OPTLSO';

title3 'A Bound-Constrained Problem, Branin Function';
/* A Bound-Constrained Problem */
data vardata;
   input _id_ $ _lb_ _ub_;
   datalines;
x1 -5 10
x2  0 15
;

proc fcmp outlib=sasuser.myfuncs.mypkg;
   function branin(x1, x2);
      pi = constant('PI');
      y1 = (x2-(5.1/(4*pi**2))*x1*x1+5*x1/pi-6)**2;
      y2 = 10*(1-1/(8*pi))*cos(x1);
      return (y1+y2+10);
   endsub;
run;

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

options cmplib = sasuser.myfuncs;
proc optlso
   primalout = solution
   variables = vardata
   objective = objdata;
   performance nthreads=4;
run;

proc print data=solution;
run;

title3 'Linear Constraint on the Decision Variables';
/* Linear Constraint on the Decision Variables */
data lindata;
   input _id_ $ _lb_ x1 x2 _ub_;
   datalines;
a1  . 1 1  0.6
;

proc optlso
   variables = vardata
   objective = objdata
   lincon    = lindata;
run;

title3 'Nonlinear Constraints on the Decision Variables';
/* Nonlinear Constraints on the Decision Variables */
data condata;
   input _id_ $ _lb_ _ub_;
   datalines;
c1  14 .
;

proc fcmp outlib=sasuser.myfuncs.mypkg;
   function c1(x1, x2);
      return (x1**2 - 2*x2);
   endsub;
run;

proc optlso
   variables = vardata
   objective = objdata
   lincon    = lindata
   nlincon   = condata;
run;


title3 'Maximum Likelihood Example';
/* Maximum Likelihood Example */

data lkhvar;
   input _id_ $ _lb_;
   datalines;
mu    .
sigma 0
;

data lkhobj1;
   input _id_ $ _function_ $ _sense_ $;
   datalines;
f loglkh1 max
;

proc fcmp outlib=sasuser.myfuncs.mypkg;
   function loglkh1(mu, sigma);
      array x[5] / nosym (1 3 4 5 7);
      s=0;
      do j=1 to 5;
         s = s - log(sigma) - 0.5*((x[j]-mu)/sigma)**2;
      end;
      return (s);
   endsub;
run;

proc optlso
   variables = lkhvar
   objective = lkhobj1;
run;

data logdata;
   input x @@;
   datalines;
1 3 4 5 7
;

data lkhobj2;
   input _id_ $ _function_ $ _sense_ $ _dataset_ $;
   datalines;
f loglkh2 max logdata
;

proc fcmp outlib=sasuser.myfuncs.mypkg;
   function loglkh2(mu, sigma, x);
      return (-log(sigma) -0.5*((x-mu)/sigma)**2);
   endsub;
run;

proc optlso
   variables = lkhvar
   objective = lkhobj2;
run;


data lkhobj3;
   input _id_ $ _function_ $ _sense_ $ _dataset_ $;
   datalines;
f loglkh3 max logdata
;

proc fcmp outlib=sasuser.myfuncs.mypkg;
   function loglkh3(x, sigma, mu);
      return (- log(sigma) - 0.5*((x-mu)/sigma)**2);
   endsub;
run;

proc optlso
   variables = lkhvar
   objective = lkhobj3;
run;


 data barddata;
    input y @@;
    datalines;
 0.14 0.18 0.22 0.25 0.29
 0.32 0.35 0.39 0.37 0.58
 0.73 0.96 1.34 2.10 4.39
 ;

 proc fcmp outlib=sasuser.myfuncs.mypkg;
    function bard(x1, x2, x3);
       array y[15] / nosym;
          rc = read_array('barddata', y);
          fx = 0;
          do k=1 to 15;
             dk  = (16-k)*x2 + min(k,16-k)*x3;
             fxk = y[k] - (x1 + k/dk);
             fx = fx + fxk**2;
          end;
       return (0.5*fx);
    endsub;
 run;

 options cmplib = sasuser.myfuncs;
 data _null_;
    bval = bard(1,2,3);
    put bval=;
 run;