Appendix M: MATLAB and SMILE

<< Click to Display Table of Contents >>

Navigation:  »No topics above this level«

Appendix M: MATLAB and SMILE

It is possible to use C/C++ code with MATLAB by compiling it into mex (MATLAB executable) file. The next section of this manual contains a complete wrapper code (matsmile.cpp file), that contains functions for reading networks from file, setting evidence, and performing inference in discrete Bayesian networks and can be easily extend to include other functionality.

Before proceeding, refer to MATLAB documentation for detailed information about configuring your C++ compiler to work MATLAB. Also, please make sure that you have SMILE library compatible with the C++ compiler that you are going to use with MATLAB.

To compile the C++ code into mex file, use the following MATLAB command:

mex matsmile.cpp

The name of the compiled file depends on the operating system that you are using. On 64-bit Windows, it will be matsmile.mexw64. If the compilation was successful, MATLAB code can call new function matsmile. Here is the example MATLAB code that reads the network file, sets the evidence for the Forecast node to Moderate, and then performs inference and displays the probabilities calculated for node Success.

net = matsmile('newNetwork');

matsmile('readFile', net, 'VentureBN.xdsl');

matsmile('setEvidence', net, 'Forecast', 'Moderate');

matsmile('updateBeliefs', net);

beliefs = matsmile('getValue', net, 'Success');

outcomeIds = matsmile('getOutcomeIds', net, 'Success');

fprintf("%s\n", outcomeIds + "=" + beliefs);

matsmile('deleteNetwork', net);

MATLAB code can access wrapper's functionality through matsmile function. First argument is always the name of the wrapper sub-function passed as string. Note that the network object created by the matsmile('newNetwork') call must be deallocated by the corresponding matsmile('deleteNetwork', net). Omitting the latter will cause memory leaks.

The MATLAB wrapper is implemented as a class MexFunction derived form matlab::mex::Function. The MexFunction::findFunction method code contains wrapFx array, which has an element for each function accessible through a matsmile call. Each of these includes function name, the number of input parameters (used for validation at runtime) and a pointer to a class member function actually implementing the call. Each member function referenced in wrapFx returns void and has two arguments of the type matlab::mex::ArgumentList& for values passed from MATLAB side as output and input arguments, respectively. Other than newNetwork, all functions expect network object to be passed as the second argument in MATLAB code.

To extend the wrapper, you can add a new class member function implementing the functionality that you want to add. Next, you need to add a new element to the wrapFx array. At runtime, the findFunction method will be able to recognize the function name passed as first argument in the MATLAB code and invoke the appropriate member function. You can get the pointer to the DSL_network pointer created earlier by matsmile('newNetwork') call:

matlab::data::TypedArray<uint64_t> arr = inputs[1];

auto net = decodePtr(arr[0]); // type of net is DSL_network*