如何在C#中实现交互式决策树

用户名

我需要允许用户通过在屏幕上显示的两个简单选项之间进行选择来选择自己的路径,以便前进到下一组选项,直到他们到达其中一个结尾,即应该实现以下目的:

在此处输入图片说明

我尝试了以下代码,但是每次仅评估左侧。我想知道如何获得如上图所示的结果(覆盖所有分支)?例如,如果用户选择“否”,则应用程序不应再向用户提出任何其他问题,而只是显示“也许您想要比萨饼”消息。我已经用决策树算法做到了这一点,需要对其进行修复,以使其像上面的图像一样覆盖左右两侧。

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            var decisionTree = MainDecisionTree();

            var client = new Client();

            Console.WriteLine("Do you want a book? true/false");
            client.Answer[0] = bool.Parse(Console.ReadLine());
            Console.WriteLine("Do you like it? true/false");
            client.Answer[1] = bool.Parse(Console.ReadLine());
            Console.WriteLine("Are you sure? true/false");
            client.Answer[2] = bool.Parse(Console.ReadLine());

            decisionTree.Evaluate(client);

            Console.WriteLine("Press any key...");
            Console.ReadKey();
        }

        private static DecisionQuery MainDecisionTree()
        {
            //Decision 2
            var wantBranch = new DecisionQuery
            {
                Title = "Do you want a book?",
                Test = (client) => client.Answer[0],
                Positive = new DecisionResult { Result = true },
                Negative = new DecisionResult { Result = false }
            };

            //Decision 1
            var deserveBranch = new DecisionQuery
            {
                Title = "Do you like it?",
                Test = (client) => client.Answer[1],
                Positive = wantBranch,
                Negative = new DecisionResult { Result = false }
            };


            //Decision 0
            var sureBranch = new DecisionQuery
            {
                Title = "Are you sure?",
                Test = (client) => client.Answer[2],
                Positive = deserveBranch,
                Negative = new DecisionResult { Result = false }
            };

            return sureBranch;
        }
    }

    public class DecisionResult : Decision
    {
        public bool Result { get; set; }

        public override void Evaluate(Client client)
        {
            Console.WriteLine("\r\nThe result: {0}", Result ? "Buy it" : "You need to wait");
        }
    }

    public class DecisionQuery : Decision
    {
        public string Title { get; set; }
        public Decision Positive { get; set; }
        public Decision Negative { get; set; }
        public Func<Client, bool> Test { get; set; }

        public override void Evaluate(Client client)
        {
            bool result = this.Test(client);
            string resultAsString = result ? "yes" : "no";

            Console.WriteLine($"\t- {this.Title}? {resultAsString}");

            if (result) this.Positive.Evaluate(client);
            else this.Negative.Evaluate(client);
        }
    }

    public abstract class Decision
    {
        public abstract void Evaluate(Client client);
    }

    public class Client
    {
        public bool[] Answer { get; set; } = new bool[3];
    }
}
奥利维尔·罗吉尔(Olivier Rogier)

如果我了解您的问题,请更正您的代码。

我重命名了一些东西。

我调用MakeDecisionTree了使用条件树初始化专家系统的方法,该方法返回根条件。

每个都condition包含一个sentenceto evaluate,并且可以是aquery或a result

对于a resultevaluate显示sentence

对于a query,该evaluate方法要求用户通过yes回答问题no并使用此答案,它调用evaluate下一个child的对应condition

很抱歉,我的英语不是我的母语,并且我没有参与AI。

