Optimization Action Set

Cardinality-Constrained Regression with CASL Evaluation

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 how to use CASL evaluation with the runOptmodel action to perform a cardinality-constrained regression. The black-box solver invokes the glm action in order to select the best set of regressors.

The following DATA step creates the input data table mycas.cars in your CAS session and promotes the data table to global scope so that it can be accessed by the subsessions that are created for CASL evaluation.

data mycas.cars(promote=yes);
   set sashelp.cars;
run;

You can use the following statements to solve the regression problem:

proc cas;
   source pgm;
      /* original problem sets and parameters */
      set REGRESSORS = /Cylinders EngineSize HorsePower
                        Invoice Length MSRP Weight Wheelbase/;
      string evalCode;

      /* solve regression subproblem for selected regressors */
      source evalCode end=endsrc;
         /* convert Select array into list for glm */
         SELECTED = {};
         do name,val over Select;
            if val > 0.5 then SELECTED = SELECTED + name;
         end;
         action regression.glm result=r /
            table='cars',
            model={ target='MPG_Highway', effects=SELECTED };
         f['obj'] = r.anova[2,'SS'];
         send_response(f);
      endsrc;

      /* declare master MINLP problem with black-box objective function */
      var Select {REGRESSORS} binary;
      con CardinalityCon:
         sum {j in REGRESSORS} Select[j] <= 4;
      min MasterObjective = caslevaln(evalCode, 'obj', Select);

      /* call black-box solver */
      solve with blackbox;

      /* show the results */
      submit SELECTED=({j in REGRESSORS : Select[j] > 0.5});
         action regression.glm /
            table='cars',
            model={ target='MPG_Highway', effects=SELECTED };
      endsubmit;
   endsource;
   optimization.runOptmodel / code=pgm printlevel=0;
quit;

The statements specify the optimization model, which selects a set of up to four regressors that minimizes the sum of squares reported by the glm action. The nested SOURCE statement saves the CASL evaluation code in the OPTMODEL evalCode parameter for use by the CASLEVALN function. The objective value is determined by the obj result from the CASL evaluation. The CASL evaluation code converts the binary values in the Select array into a list of regressors for the effects parameter of the glm action. After the CASL evaluation code invokes the glm action, the code extracts the error sum of squares value from the action’s ANOVA results table and uses the SEND_RESPONSE function to return it as the result of the evaluation.

Output 2.10.1 displays the ANOVA table and parameter estimates from the glm action for the selected regressors. The solver determines that the combination of the EngineSize, HorsePower, Invoice, and Weight regressors provides the best model.

Output 2.10.1: Selected Regressors

Results from optimization.runOptmodel

Analysis of Variance
SourceDFSum of
Squares
Mean
Square
F ValuePr > F
Model49430.477152357.61929214.74<.0001
Error4234644.0345310.97881  
Corrected Total42714075   

Parameter Estimates
ParameterDFEstimateStandard
Error
t ValuePr > |t|
Intercept148.5945190.85522656.82<.0001
EngineSize1-0.0698010.313497-0.220.8239
Horsepower1-0.0305110.005432-5.62<.0001
Invoice10.0000509950.0000165233.090.0022
Weight1-0.0046040.000359-12.84<.0001


Cardinality-Constrained Regression with CASL Evaluation

This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.

Note: In order to run this code, 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 cars data to the comma-separated-value (CSV) file cars.csv and then use the following code to load the CSV file into CAS:

s:loadtable{casLib="casuser", path="cars.csv"}

For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.

This example illustrates how to use CASL evaluation with the runOptmodel action to perform a cardinality-constrained regression. The black-box solver invokes the glm action in order to select the best set of regressors.

The cars data table must be promoted to global scope so that it can be accessed from the subsessions created for CASL evaluation:

s:table_promote{table="cars"}

You can use the following statements to solve the regression problem:

s:optimization_runOptmodel{
   printlevel=0,
   code=[[
      /* original problem sets and parameters */
      set REGRESSORS = /Cylinders EngineSize HorsePower
                        Invoice Length MSRP Weight Wheelbase/;
      string evalCode;
      /* solve regression subproblem for selected regressors */
      source evalCode end=endsrc;
         /* convert Select array into list for glm */
         SELECTED = {};
         do name,val over Select;
            if val > 0.5 then SELECTED = SELECTED + name;
         end;
         action regression.glm result=r /
            table='cars',
            model={ target='MPG_Highway', effects=SELECTED };
         f['obj'] = r.anova[2,'SS'];
         send_response(f);
      endsrc;
      /* declare master MINLP problem with black-box objective function */
      var Select {REGRESSORS} binary;
      con CardinalityCon:
         sum {j in REGRESSORS} Select[j] <= 4;
      min MasterObjective = caslevaln(evalCode, 'obj', Select);
      /* call black-box solver */
      solve with blackbox;
      /* show the results */
      submit SELECTED=({j in REGRESSORS : Select[j] > 0.5});
         action regression.glm /
            table='cars',
            model={ target='MPG_Highway', effects=SELECTED };
      endsubmit;
   ]] }

