Xpress R examples
Welcome to the FICO Xpress-R interface examples. Each example is a standalone
R script. Some examples may require additional R packages that must
be installed to successfully run the example. This can be done
via the install.packages command within an interactive R session.
Each example is documented using Roxygen comments. Some of the
explanations contain mathematical notation in LaTeX markup. To render a
script as standalone HTML page that properly displays the mathematical
content, please use the spin and knit commands of the
knitr package, see
the R Markdown Cookbook.
For example, to render the standalone script "first_lp_problem.R" into an HTML page,
you can use:
knitr::spin("first_lp_problem.R", format="Rmd")
knitr::knit("first_lp_problem.Rmd", output = "first_lp_problem.html")
or simply press CTRL/CMD+SHIFT+K from inside R studio,
which executes both of the above steps with one command.
|
| Load and solve a simple 2x2 linear program: xprs_loadproblemdata, displaying a solution as a data frame, a simple pipe workflow
|
| |
|
| Type: |
Programming |
| Rating: |
1 (simple) |
| Description: |
We load and solve a 2x2 linear program
|
| File(s): |
first_lp_problem.R |
|
|
| Formulate and solve a Facility Location Problem: xprs_loadproblemdata, writeprob, setoutput, xprs_optimize, getsolution
|
| |
|
| Type: |
Programming |
| Rating: |
1 (simple) |
| Description: |
Formulate and solve a Facility Location Problem |
| File(s): |
facility_location.R |
|
|
| Solve an LP and a MIP using Xpress-R: xprs_loadproblemdata, getintattrib, getdblattrib, readprob, getmipsol, getsolution
|
| |
|
| Type: |
Programming |
| Rating: |
1 (simple) |
| Description: |
This example shows how to solve LP and MIP models.
|
| File(s): |
solving_an_lp_and_a_mip.R |
| Data file(s): |
flp.lp |
|
|
| Read and write problems to and from standard formats MPS and LP: readprob, setintcontrol, writeprob
|
| |
|
| 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 |
|
|
| Set controls of Xpress: readprob, setintcontrol, setdefaultcontrol, getintattrib, getdblattrib, setdefaults, dumpcontrols
|
| |
|
| Type: |
Programming |
| Rating: |
1 (simple) |
| Description: |
Learn about setting controls of Xpress
|
| File(s): |
set_controls.R |
| Data file(s): |
flp.lp |
|
|
| Query Attributes of Xpress: createprob, readprob, printing, getintattrib
|
| |
|
| Type: |
Programming |
| Rating: |
1 (simple) |
| Description: |
Query Attributes of Xpress
|
| File(s): |
query_attributes.R |
| Data file(s): |
flp.lp |
|
|
| Add indicator constraints: createprob, readprob, mipoptimize, getsolution, addrows, setindicator
|
| |
|
| Type: |
Programming |
| Rating: |
1 (simple) |
| Description: |
Add a new row and an indicator constraint
|
| File(s): |
indicator.R |
| Data file(s): |
flp.lp |
|
|
| Register an R function as callback into Xpress: readprob, getintattrib, addcboptnode, addcbintnode, getdblattrib
|
| |
|
| Type: |
Programming |
| Rating: |
3 (intermediate) |
| Description: |
This example shows how to collect data about solution process using callbacks.
|
| File(s): |
callback.R |
| Data file(s): |
flp.lp |
|
|
| Formulate a quadratic optimization problem in Xpress: xprs_loadproblemdata, writeprob, xprs_optimize
|
| |
|
| Type: |
Programming |
| Rating: |
1 (simple) |
| Description: |
Formulate a quadratic optimization problem in Xpress
|
| File(s): |
quadratic.R |
|
|
| Solving different types of Sudokus with the FICO Xpress Optimizer: xprs_loadproblemdata, addrows, getmipsol, mipoptimize
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
We formulate and solve Sudoku puzzles as binary optimization problems.
|
| File(s): |
sudoku.R |
|
|
| Modeling Sudokus creating columns and rows incrementally using xprs_newcol and xprs_addrow: createprob, setprobname, chgbounds, xprs_newcol, xprs_addrow, xprs_newrow, setoutput, getmipsol, mipoptimize
|
| |
|
| Type: |
Programming |
| Rating: |
1 (simple) |
| Description: |
Incremental modelling of different Sudoku variants of increasing difficulty
|
| File(s): |
sudoku_incremental.R |
|
|
| Solve Traveling Salesperson Problems using callbacks or delayed rows: xprs_loadproblemdata, addrows, addcuts, addcboptnode, addcbpreintsol, getcallbacksolution, loaddelayedrows
|
| |
|
| Type: |
Programming |
| Rating: |
3 (intermediate) |
| Description: |
In this example, we show how to use the FICO Xpress R-interface to model and
solve the famous Travelling Salesperson Problem (TSP). The TSP is
the classic combinatorial optimization problem to find the shortest so-called
tour through a complete graph.
|
| File(s): |
tsp.R |
|
|
| Solve a maximum flow problem and visualize the result: createprob, chgobjsense, setprobname, xprs_newcol, chgobj, xprs_addrow, getsolution
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
This example is a classical maximum flow problem. There are 2 reservoirs, 3 cities
and 5 pumping stations. The three cities are supplied from the two reservoirs.
All the 10 nodes are connected by a network of pipes, and we want to determine the
maximum flow in this network to see whether the demands can be satisfied entirely.
|
| File(s): |
watersupply.R |
|
|
| Solve a modified bin packing problem with the addition of a simple greedy heuristic: addmipsol, createprob, xprs_newcol, xprs_newrow, getsolution
|
| |
|
| Type: |
Programming |
| Rating: |
3 (intermediate) |
| Description: |
In this example, 16 boxes are required to be loaded into 3 railway wagons each with
capacity of 100 quintals. This example is similar to the bin packing problem, but
instead of minimizing the number of bins used, here we would like to minimize the
heaviest wagon load.
This example also shows how to add a user solution found by a simple greedy heuristic.
|
| File(s): |
wagonloadbalancing.R |
|
|
| Solve a routing problem using an arc-paths formulation for a telecommunication network: createprob, chgobjsense, xprs_newcol, xprs_addrow, getsolution
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
A private telephone company exploits a network (shown below) among five cities and
the edge labels represent the capacities of the link in terms of circuits. The capacity
of an edge is consumed in increments of a bidirectional circuit and we do not consider
any directed flows. Faced with demands for circuits,
the company wants to know whether it is possible to satisfy the demands entirely.
If it is not possible, the company wants to transmit as much as possible.
|
| File(s): |
routingtelecalls.R |
|
|
| Solve a production planning problem: createprob, chgbounds, xprs_newcol, xprs_newrow, getsolution
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
A company produces 6 types of glasses and wishes to plan its production for the next
12 weeks. We want to decide the quantities of each product to produce in each week to minimize
total cost of production and storage.
|
| File(s): |
production_of_glasses.R |
|
|
| Model a piecewise linear function using binary variables: createprob, chgbounds, xprs_newcol, xprs_addrow, getsolution
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
This example simulates the situation where we are buying a required number of items
and we get discounts incrementally, and we want to minimize the total cost.
This is a problem suitable to be modeled with SOS2 since we can express the cost as a
piecewise linear function of the number of items bought. For SOS2 formulation,
please refer to the file 'approx.R'. In this file we
model the incremental price breaks using binary variables.
|
| File(s): |
price_breaks.R |
|
|
| Optimize a composition of investment portfolios using semi-continuous variables: createprob, chgcoltype, chgglblimit, chgrhs, chgobjsense, xprs_newcol, xprs_newrow, getsolution
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
This example concerns the optimal composition of investment portfolios according to
maximum total return. We modify the problem several times by changing variable types or right hand sides.
|
| File(s): |
portfolio_selection.R |
|
|
| Find an optimal schedule of jobs on different machines and visualize the result in ggplot2: createprob, setprobname, xprs_newcol, xprs_newrow, xprs_addrow, setoutput, getdblattrib, getsolution
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
In this example, we find an optimal schedule of jobs to different machines with different durations. The schedule must preserve
the order in which the jobs need to be processed if only a single job can be run on a machine non-preemptively, i.e., without
interrupting the job and picking it up later. The goal is to minimize the completion time of all jobs.
|
| File(s): |
job_shop_scheduling.R |
|
|
| Solve two related problems assigning pilots to crews under different constraints: createprob, chgobjsense, chgobj, setprobname, xprs_newcol, xprs_addrow, setoutput, getsolution
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
A crew consists of two pilots. Pilots are assigned to crews based on their language and aircraft skills. Two such matching
problems are solved. The first is to determine whether there exists a perfect matching, i.e., one that assigns all pilots
to some crew.
The second problem is to find a perfect matching that maximizes a total crew score.
|
| File(s): |
flight_crew.R |
|
|
| Solve a timetabling problem for college courses under various constraints: createprob, chgbounds, chgobj, setprobname, xprs_newcol, xprs_newrow, setoutput, getsolution
|
| |
|
| Solve a small production problem with 2 products and 2 resource constraints: createprob, chgobjsense, setprobname, xprs_newcol, xprs_addrow, setoutput, getsolution
|
| |
|
| Type: |
Programming |
| Rating: |
1 (simple) |
| Description: |
An introductory examples that shows how to model a production problem with 2 products and 2 resource constraints
|
| File(s): |
chess2.R |
|
|
| Solve a minimum cost flow problem in a bipartite graph: createprob, setprobname, xprs_newcol, xprs_addrow, setoutput, getsolution
|
| |
|
| Type: |
Programming |
| Rating: |
1 (simple) |
| Description: |
We would like to find a minimum cost plan for moving rental cars between agencies to satisfy all the requirements.
|
| File(s): |
car_rental.R |
|
|
| Modeling a piecewise linear objective function using SOS2 constraints: createprob, setprobname, addsets, getdblattrib, xprs_newcol, xprs_addrow, setoutput, getsolution
|
| |
|
| Type: |
Programming |
| Rating: |
1 (simple) |
| Description: |
We model a piecewise linear function with an SOS2
constraint
|
| File(s): |
approx.R |
|
|
| Infeasibility and IIS Detection: createprob, getiisdata, getintattrib, iisall, iisstatus, iisisolations, xprs_newcol, xprs_newrow, xprs_optimize
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
In this example, we model an infeasible linear programming problem. For analyzing the infeasibility, we use IIS detection
of the Xpress Optimizer.
|
| File(s): |
infeasible.R |
|
|
| Basis and Stability: bndsa, createprob, getbasis, getnamelist, loadbasis, objsa, rhssa, writebasis, writeslxsol, getsolution, xprs_newcol, xprs_newrow,
xprs_optimize
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
We solve a simple 2x2 LP problem to optimality,
which serves as a showcase for basis handling methods and
sensitivity analysis.
|
| File(s): |
basis_sensitivity.R |
|
|
| Using the Tuner: createprob, setoutput, readprob, setintcontrol, tune, tunerreadmethod, tunerwritemethod, xprs_optimize
|
| |
|
| Type: |
Programming |
| Rating: |
2 (easy-medium) |
| Description: |
We showcase how to use the tuning capabilities with a default or custom tuner method.
|
| File(s): |
tuner.R |
| Data file(s): |
mymethod.xtm, miplib-rndinst.mps.gz |
|
|
| Goal programming: createprob, setoutput, xprs_newcol, xprs_addrow, chgobj, chgobjn, setobjintcontrol, setobjdblcontrol, xprs_optimize, getintattrib,
getmipsol
|
| |
|
| Type: |
Production planning |
| Rating: |
3 (intermediate) |
| Description: |
This example tries to construct a production plan which meets four different goals in order of priority.
|
| File(s): |
goalprog.R |
|
|
|