Optimization Action Set
Black-Box Objective with Linear Constraints
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.
The problem in this example is to minimize the six-hump camelback function (Michalewicz 1996).
Minimize
subject to
Providing derivative-free algorithms with good estimates for lower and upper bounds often greatly improves performance because the algorithm is prevented from unnecessarily sampling in regions that you do not want to explore. For this problem, the variable bounds are explicitly specified: and
.
This example assumes that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
/* A dataset defining the linear constraints */
data mycas.lindata;
input _id_ $ _lb_ x1-x2 _ub_;
datalines;
a1 . 2 1 2
a2 -2 1 -1 .
a3 -2 1 2 10
;
proc cas noqueue;
/* The CASL code for evaluating the objective */
source caslEval;
fsub1 = x1*x1*(4 - 2.1*x1*x1 + x1*x1*x1*x1/3);
fsub2 = x1*x2;
fsub3 = x2*x2*(4*x2*x2 - 4);
fx = fsub1 + fsub2 + fsub3;
f['obj'] = fx;
send_response(f);
endsource;
/* Invoke the solveBlackbox action */
optimization.solveBlackbox /
decVars = {
{name='x1', lb=-2, ub=2}
{name='x2', lb=-2, ub=2}
},
obj = {{name='obj', type='min'}},
linCon="lindata",
func = {eval=caslEval},
primalOut={name="p_out", replace=true}
;
run; quit;
proc print data=mycas.p_out; run;
Output 2.6.1 shows the output from running the preceding code.
Output 2.6.1: Black-Box Objective with Linear Constraints
| Problem Summary | |
|---|---|
| Problem Type | NLP |
| Number of Variables | 2 |
| Continuous Variables | 2 |
| Integer Variables | 0 |
| Number of Constraints | 3 |
| Linear Constraints | 3 |
| Nonlinear Constraints | 0 |
| Number of Objectives | 1 |
| Objective Sense | Minimize |
| Option Summary | |
|---|---|
| Convergence Tolerance | 1E-6 |
| Cache Max Size | 500000 |
| Cache Tolerance | 1E-9 |
| Feasibility Tolerance | 0.001 |
| Max Func Evaluations | 120000 |
| Max Iterations | 10 |
| Max Time | 1.797693E308 |
| Num Global Solvers | 1 |
| Num Local Solvers | 4 |
| Population Size | 20 |
| Seed | 1 |
| Log Frequency | 1 |
| Log Level | 1 |
| Solution Summary | |
|---|---|
| Solution Status | Generations complete |
| Objective | -1.02955084 |
| Infeasibility | 0 |
| Iterations | 10 |
| Evaluations | 172 |
| Cached Evaluations | 7 |
| Obs | _tag_ | x1 | x2 | obj | _inf_ | _iter_ | _evalTime_ |
|---|---|---|---|---|---|---|---|
| 1 | 154 | 0.11302 | -0.71296 | -1.02955 | 0 | 10 | 0.099126 |
Black-Box Objective with Linear Constraints
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.
The problem in this example is to minimize the six-hump camelback function (Michalewicz 1996).
Minimize
subject to
Providing derivative-free algorithms with good estimates for lower and upper bounds often greatly improves performance because the algorithm is prevented from unnecessarily sampling in regions that you do not want to explore. For this problem, the variable bounds are explicitly specified: and
.
In order to run the code in this example, 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 lindata data to a comma-separated-value (CSV) file lindata.csv. The contents of lindata.csv should look like this:
_id_, _lb_, x1, x2, _ub_
a1, ., 2, 1, 2
a2, -2, 1, -1, .
a3, -2, 1, 2, 10
The following code loads the linear constraint data from the CSV file lindata.csv into a CAS table, defines the objective function code in CASL syntax (CASL is the language expected by the solveBlackbox action), and then invokes the solveBlackbox action to optimize the problem:
s:loadTable{caslib="CASUSER", path="lindata.csv", promote=yes}
-- The CASL code for evaluating the objective
caslEval = [[
fsub1 = x1*x1*(4 - 2.1*x1*x1 + x1*x1*x1*x1/3);
fsub2 = x1*x2;
fsub3 = x2*x2*(4*x2*x2 - 4);
fx = fsub1 + fsub2 + fsub3;
f['obj'] = fx;
send_response(f);
]]
-- Invoke the solveBlackbox action
s:optimization_solveBlackbox {
decVars = {
{name='x1', lb=-2, ub=2},
{name='x2', lb=-2, ub=2}
},
obj = {
{name='obj', type='min'}
},
linCon = "lindata",
func = {eval=caslEval},
primalOut={name='p_out', replace=true}
}
Black-Box Objective with Linear Constraints
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.
The problem in this example is to minimize the six-hump camelback function (Michalewicz 1996).
Minimize
subject to
Providing derivative-free algorithms with good estimates for lower and upper bounds often greatly improves performance because the algorithm is prevented from unnecessarily sampling in regions that you do not want to explore. For this problem, the variable bounds are explicitly specified: and
.
The following code loads the optimization action set, creates a data frame for the linear constraint data and uploads it to a CAS table, defines the objective function code in CASL syntax (CASL is the language expected by the solveBlackbox action), and then invokes the solveBlackbox action to optimize the problem:
s.loadactionset('optimization')
# Create a data frame for the linear constraints and
# upload the data frame as a CAS table named 'lindata'.
coeffMatrix = np.array(
[
[np.NaN, 2, 1, 2 ],
[ -2, 1, -1, np.NaN],
[ -2, 1, 2, 10 ]
],
dtype='float32'
);
colNames=["_lb_"];
for i in range(1,3):
colNames = colNames + ["x%d" % i];
colNames=colNames+["_ub_"];
df = pd.DataFrame(coeffMatrix, columns=colNames);
df['_id_'] =['a1', 'a2', 'a3'];
s.upload_frame(df, casout=dict(name='lindata', replace=True));
# The CASL code for evaluating the objective
caslEval='''
fsub1 = x1*x1*(4 - 2.1*x1*x1 + x1*x1*x1*x1/3);
fsub2 = x1*x2;
fsub3 = x2*x2*(4*x2*x2 - 4);
fx = fsub1 + fsub2 + fsub3;
f['obj'] = fx;
send_response(f);
''';
# Invoke the solveBlackbox action
s.optimization.solveBlackBox(
decVars = [
dict(name='x1', lb=-2, ub=2),
dict(name='x2', lb=-2, ub=2)
],
obj = [dict(name='obj', type='min')],
linCon = s.CASTable("lindata"),
func = dict(eval=caslEval),
primalOut = dict(name="p_out", replace=True)
);
Black-Box Objective with Linear Constraints
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.
The problem in this example is to minimize the six-hump camelback function (Michalewicz 1996).
Minimize
subject to
Providing derivative-free algorithms with good estimates for lower and upper bounds often greatly improves performance because the algorithm is prevented from unnecessarily sampling in regions that you do not want to explore. For this problem, the variable bounds are explicitly specified: and
.
In order to run the code in this example, 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 lindata data to a comma-separated-value (CSV) file lindata.csv. The contents of lindata.csv should look like this:
_id_, _lb_, x1, x2, _ub_
a1, ., 2, 1, 2
a2, -2, 1, -1, .
a3, -2, 1, 2, 10
The following code loads the linear constraint data from the CSV file lindata.csv into a CAS table, defines the objective function code in CASL syntax (CASL is the language expected by the solveBlackbox action), and then invokes the solveBlackbox action to optimize the problem:
# Load linear constraint table from CSV file
cas.table.loadTable(s, caslib='CASUSER', path='lindata.csv', promote='yes')
# The CASL code for evaluating the objective
caslEval = "
fsub1 = x1*x1*(4 - 2.1*x1*x1 + x1*x1*x1*x1/3);
fsub2 = x1*x2;
fsub3 = x2*x2*(4*x2*x2 - 4);
fx = fsub1 + fsub2 + fsub3;
f['obj'] = fx;
send_response(f);
"
# Invoke the solveBlackbox action
cas.optimization.solveBlackbox(s,
decVars = list(
list(name='x1', lb=-2, ub=2),
list(name='x2', lb=-2, ub=2)
),
obj = list(
list(name='obj', type='min')
),
linCon = list(name='lindata'),
func = list(eval=caslEval),
primalOut=list(name='p_out', replace='true')
)