next up previous contents
Next: OSParsers Up: OSModelInterfaces Previous: Converting AMPL nl Files   Contents


Using MATLAB

Linear, integer, and quadratic problems can be formulated in MATLAB and then optimized either locally or over the network using the OS Library. The OSMatlab class functions much like OSnl2osil and OSmps2osil and takes MATLAB arrays and creates an OSiL instance. This class is part of the OS library. In order to use the OS library with MATLAB the user should do the following. In order to use the OSMatlab class it is necessary to compile matlabSolver.cpp into a MATLAB Executable file. The matlabSolver.cpp file is in the OSModelInterfaces directory even though it is not part of the OS library. The following steps should be followed.

Step 1:
In the project root run make install.

Step 2:
Either leave matlabSolver.cpp in the the OSModelInterfaces or copy it to another desired directory.

Step 3:
Edit the MATLAB mexopts.sh (UNIX) or mexopts.bat so that the CXXFLAGS option includes the header files in the cppad directory and the include directory in the project root. For example, it should look like:
CXXFLAGS='-fno-common -no-cpp-precomp -fexceptions
    -I/Users/kmartin/Documents/files/code/cpp/OScpp/COIN-OSX/
    -I/Users/kmartin/Documents/files/code/cpp/OScpp/COIN-OSX/include'

Next edit the CXXLIBS flag so that the OS and supporting libraries are included. For example, it should look like:

CXXLIBS="$MLIBS -lstdc++
    -L/Users/kmartin/Documents/files/code/ipopt/macosx/Ipopt-3.2.2/lib
    -L/Users/kmartin/Documents/files/code/cpp/OScpp/COIN-OSX/lib
    -lOS  -lIpopt -lOsiCbc -lOsiClp -lCbc -lCgl -lOsi -lClp -lCoinUtils  -lm"

For a UNIX system the mexopts.sh file will usually be found in a directory with the release name in $ \sim$/.matlab. For example, $ \sim$/.matlab/R14SP3.

On a Windows system, the mexopts.bat file will usually be in a directory with the release name in C: $ \backslash$Documents and Settings $ \backslash$Username $ \backslash$Application Data $ \backslash$Mathworks $ \backslash$MATLAB

Step 4:
Build the MATLAB executable file. Start MATLAB and in the MATLAB command window connect to the directory containing the file matlabSolver.cpp. Execute the command:

mex -v matlabSolver.cpp

On a MAC OS X the resulting executable will be named matlabSolver.mexmac. On the Windows system the file is named matlabSolver.mexw32.

Step 5:
Set the MATLAB path to include the directory with the matlabSolver executable. Also, put the m-file callMatlabSolver.m in a directory which is on a MATLAB path. This file is in the OSModelInterfaces directory.

To use the matlabSolver it is necessary to put the coefficients from a linear, integer, or quadratic problem into MATLAB arrays.

  Minimize $\displaystyle \quad 10 x_{1} + 9 x_{2}$ (5)
  Subject to $\displaystyle \quad .7x_{1} + x_{2}$ $\displaystyle \le 630$ (6)
    $\displaystyle .5x_{1} + (5/6) x_{2}$ $\displaystyle \le 600$ (7)
    $\displaystyle x_{1} + (2/3) x_{2}$ $\displaystyle \le 708$ (8)
    $\displaystyle .1x_{1} + .25 x_{2}$ $\displaystyle \le 135$ (9)
    $\displaystyle x_{1}, x_{2}$ $\displaystyle \ge 0$ (10)

The MATLAB representation of this problem in MATLAB arrays is

% the number of constraints
numCon = 4;
% the number of variables
numVar = 2;
% variable types
VarType='CC';
% constraint types
A = [.7  1; .5  5/6; 1   2/3  ; .1   .25];
BU = [630 600  708  135];
BL = [];
OBJ = [10  9];
VL = [-inf -inf];
VU = [];
ObjType = 1;
% leave Q empty if there are no quadratic terms
Q = [];
prob_name = 'ParInc Example'
password = 'chicagoesmuyFRIO';
%
%
%the solver
solverName = 'lindo';
%the remote service service address
%if left empty we solve locally
serviceAddress='http://gsbkip.chicagogsb.edu/os/OSSolverService.jws';
% now solve
callMatlabSolver( numVar, numCon, A, BL, BU, OBJ, VL, VU, ObjType, ...
    VarType, Q, prob_name, password, solverName, serviceAddress)
This example m-file is in the data directory and is file parincLinear.m. Note that in addition to the problem formulation we can specify which solver to use through the solverName variable. If solution with a remote solver is desired this can be specified with the serviceAddress variable. If the serviceAddress is left empty, i.e.
serviceAddress='';
then a local solver is used. In this case it is crucial that the appropriate solver is linked in with the matlabSolver executable using the CXXLIBS option.

The data directory also contains the m-file template.m which contains extensive comments about how to formulate the problems in MATLAB. A second example which is a quadratic problem is given in the Appendix. The appropriate m-file is markowitz.m.


next up previous contents
Next: OSParsers Up: OSModelInterfaces Previous: Converting AMPL nl Files   Contents
Kipp Martin 2008-01-16