Optimization Action Set

Simple Nonlinear Program with FCMP Objective

This example illustrates how to use the runOptmodel action to solve a simple optimization problem that has a nonlinear objective. It uses FCMP code to define the objective function, which is the same Bard function that is used in Example 2.3: Simple Nonlinear Program. In this case, the least-squares data values are defined inside the FCMP routine.

You can use the following statements to define the FCMP objective routine:

proc cas;
   loadactionset "fcmpact";
   setSessOpt{cmplib="casuser.myactdef"};
   run;

   addRoutines{
      routineCode = {"
         function bard(x1, x2, x3);
                array y[15] /nosymbols (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);
                fx = 0;
                do k = 1 to 15;
                        vk = 16 - k;
                        wk = min(k,vk);
                        fxk = y[k] - (x1 + k/(vk*x2 + wk*x3));
                        fx = fx + fxk**2;
                end;
                return (0.5*fx);
         endsub;" },
      package   = "mypkg",
      saveTable = true,
      funcTable = {name="myactdef", caslib="casuser", replace=true}
      };
   run;
quit;

You can then run the following statements to perform the optimization and display the results:

proc cas;
   loadactionset "optimization";
   source pgm;
      var x {1..3} >= -1000 <= 1000 init 1;
      min f = bard(of x[*]);
      solve;
      create data xdata from [i] xd=x;
   endsource;
   action runOptmodel result=r / code=pgm;
   print r["Solve1.ProblemSummary"];
   print r["Solve1.SolutionSummary"];
   run;
   action table.fetch / table="xdata";
   run;
quit;

The problem summary and solution summary are shown in Output 2.4.1 and Output 2.4.2.

Output 2.4.1: Problem Summary

Solve1.ProblemSummary: Results from optimization.runOptmodel

Problem Summary
Objective SenseMinimization
Objective Functionf
Objective TypeNonlinear
  
Number of Variables3
Bounded Above0
Bounded Below0
Bounded Below and Above3
Free0
Fixed0
  
Number of Constraints0


Output 2.4.2: Solution Summary

Solve1.SolutionSummary: Results from optimization.runOptmodel

Solution Summary
SolverNLP
AlgorithmInterior Point Direct
Objective Functionf
Solution StatusOptimal
Objective Value0.0041074387
  
Optimality Error9.0909092E-8
Infeasibility0
  
Iterations8
Presolve Time0.00
Solution Time0.00


The data table xdata displayed in Output 2.4.3 shows the solution that is found by the NLP solver.

Output 2.4.3: Simple Nonlinear Program Solution with FCMP

Results from table.fetch

Selected Rows from Table XDATA
_Index_ixd
110.0824105598
221.1330360921
332.3436951786


Simple Nonlinear Program with FCMP Objective

This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.

For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.

This example solves the nonlinear optimization problem by using an FCMP function to define the objective:

s:setSessOpt{cmplib="casuser.myactdef"}
s:fcmpact_addRoutines{
   routineCode = {[[
      function bard(x1, x2, x3);
              array y[15] /nosymbols (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);
              fx = 0;
              do k = 1 to 15;
                      vk = 16 - k;
                      wk = min(k,vk);
                      fxk = y[k] - (x1 + k/(vk*x2 + wk*x3));
                      fx = fx + fxk**2;
              end;
              return (0.5*fx);
      endsub;]] },
   package   = "mypkg",
   saveTable = true,
   funcTable = {name="myactdef", caslib="casuser", replace=true} }
s:optimization_runOptmodel{ code = [[
   var x {1..3} >= -1000 <= 1000 init 1;
   min f = bard(of x[*]);
   solve;
   create data xdata from [i] xd=x;]] }

Simple Nonlinear Program with FCMP Objective

This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.

For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.

This example solves the nonlinear optimization problem by using an FCMP function to define the objective:

s.setSessOpt(cmplib="casuser.myactdef")
s.loadActionSet(actionset="fcmpact")
s.addRoutines(
    routineCode = {"""
        function bard(x1, x2, x3);
                array y[15] /nosymbols (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);
                fx = 0;
                do k = 1 to 15;
                        vk = 16 - k;
                        wk = min(k,vk);
                        fxk = y[k] - (x1 + k/(vk*x2 + wk*x3));
                        fx = fx + fxk**2;
                end;
                return (0.5*fx);
        endsub;""" },
    package   = "mypkg",
    saveTable = True,
    funcTable = s.CASTable("myactdef", caslib="casuser", replace=True) )
s.loadActionSet(actionset="optimization")
s.runOptmodel( code = """
    var x {1..3} >= -1000 <= 1000 init 1;
    min f = bard(of x[*]);
    solve;
    create data xdata from [i] xd=x;""" )

Simple Nonlinear Program with FCMP Objective

This section contains R code for the analysis in the CASL version of this example, which contains details about the results.

For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.

This example solves the nonlinear optimization problem by using an FCMP function to define the objective:

cas.builtins.loadActionSet(s, actionSet="sessionProp")
cas.sessionProp.setSessOpt(s, cmplib="casuser.myactdef")
cas.builtins.loadActionSet(s, actionSet="fcmpact")
cas.fcmpact.addRoutines(s,
   routineCode=c("
      function bard(x1, x2, x3);
              array y[15] /nosymbols (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);
              fx = 0;
              do k = 1 to 15;
                      vk = 16 - k;
                      wk = min(k,vk);
                      fxk = y[k] - (x1 + k/(vk*x2 + wk*x3));
                      fx = fx + fxk**2;
              end;
              return (0.5*fx);
      endsub;"),
   package="mypkg",
   saveTable=TRUE,
   funcTable=c(name="myactdef", caslib="casuser", replace=TRUE) )

cas.builtins.loadActionSet(s, actionSet="optimization")
cas.optimization.runOptmodel(s,
   code="
      var x {1..3} >= -1000 <= 1000 init 1;
      min f = bard(of x[*]);
      solve;
      create data xdata from [i] xd=x;
   ")
Last updated: April 22, 2022