- Notifications
You must be signed in to change notification settings - Fork0
fermendezmx/Decision.Trees
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The purpose of this solution is to get the sum of all the decision tree results.
Input: - 1000 Decimal variables: VAR_0 ... VAR_999 - 50 Decision trees * Multilevel * Each level has a condition based on a variable Example: * VAR_56 < 0.5 * VAR_475 > 544 and VAR_475 < 892 * The result of a decision tree is a decimal number Example: 0.2319949 Output: - SCORE = Sum of the decision tree results
Solution:
publicabstractclassDecision{publicabstractdecimalDecide(decimal[]input);}
publicclassNode:Decision{publicDecisionAffirmative{get;set;}publicDecisionNegative{get;set;}publicFunc<decimal[],bool>Condition{get;set;}publicoverridedecimalDecide(decimal[]input){returnCondition(input)?Affirmative.Decide(input):Negative.Decide(input);}}
publicclassLeaf:Decision{publicdecimalResult{get;set;}publicoverridedecimalDecide(decimal[]input){returnResult;}}
/// <summary>/// Define a set of multilevel trees for testing/// Where each level has a condition based on a variable/// </summary>publicstaticclassForest{publicstaticNodeTreeA(){NodenodeC=newNode{Condition=(values)=>values[0]>0.5M,Affirmative=newLeaf{Result=1.2M},Negative=newLeaf{Result=0.4M}};NodenodeB=newNode{Condition=(values)=>values[4]>1.0M||values[4]==0.8M,Affirmative=nodeC,Negative=newLeaf{Result=2.2M}};NodenodeA=newNode{Condition=(values)=>values[1]==0.2M||values[1]>0.5M,Affirmative=newLeaf{Result=1.3M},Negative=nodeB};returnnodeA;}publicstaticNodeTreeB(){NodenodeC=newNode{Condition=(values)=>values[1]>0.1M||values[1]==0.0M,Affirmative=newLeaf{Result=0.6M},Negative=newLeaf{Result=2.4M}};NodenodeB=newNode{Condition=(values)=>values[3]>0.4M,Affirmative=newLeaf{Result=0.7M},Negative=nodeC};NodenodeA=newNode{Condition=(values)=>values[4]<0.3M||values[4]>0.7M,Affirmative=nodeB,Negative=newLeaf{Result=1.5M}};returnnodeA;}}
publicclassTree{publicTree(Noderoot,decimal[]values){Root=root;Values=values;}publicNodeRoot{get;set;}publicdecimal[]Values{get;set;}}
classProgram{staticvoidMain(string[]args){decimalresult=0;NoderootA=Forest.TreeA();NoderootB=Forest.TreeB();List<Tree>trees=newList<Tree>();// 1000 decimal variables, VAR_0 ... VAR_999decimal[]inputA={1.1M,0.5M,2.0M,0.3M,0.2M};decimal[]inputB={0.4M,0.2M,3.5M,1.0M,0.5M};// 50 decision treestrees.Add(newTree(rootA,inputA));trees.Add(newTree(rootA,inputB));trees.Add(newTree(rootB,inputA));trees.Add(newTree(rootB,inputB));foreach(Treetreeintrees){// The result of a decision tree is a decimal numberresult+=tree.Root.Decide(tree.Values);}// Sum of the decision tree resultsConsole.WriteLine(result);Console.ReadKey();}}