Skip to content
Stefan Vigerske edited this page Mar 5, 2019 · 1 revision

USING MATLAB

We have written code matlabSolver.cpp that generates a MATLAB executable file (.mex). This MATLAB executable file is linked with the OS library. It takes constraint matrices for the linear and quadratic programs generated in MATLAB and converts them to OSiL instances. A remote solver is called, the result in OSrL is passed back to MATLAB where it transformed using XSLT and displayed in a Web Browser.

Here is an example of how to create an instance of a Markowitz portfolio optimization problem in MATLAB.

% Markowitz example from www.lindo.com
% x0 = .530093,  x1 = .356411, x2 = .113497
% obj = .0224138
% the number of constraints
numCon = 2;
% the number of variables
numVar = 3;
% variable types 
VarType='CCC';
% constraint types
A = [ 1 1 1  ;
     .0890833  .213667  .234583];
BU = [1  inf];
BL = [1   .15];
OBJ = [0 0 0];
VL = [0  0  0];
VU = [.75 .75 .75];
ObjType = 0;
% leave Q empty if there are no quadratic terms
Q = [ -1 -1 -1 -1 -1 -1 -1 -1 -1 ;
     0 0 0  1 1 1  2 2 2;
     0 1 2  0 1 2  0 1 2 ;
.01080754 .01240721 .01307513    
.01240721 .05839170 .05542639    
.01307513 .05542639 .09422681];
prob_name = 'Makowitz Example'
password = '***********';

Once the MATLAB instance is generated we call the m-file function callMatlabSolver as follows.

callMatlabSolver( numVar, numCon, A, BL, BU, OBJ, VL, VU, ObjType, VarType, Q, prob_name, password);

The m-file function callMatlabSolver is

function callMatlabSolver(numVar, numCon, A, BL, BU, OBJ, VL, VU, ObjType, VarType, Q, prob_name, password)
% callMatLabSolver invokes the Matlab mex routine to solve a MATLAB
% formulated problem using optimization services.
os_result = matlabSolver( numVar, numCon, A, BL, BU, OBJ, VL, VU, ObjType, VarType, Q, prob_name, password);
if size(os_result) > 0
     fid = fopen('osrl.xml', 'w');
     fprintf(fid, '%s' ,os_result);
     fclose( fid);
     xslt('osrl.xml', 'OSrL.xslt', 'test.html');
     curdir = pwd;
     file_path = strcat('file://',curdir, '/test.html');
     web(file_path);
end

This functions calls the MATLAB mex file matlabSolver which in turns communicates with the OS library to solve the instance.

Both the m-file function callMatlabSolver.m and the code matlabSolver.cpp are in the src directory OSModelInterfaces. Two MATLAB instance files are in the OS/data directory. They are parincLinear.m and markowitz.m.