source:branches/parallel/Ipopt/contrib/MatlabInterface/examples/bayesnet/bopt.m@1887

Last change on this file since 1887 was 1887, checked in by andreasw, 3 years ago

synchronized with trunk rev 1886; fixed bug in Matrix-vector product for parallel matrix

File size: 2.8 KB
RevLine
[1887]2% This code is published under the Eclipse Public License.
[970]3%
4% Author: Peter Carbonetto
5%         Dept. of Computer Science
6%         University of British Columbia
7%         May 19, 2007
8
9function qR = bopt (K, C, f, Rv, Rf, Sv, Sf, NS, verbose)
10  maxiter   = 100;         % The maximum number of iterations.
11  tolerance = 1e-6;        % The convergence criterion.
12  nr        = length(Rv);  % The number of large regions.
13  ns        = length(Sv);  % The number of small regions.
14
15  % Convert the input K to a row vector.
16  K = K(:)';
17
18  % Get the degrees of the separators.
19  d = degrees(NS);
20
21  % Initialize the marginals on the large and small regions to uniform
22  % probability tables.
23  qR = initmarginals(K,Rv);
24  qS = initmarginals(K,Sv);
25
26  % Compute the total number consistency constaints. We have one
27  % constraint for every pair (R,S), then again for every possible
28  % configuration xS.
29  nc = numconsistencyconstraints(qS,d);
30
31  % Run the IPOPT solver.
[1203]32  qR             = vectorize(qR);
33  qS             = vectorize(qS);
34  nqr            = length(qR);
35  nqs            = length(qS);
36  [status qR qS] = ipopt({qR qS},{ repmat(eps,nqr,1) repmat(eps,nqs,1) },...
[970]37                  { repmat(inf,nqr,1) repmat(inf,nqs,1) },...
38                  [ ones(1,nr) ones(1,ns) zeros(1,nc) ],...
39                  [ ones(1,nr) ones(1,ns) zeros(1,nc) ],...
41                  @computeJGConstraints,@computeJGJacobian,...
42                  @computeJGHessian,{ K C f Rv Rf Sv Sf NS d },'',...
[1131]44                  'tol',tolerance,'jac_c_constant','yes',...
45                  'jac_d_constant','yes','print_level',verbose*5);
[970]46
47  % Reshape the solution.
48  qR = reshapemarginals(qR,Rv,K);
49  qS = reshapemarginals(qS,Sv,K);
50
51% ------------------------------------------------------------------
52function d = degrees (NS)
53  ns = length(NS);  % The number of separators.
54  d  = zeros(1,ns);
55
56  % Repeat for each separator.
57  for s = 1:ns
58    d(s) = length(NS{s});
59  end
60
61% ----------------------------------------------------------------
62function qR = initmarginals (K, Rv)
63  nr = length(Rv);  % The number of regions.
64  qR = cell(1,nr);  % The return value.
65
66  % Repeat for each region.
67  for r = 1:nr
68    is    = Rv{r};  % The variables nodes in the large region.
69    table = ones([K(is) 1]);
70    qR{r} = table / sum(table(:));
71  end
72
73% ----------------------------------------------------------------
74function nc = numconsistencyconstraints (qS, d)
75  ns = length(qS);  % The number of small regions.
76  nc = 0;           % The return value.
77
78  % Repeat for each small region.
79  for s = 1:ns
80    tablesize = numel(qS{s});
81    nc        = nc + d(s) * tablesize;
82  end
Note: See TracBrowser for help on using the repository browser.