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 ,
The minimum function value 4.107E–3 is at the point
. The starting point
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
| Problem Summary | |
|---|---|
| Objective Sense | Minimization |
| Objective Function | f |
| Objective Type | Nonlinear |
| Number of Variables | 3 |
| Bounded Above | 0 |
| Bounded Below | 0 |
| Bounded Below and Above | 0 |
| Free | 3 |
| Fixed | 0 |
| Number of Constraints | 0 |
Output 2.3.2: Solution Summary
| Solution Summary | |
|---|---|
| Solver | NLP |
| Algorithm | Interior Point Direct |
| Objective Function | f |
| Solution Status | Optimal |
| Objective Value | 0.0041074387 |
| Optimality Error | 3.1901665E-8 |
| Infeasibility | 0 |
| Iterations | 7 |
| Presolve Time | 0.00 |
| Solution Time | 0.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
| Selected Rows from Table XDATA | ||
|---|---|---|
| _Index_ | i | xd |
| 1 | 1 | 0.0824105507 |
| 2 | 2 | 1.1330357394 |
| 3 | 3 | 2.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;
")