Optimization Action Set

Simple Linear Program

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 you can use the solveLp action to solve linear programs. Suppose you want to solve the following problem:

StartLayout 1st Row 1st Column min 2nd Column 2 x 1 3rd Column minus 4th Column 3 x 2 5th Column minus 6th Column 4 x 3 7th Column Blank 8th Column Blank 9th Column Blank 2nd Row 1st Column subject to 2nd Column Blank 3rd Column minus 4th Column 2 x 2 5th Column minus 6th Column 3 x 3 7th Column greater-than-or-equal-to 8th Column negative 5 9th Column left-parenthesis upper R 1 right-parenthesis 3rd Row 1st Column Blank 2nd Column x 1 3rd Column plus 4th Column x 2 5th Column plus 6th Column 2 x 3 7th Column less-than-or-equal-to 8th Column 4 9th Column left-parenthesis upper R 2 right-parenthesis 4th Row 1st Column Blank 2nd Column x 1 3rd Column plus 4th Column 2 x 2 5th Column plus 6th Column 3 x 3 7th Column less-than-or-equal-to 8th Column 7 9th Column left-parenthesis upper R 3 right-parenthesis 5th Row 1st Column Blank 2nd Column Blank 3rd Column Blank 4th Column x 1 comma 5th Column x 2 comma 6th Column x 3 7th Column greater-than-or-equal-to 8th Column 0 9th Column Blank EndLayout

The following DATA step creates the corresponding MPS-format data table. The DATA step assumes that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.

data mycas.example;
   input _id_ field1 $ field2 $ field3 $ field4 field5 $ field6;
   datalines;
1  NAME        .      EXAMPLE   .    .     .
2  ROWS        .      .         .    .     .
3  N           COST   .         .    .     .
4  G           R1     .         .    .     .
5  L           R2     .         .    .     .
6  L           R3     .         .    .     .
7  COLUMNS     .      .         .    .     .
8  .           X1     COST      2    R2    1
9  .           X1     R3        1    .     .
10 .           X2     COST     -3    R1   -2
11 .           X2     R2        1    R3    2
12 .           X3     COST     -4    R1   -3
13 .           X3     R2        2    R3    3
14 RHS         .      .         .    .     .
15 .           RHS    R1       -5    R2    4
16 .           RHS    R3        7    .     .
17 ENDATA      .      .         .    .     .
;

Alternatively, you can use the upload action on a CSV file that has the following content:

_id_ ,field1  ,field2 ,field3  ,field4 ,field5 ,field6
1    ,NAME    ,       ,EXAMPLE ,       ,       ,
2    ,ROWS    ,       ,        ,       ,       ,
3    ,N       ,COST   ,        ,       ,       ,
4    ,G       ,R1     ,        ,       ,       ,
5    ,L       ,R2     ,        ,       ,       ,
6    ,L       ,R3     ,        ,       ,       ,
7    ,COLUMNS ,       ,        ,       ,       ,
8    ,        ,X1     ,COST    ,2      ,R2     ,1
9    ,        ,X1     ,R3      ,1      ,       ,
10   ,        ,X2     ,COST    ,-3     ,R1     ,-2
11   ,        ,X2     ,R2      ,1      ,R3     ,2
12   ,        ,X3     ,COST    ,-4     ,R1     ,-3
13   ,        ,X3     ,R2      ,2      ,R3     ,3
14   ,RHS     ,       ,        ,       ,       ,
15   ,        ,RHS    ,R1      ,-5     ,R2     ,4
16   ,        ,RHS    ,R3      ,7      ,       ,
17   ,ENDATA  ,       ,        ,       ,       ,

You can use the following call to PROC CAS to solve the linear programming (LP) problem:

proc cas;
   loadactionset "optimization";
   action optimization.solveLp result=r status=s /
      data      = {name="example"}
      primalOut = {name="expout" replace=true}
      dualOut   = {name="exdout" replace=true}
      objSense  = "min"
      algorithm = "primal"
      logFreq   = 1;
   run;
   print r.SolutionSummary; run;
   action table.fetch / table = "expout"; run;
