Optimization Action Set

Simple Nonlinear Program

This section contains PROC CAS code.

Note: Input data must be accessible in your CAS session, either as a CAS table or as a transient-scope table. A CAS table has a two-level name: the first level is your CAS engine libref, and the second level is the table name. You refer to this table in the CAS procedure by specifying only the second level. For more information about two-level names, see Chapter 3, Shared Concepts (SAS Optimization: Mathematical Optimization Procedures). A transient-scope table is called directly from the action and exists in memory for the duration of the action. For more information about accessing data, see SAS Viya: System Programming Guide. For more information about PROC CAS and programming in CASL, see SAS Cloud Analytic Services: CASL Programmer’s Guide and SAS Cloud Analytic Services: CASL Reference.

This example illustrates how to use the runOptmodel action to solve a simple optimization problem that has a nonlinear objective. It uses the READ DATA statement to read from a CAS table. The objective is the Bard function, which is a least squares problem with upper I equals StartSet 1 comma 2 comma ellipsis comma 15 EndSet,

f left-parenthesis x right-parenthesis equals one-half sigma-summation Underscript k element-of upper I Endscripts left-bracket y Subscript k Baseline minus left-parenthesis x 1 plus StartFraction k Over v Subscript k Baseline x 2 plus w Subscript k Baseline x 3 EndFraction right-parenthesis right-bracket squared
x equals left-parenthesis x 1 comma x 2 comma x 3 right-parenthesis comma y equals left-parenthesis y 1 comma y 2 comma ellipsis comma y 15 right-parenthesis

where v Subscript k Baseline equals 16 minus k, w Subscript k Baseline equals min left-parenthesis k comma v Subscript k Baseline right-parenthesis (k element-of upper I), and

y equals left-parenthesis 0.14 comma 0.18 comma 0.22 comma 0.25 comma 0.29 comma 0.32 comma 0.35 comma 0.39 comma 0.37 comma 0.58 comma 0.73 comma 0.96 comma 1.34 comma 2.10 comma 4.39 right-parenthesis

The minimum function value f left-parenthesis x Superscript asterisk Baseline right-parenthesis equals 4.107E–3 is at the point left-parenthesis 0.08 comma 1.13 comma 2.34 right-parenthesis. The starting point x Superscript 0 Baseline equals left-parenthesis 1 comma 1 comma 1 right-parenthesis is used.

The data index k is used directly by value in the objective. Because running on CAS can reorder the data, the index is saved with each observation. The following DATA step assumes that CAS engine libref is named mycas, but you can substitute any appropriately named CAS engine libref.

data mycas.bard;
   input y @@;
   k = _n_;
   datalines;
.14  .18  .22  .25  .29  .32  .35  .39
.37  .58  .73  .96 1.34 2.10 4.39
;

You can use the following statements to read the data and solve the problem:

proc cas;
   loadactionset "optimization";
   source pgm;
      set I;
      number y{I};
      read data bard into I=[k] y;
      number v{k in I} = 16 - k;
      number w{k in I} = min(k, v[k]);
      var x{1..3} init 1;
      min f = 0.5*
         sum{k in I}
            (y[k] - (x[1] + k /
                     (v[k]*x[2] + w[k]*x[3])))**2;
      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.3.1 and Output 2.3.2.

Output 2.3.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 Above0
Free3
Fixed0
  
Number of Constraints0


Output 2.3.2: Solution Summary

Solve1.SolutionSummary: Results from optimization.runOptmodel

Solution Summary
SolverNLP
AlgorithmInterior Point Direct
Objective Functionf
Solution StatusOptimal
Objective Value0.0041074387
  
Optimality Error3.1901665E-8
Infeasibility0
  
Iterations7
Presolve Time0.00
Solution Time0.00


The data table xdata displayed in Output 2.3.3 shows the optimal solution that is found.

Output 2.3.3: Simple Nonlinear Program Solution

Results from table.fetch

Selected Rows from Table XDATA
_Index_ixd
110.0824105507
221.1330357394
332.3436955145


Simple Nonlinear Program

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

Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the bard data to the comma-separated-value (CSV) file bard.csv and then use the following code to load the CSV file into CAS:

s:loadtable{casLib="casuser", path="bard.csv"}

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 data that are stored in the table bard:

s:optimization_runOptmodel{
   code=[[
      set I;
      number y{I};
      read data bard into I=[k] y;
      number v{k in I} = 16 - k;
      number w{k in I} = min(k, v[k]);
      var x{1..3} init 1;
      min f = 0.5*
         sum{k in I}
            (y[k] - (x[1] + k /
                     (v[k]*x[2] + w[k]*x[3])))**2;
      solve;
      create data xdata from [i] xd=x;
   ]] }

Simple Nonlinear Program

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

Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the bard data to the comma-separated-value (CSV) file bard.csv and then use the following code to load the CSV file into CAS:

s.upload_file('bard.csv')

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 data that are stored in the table bard:

s.loadActionSet(actionset="optimization")
s.runOptmodel(
    code="""
        set I;
        number y{I};
        read data bard into I=[k] y;
        number v{k in I} = 16 - k;
        number w{k in I} = min(k, v[k]);
        var x{1..3} init 1;
        min f = 0.5*
           sum{k in I}
              (y[k] - (x[1] + k /
                       (v[k]*x[2] + w[k]*x[3])))**2;
        solve;
        create data xdata from [i] xd=x;
    """)

Simple Nonlinear Program

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

Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the bard data to the comma-separated-value (CSV) file bard.csv and then use the following code to load the CSV file into CAS:

m <- cas.read.csv(s, "bard.csv", casOut=list(name="bard"))

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 data that are stored in the table bard:

cas.builtins.loadActionSet(s, actionSet="optimization")
cas.optimization.runOptmodel(s,
   code="
      set I;
      number y{I};
      read data bard into I=[k] y;
      number v{k in I} = 16 - k;
      number w{k in I} = min(k, v[k]);
      var x{1..3} init 1;
      min f = 0.5*
         sum{k in I}
            (y[k] - (x[1] + k /
                     (v[k]*x[2] + w[k]*x[3])))**2;
      solve;
      create data xdata from [i] xd=x;
   ")
Last updated: April 22, 2022