forked from MustafaSaraoglu/TreeGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.m
76 lines (54 loc) · 2.62 KB
/
Tree.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
classdef Tree
%TREE Summary of this class goes here
% Detailed explanation goes here
properties
digraph
visualization
end
methods
function obj = Tree(RootNodes,leafNodes)
%TREE Construct an instance of this class
% Detailed explanation goes here
AllNodes = [RootNodes leafNodes];
sourceNodes = [AllNodes.sourceNodeID];
targetNodes = [AllNodes.id];
targetNodes(1) = [];
G = digraph(sourceNodes,targetNodes);
p = plot(G,"layout","layered"); % Plot
% Edit the plot
edgeLabels = cellfun(@getName, [AllNodes.sourceEdgeName],'UniformOutput',false);
p.EdgeLabel = edgeLabels;
UnsafeNodes = AllNodes([AllNodes.UnsafetyValue] > 0.02); % Unsafety value - Min
p.highlight([UnsafeNodes.id],"NodeColor","red") % Mark unsafe nodes
SafeNodes = leafNodes([leafNodes.UnsafetyValue] <0.0001);
%% Find the best node among the safest nodes
[val, idx] = max([cat(1,SafeNodes.state).s]); % Performance value - Max
p.highlight(leafNodes(idx).id,"NodeColor","green") % Mark safe node green,
disp(['Best Node ID: ',num2str(leafNodes(idx).id),', total distance: ',num2str(val)])
%% Mark all safe actions and states
allDecisionNodes = Tree.getAlltheParentNodes(leafNodes(idx),AllNodes);
allsafe = [allDecisionNodes.id];
p.highlight([allDecisionNodes.id],"NodeColor","green") % Mark safe node green,
p.highlight(allsafe(2:end),allsafe(1:end-1));
%p.highlight(allsafe(2:end),allsafe(1:end-1),"EdgeColor","green","EdgeFontSize",8) % Mark all the edges leading to the safe node green
p.MarkerSize = 8;
% Assign
obj.digraph = G;
obj.visualization =p;
end
end
methods (Static)
function allDecisionNodes = getAlltheParentNodes(safeNode,AllNodes)
allDecisionNodes = safeNode;
while true
parentNode = AllNodes(safeNode.sourceNodeID);
allDecisionNodes = [allDecisionNodes parentNode];
if isempty(parentNode.sourceNodeID)
break
else
safeNode = parentNode;
end
end
end
end
end