首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用c#实现决策树(visual studio 2008) -帮助

如何用c#实现决策树(visual studio 2008) -帮助
EN

Stack Overflow用户
提问于 2010-10-08 17:30:34
回答 2查看 24.1K关注 0票数 19

我有一个决策树,我需要将其转换为C#中的代码

简单的方法是使用if-else语句,但在这个解决方案中,我需要创建4-5个嵌套条件。

我正在寻找一种更好的方法,到目前为止,我读到了一些关于规则引擎的知识。

对于开发具有4-5个嵌套条件的决策树的有效方法,您还有什么建议吗?

EN

回答 2

Stack Overflow用户

发布于 2010-10-08 18:08:11

我在我的书中实现了一个简单的决策树作为示例。代码可以在online here上找到,所以也许您可以将其作为一种灵感。决策实质上表示为一个类,该类引用了true分支和false分支,并包含一个执行测试的函数:

代码语言:javascript
运行
复制
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);
  }
}

在这里,Decision是一个包含Evaluate方法的基类,而源包含一个额外的派生类型,该派生类型包含树的最终决策(是/否)。Client类型是使用树分析的示例输入数据。

要创建决策树,可以编写如下代码:

代码语言:javascript
运行
复制
var tree = new DecisionQuery {
    Test = (client) => client.Income > 40000,
    Positive = otherTree,
    Negative = someOtherTree
  };

如果您只想编写五个嵌套的静态if子句,那么也许只编写if就可以了。使用这种类型的好处是你可以很容易地构建树-例如重用树的一部分或模块化结构。

票数 23
EN

Stack Overflow用户

发布于 2016-08-18 00:05:48

下面是答案https://stackoverflow.com/a/3889544/5288052中提到的Tomas Petricek的代码。

包含“真正的函数式编程”这本书的所有源代码的压缩文件可以在https://www.manning.com/books/real-world-functional-programming上找到。

代码语言:javascript
运行
复制
// 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();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3889301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档