Optimization Action Set
Simple Quadratic 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.
The following example illustrates how you can use the solveQp action to solve quadratic programs. Suppose you want to solve the following problem:
The preceding problem can be expressed in the following manner in an input data table that uses a quadratic programming system (QPS) format. 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 OBJ . . . .
4 L R1 . . . .
5 G R2 . . . .
6 COLUMNS . . . . .
7 . X1 R1 1.0 R2 1.0
8 . X1 OBJ 2.0 . .
9 . X2 R1 -1.0 R2 2.0
10 . X2 OBJ 3.0 . .
11 RHS . . . . .
12 . RHS R1 1.0 . .
13 . RHS R2 100 . .
14 RANGES . . . . .
15 BOUNDS . . . . .
16 QUADOBJ . . . . .
17 . X1 X1 2.0 . .
18 . X1 X2 2.5 . .
19 . X2 X2 20 . .
20 ENDATA . . . . .
;
You can use the following call to PROC CAS to solve the QP problem:
proc cas;
loadactionset "optimization";
action optimization.solveQp result=r status=s /
data={name="example"}
primalOut={name="expout" replace=true}
dualOut={name="exdout" replace=true}
numThreads=4
objSense="min"
logFreq=1;
run;
print r.SolutionSummary; run;
action table.fetch / table = "expout"; run;
quit;
The primal solution is displayed in Output 2.14.1.
Output 2.14.1: Example 1: Primal Solution Output
| Selected Rows from Table EXPOUT | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| _Index_ | Objective Function ID | RHS ID | Variable Name | Variable Type | Linear Objective Coefficient | Lower Bound | Upper Bound | Variable Value | Variable Status |
| 1 | OBJ | RHS | X1 | N | 2 | 0 | 1.797693E308 | 33.999999914 | O |
| 2 | OBJ | RHS | X2 | N | 3 | 0 | 1.797693E308 | 33.000000079 | O |
The progress of the solution is printed to the log, as shown in Output 2.14.2.
Output 2.14.2: Log: Solution Progress
| NOTE: Active Session now MYSESS. |
| NOTE: Added action set 'optimization'. |
| NOTE: The problem EXAMPLE has 2 variables (0 free, 0 fixed). |
| NOTE: The problem has 2 constraints (1 LE, 0 EQ, 1 GE, 0 range). |
| NOTE: The problem has 4 constraint coefficients. |
| NOTE: The objective function has 2 Hessian diagonal elements and 1 Hessian |
| elements above the diagonal. |
| NOTE: The QP presolver value AUTOMATIC is applied. |
| NOTE: The QP presolver removed 0 variables and 0 constraints. |
| NOTE: The QP presolver removed 0 constraint coefficients. |
| NOTE: The presolved problem has 2 variables, 2 constraints, and 4 constraint |
| coefficients. |
| NOTE: The QP solver is called. |
| NOTE: The Interior Point algorithm is used. |
| NOTE: The deterministic parallel mode is enabled. |
| NOTE: The Interior Point algorithm is using up to 4 threads. |
| Primal Bound Dual |
| Iter Complement Duality Gap Infeas Infeas Infeas Time |
| 0 4.4604E+03 2.6380E-01 1.7962E-02 1.8143E+00 2.7770E-14 0 |
| 1 1.2367E+02 7.8255E-03 1.7962E-04 1.8143E-02 1.9285E-14 0 |
| 2 1.2365E+00 7.8496E-05 1.7973E-06 1.8154E-04 4.6816E-14 0 |
| 3 1.2364E-02 7.8498E-07 1.7973E-08 1.8154E-06 1.9865E-14 0 |
| 4 0.0000E+00 7.8498E-09 1.7861E-09 1.8154E-08 3.4973E-07 0 |
| NOTE: Optimal. |
| NOTE: Objective = 15018.000046. |
| NOTE: The Interior Point solve time is 0.00 seconds. |
The PRINT statement that follows immediately after the PROC CAS step prints the value of the SolutionSummary results table to the log, as shown in Output 2.14.3.
Output 2.14.3: Log: SolutionSummary Results Table
| Solution Summary | |
|---|---|
| Solver | QP |
| Algorithm | Interior Point |
| Objective Function | OBJ |
| Solution Status | Optimal |
| Objective Value | 15018.000046 |
| Primal Infeasibility | 0 |
| Dual Infeasibility | 0 |
| Bound Infeasibility | 0 |
| Duality Gap | 7.8497853E-9 |
| Complementarity | 0 |
| Iterations | 4 |
| Presolve Time | 0.00 |
| Solution Time | 0.00 |
Simple Quadratic 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 quadratic program stored in the data table example:
s:optimization_solveQp{
data = {name = "example"},
primalOut = {name = "expout", replace=true},
dualOut = {name = "exdout", replace=true},
objSense = "min",
logFreq = 1}
Simple Quadratic 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 quadratic program stored in the data table example:
s.optimization.solveQp(
data = {"name": "example"},
primalOut = {"name": "expout", "replace":True},
dualOut = {"name": "exdout", "replace":True},
objSense = "min",
logFreq = 1)
Simple Quadratic 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 quadratic program stored in the data table example:
cas.optimization.solveQp(s,
data = "example",
primalOut = list(name="expout", replace=TRUE),
dualOut = list(name="exdout", replace=TRUE),
objSense = "min",
logFreq = 1)