I would like to use Mathematica to help me simplify a given Binary Decision Diagram (i.e. pick the optimal variable ordering). I don't seem to find anything at all on the topic, although it is something everyone with a little CS background knows very well.
Given MMA's ability of dealing with graphs and boolean functions the task seems feasible, but I wouldn't want to waste a long time to solve a problem that is already solved. Is anybody aware of MMA resources on the matter?
Answer
This answer is a modification of my answer given in the discussion Creating Identification/Classification trees.
With this solution I am trying to achieve the simplification by using the impurity function applied to the data (the truth table in this case).
Make the truth table from the linked Wikipedia article (Binary Decision Diagram):
truthTable = {{0, 0, 0, 1}, {0, 0, 1, 0}, {0, 1, 0, 0}, {0, 1, 1,
1}, {1, 0, 0, 0}, {1, 0, 1, 0}, {1, 1, 0, 1}, {1, 1, 1, 1}};
truthTable = Map[ToString, truthTable, {-1}];
colNames = {"x1", "x2", "x3", "f"};
TableForm[truthTable, TableHeadings -> {None, colNames}]
Import the package:
Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/AVCDecisionTreeForest.m"]
Build a decision tree:
dtree = BuildDecisionTree[truthTable, "ImpurityFunction" -> "Gini"]
(* {{0.125, "0", 2, Symbol,
8}, {{0.125, "0", 1, Symbol,
4}, {{0.5, "0", 3, Symbol,
2}, {{{1, "1"}}}, {{{1, "0"}}}}, {{{2, "0"}}}}, {{0.125, "0", 1, Symbol,
4}, {{0.5, "0", 3, Symbol, 2}, {{{1, "0"}}}, {{{1, "1"}}}}, {{{2, "1"}}}}} *)
The impurity function "Gini" is used by default. Another possible value is "Entropy". Note that to make the decision tree I converted the 0-1 values in the truth table into strings.
Visualize the tree:
trules = dtree // DecisionTreeToRules;
trules = trules /. ({m_, v_, cInd_Integer, s_, n_, id_} :> {m, v, colNames[[cInd]], s, n, id});
LayeredGraphPlot[trules, VertexLabeling -> True]
The last commands produce the plot:
The second row of a leaf shows the corresponding number of records and label.
The tree rules can be manipulated to produce a graph closer to the one in the Wikipedia article, if that is desired.
See these blog posts for application examples, parameters explanations, and tweaking discussions:
Decision trees posts at MathematicaForPrediction at WordPress.


Comments
Post a Comment