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
| 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 | 3 |
| Free | 0 |
| Fixed | 0 |
| Number of Constraints | 0 |
Output 2.4.2: Solution Summary
| Solution Summary | |
|---|---|
| Solver | NLP |
| Algorithm | Interior Point Direct |
| Objective Function | f |
| Solution Status | Optimal |
| Objective Value | 0.0041074387 |
| Optimality Error | 9.0909092E-8 |
| Infeasibility | 0 |
| Iterations | 8 |
| Presolve Time | 0.00 |
| Solution Time | 0.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
| Selected Rows from Table XDATA | ||
|---|---|---|
| _Index_ | i | xd |
| 1 | 1 | 0.0824105598 |
| 2 | 2 | 1.1330360921 |
| 3 | 3 | 2.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;
")