static private void DecisionTreeTest()
{
  Console.WriteLine("Please, answer a few questions with yes or no.");
  Console.WriteLine();
  MakeDecisionTree().Evaluate();
}
static private bool GetUserAnswer(string question)
{
  Console.WriteLine(question);
  string userInput;
  while ( true )
  {
    userInput = Console.ReadLine().ToLower();
    if ( userInput == "yes" )
      return true;
    else
    if ( userInput == "no" )
      return false;
    else
      Console.WriteLine("Your answer is not supported, retry please." +
                        Environment.NewLine + Environment.NewLine +
                        question);
  }
}
static private DecisionTreeQuery MakeDecisionTree()
{
  var queryAreYouSure
    = new DecisionTreeQuery("Are you sure?",
                            new DecisionTreeResult("Buy it."),
                            new DecisionTreeResult("You need to wait."),
                            GetUserAnswer);
  var queryIsItAGoodBook
    = new DecisionTreeQuery("Is it a good book?",
                            new DecisionTreeResult("What are you waiting for? Just buy it."),
                            new DecisionTreeResult("Find another one."),
                            GetUserAnswer);
  var queryDoYouLikeIt
    = new DecisionTreeQuery("Do you like it?",
                            queryAreYouSure,
                            queryIsItAGoodBook,
                            GetUserAnswer);
  var queryDoYouWantABook
    = new DecisionTreeQuery("Do you want a book?",
                            queryDoYouLikeIt,
                            new DecisionTreeResult("Maybe you want a pizza."),
                            GetUserAnswer);
  return queryDoYouWantABook;
}
abstract public class DecisionTreeCondition
{
  protected string Sentence { get; private set; }
  abstract public void Evaluate();
  public DecisionTreeCondition(string sentence)
  {
    Sentence = sentence;
  }
}
public class DecisionTreeQuery : DecisionTreeCondition
{
  private DecisionTreeCondition Positive;
  private DecisionTreeCondition Negative;
  private Func<string, bool> UserAnswerProvider;
  public override void Evaluate()
  {
    if ( UserAnswerProvider(Sentence) )
      Positive.Evaluate();
    else
      Negative.Evaluate();
  }
  public DecisionTreeQuery(string sentence,
                           DecisionTreeCondition positive,
                           DecisionTreeCondition negative,
                           Func<string, bool> userAnswerProvider)
    : base(sentence)
  {
    Positive = positive;
    Negative = negative;
    UserAnswerProvider = userAnswerProvider;
  }
}
public class DecisionTreeResult : DecisionTreeCondition
{
  public override void Evaluate()
  {
    Console.WriteLine(Sentence);
  }
  public DecisionTreeResult(string sentence)
    : base(sentence)
  {
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Java中的Apache Spark中的决策树实现问题

来自分类Dev

如何在R中为决策树模型创建收益图?

来自分类Dev

在C#中运行交互式python脚本

来自分类Dev

如何存储决策树

来自分类Dev

如何在PhysicsJS中设置交互式和非交互式对象?

来自分类Dev

如何在R中运行c5.0决策树和基于规则的模型时提取错误率?

来自分类Dev

SPARK:如何为LabeledPoint中的决策树创建categoricalFeaturesInfo?

来自分类Dev

重复的行如何影响决策树?

来自分类Dev

sklearn中的交叉验证+决策树

来自分类Dev

CART决策树中的拆分冲突

来自分类Dev

在决策树中解释数字

来自分类Dev

如何重塑决策树的数据?

来自分类Dev

在决策树中查找到决策边界的距离

来自分类Dev

Python中的基本决策树内容

来自分类Dev

Python中的基本决策树

来自分类Dev

如何在Jupyter Notebook中添加交互式绘图?

来自分类Dev

交互式树命令

来自分类Dev

在C#中运行交互式python脚本

来自分类Dev

如何在交互式shell中访问字典的字典

来自分类Dev

无法在R中创建决策树

来自分类Dev

用Java实现决策树数据库

来自分类Dev

排序算法中的决策树分析

来自分类Dev

如何获取R中的rpart(CART)决策树的属性?

来自分类Dev

如何在R中的决策树中指定分支数

来自分类Dev

决策树深度

来自分类Dev

决策树中的递归编程

来自分类Dev

在 sklearn 中,one-hot encoding 如何在构建具有分类特征的决策树时提供帮助?

来自分类Dev

如何在ignite中保存决策树训练模型?

来自分类Dev

如何显示此 scikit-learn 决策树脚本的图形决策树?