Optimization Action Set
Converting to an MPS-Format Data Table
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 example illustrates how you can use the convertMps action to make a problem instance available to solver actions. It uses example_fix.mps, the MPS file shown in Example 8.2: Fixed-MPS-Format File (SAS Optimization: Mathematical Optimization Procedures).
The following statements first use the DATA step to create a two-column data table from example_fix.mps. The DATA step assumes that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref. The TEXT column has the text from each line of the MPS file, and the _ID_ column has the corresponding line number. Then the CASL statements run the action on that table and fetch the new seven-column data table.
data mycas.mpstable;
infile 'example_fix.mps' truncover;
input TEXT $char100.;
_ID_ = _n_;
run;
proc cas;
loadactionset "optimization";
action optimization.convertMps /
data = "mpstable"
casOut = {name = "example_fix" replace = true}
format = "FIXED";
run;
action table.fetch / table = "example_fix" to = 24; run;
quit;
The data table example_fix, which is displayed in Output 2.1.1, shows the data table that the action produces. Note that the resulting table is different from the one that the loadMps action produces in Example 2.2: Loading an MPS File. They produce the same results from the solver because Field1–Field6 are the same when they are sorted by the _ID_ column.
Output 2.1.1: Converted Data Table
| Selected Rows from Table EXAMPLE_FIX | |||||||
|---|---|---|---|---|---|---|---|
| _Index_ | Field1 | Field2 | Field3 | Field4 | Field5 | Field6 | _id_ |
| 1 | N | PROFIT | . | . | 4 | ||
| 2 | L | FINISH | . | . | 7 | ||
| 3 | DESK | FINISH | 10 | PROFIT | -95 | 10 | |
| 4 | CABINET | STAMP | 2 | ASSEMB | 8 | 13 | |
| 5 | BOOKCSE | FINISH | 7 | PROFIT | -76 | 16 | |
| 6 | TIME | FINISH | 800 | . | 19 | ||
| 7 | BOUNDS | . | . | 22 | |||
| 8 | ENDATA | . | . | 25 | |||
| 9 | NAME | PROD_MIX | . | . | 2 | ||
| 10 | L | STAMP | . | . | 5 | ||
| 11 | COLUMNS | . | . | 8 | |||
| 12 | CHAIR | STAMP | 1.5 | ASSEMB | 6 | 11 | |
| 13 | CABINET | FINISH | 8 | PROFIT | -84 | 14 | |
| 14 | RHS | . | . | 17 | |||
| 15 | RANGES | . | . | 20 | |||
| 16 | UP | BND | CHAIR | 75 | . | 23 | |
| 17 | ROWS | . | . | 3 | |||
| 18 | L | ASSEMB | . | . | 6 | ||
| 19 | DESK | STAMP | 3 | ASSEMB | 10 | 9 | |
| 20 | CHAIR | FINISH | 8 | PROFIT | -41 | 12 | |
| 21 | BOOKCSE | STAMP | 2 | ASSEMB | 7 | 15 | |
| 22 | TIME | STAMP | 800 | ASSEMB | 1200 | 18 | |
| 23 | T1 | ASSEMB | 900 | . | 21 | ||
| 24 | LO | BND | BOOKCSE | 50 | . | 24 | |
Converting to an MPS-Format Data Table
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 how you can use the convertMps action in Lua code to make a problem instance available to solver actions. The following code loads and converts the problem instance from the MPS file, example_fix.mps, in Example 8.2: Fixed-MPS-Format File (SAS Optimization: Mathematical Optimization Procedures):
mpsWithId = io.open("mpsWithId.mps.csv","w")
io.output(mpsWithId)
io.write("_ID_\tText\n")
id = 0
for line in io.lines("example_fix.mps") do
id = id + 1
io.write(id .. "\t" .. line .. "\n")
end
io.close(mpsWithId)
s:loadtable{
caslib = "casuser",
path = "mpsWithId.mps.csv",
casOut = {name = "mpstable", replace = true},
importOptions = {fileType = "CSV", delimiter = "\t"}
}
s:optimization_convertMps{
data = "mpstable",
casOut = {name="example_fix", replace=true},
format = "FIXED"
}
Converting to an MPS-Format Data Table
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 how you can use the convertMps action in Python code to make a problem instance available to solver actions. The following code loads and converts the problem instance from the MPS file, example_fix.mps, in Example 8.2: Fixed-MPS-Format File (SAS Optimization: Mathematical Optimization Procedures):
# Make new file with ID separated by tab
def mps2mpsWithId(mpsOriginal):
mpsWithIdFileName = 'mpsWithId.mps.csv'
mpsWithId = open(mpsWithIdFileName,'w')
mpsWithId.write('_ID_\tText\n')
with open(mpsOriginal, 'r') as f:
id = 0
for line in f:
id += 1
mpsWithId.write(str(id) + '\t' + line.rstrip() + '\n')
mpsWithId.close()
return mpsWithIdFileName
# Make new file with two tab-delimited columns,
# upload to CAS, create seven-column MPS table
mpsWithIdFileName = mps2mpsWithId('example_fix.mps')
s.upload_file(
mpsWithIdFileName,
casout = {"name":"mpstable", "replace":True},
importoptions = {"filetype":"CSV", "delimiter":"\t"}
)
s.optimization.convertMps(
data = "mpstable",
casOut = {"name":"example_fix", "replace":True},
format = "FIXED"
)
Converting to an MPS-Format Data Table
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 how you can use the convertMps action in R code to make a problem instance available to solver actions. The following code loads and converts the problem instance from the MPS file, example_fix.mps, in Example 8.2: Fixed-MPS-Format File (SAS Optimization: Mathematical Optimization Procedures):
# Make new file with ID separated by tab
mps2mpsWithId <- function(mpsOriginal) {
mpsWithIdFileName <- "mpsWithId.mps.csv"
fileString <- "_ID_\tText\n"
f <- file(mpsOriginal, open="r")
lines <- readLines(f)
id <- 0
for (i in 1:length(lines)) {
id <- i + 1
fileString = paste(fileString, id, "\t", lines[i], "\n", sep="")
}
write(fileString, mpsWithIdFileName)
close(f)
return(mpsWithIdFileName)
}
# Make new file with two tab-delimited columns,
# load to CAS, create seven-column MPS table
mpsWithIdFileName = mps2mpsWithId("example_fix.mps")
cas.table.loadTable(s,
caslib = "CASUSER",
path = mpsWithIdFileName,
casOut = list(caslib="CASUSER",name="mpstable",replace=TRUE),
importOptions = list(fileType="CSV",delimiter="\t")
)
cas.optimization.convertMps(s,
data=list(caslib="CASUSER", name="mpstable"),
casOut=list(name="example_fix", replace=TRUE),
format="FIXED"
)