annotate toolboxes/FullBNT-1.0.7/bnt/general/mk_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 bnet = mk_limid(dag, node_sizes, varargin)
Daniel@0 2 % MK_LIMID Make a limited information influence diagram
Daniel@0 3 %
Daniel@0 4 % BNET = MK_LIMID(DAG, NODE_SIZES, ...)
Daniel@0 5 % DAG is the adjacency matrix for a directed acyclic graph.
Daniel@0 6 % The nodes are assumed to be in topological order. Use TOPOLOGICAL_SORT if necessary.
Daniel@0 7 % For decision nodes, the parents must explicitely include all nodes
Daniel@0 8 % on which it can depends, in contrast to the implicit no-forgetting assumption of influence diagrams.
Daniel@0 9 % (For details, see "Representing and solving decision problems with limited information",
Daniel@0 10 % Lauritzen and Nilsson, Management Science, 2001.)
Daniel@0 11 %
Daniel@0 12 % node_sizes(i) is the number of values node i can take on,
Daniel@0 13 % or the length of node i if i is a continuous-valued vector.
Daniel@0 14 % node_sizes(i) = 1 if i is a utility node.
Daniel@0 15 %
Daniel@0 16 % The list below gives optional arguments [default value in brackets].
Daniel@0 17 %
Daniel@0 18 % chance - the list of nodes which are random variables [1:N]
Daniel@0 19 % decision - the list of nodes which are decision nodes [ [] ]
Daniel@0 20 % utility - the list of nodes which are utility nodes [ [] ]
Daniel@0 21 % equiv_class - equiv_class(i)=j means node i gets its params from CPD{j} [1:N]
Daniel@0 22 %
Daniel@0 23 % e.g., limid = mk_limid(dag, ns, 'chance', [1 3], 'utility', [2])
Daniel@0 24
Daniel@0 25 n = length(dag);
Daniel@0 26
Daniel@0 27 % default values for parameters
Daniel@0 28 bnet.chance_nodes = 1:n;
Daniel@0 29 bnet.equiv_class = 1:n;
Daniel@0 30 bnet.utility_nodes = [];
Daniel@0 31 bnet.decision_nodes = [];
Daniel@0 32 bnet.dnodes = 1:n; % discrete
Daniel@0 33
Daniel@0 34 if nargin >= 3
Daniel@0 35 args = varargin;
Daniel@0 36 nargs = length(args);
Daniel@0 37 if ~isstr(args{1})
Daniel@0 38 if nargs >= 1, bnet.dnodes = args{1}; end
Daniel@0 39 if nargs >= 2, bnet.equiv_class = args{2}; end
Daniel@0 40 else
Daniel@0 41 for i=1:2:nargs
Daniel@0 42 switch args{i},
Daniel@0 43 case 'equiv_class', bnet.equiv_class = args{i+1};
Daniel@0 44 case 'chance', bnet.chance_nodes = args{i+1};
Daniel@0 45 case 'utility', bnet.utility_nodes = args{i+1};
Daniel@0 46 case 'decision', bnet.decision_nodes = args{i+1};
Daniel@0 47 case 'discrete', bnet.dnodes = args{i+1};
Daniel@0 48 otherwise,
Daniel@0 49 error(['invalid argument name ' args{i}]);
Daniel@0 50 end
Daniel@0 51 end
Daniel@0 52 end
Daniel@0 53 end
Daniel@0 54
Daniel@0 55 bnet.limid = 1;
Daniel@0 56
Daniel@0 57 bnet.dag = dag;
Daniel@0 58 bnet.node_sizes = node_sizes(:)';
Daniel@0 59
Daniel@0 60 bnet.cnodes = mysetdiff(1:n, bnet.dnodes);
Daniel@0 61 % too many functions refer to cnodes to rename it to cts_nodes -
Daniel@0 62 % We hope it won't be confused with chance nodes!
Daniel@0 63
Daniel@0 64 bnet.parents = cell(1,n);
Daniel@0 65 for i=1:n
Daniel@0 66 bnet.parents{i} = parents(dag, i);
Daniel@0 67 end
Daniel@0 68
Daniel@0 69 E = max(bnet.equiv_class);
Daniel@0 70 mem = cell(1,E);
Daniel@0 71 for i=1:n
Daniel@0 72 e = bnet.equiv_class(i);
Daniel@0 73 mem{e} = [mem{e} i];
Daniel@0 74 end
Daniel@0 75 bnet.members_of_equiv_class = mem;
Daniel@0 76
Daniel@0 77 bnet.CPD = cell(1, E);
Daniel@0 78
Daniel@0 79 % for e=1:E
Daniel@0 80 % i = bnet.members_of_equiv_class{e}(1); % pick arbitrary member
Daniel@0 81 % switch type{e}
Daniel@0 82 % case 'tabular', bnet.CPD{e} = tabular_CPD(bnet, i);
Daniel@0 83 % case 'gaussian', bnet.CPD{e} = gaussian_CPD(bnet, i);
Daniel@0 84 % otherwise, error(['unrecognized CPD type ' type{e}]);
Daniel@0 85 % end
Daniel@0 86 % end
Daniel@0 87
Daniel@0 88 directed = 1;
Daniel@0 89 if ~acyclic(dag,directed)
Daniel@0 90 error('graph must be acyclic')
Daniel@0 91 end
Daniel@0 92
Daniel@0 93 bnet.order = topological_sort(bnet.dag);