MAJOR: first commit
This commit is contained in:
parent
1223366400
commit
95cf42c513
29
app/baysian-network/engine3.erl
Normal file
29
app/baysian-network/engine3.erl
Normal file
@ -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.
|
||||
41
app/decision-tree/dec_tree.erl
Normal file
41
app/decision-tree/dec_tree.erl
Normal file
@ -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]}.
|
||||
30
app/decision-tree/engine.erl
Normal file
30
app/decision-tree/engine.erl
Normal file
@ -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}.
|
||||
Loading…
Reference in New Issue
Block a user