Initializing help system before first use

Read and write problems to and from standard formats MPS and LP


Type: Programming
Rating: 1 (simple)
Description:

This example shows how to read and write from and to MPS and LP files. Both formats are standard formats and can be input into most optimization solvers. While the MPS file preserves the order of the columns of a matrix, the LP format is more human-readable and suitable for debug purposes.

File(s): read_write_mps_lp.R
Data file(s): flp.lp


read_write_mps_lp.R
#####################################
# This file is part of the          #
# Xpress-R interface examples       #
#                                   #
#   (c) 2022-2025 Fair Isaac Corporation #
#####################################
#' ---
#' title: "Reading From/Writing to an MPS/LP File"
#' author: Gregor Hendel
#' date: Dec. 2020
#' ---
#' 

#' 
#' This example shows how to read and write from and to MPS and LP files. Both
#' formats are standard formats and can be input into most optimization solvers.
#' While the MPS file preserves the order of the columns of a matrix, the LP format
#' is more human-readable and suitable for debug purposes.
#' 
#' If you haven't done so already, please familiarize yourself with the Facility
#' Location Example, which we use throughout our quick examples.
#' 
## ----Printing out the FLP LP File---------------------------------------------
cat(readLines("flp.lp"), sep = "\n")

#' 
## ----Read the LP File into the Optimizer--------------------------------------
suppressMessages(library(xpress))
p <- createprob()
readprob(p, "flp.lp")

#' 
#' We can use Base R's `print` function to inspect the problem we just read. The
#' number of rows, columns, and nonzeros is as expected.
## ----Print the XPRSprob-------------------------------------------------------
print(p)

#' 
#' Now, let's write this problem as an MPS file.
## ----Write the Problem as MPS-------------------------------------------------
writeprob(p, "flp.mps")

#' 
#' 
#' By specifying the flag 'l', we can also write LP files to disk. Actually, this
#' is the way we created 'flp.lp' in the first place. Let's instead write the
#' presolved model to disk in LP format. For this, we set the LP Iteration limit to
#' 0, which effectively stops the solution process immediately after presolving.
#' 
#' We interrupt the Optimizer during the Root LP to have access to the presolved
#' problem
#' 
## ----Write The Presolved Problem as LP----------------------------------------
setintcontrol(p, xpress:::LPITERLIMIT, 0L);
mipoptimize(p)

# note the use of the flag 'l'
writeprob(p, "flp_presolved.lp", "l")

#' 
#' When we inspect the resulting LP file, we see that bounds have been derived for
#' all the connection variables:
#' 
## ----Inspect the Presolved Problem--------------------------------------------
cat(readLines("flp_presolved.lp"), sep = "\n")

#' 
#' This already concludes the example how to read and write from MPS and LP files.
#' Here is the content of the MPS file.
#' 
## ----Display the content of the MPS file--------------------------------------
cat(readLines("flp.mps"), sep = "\n")

#' 
#' 
#' 

© 2001-2025 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.