Merged mf.* from SVN head (which implements damping)...
[libdai.git] / matlab / dai.cpp
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 /*=================================================================*
23 * *
24 * This is a MEX-file for MATLAB. *
25 * *
26 * [logZ,q,md] = dai(psi,method,opts); *
27 * *
28 *=================================================================*/
29
30
31 #include <iostream>
32 #include <dai/matlab/matlab.h>
33 #include "mex.h"
34 #include <dai/alldai.h>
35
36
37 using namespace std;
38 using namespace dai;
39
40
41 /* Input Arguments */
42
43 #define PSI_IN prhs[0]
44 #define METHOD_IN prhs[1]
45 #define OPTS_IN prhs[2]
46 #define NR_IN 3
47
48
49 /* Output Arguments */
50
51 #define LOGZ_OUT plhs[0]
52 #define Q_OUT plhs[1]
53 #define MD_OUT plhs[2]
54 #define NR_OUT 3
55
56
57 void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] )
58 {
59 size_t buflen;
60
61 /* Check for proper number of arguments */
62 if( (nrhs != NR_IN) || (nlhs != NR_OUT) ) {
63 mexErrMsgTxt("Usage: [logZ,q,md] = dai(psi,method,opts)\n\n"
64 "\n"
65 "INPUT: psi = linear cell array containing the factors \n"
66 " psi{i} should be a structure with a Member field\n"
67 " and a P field, like a CPTAB).\n"
68 " method = name of the method (see README)\n"
69 " opts = string of options (see README)\n"
70 "\n"
71 "OUTPUT: logZ = approximation of the logarithm of the partition sum.\n"
72 " q = linear cell array containing all final beliefs.\n"
73 " md = maxdiff (final linf-dist between new and old single node beliefs).\n");
74 }
75
76 char *method;
77 char *opts;
78
79
80 // Get psi and construct factorgraph
81 vector<Factor> factors = mx2Factors(PSI_IN, 0);
82 FactorGraph fg(factors);
83 long nr_v = fg.nrVars();
84
85 // Get method
86 buflen = mxGetN( METHOD_IN ) + 1;
87 method = (char *)mxCalloc( buflen, sizeof(char) );
88 mxGetString( METHOD_IN, method, buflen );
89
90 // Get options string
91 buflen = mxGetN( OPTS_IN ) + 1;
92 opts = (char *)mxCalloc( buflen, sizeof(char) );
93 mxGetString( OPTS_IN, opts, buflen );
94 // Convert to options object props
95 stringstream ss;
96 ss << opts;
97 Properties props;
98 ss >> props;
99
100 // Construct InfAlg object, init and run
101 InfAlg *obj = newInfAlg( method, fg, props );
102 obj->init();
103 obj->run();
104
105
106 // Save logZ
107 double logZ = obj->logZ();
108
109 // Save maxdiff
110 double maxdiff = obj->MaxDiff();
111
112
113 // Hand over results to MATLAB
114 LOGZ_OUT = mxCreateDoubleMatrix(1,1,mxREAL);
115 *(mxGetPr(LOGZ_OUT)) = logZ;
116
117 Q_OUT = Factors2mx(obj->beliefs());
118
119 MD_OUT = mxCreateDoubleMatrix(1,1,mxREAL);
120 *(mxGetPr(MD_OUT)) = maxdiff;
121
122
123 return;
124 }