首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在javascript中实现决策树。寻找比我丑陋的解决方案更好的解决方案

如何在javascript中实现决策树。寻找比我丑陋的解决方案更好的解决方案
EN

Stack Overflow用户
提问于 2011-12-03 23:28:47
回答 1查看 14.9K关注 0票数 10

我正在寻找一种用javascript实现决策树的更好的方法。作为编程新手,我的工具箱中的工具数量非常有限。我所知道的做这件事的唯一方法是:.with一个巨大的难看的、难以维护和遵循的if if语句,.I可以使用switch/case语句并做状态机类型的事情。

感谢您的建议和理论。此外,小的代码示例将非常有帮助。谢谢你看一看。

戴尔

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-04 00:50:01

如果它是一棵真正的大树,特别是如果它是从数据生成的,那么您可以使用函数方法将决策函数视为数据。例如:

代码语言:javascript
复制
var decisionTree = 
    new Case( true, Array(
                  new Case ( function(n){ return n < 0; }, Math.sin ),
                  new Case ( function(n){ return n < 2; }, "0<= n < 2" ),
                  new Case ( true, "Greater than two " )));

decisionTree.evaluate(1); // evaluates to string "0<= n < 2"
decisionTree.evaluate(-Math.PI/2); // evaluates to -1
decisionTree.evaluate(5); // evaluates to string "Greater than two"

使用此实现,您可以任意嵌套您的树:

代码语言:javascript
复制
// Represents a predicate and corresponding action to take if predicate is a
// match.
//
// predicate : true or Function( object ) returning a boolean.
//
// action : One of Function, Case, Array of Cases or other value (see
//          Case.evaluate as to how each is applied)
//
//
Case = function (predicate, action) {  
    this.predicate = predicate;
    this.action = action;
};


Case.prototype = {
    nomatch : { match : false },
    match : function (v) { return { match : true, result :v }; },


    // Recursively test Cases and applies corresponding action on
    // `object`.
    //
    // The action applied depends on the datatype of `action`:
    //
    // - Function : evaluates to `action( object )`
    // 
    // - Case : A subsequent test is performed.  Evaluates to whatever
    //          the Cases action evaluates to.
    //          
    // - Array of Cases : Subsequent tests are performed.  Evaluates to whatever
    //          the action of the first matching Case evaluates to.
    //
    // - Any other Value : Evaluates to itself
    // 
    // returns object containing fields:
    //
    //     match:  boolean, indicates if Case was a match
    //
    //     result:  result of action applied
    // 
    evaluate : function( object ) {
        var match = this.predicate;

        if ( match instanceof Function )
            match = match( object );

        if ( match ) {

            if (this.action instanceof Function )
                return this.match( this.action(object) );

            if ( this.action instanceof Case )
                return this.action.evaluate( object );

            if ( this.action instanceof Array ) {
                var decision;
                var result;
                for (var c = 0; c < this.action.length; c++ ) {
                    decision = this.action[c];
                    if ( decision instanceof Case )  {
                        result = decision.evaluate( object );
                        if (result.match)
                            return result;
                    } else throw("Array of Case expected");
                }

                return this.nomatch;
            }

            return this.match(this.action);
        } 
        return this.nomatch;
    }
};
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8368698

复制
相关文章

相似问题

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