Resources

Unconstrained Rosenbrock Function

/****************************************************************/
/*          S A S   S A M P L E   L I B R A R Y                 */
/*                                                              */
/*    NAME: IMNLPEX0                                            */
/*   TITLE: Unconstrained Rosenbrock Function                   */
/* PRODUCT: IML                                                 */
/*  SYSTEM: ALL                                                 */
/*    KEYS:                                                     */
/*   PROCS: IML                                                 */
/*    DATA:                                                     */
/*                                                              */
/* SUPPORT: saswmh                      UPDATE:frwick 5/2015    */
/*     REF: Technical Report Examples                           */
/*    MISC:                                                     */
/*                                                              */
/****************************************************************/

proc iml;
start F_ROSEN(x);
   y1 = 10. * (x[2] - x[1] * x[1]);
   y2 = 1. - x[1];
   f  = .5 * (y1 * y1 + y2 * y2);
   return(f);
finish F_ROSEN;

start G_ROSEN(x);
   g = j(1,2,0.);
   g[1] = -200.*x[1]*(x[2]-x[1]*x[1]) - (1.-x[1]);
   g[2] =  100.*(x[2]-x[1]*x[1]);
   return(g);
finish G_ROSEN;

x = {-1.2 1.};
optn = {0 2};
call nlptr(rc,xres,"F_ROSEN",x,optn) grd="G_ROSEN";
quit;

proc iml;
start F_ROSEN_LS(x);
  y = j(1,2,0.);
  y[1] = 10. * (x[2] - x[1] * x[1]);
  y[2] = 1. - x[1];
 return(y);
finish F_ROSEN_LS;

x = {-1.2 1.};
optn = {2 2};
call nlplm(rc,xres,"F_ROSEN_LS",x,optn);
quit;

proc iml;
start F_BETTS(x);
   f = .01 * x[1] * x[1] + x[2] * x[2] - 100.;
   return(f);
finish F_BETTS;

con = {  2. -50.  .   .,
        50.  50.  .   .,
        10.  -1. 1. 10.};
x = {-1. -1.};
optn = {0 2};
ods select ParameterEstimates LinCon ProblemDescription
    IterStart IterHist IterStop LinConSol;
call nlpcg(rc,xres,"F_BETTS",x,optn,con);
quit;

