annotate toolboxes/FullBNT-1.0.7/bnt/general/solve_limid.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [strategy, MEU, niter] = solve_limid(engine, varargin)
Daniel@0 2 % SOLVE_LIMID Find the (locally) optimal strategy for a LIMID
Daniel@0 3 % [strategy, MEU, niter] = solve_limid(inf_engine, ...)
Daniel@0 4 %
Daniel@0 5 % strategy{d} = stochastic policy for node d (a decision node)
Daniel@0 6 % MEU = maximum expected utility
Daniel@0 7 % niter = num iterations used
Daniel@0 8 %
Daniel@0 9 % The following optional arguments can be specified in the form of name/value pairs:
Daniel@0 10 % [default in brackets]
Daniel@0 11 %
Daniel@0 12 % max_iter - max. num. iterations [ 1 ]
Daniel@0 13 % tol - tolerance required of consecutive MEU values, used to assess convergence [1e-3]
Daniel@0 14 % order - order in which decision nodes are optimized [ reverse numerical order ]
Daniel@0 15 %
Daniel@0 16 % e.g., solve_limid(engine, 'tol', 1e-2, 'max_iter', 10)
Daniel@0 17
Daniel@0 18 bnet = bnet_from_engine(engine);
Daniel@0 19
Daniel@0 20 % default values
Daniel@0 21 max_iter = 1;
Daniel@0 22 tol = 1e-3;
Daniel@0 23 D = bnet.decision_nodes;
Daniel@0 24 order = D(end:-1:1);
Daniel@0 25
Daniel@0 26 args = varargin;
Daniel@0 27 nargs = length(args);
Daniel@0 28 for i=1:2:nargs
Daniel@0 29 switch args{i},
Daniel@0 30 case 'max_iter', max_iter = args{i+1};
Daniel@0 31 case 'tol', tol = args{i+1};
Daniel@0 32 case 'order', order = args{i+1};
Daniel@0 33 otherwise,
Daniel@0 34 error(['invalid argument name ' args{i}]);
Daniel@0 35 end
Daniel@0 36 end
Daniel@0 37
Daniel@0 38 CPDs = bnet.CPD;
Daniel@0 39 ns = bnet.node_sizes;
Daniel@0 40 N = length(ns);
Daniel@0 41 evidence = cell(1,N);
Daniel@0 42 strategy = cell(1, N);
Daniel@0 43
Daniel@0 44 iter = 1;
Daniel@0 45 converged = 0;
Daniel@0 46 oldMEU = 0;
Daniel@0 47 while ~converged & (iter <= max_iter)
Daniel@0 48 for d=order(:)'
Daniel@0 49 engine = enter_evidence(engine, evidence, 'exclude', d);
Daniel@0 50 [m, pot] = marginal_family(engine, d);
Daniel@0 51 %pot = marginal_family_pot(engine, d);
Daniel@0 52 [policy, score] = upot_to_opt_policy(pot);
Daniel@0 53 e = bnet.equiv_class(d);
Daniel@0 54 CPDs{e} = set_fields(CPDs{e}, 'policy', policy);
Daniel@0 55 engine = update_engine(engine, CPDs);
Daniel@0 56 strategy{d} = policy;
Daniel@0 57 end
Daniel@0 58 engine = enter_evidence(engine, evidence);
Daniel@0 59 [m, pot] = marginal_nodes(engine, []);
Daniel@0 60 %pot = marginal_family_pot(engine, []);
Daniel@0 61 [dummy, MEU] = upot_to_opt_policy(pot);
Daniel@0 62 if approxeq(MEU, oldMEU, tol)
Daniel@0 63 converged = 1;
Daniel@0 64 end
Daniel@0 65 oldMEU = MEU;
Daniel@0 66 iter = iter + 1;
Daniel@0 67 end
Daniel@0 68 niter = iter - 1;