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

f left-parenthesis x right-parenthesis equals 9 x 1 plus 15 x 2 minus 6 x 3 minus 16 x 4 minus 10 x 5

subject to

StartLayout 1st Row  x 3 plus x 4 equals x 6 plus x 7 2nd Row  x 6 plus x 8 equals x 1 3rd Row  x 7 plus x 9 equals x 2 4th Row  x 8 plus x 9 equals x 5 5th Row  2.5 x 1 minus x 10 x 6 minus 2 x 8 greater-than-or-equal-to 0 6th Row  1.5 x 2 minus x 10 x 7 minus 2 x 9 greater-than-or-equal-to 0 7th Row  3 x 3 plus x 4 minus x 10 left-parenthesis x 3 plus x 4 right-parenthesis equals 0 EndLayout

and

StartLayout 1st Row  0 less-than-or-equal-to x 1 less-than-or-equal-to 100 2nd Row  0 less-than-or-equal-to x 2 less-than-or-equal-to 200 3rd Row  1 less-than-or-equal-to x 10 less-than-or-equal-to 3 4th Row  0 less-than-or-equal-to x Subscript i Baseline comma for i equals 3 comma ellipsis comma 9 EndLayout

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

Results from optimization.solveBlackbox

Problem Summary
Problem TypeNLP
Number of Variables10
Continuous Variables10
Integer Variables0
Number of Constraints7
Linear Constraints4
Nonlinear Constraints3
Number of Objectives1
Objective SenseMaximize

Option Summary
Convergence Tolerance1E-6
Cache Max Size100000
Cache Tolerance1E-9
Feasibility Tolerance0.001
Max Func Evaluations2080000
Max Iterations20
Max Time1.797693E308
Num Global Solvers1
Num Local Solvers20
Population Size200
Seed1
Log Frequency1
Log Level1

Solution Summary
Solution StatusGenerations complete
Objective399.99902802
Infeasibility0
Iterations20
Evaluations2872
Cached Evaluations314


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

f left-parenthesis x right-parenthesis equals 9 x 1 plus 15 x 2 minus 6 x 3 minus 16 x 4 minus 10 x 5

subject to

StartLayout 1st Row  x 3 plus x 4 equals x 6 plus x 7 2nd Row  x 6 plus x 8 equals x 1 3rd Row  x 7 plus x 9 equals x 2 4th Row  x 8 plus x 9 equals x 5 5th Row  2.5 x 1 minus x 10 x 6 minus 2 x 8 greater-than-or-equal-to 0 6th Row  1.5 x 2 minus x 10 x 7 minus 2 x 9 greater-than-or-equal-to 0 7th Row  3 x 3 plus x 4 minus x 10 left-parenthesis x 3 plus x 4 right-parenthesis equals 0 EndLayout

and

StartLayout 1st Row  0 less-than-or-equal-to x 1 less-than-or-equal-to 100 2nd Row  0 less-than-or-equal-to x 2 less-than-or-equal-to 200 3rd Row  1 less-than-or-equal-to x 10 less-than-or-equal-to 3 4th Row  0 less-than-or-equal-to x Subscript i Baseline comma for i equals 3 comma ellipsis comma 9 EndLayout

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

f left-parenthesis x right-parenthesis equals 9 x 1 plus 15 x 2 minus 6 x 3 minus 16 x 4 minus 10 x 5

subject to

StartLayout 1st Row  x 3 plus x 4 equals x 6 plus x 7 2nd Row  x 6 plus x 8 equals x 1 3rd Row  x 7 plus x 9 equals x 2 4th Row  x 8 plus x 9 equals x 5 5th Row  2.5 x 1 minus x 10 x 6 minus 2 x 8 greater-than-or-equal-to 0 6th Row  1.5 x 2 minus x 10 x 7 minus 2 x 9 greater-than-or-equal-to 0 7th Row  3 x 3 plus x 4 minus x 10 left-parenthesis x 3 plus x 4 right-parenthesis equals 0 EndLayout

and

StartLayout 1st Row  0 less-than-or-equal-to x 1 less-than-or-equal-to 100 2nd Row  0 less-than-or-equal-to x 2 less-than-or-equal-to 200 3rd Row  1 less-than-or-equal-to x 10 less-than-or-equal-to 3 4th Row  0 less-than-or-equal-to x Subscript i Baseline comma for i equals 3 comma ellipsis comma 9 EndLayout

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

f left-parenthesis x right-parenthesis equals 9 x 1 plus 15 x 2 minus 6 x 3 minus 16 x 4 minus 10 x 5

subject to

StartLayout 1st Row  x 3 plus x 4 equals x 6 plus x 7 2nd Row  x 6 plus x 8 equals x 1 3rd Row  x 7 plus x 9 equals x 2 4th Row  x 8 plus x 9 equals x 5 5th Row  2.5 x 1 minus x 10 x 6 minus 2 x 8 greater-than-or-equal-to 0 6th Row  1.5 x 2 minus x 10 x 7 minus 2 x 9 greater-than-or-equal-to 0 7th Row  3 x 3 plus x 4 minus x 10 left-parenthesis x 3 plus x 4 right-parenthesis equals 0 EndLayout

and

StartLayout 1st Row  0 less-than-or-equal-to x 1 less-than-or-equal-to 100 2nd Row  0 less-than-or-equal-to x 2 less-than-or-equal-to 200 3rd Row  1 less-than-or-equal-to x 10 less-than-or-equal-to 3 4th Row  0 less-than-or-equal-to x Subscript i Baseline comma for i equals 3 comma ellipsis comma 9 EndLayout

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