proc iml;
start F_HS43(x);
   f = x*x` + x[3]*x[3] - 5*(x[1] + x[2]) - 21*x[3] + 7*x[4];
   return(f);
finish F_HS43;
start C_HS43(x);
   c = j(3,1,0.);
   c[1] = 8 - x*x` - x[1] + x[2] - x[3] + x[4];
   c[2] = 10 - x*x` - x[2]*x[2] - x[4]*x[4] + x[1] + x[4];
   c[3] = 5 - 2.*x[1]*x[1] - x[2]*x[2] - x[3]*x[3]
            - 2.*x[1] + x[2] + x[4];
   return(c);
finish C_HS43;
x = j(1,4,1);
optn= j(1,11,.); optn[2]= 3; optn[10]= 3; optn[11]=0;
ods select ProblemDescription IterStart IterHist IterStop ParameterEstimates;
call nlpqn(rc,xres,"F_HS43",x,optn) nlc="C_HS43";

proc iml;
start F_GLOBAL(x);
   f=(3*x[1]**4-28*x[1]**3+84*x[1]**2-96*x[1]+64)/27 + x[2]**2;
   return(f);
finish F_GLOBAL;
xa = {.5 1.5};
xb = {3 -1};
optn = {0 2};
call nlptr(rca,xra,"F_GLOBAL",xa,optn);
call nlptr(rcb,xrb,"F_GLOBAL",xb,optn);
print xra, xrb;

proc iml;
start F_ROSEN(x);
   y1 = 10. * (x[2] - x[1] * x[1]);
   y2 = 1. - x[1];
   f  = .5 * (y1 * y1 + y2 * y2);
   return(f);
finish F_ROSEN;

start G_ROSEN(x);
   g = j(1,2,0.);
   g[1] = -200.*x[1]*(x[2]-x[1]*x[1]) - (1.-x[1]);
   g[2] =  100.*(x[2]-x[1]*x[1]);
   return(g);
finish G_ROSEN;

start H_ROSEN(x);
   h = j(2,2,0.);
   h[1,1] = -200.*(x[2] - 3.*x[1]*x[1]) + 1.;
   h[2,2] =  100.;
   h[1,2] = -200. * x[1];
   h[2,1] = h[1,2];
   return(h);
finish H_ROSEN;

start F_ROSEN_LS(x);
   y = j(1,2,0.);
   y[1] = 10. * (x[2] - x[1] * x[1]);
   y[2] = 1. - x[1];
  return(y);
finish F_ROSEN_LS;

start J_ROSEN(x);
   jac = j(2,2,0.);
   jac[1,1] = -20. * x[1]; jac[1,2] = 10.;
   jac[2,1] = -1.;         jac[2,2] =  0.;
   return(jac);
finish J_ROSEN;

proc iml;
start f_nlp22(x);
   n=ncol(x);
   f = 0.;
   do i=1 to n-3 by 4;
      f1 = x[i] + 10. * x[i+1];
      r2 = x[i+2] - x[i+3];
      f2 = sqrt(5) * r2;
      r3 = x[i+1] - 2. * x[i+2];
      f3 = r3 * r3;
      r4 = x[i] - x[i+3];
      f4 = sqrt(10) * r4 * r4;
      f = f + f1 * f1 + r2 * f2 + f3 * f3 + r4 * r4 * f4;
   end;
   f = 0.5 * f;
   return(f);
finish f_nlp22;

start g_nlp22(x);
   n=ncol(x);
   g = j(1,n,0.);
      do i=1 to n-3 by 4;
         f1 = x[i] + 10. * x[i+1];
         f2 = sqrt(5) * (x[i+2] - x[i+3]);
         r3 = x[i+1] - 2. * x[i+2];
         f3 = r3 * r3;
         r4 = x[i] - x[i+3];
         f4 = sqrt(10) * r4 * r4;
         g[i]   = f1 + 2. * r4 * f4;
         g[i+1] = 10. * f1 + 2. * r3 * f3;
         g[i+2] = f2 - 4. * r3 * f3;
         g[i+3] = -f2 - 2. * r4 * f4;
      end;
      return(g);
finish g_nlp22;

start hs_nlp22(x);
   n=ncol(x);
   nnz = 8 * (n / 4);
   h = j(nnz,3,0.);
   j = 0;
   do i=1 to n-3 by 4;
      f1 = x[i] + 10. * x[i+1];
      f2 = sqrt(5) * (x[i+2] - x[i+3]);
      r3 = x[i+1] - 2. * x[i+2];
      f3 = r3 * r3;
      r4 = x[i] - x[i+3];
      f4 = sqrt(10) * r4 * r4;
      j= j + 1; h[j,1] = i; h[j,2] = i;
      h[j,3] = 1. + 4. * f4;
      h[j,3] = h[j,3] + 2. * f4;
      j= j+1; h[j,1] = i; h[j,2] = i+1;
      h[j,3] = 10.;
      j= j+1; h[j,1] = i; h[j,2] = i+3;
      h[j,3] = -4. * f4;
      h[j,3] = h[j,3] - 2. * f4;
      j= j+1; h[j,1] = i+1; h[j,2] = i+1;
      h[j,3] = 100. + 4. * f3;
      h[j,3] = h[j,3] + 2. * f3;
      j= j+1; h[j,1] = i+1; h[j,2] = i+2;
      h[j,3] = -8. * f3;
      h[j,3] = h[j,3] - 4. * f3;
      j= j+1; h[j,1] = i+2; h[j,2] = i+2;
      h[j,3] = 5. + 16. * f3;
      h[j,3] = h[j,3] + 8. * f3;
      j= j+1; h[j,1] = i+2; h[j,2] = i+3;
      h[j,3] = -5.;
      j= j+1; h[j,1] = i+3; h[j,2] = i+3;
      h[j,3] = 5. + 4. * f4;
      h[j,3] = h[j,3] + 2. * f4;
   end;
   return(h);
finish hs_nlp22;

n = 40;
x = j(1,n,0.);
do i=1 to n-3 by 4;
   x[i] = 3.; x[i+1] = -1.; x[i+3] = 1.;
end;
opt = j(1,11,.); opt[2]= 3; opt[9]= 8 * (n / 4);
call nlpnra(xr,rc,"f_nlp22",x,opt) grd="g_nlp22" hes="hs_nlp22";

proc iml;
con = {   2   .   .   0    .    . ,
        100  40   .   .    .    . ,
          4   3  -1   .   -1   30 ,
          .   1   .   6    1   17 ,
          1  -1   .   .    0    8 };

proc iml;
start F_UC2D(x);
   f = x[1] * x[2];
   return(f);
finish F_UC2D;

start C_UC2D(x);
   c = 1. - x * x`;
   return(c);
finish C_UC2D;

x = j(1,2,1.);
optn= j(1,10,.); optn[2]= 3; optn[10]= 1;
CALL NLPNMS(rc,xres,"F_UC2D",x,optn) nlc="C_UC2D";