I have a decision tree that i need to turn to a code in C#
The simple way of doing it is using if-else statements but in this solution i will need to create 4-5 nested conditions.
I am looking for a better way to do it and so far i read a little bit about rule engines.
Do you have something else to suggest for an efficient way to develop decision tree with 4-5 nested conditions?
I implemented a simple decision tree as a sample in my book. The code is available online here, so perhaps you could use it as an inspiration. A decision is essentially represented as a class that has references to
true
branch andfalse
branch and contains a function that does the test:Here,
Decision
is a base class that containsEvaluate
method and the source contains one additional derived type that contains a final decision of the tree (yes/no). The typeClient
is a sample input data that you're analysing using the tree.To create a decision tree, you can write something like:
If you just want to write five nested static
if
clauses then maybe just writingif
is fine. The benefit of using a type like this one is that you can easily compose trees - e.g. reuse a part of a tree or modularize the construction.Below is the Tomas Petricek's code mentioned in the answer https://stackoverflow.com/a/3889544/5288052 .
The zip containing all the source code of the book "Real-World Functional Programming" is available here https://www.manning.com/books/real-world-functional-programming .
Just because... I had a go at this and the results are here ... https://github.com/jkennerley/DeeTree
I had to implement a decision tree using the ID3 algorithm during my masters in C#.
I wrote about my implementation here. The code can be downloaded from GitHub.