quit;

The primal solution is displayed in Output 2.12.1.

Output 2.12.1: Primal Solution Output

Results from table.fetch

Selected Rows from Table EXPOUT
_Index_Objective Function IDRHS IDVariable NameVariable
Type
Objective CoefficientLower BoundUpper BoundVariable ValueVariable
Status
Reduced Cost
1COSTRHSX1N201.797693E3080L2
2COSTRHSX2N-301.797693E3082.5B0
3COSTRHSX3N-401.797693E3080L0.5


The progress of the solution is printed to the log, as shown in Output 2.12.2.

Output 2.12.2: Log: Solution Progress

NOTE: Active Session now MYSESS.                                                
NOTE: Added action set 'optimization'.                                          
NOTE: The problem EXAMPLE has 3 variables (0 free, 0 fixed).                    
NOTE: The problem has 3 constraints (2 LE, 0 EQ, 1 GE, 0 range).                
NOTE: The problem has 8 constraint coefficients.                                
NOTE: The LP presolver value AUTOMATIC is applied.                              
NOTE: The LP presolver time is 0.00 seconds.                                    
NOTE: The LP presolver removed 1 variables and 1 constraints.                   
NOTE: The LP presolver removed 4 constraint coefficients.                       
NOTE: The presolved problem has 2 variables, 2 constraints, and 4 constraint    
      coefficients.                                                             
NOTE: The LP solver is called.                                                  
NOTE: The Primal Simplex algorithm is used.                                     
                           Objective                Entering      Leaving       
      Phase Iteration        Value         Time     Variable      Variable      
       P 2          1    0.000000E+00         0         X3             R1 (S)   
       P 2          2   -6.666924E+00         0         X2             X3       
       P 2          3   -7.500289E+00         0                                 
       D 2          4   -7.500000E+00         0                                 
NOTE: Optimal.                                                                  
NOTE: Objective = -7.5.                                                         
NOTE: The Primal Simplex solve time is 0.00 seconds.                            


Note that the PRINT statement immediately after the call to the solveLp action prints the value of the SolutionSummary results table to the log, as shown in Output 2.12.3.

Output 2.12.3: Value of the SolutionSummary Results Table

SolutionSummary: Results from optimization.solveLp

Solution Summary
SolverLP
AlgorithmPrimal Simplex
Objective FunctionCOST
Solution StatusOptimal
Objective Value-7.5
  
Primal Infeasibility0
Dual Infeasibility0
Bound Infeasibility0
  
Iterations4
Presolve Time0.00
Solution Time0.00


Simple Linear Program

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 example data to the comma-separated-value (CSV) file example.csv and then use the following code to load the CSV file into CAS:

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

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

The following code solves the simple linear program stored in the data table example:

s:optimization_solveLp{
   data      = {name = "example"},
   primalOut = {name = "expout", replace=true},
   dualOut   = {name = "exdout", replace=true},
   objSense  = "min",
   algorithm = "primal",
   logFreq   = 1}

Simple Linear Program

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 example data to the comma-separated-value (CSV) file example.csv and then use the following code to load the CSV file into CAS:

s.upload_file('example.csv')

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

The following code solves the simple linear program stored in the data table example:

s.optimization.solveLp(
    data      = {"name": "example"},
    primalOut = {"name": "expout", "replace":True},
    dualOut   = {"name": "exdout", "replace":True},
    objSense  = "min",
    algorithm = "primal",
    logFreq   = 1)

Simple Linear Program

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 example data to the comma-separated-value (CSV) file example.csv and then use the following code to load the CSV file into CAS:

m <- s$upload("example.csv", casOut=list(name="example"))

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

The following code solves the simple linear program stored in the data table example:

cas.optimization.solveLp(s,
   data      = "example",
   primalOut = list(name="expout", replace=TRUE),
   dualOut   = list(name="exdout", replace=TRUE),
   objSense  = "min",
   algorithm = "primal",
   logFreq   = 1)
Last updated: April 22, 2022