The statements specify the optimization model, which selects a set of up to four regressors that minimizes the sum of squares reported by the glm action. The SOURCE statement saves the CASL evaluation code in the OPTMODEL evalCode parameter for use by the CASLEVALN function. The objective value is determined by the obj result from the CASL evaluation. The CASL evaluation code converts the binary values in the Select array into a list of regressors for the effects parameter of the glm action. After the CASL evaluation code invokes the glm action, the code extracts the error sum of squares value from the action’s ANOVA results table and uses the SEND_RESPONSE function to return it as the result of the evaluation.

Cardinality-Constrained Regression with CASL Evaluation

This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.

Note: In order to run this code, 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 cars data to the comma-separated-value (CSV) file cars.csv and then use the following code to load the CSV file into CAS:

s.upload_file('cars.csv')

For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.

This example illustrates how to use CASL evaluation with the runOptmodel action to perform a cardinality-constrained regression. The black-box solver invokes the glm action in order to select the best set of regressors.

The cars data table must be promoted to global scope so that it can be accessed from the subsessions created for CASL evaluation:

s.table.promote(
    table='cars')

You can use the following statements to solve the regression problem:

s.optimization.runoptmodel(
    code='''
         /* original problem sets and parameters */
         set REGRESSORS = /Cylinders EngineSize HorsePower
                           Invoice Length MSRP Weight Wheelbase/;
         string evalCode;
         /* solve regression subproblem for selected regressors */
         source evalCode end=endsrc;
            /* convert Select array into list for glm */
            SELECTED = {};
            do name,val over Select;
               if val > 0.5 then SELECTED = SELECTED + name;
            end;
            action regression.glm result=r /
               table='cars',
               model={ target='MPG_Highway', effects=SELECTED };
            f['obj'] = r.anova[2,'SS'];
            send_response(f);
         endsrc;
         /* declare master MINLP problem with black-box objective function */
         var Select {REGRESSORS} binary;
         con CardinalityCon:
            sum {j in REGRESSORS} Select[j] <= 4;
         min MasterObjective = caslevaln(evalCode, 'obj', Select);
         /* call black-box solver */
         solve with blackbox;
         /* show the results */
         submit SELECTED=({j in REGRESSORS : Select[j] > 0.5});
            action regression.glm /
               table='cars',
               model={ target='MPG_Highway', effects=SELECTED };
         endsubmit;
      ''',
    printlevel='0')

The statements specify the optimization model, which selects a set of up to four regressors that minimizes the sum of squares reported by the glm action. The SOURCE statement saves the CASL evaluation code in the OPTMODEL evalCode parameter for use by the CASLEVALN function. The objective value is determined by the obj result from the CASL evaluation. The CASL evaluation code converts the binary values in the Select array into a list of regressors for the effects parameter of the glm action. After the CASL evaluation code invokes the glm action, the code extracts the error sum of squares value from the action’s ANOVA results table and uses the SEND_RESPONSE function to return it as the result of the evaluation.

Cardinality-Constrained Regression with CASL Evaluation

This section contains R code for the analysis in the CASL version of this example, which contains details about the results.

Note: In order to run this code, 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 cars data to the comma-separated-value (CSV) file cars.csv and then use the following code to load the CSV file into CAS:

m <- cas.read.csv(s, "cars.csv", casOut=list(name="cars"))

For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.

This example illustrates how to use CASL evaluation with the runOptmodel action to perform a cardinality-constrained regression. The black-box solver invokes the glm action in order to select the best set of regressors.

The cars data table must be promoted to global scope so that it can be accessed from the subsessions created for CASL evaluation:

cas.table.promote(s,
    table='cars')

You can use the following statements to solve the regression problem:

cas.optimization.runOptmodel(s,
    code='
         /* original problem sets and parameters */
         set REGRESSORS = /Cylinders EngineSize HorsePower
                           Invoice Length MSRP Weight Wheelbase/;
         string evalCode;
         /* solve regression subproblem for selected regressors */
         source evalCode end=endsrc;
            /* convert Select array into list for glm */
            SELECTED = {};
            do name,val over Select;
               if val > 0.5 then SELECTED = SELECTED + name;
            end;
            action regression.glm result=r /
               table=\'cars\',
               model={ target=\'MPG_Highway\', effects=SELECTED };
            f[\'obj\'] = r.anova[2,\'SS\'];
            send_response(f);
         endsrc;
         /* declare master MINLP problem with black-box objective function */
         var Select {REGRESSORS} binary;
         con CardinalityCon:
            sum {j in REGRESSORS} Select[j] <= 4;
         min MasterObjective = caslevaln(evalCode, \'obj\', Select);
         /* call black-box solver */
         solve with blackbox;
         /* show the results */
         submit SELECTED=({j in REGRESSORS : Select[j] > 0.5});
            action regression.glm /
               table=\'cars\',
               model={ target=\'MPG_Highway\', effects=SELECTED };
         endsubmit;
      ',
    printlevel='0')

The statements specify the optimization model, which selects a set of up to four regressors that minimizes the sum of squares reported by the glm action. The SOURCE statement saves the CASL evaluation code in the OPTMODEL evalCode parameter for use by the CASLEVALN function. The objective value is determined by the obj result from the CASL evaluation. The CASL evaluation code converts the binary values in the Select array into a list of regressors for the effects parameter of the glm action. After the CASL evaluation code invokes the glm action, the code extracts the error sum of squares value from the action’s ANOVA results table and uses the SEND_RESPONSE function to return it as the result of the evaluation.

Last updated: April 22, 2022