Optimization Action Set
Using Black-Box 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.
This example illustrates a problem that has both linear and nonlinear constraints and a linear objective. The following optimization problem is discussed in Haverly (1978) and Liebman et al. (1986).
Maximize
subject to
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-x10 _ub_;
datalines;
c1 0 0 0 1 1 0 -1 -1 0 0 0 0
c2 0 -1 0 0 0 0 1 0 1 0 0 0
c3 0 0 -1 0 0 0 0 1 0 1 0 0
c4 0 0 0 0 0 -1 0 0 1 1 0 0
;
proc cas noqueue;
/* The CASL code for evaluating the objective and black-box constraints */
source caslEval;
fx = 9*x1 + 15*x2 - 6*x3 - 16*x4 - 10*x5;
cx5 = 2.5*x1 - x10*x6 - 2*x8;
cx6 = 1.5*x2 - x10*x7 - 2*x9;
cx7 = 3*x3 + x4 - x10*x3 - x10*x4;
f['obj'] = fx;
c['c5'] = cx5;
c['c6'] = cx6;
c['c7'] = cx7;
send_response(f);
send_response(c);
endsource;
/* Invoke the solveBlackbox action */
optimization.solveBlackbox /
decVars = {
{name='x1', lb=0, ub=100}
{name='x2', lb=0, ub=200}
{name='x3', lb=0}
{name='x4', lb=0}
{name='x5', lb=0}
{name='x6', lb=0}
{name='x7', lb=0}
{name='x8', lb=0}
{name='x9', lb=0}
{name='x10', lb=1, ub=3}
},
obj = {{name='obj', type='max'}},
con = {
{name='c5', lb=0}
{name='c6', lb=0}
{name='c7', lb=0, ub=0}
},
linCon="lindata",
func = {eval=caslEval},
popSize=200,
maxGen=20
;
run; quit;
Output 2.7.1 shows the output from running the preceding code.
Output 2.7.1: Using Black-Box Constraints
| Problem Summary | |
|---|---|
| Problem Type | NLP |
| Number of Variables | 10 |
| Continuous Variables | 10 |
| Integer Variables | 0 |
| Number of Constraints | 7 |
| Linear Constraints | 4 |
| Nonlinear Constraints | 3 |
| Number of Objectives | 1 |
| Objective Sense | Maximize |
| Option Summary | |
|---|---|
| Convergence Tolerance | 1E-6 |
| Cache Max Size | 100000 |
| Cache Tolerance | 1E-9 |
| Feasibility Tolerance | 0.001 |
| Max Func Evaluations | 2080000 |
| Max Iterations | 20 |
| Max Time | 1.797693E308 |
| Num Global Solvers | 1 |
| Num Local Solvers | 20 |
| Population Size | 200 |
| Seed | 1 |
| Log Frequency | 1 |
| Log Level | 1 |
| Solution Summary | |
|---|---|
| Solution Status | Generations complete |
| Objective | 399.99902802 |
| Infeasibility | 0 |
| Iterations | 20 |
| Evaluations | 2872 |
| Cached Evaluations | 314 |
Using Black-Box 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.
This example illustrates a problem that has both linear and nonlinear constraints and a linear objective. The following optimization problem is discussed in Haverly (1978) and Liebman et al. (1986).
Maximize
subject to
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, x3, x4, x5, x6, x7, x8, x9, x10, _ub_
c1, 0, 0, 0, 1, 1, 0, -1, -1, 0, 0, 0, 0
c2, 0, -1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0
c3, 0, 0, -1, 0, 0, 0, 0, 1, 0, 1, 0, 0
c4, 0, 0, 0, 0, 0, -1, 0, 0, 1, 1, 0, 0
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 and black-box constraints
caslEval = [[
fx = 9*x1 + 15*x2 - 6*x3 - 16*x4 - 10*x5;
cx5 = 2.5*x1 - x10*x6 - 2*x8;
cx6 = 1.5*x2 - x10*x7 - 2*x9;
cx7 = 3*x3 + x4 - x10*x3 - x10*x4;
f['obj'] = fx;
c['c5'] = cx5;
c['c6'] = cx6;
c['c7'] = cx7;
send_response(f);
send_response(c);
]]
-- Invoke the solveBlackbox action
s:optimization_solveBlackbox {
decVars = {
{name='x1', lb=0, ub=100},
{name='x2', lb=0, ub=200},
{name='x3', lb=0},
{name='x4', lb=0},
{name='x5', lb=0},
{name='x6', lb=0},
{name='x7', lb=0},
{name='x8', lb=0},
{name='x9', lb=0},
{name='x10', lb=1, ub=3}
},
obj = {
{name='obj', type='max'}
},
con = {
{name='c5', lb=0},
{name='c6', lb=0},
{name='c7', lb=0, ub=0}
},
linCon = "lindata",
func = {eval=caslEval},
popSize = 200,
maxGen = 20
}
Using Black-Box 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.
This example illustrates a problem that has both linear and nonlinear constraints and a linear objective. The following optimization problem is discussed in Haverly (1978) and Liebman et al. (1986).
Maximize
subject to
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(
[
[ 0, 0, 0, 1, 1, 0, -1, -1, 0, 0, 0, 0],
[ 0, -1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
[ 0, 0, -1, 0, 0, 0, 0, 1, 0, 1, 0, 0],
[ 0, 0, 0, 0, 0, -1, 0, 0, 1, 1, 0, 0]
],
dtype='float32'
);
colnames=["_lb_"];
for i in range(1,11):
colnames = colnames + ["x%d" % i];
colnames=colnames+["_ub_"];
lindataFrame = pd.DataFrame(coeffMatrix, columns=colnames);
lindataFrame['_id_'] =['c1', 'c2', 'c3', 'c4'];
s.upload_frame(lindataFrame, casout=dict(name='lindata', replace=True));
# The CASL code for evaluating the objective and black-box constraints
caslEval='''
fx = 9*x1 + 15*x2 - 6*x3 - 16*x4 - 10*x5;
cx5 = 2.5*x1 - x10*x6 - 2*x8;
cx6 = 1.5*x2 - x10*x7 - 2*x9;
cx7 = 3*x3 + x4 - x10*x3 - x10*x4;
f['obj'] = fx;
c['c5'] = cx5;
c['c6'] = cx6;
c['c7'] = cx7;
send_response(f);
send_response(c);
''';
# Invoke the solveBlackbox action
s.optimization.solveBlackbox(
decVars = [
dict(name='x1', lb=0, ub=100),
dict(name='x2', lb=0, ub=200),
dict(name='x3', lb=0),
dict(name='x4', lb=0),
dict(name='x5', lb=0),
dict(name='x6', lb=0),
dict(name='x7', lb=0),
dict(name='x8', lb=0),
dict(name='x9', lb=0),
dict(name='x10', lb=1, ub=3)
],
obj = [dict(name='obj', type='max')],
con = [
dict(name='c5', lb=0),
dict(name='c6', lb=0),
dict(name='c7', lb=0, ub=0)
],
linCon = s.CASTable("lindata"),
func = dict(eval=caslEval),
popSize = 200,
maxGen = 20
);
Using Black-Box 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.
This example illustrates a problem that has both linear and nonlinear constraints and a linear objective. The following optimization problem is discussed in Haverly (1978) and Liebman et al. (1986).
Maximize
subject to
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, x3, x4, x5, x6, x7, x8, x9, x10, _ub_
c1, 0, 0, 0, 1, 1, 0, -1, -1, 0, 0, 0, 0
c2, 0, -1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0
c3, 0, 0, -1, 0, 0, 0, 0, 1, 0, 1, 0, 0
c4, 0, 0, 0, 0, 0, -1, 0, 0, 1, 1, 0, 0
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 and black-box constraints
caslEval = "
fx = 9*x1 + 15*x2 - 6*x3 - 16*x4 - 10*x5;
cx5 = 2.5*x1 - x10*x6 - 2*x8;
cx6 = 1.5*x2 - x10*x7 - 2*x9;
cx7 = 3*x3 + x4 - x10*x3 - x10*x4;
f['obj'] = fx;
c['c5'] = cx5;
c['c6'] = cx6;
c['c7'] = cx7;
send_response(f);
send_response(c);
"
# Invoke the solveBlackbox action
cas.optimization.solveBlackbox(s,
decVars = list(
list(name='x1', lb=0, ub=100),
list(name='x2', lb=0, ub=200),
list(name='x3', lb=0),
list(name='x4', lb=0),
list(name='x5', lb=0),
list(name='x6', lb=0),
list(name='x7', lb=0),
list(name='x8', lb=0),
list(name='x9', lb=0),
list(name='x10', lb=1, ub=3)
),
obj = list(
list(name='obj', type='max')
),
con = list(
list(name='c5', lb=0),
list(name='c6', lb=0),
list(name='c7', lb=0, ub=0)
),
linCon = list(name='lindata'),
func = list(eval=caslEval),
popSize = 200,
maxGen = 20
)