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:
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
| Selected Rows from Table EXPOUT | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| _Index_ | Objective Function ID | RHS ID | Variable Name | Variable Type | Objective Coefficient | Lower Bound | Upper Bound | Variable Value | Variable Status | Reduced Cost |
| 1 | COST | RHS | X1 | N | 2 | 0 | 1.797693E308 | 0 | L | 2 |
| 2 | COST | RHS | X2 | N | -3 | 0 | 1.797693E308 | 2.5 | B | 0 |
| 3 | COST | RHS | X3 | N | -4 | 0 | 1.797693E308 | 0 | L | 0.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
| Solution Summary | |
|---|---|
| Solver | LP |
| Algorithm | Primal Simplex |
| Objective Function | COST |
| Solution Status | Optimal |
| Objective Value | -7.5 |
| Primal Infeasibility | 0 |
| Dual Infeasibility | 0 |
| Bound Infeasibility | 0 |
| Iterations | 4 |
| Presolve Time | 0.00 |
| Solution Time | 0.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)