Initializing help system before first use

Running Mosel models from MATLAB


Type: Programming
Rating: 1 (simple)
Description: Simple models to illustrate the interface:
  • example_m1: trivial Mosel program to be executed from MATLAB
  • example_m2: Mosel program that uses the MATLAB I/O driver
  • example_m3: MATLAB script embedding a Mosel program
  • example_m4: pass data from MATLAB to Mosel via an 'initializations' block
  • example_m5: Mosel retrieving a MATLAB sparse matrix
  • example_m6: MATLAB using the Mosel Java interface
File(s): example_m1.mos, example_m2.mos, example_m3.m, example_m4.m, example_m4.mos, example_m5.m, example_m6.m


example_m1.mos
(!******************************************************
   Mosel Matlab Example Problems
   =============================

   file example_m1.mos
   `````````````````````
   Trivial Mosel program to be executed from MATLAB.
   
  (c) 2014 Fair Isaac Corporation
      author: L.Bertacco, Apr. 2014
*******************************************************!)

model "example_m1" 
exit(10)
end-model

example_m2.mos
(!******************************************************
   Mosel Matlab Example Problems
   =============================

   file example_m2.mos
   `````````````````````
   Printing a MATLAB string variable 
   
  (c) 2014 Fair Isaac Corporation
      author: L.Bertacco, Apr. 2014
*******************************************************!)

model "example_m2" 
 uses "mmsystem";
 fcopy("matlab.mws:message", "")
 writeln
end-model

example_m3.m
%*******************************************************
%  Mosel Matlab Example Problems
%  =============================
%
%  file example_m3.m
%  ``````````````````
%  Executing a Mosel program contained
%  in a MATLAB cell array of strings
%  
% (c) 2014 Fair Isaac Corporation
%     author: L.Bertacco, Apr. 2014, rev. Sep. 2017
%*******************************************************

mos={
'model "example_m3"        '
' uses "mmxprs", "mmnl";   '
' declarations             '
'  a:mpvar                 '
' end-declarations         '
' minimize(a*a-5*a+10)     '
' writeln(getobjval)       '
'end-model                 '
};
moselexec('matlab.mws:mos')

example_m4.m
%*******************************************************
%  Mosel Matlab Example Problems
%  =============================
%
%  file example_m4.m
%  ``````````````````
%  Initializations from MATLAB expressions
%  
% (c) 2014 Fair Isaac Corporation
%     author: L.Bertacco, Apr. 2014
%*******************************************************

foo=pi;
bar=exp(1);
moselexec('example_m4.mos');

example_m4.mos
(!******************************************************
   Mosel Matlab Example Problems
   =============================

   file example_m4.mos
   ```````````````````
   Initializiations from MATLAB expressions 
   
  (c) 2014 Fair Isaac Corporation
      author: L.Bertacco, Apr. 2014
*******************************************************!)

model "example_m4"                               
 declarations                                    
  answer: integer                                
  foo: real                                      
  var: real                                      
  today: string                                  
  i: range                                       
  data: array(i) of real                         
 end-declarations                                

 initializations from "matlab.mws:"              
  answer as "42"                                 
  foo                                            
  var as "bar"                                   
  today as "date"                                
  data as "sum(magic(foo*bar))"                  
 end-initializations                             

 writeln("answer to ultimate question: ", answer)
 writeln("foo: ", foo)                           
 writeln("bar: ", var)                           
 writeln("today: ", today)                       
 writeln("data: ", data)                         
end-model

example_m5.m
%*******************************************************
%  Mosel Matlab Example Problems
%  =============================
%
%  file example_m5.m
%  ``````````````````
%  Initialization from a MATLAB sparse matrix
%  
% (c) 2014 Fair Isaac Corporation
%     author: L.Bertacco, Apr. 2014
%*******************************************************

mos={
'model "example_m5"                  '
' declarations                       '
'  i,j: range                        '
'  sparse: dynamic array(i,j) of real'
' end-declarations                   '
' initializations from "matlab.mws:" '
'  sparse as "sprand(4,4,.5)"        '
' end-initializations                '
' writeln("sparse is: ", sparse)     '
' writeln("row indices: ", i)        '
' writeln("col indices: ", j)        '
'end-model                           '
};
moselexec('matlab.mws:mos');

example_m6.m
%*******************************************************
%  Mosel Matlab Example Problems
%  =============================
%
%  file example_m6.m
%  ``````````````````
%  Accessing Mosel arrays
%  
% (c) 2014 Fair Isaac Corporation
%     author: L.Bertacco, Apr. 2014, rev. Sep. 2018
%*******************************************************

mos={
'model example                                                  '
' public declarations                                           '
'  CITIES = {"london", "paris", "madrid", "rome", "florence"}   '
'  ZONES = {"north", "south", "east", "west"}                   '
'  VALUE: dynamic array(CITIES,ZONES) of real                   '
' end-declarations                                              '
'                                                               '
' VALUE("london", "east")  := 1                                 '
' VALUE("rome",   "west")  := 2                                 '
' VALUE("paris",  "south") := 3                                 '
' VALUE("madrid", "east")  := 4                                 '
'end-model                                                      '
};

mosel = com.dashoptimization.XPRM; 
mosel.compile('', 'matlab.mws:mos', 'example_m6.bim');
mod = mosel.loadModel('example_m6.bim');
mod.run;

value = mod.findIdentifier('VALUE'); 
value_iter = value.indices(true);
sets = value.getIndexSets();
while value_iter.hasNext    
  indices = value_iter.next;
  fprintf(1, 'VALUE ( ');
  for i=1:size(indices,1)
    fprintf(1, '%s ', char(sets(i).get(indices(i))));
  end
  fprintf(1, ') = %g\n', value.getAsReal(indices));
end