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

f left-parenthesis x right-parenthesis equals left-parenthesis 4 minus 2.1 x 1 squared plus StartFraction x 1 Superscript 4 Baseline Over 3 EndFraction right-parenthesis x 1 squared plus x 1 x 2 plus left-parenthesis negative 4 plus 4 x 2 squared right-parenthesis x 2 squared

subject to

StartLayout 1st Row 1st Column 2 x 1 plus x 2 2nd Column less-than-or-equal-to 3rd Column 2 2nd Row 1st Column x 1 minus x 2 2nd Column greater-than-or-equal-to 3rd Column negative 2 3rd Row 1st Column x 1 plus 2 x 2 2nd Column greater-than-or-equal-to 3rd Column negative 2 EndLayout

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: negative 2 less-than-or-equal-to x 1 less-than-or-equal-to 2 and negative 2 less-than-or-equal-to x 2 less-than-or-equal-to 2.

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

Results from optimization.solveBlackbox

Problem Summary
Problem TypeNLP
Number of Variables2
Continuous Variables2
Integer Variables0
Number of Constraints3
Linear Constraints3
Nonlinear Constraints0
Number of Objectives1
Objective SenseMinimize

Option Summary
Convergence Tolerance1E-6
Cache Max Size500000
Cache Tolerance1E-9
Feasibility Tolerance0.001
Max Func Evaluations120000
Max Iterations10
Max Time1.797693E308
Num Global Solvers1
Num Local Solvers4
Population Size20
Seed1
Log Frequency1
Log Level1

Solution Summary
Solution StatusGenerations complete
Objective-1.02955084
Infeasibility0
Iterations10
Evaluations172
Cached Evaluations7

Obs_tag_x1x2obj_inf__iter__evalTime_
11540.11302-0.71296-1.029550100.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

f left-parenthesis x right-parenthesis equals left-parenthesis 4 minus 2.1 x 1 squared plus StartFraction x 1 Superscript 4 Baseline Over 3 EndFraction right-parenthesis x 1 squared plus x 1 x 2 plus left-parenthesis negative 4 plus 4 x 2 squared right-parenthesis x 2 squared

subject to

StartLayout 1st Row 1st Column 2 x 1 plus x 2 2nd Column less-than-or-equal-to 3rd Column 2 2nd Row 1st Column x 1 minus x 2 2nd Column greater-than-or-equal-to 3rd Column negative 2 3rd Row 1st Column x 1 plus 2 x 2 2nd Column greater-than-or-equal-to 3rd Column negative 2 EndLayout

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: negative 2 less-than-or-equal-to x 1 less-than-or-equal-to 2 and negative 2 less-than-or-equal-to x 2 less-than-or-equal-to 2.

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

f left-parenthesis x right-parenthesis equals left-parenthesis 4 minus 2.1 x 1 squared plus StartFraction x 1 Superscript 4 Baseline Over 3 EndFraction right-parenthesis x 1 squared plus x 1 x 2 plus left-parenthesis negative 4 plus 4 x 2 squared right-parenthesis x 2 squared

subject to

StartLayout 1st Row 1st Column 2 x 1 plus x 2 2nd Column less-than-or-equal-to 3rd Column 2 2nd Row 1st Column x 1 minus x 2 2nd Column greater-than-or-equal-to 3rd Column negative 2 3rd Row 1st Column x 1 plus 2 x 2 2nd Column greater-than-or-equal-to 3rd Column negative 2 EndLayout

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: negative 2 less-than-or-equal-to x 1 less-than-or-equal-to 2 and negative 2 less-than-or-equal-to x 2 less-than-or-equal-to 2.

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

f left-parenthesis x right-parenthesis equals left-parenthesis 4 minus 2.1 x 1 squared plus StartFraction x 1 Superscript 4 Baseline Over 3 EndFraction right-parenthesis x 1 squared plus x 1 x 2 plus left-parenthesis negative 4 plus 4 x 2 squared right-parenthesis x 2 squared

subject to

StartLayout 1st Row 1st Column 2 x 1 plus x 2 2nd Column less-than-or-equal-to 3rd Column 2 2nd Row 1st Column x 1 minus x 2 2nd Column greater-than-or-equal-to 3rd Column negative 2 3rd Row 1st Column x 1 plus 2 x 2 2nd Column greater-than-or-equal-to 3rd Column negative 2 EndLayout

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: negative 2 less-than-or-equal-to x 1 less-than-or-equal-to 2 and negative 2 less-than-or-equal-to x 2 less-than-or-equal-to 2.

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')
)
Last updated: April 22, 2022