From 95cf42c51384ffa9fed9f9490af05b13207e425a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Almeida?= Date: Thu, 9 May 2019 15:58:45 +0100 Subject: [PATCH] MAJOR: first commit --- app/baysian-network/engine3.erl | 29 +++++++++++++++++++++++ app/decision-tree/dec_tree.erl | 41 +++++++++++++++++++++++++++++++++ app/decision-tree/engine.erl | 30 ++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 app/baysian-network/engine3.erl create mode 100644 app/decision-tree/dec_tree.erl create mode 100644 app/decision-tree/engine.erl diff --git a/app/baysian-network/engine3.erl b/app/baysian-network/engine3.erl new file mode 100644 index 0000000..6560a71 --- /dev/null +++ b/app/baysian-network/engine3.erl @@ -0,0 +1,29 @@ +-module(engine3). +-compile([export_all]). + +events() -> [{m1, ok}, {m2, ok}]. + +p({m1, ok}) -> 0.2; +p(A) when is_tuple(A) -> none. + +cp({m2, ok}, {m1, ok}) -> 0.8; +cp(_, _) -> none. + +getp({A, B}) when is_tuple(A), is_tuple(B) -> + case cp(A, B) of + none -> + Pba = cp(B, A), + Pa = getp(A), + Pb = getp(B), + Pba * Pa / Pb; + X -> + X + end; + +getp(A) when is_tuple(A) -> + case p(A) of + none -> + lists:sum([ getp({A, B})*getp(B) || B <- events(), p(B) /= none, cp(A,B) /= none ]); + X -> + X + end. \ No newline at end of file diff --git a/app/decision-tree/dec_tree.erl b/app/decision-tree/dec_tree.erl new file mode 100644 index 0000000..24f48c3 --- /dev/null +++ b/app/decision-tree/dec_tree.erl @@ -0,0 +1,41 @@ +-module(dec_tree). +-export([start/0, decision_tree/0]). + +%%% Interface + +start() -> Pid = spawn(?MODULE, decision_tree, []), Pid. + +decision_tree() -> T = create_tree(), decision_tree(T). +decision_tree(Tree) -> + io:format("~w~n", [Tree]), %% for debug purposes we always print the Tree + receive + {create_node, Node, Edge, Parent} -> NewTree = create_node(Tree, Node, Edge, Parent), decision_tree(NewTree); + {print} -> decision_tree(Tree); % no need to print the tree, since we already printed before receive... + {solve} -> io:format("Solution: ~w~n", [solve(Tree)]), decision_tree(Tree) + end. + +%%% Create the decision Tree + +create_tree() -> none. + +create_node(none, Node, Edge, _) -> {Node, Edge, []}; % 1st node +create_node({Parent, P_Edge, L}, Node, Edge, Parent) -> {Parent, P_Edge, [{Node, Edge, []}|L]}; % we find the parent node and insert the new node +create_node({Root, R_Edge, []}, _, _, Parent) -> {Root, R_Edge, []}; % recursion stop when the parent node is not found +create_node({Root, R_Edge, L}, Node, Edge, Parent) -> {Root, R_Edge, lists:map(fun(N) -> create_node(N, Node, Edge, Parent) end, L)}. % we try to insert the node in all subtrees + +%%% Solve the decision Tree + +% Determines the path by choosing the greatest edge +solve_max([{Edge, Path}]) -> {Edge, Path}; +solve_max([{H_Edge, E_Path}|T]) -> + {T_max, T_Path} = solve_max(T), + if + H_Edge > T_max -> + {H_Edge, E_Path}; + true -> % works as an 'else' branch + {T_max, T_Path} + end. + +solve(none) -> false; %there is no solition for an empty decision tree +solve({Node, Edge, []}) -> {Edge,[Node]}; %recursion stop +solve({Node, Edge, L}) -> {C_Edge, C_Path} = solve_max(lists:map(fun(N) -> solve(N) end, L)), {Edge + C_Edge, [Node|C_Path]}. \ No newline at end of file diff --git a/app/decision-tree/engine.erl b/app/decision-tree/engine.erl new file mode 100644 index 0000000..3c954ef --- /dev/null +++ b/app/decision-tree/engine.erl @@ -0,0 +1,30 @@ +-module(engine). +-compile([export_all]). + +% Decision Tree +% +% +----+ +% +---> OK | +% | +----+ +% | +% +----------| +----+ +% | DO MED 1 +---> KO | +% +----------+ +----+ +----+ +% | +---> OK | +% | +---------------| +----+ +% +---> DO MED 2 + +% +---------------| +----+ +% +---> KO | +% +----+ +% Each edge can be calculated from the table + + +doAll() -> + T = dec_tree:start(), + T ! {create_node, 'DO MED1', 0, none}, + T ! {create_node, 'MED1 OK', 0.980, 'DO MED1'}, + T ! {create_node, 'MED1 KO', 0.225, 'DO MED1'}, + T ! {create_node, 'DO MED2', 0.356, 'DO MED1'}, + T ! {create_node, 'MED 2 OK', 0.62, 'DO MED2'}, + T ! {create_node, 'MED 2 KO', 0.38, 'DO MED2'}, + T ! {solve}.