# Using Xpress with MATLAB

Document created by oliverbastert@fico.com on Nov 17, 2014Last modified by oliverbastert@fico.com on Mar 9, 2018
Version 2Show Document

The Xpress MATLAB interface is a tool that makes Xpress optimization algorithms available directly from within the MATLAB environment, enabling users to easily define mathematical programming problems within MATLAB and solve them using the Xpress Optimization suite.

The interface comprises two components: the first one is the Xpress-Optimizer interface which provides toolbox functions, akin to those in the MATLAB  Optimization  Toolbox, for solving linear and quadratic programming problems, and their mixed integer versions. These functions are designed to take a model description as input and produce a solution as output.

The second component is the Xpress-Mosel interface that provides access and interaction capabilities to the Xpress-Mosel modeling tool. The Mosel language is an algebraic modeling and procedural programming language specifically designed to be an easy-to-learn, robust way to interact with Xpress solver engines. Xpress-Mosel also allows multiple problems to be defined within one model file for easier implementation of more advanced solution algorithms and its parallel and distributed modeling features leverage multiple cores and computers across the network.

The Xpress-Optimizer interface

The Xpress-Optimizer MATLAB interface gives access to the state-of-the-art large-scale mixed-integer linear and quadratic mathematical programming Xpress solvers. Using this interface, the model needs to be directly expressed as a problem matrix in MATLAB. As an example, consider this tiny problem:

Find x that minimizes

f(x) = –5x1 – 4x2 – 6x3,

subject to:

x1     –    x2     +    x3     ≤ 20

3 x1     +  2x2     + 4x3     ≤ 42

3 x1     +  2x2                        ≤ 30                                   0 ≤ x1, 0 ≤ x2, 0 ≤ x3

This could be solved with the following MATLAB commands:

>> f = [-5; -4; -6];

>> A = [1 -1  1

>>      3  2  4

>>      3  2  0];

>> b = [20; 42; 30];

>> lb = zeros(3,1);

>> xprslp(f,A,b,’L’,lb);

The Xpress-Optimizer MATLAB interface supports all problem types that can be solved natively by the Xpress-Optimizer, namely mixed-integer linear, quadratic and quadratically constrained programming problems including the Second Order Cone variants (SOCPs and MISCOCPs). It does not support general non-linear problems at this point. Furthermore it offers these features:

• toolbox like functions that can easily be used in place of native MATLAB ones;
• full handling of all the Xpress-Optimizer controls to govern the solution procedure;
• access to the problem and solution attributes that are saved by the Optimizer during the optimization (such as the algorithm used, the number of nodes processed, etc.) ;
• option to export the problem definition in MPS or LP format.

The Xpress-Mosel interface

The MATLAB interface for Mosel provides functions for running Mosel programs from within MATLAB and exchanging data between the Mosel models and the MATLAB environment.

In its simplest form, it is possible to call the moselexec function from MATLAB to execute a Mosel program:

>> moselexec example.mos

Data exchange is then controlled from within the Mosel pogram, using the matlab module and the Mosel “initializations from/to” constructs. An initializations to block can be used to send data from Mosel to MATLAB. For example the following block, at the end of a Mosel model:

initializations to "matlab.mws:"

simplexiter

evaluation of getobjval as "objval"

evaluation of array(s in SHARES) frac(s).sol as "frac"

end-initializations

would store, to the MATLAB workspace, the number of simplex iterations taken to solve the problem, the value of the objective function and the values taken by variables frac in the optimal solution. These would then be available in the MATLAB workspace as variables “simplexiter”, “objval” and (vector) “frac”.

For the opposite direction, Initializations from blocks are used to import data from MATLAB to Mosel. This also provides a mechanism to call arbitrary MATLAB functions as the entities to load are queried through the MATLAB evaluator. Thus in a Mosel program, it is possible to write for example:

initializations from "matlab.mws:"

magicsize as "mysize"

total as "sum(sum(magic(mysize)))"

end-initializations

to assign the MATLAB variable mysize to Mosel variable magicsize, and use MATLAB functions to calculate the sum of all elements in a magic square of order mysize.

Finally, using the Mosel Java Run-Time Interface from MATLAB it is also possible to get a finer control on Mosel program execution and:

• compile Mosel model source files into binary model (bim) files
• load and unload bim files, handling several models at a time
• execute models
• access the Mosel internal data structures through the Post Processing Interface
• manage the dynamic shared objects (or Mosel 'modules') used by Mosel