19

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?

casperOne
  • 73,706
  • 19
  • 184
  • 253
Chen
  • 191
  • 1
  • 1
  • 3
  • 1
    use windows workflow (as you alluded to)? – Mitch Wheat Oct 08 '10 at 09:31
  • Could you give a small sample of the naïve solution as the context will make answering the question easier. – Paul Ruane Oct 08 '10 at 09:41
  • whats wrong with 4-5 nested conditions? show us the code, at least if-else statements.. what type of variable is being checked? – markmnl Oct 08 '10 at 10:06
  • Possible duplicate of [How to implement an interactive decision tree in C#](https://stackoverflow.com/questions/58249079/how-to-implement-an-interactive-decision-tree-in-c-sharp) –  Oct 05 '19 at 17:40

2 Answers2

23

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 and false branch and contains a function that does the test:

class DecisionQuery : Decision {
  public Decision Positive { get; set; }
  public Decision Negative { get; set; }
  // Primitive operation to be provided by the user
  public Func<Client, bool> Test { get; set; }

  public override bool Evaluate(Client client) {
    // Test a client using the primitive operation
    bool res = Test(client);
    // Select a branch to follow
    return res ? Positive.Evaluate(client) : Negative.Evaluate(client);
  }
}

Here, Decision is a base class that contains Evaluate method and the source contains one additional derived type that contains a final decision of the tree (yes/no). The type Client is a sample input data that you're analysing using the tree.

To create a decision tree, you can write something like:

var tree = new DecisionQuery {
    Test = (client) => client.Income > 40000,
    Positive = otherTree,
    Negative = someOtherTree
  };

If you just want to write five nested static if clauses then maybe just writing if 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.

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • Sounds interesting. I will check it out and tell you if it feets for me. – Chen Oct 08 '10 at 11:23
  • 4
    The code is no longer available at http://code.msdn.microsoft.com/realworldfp/. Could you post it somewhere else? – blueshift Oct 01 '15 at 15:19
  • [Here](https://web.archive.org/web/20131220123506/http://archive.msdn.microsoft.com/realworldfp) is a snapshot from Archive.org – chrisv Feb 21 '18 at 10:04
  • This makes unit testing really easy. But what happens when nested conditionals grow a lot? How can you write such flow keeping a good readability of the code? – xleon Apr 17 '22 at 23:29
2

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 .

// Section 8.4.2 Decision trees in C#

// Listing 8.15 Object oriented decision tree (C#)

abstract class Decision {
  // Tests the given client 
  public abstract void Evaluate(Client client);
}

class DecisionResult : Decision {
  public bool Result { get; set; }
  public override void Evaluate(Client client) {
    // Print the final result
    Console.WriteLine("OFFER A LOAN: {0}", Result ? "YES" : "NO");
  }
}


// Listing 8.16 Simplified implementation of Template method
class DecisionQuery : Decision {
  public string Title { get; set; }
  public Decision Positive { get; set; }
  public Decision Negative { get; set; }
  // Primitive operation to be provided by the user
  public Func<Client, bool> Test { get; set; }

  public override void Evaluate(Client client) {
    // Test a client using the primitive operation
    bool res = Test(client);
    Console.WriteLine("  - {0}? {1}", Title, res ? "yes" : "no");
    // Select a branch to follow
    if (res) Positive.Evaluate(client);
    else Negative.Evaluate(client);
  }
}

static void MainDecisionTrees()
{
  // The tree is constructed from a query
  var tree =
      new DecisionQuery
      {
        Title = "More than $40k",
        // Test is specified using a lambda function
        Test = (client) => client.Income > 40000,
        // Sub-trees can be 'DecisionResult' or 'DecisionQuery'
        Positive = new DecisionResult { Result = true },
        Negative = new DecisionResult { Result = false }
      };

  // Test a client using this tree
  // Create client using object initializer
  var john = new Client {
      Name = "John Doe", Income = 40000, YearsInJob = 1,
      UsesCreditCard = true, CriminalRecord = false 
    };
  tree.Evaluate(john);
}

private static void Main(string[] args)
{
  MainDecisionTrees();
}
Community
  • 1
  • 1
Stefano Spinucci
  • 554
  • 6
  • 13