ID3 Java 枚举树 [英] ID3 Java Enum Tree

查看:26
本文介绍了ID3 Java 枚举树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个非二元学习树,它是 ID3 算法的简化版本.为此,我尝试使用枚举,因为有几个参考资料教授枚举层次结构,但是我在将枚举转移到我需要制作树的函数时遇到了麻烦.我已尽我所能为树设置了所需的一切,但我在树的初始构建过程中遇到了麻烦.

I'm trying to make a non-binary learning tree that's a simplified version of the ID3 algorithm. To do this, I tried to use enums, because there are several references teaching enum hierarchies, but I'm having trouble with the transfer of enums to the functions I need to make the tree. I've set up everything I need for the tree as best as I could, but I'm having trouble with the initial construction of the tree.

首先,我制作了六个枚举,每个都有自己的文件,所以我不需要到处写main.enumname".前五个枚举代表汽车诊断.

First, I made six enums, each with their own file so I wouldn't need to write "main.enumname" everywhere. These first five enums represent car diagnostics.

public enum fuelstats {notempty, empty}
public enum lightstatus {Dim, Normal}
public enum scents {normal, gas}
public enum soundstatus {Normal, Howl, Screech, Click}
public enum turn {no, yes}

接下来,我又做了两个枚举.一种针对不同的诊断结果,一种针对汽车诊断的不同主题".

Next, I made two more enums. One for the different diagnostic results, and one for the different "topics" of car diagnostics.

public enum problems {battery, starter, solenoid, outofgas, flooding}
public enum features {lightstatus, soundstatus, fuelstats, scents, turn, problems}

然后我制作了五个不同汽车诊断的数据示例,以便在树中进行排序.

I then made five data examples of different car diagnostics to be sorted in the tree.

Example example1 = new Example(lightstatus.Dim, soundstatus.Howl, turn.yes, fuelstats.notempty, scents.normal, problems.battery);
Example example2 = new Example(lightstatus.Normal, soundstatus.Screech, turn.no, fuelstats.notempty, scents.normal, problems.starter);
Example example3 = new Example(lightstatus.Normal, soundstatus.Click, turn.no, fuelstats.notempty, scents.normal, problems.solenoid);
Example example4 = new Example(lightstatus.Normal, soundstatus.Normal, turn.yes, fuelstats.empty, scents.normal, problems.outofgas);
Example example5 = new Example(lightstatus.Normal, soundstatus.Normal, turn.yes, fuelstats.notempty, scents.gas, problems.flooding);

//make an array list of Examples.
ArrayList<Example> Examples = new ArrayList<Example>();
Examples.add(example1);

Examples.add(example2);
Examples.add(example3);
Examples.add(example4);
Examples.add(example5);

我将称为 Features 的各种汽车诊断信息放在一个 ArrayList 中以进行混洗,因为它们将随机用于构建树.

I put the various car diagnostics, called Features, in an ArrayList for shuffling purposes, because they will be randomly used to build the tree.

//This ArrayList holds the Enums for shuffling purposes.
ArrayList<features> Features = new ArrayList<features>();

Features.add(features.soundstatus);
Features.add(features.lightstatus);
Features.add(features.turn);
Features.add(features.scents);
Features.add(features.fuelstats);

// Shuffle the elements in the list
Collections.shuffle(Features);

//The Features Array List is now a shuffled tree.
//We will do a single loop that will serve as our stack.

//First we take the top of the list and assign it to the root.
Tree id3 = new Tree(Features.get(0),Examples);

但是我如何写一棵树:接受一个特征枚举,使根的主题与枚举相匹配,而枚举的所有不同状态都是子元素?例如,如果 soundstatus 是根,它应该创建四个孩子,分别是 Normal、Howl、Screech 和 Click.这样我就可以将示例声音与儿童声音相匹配.到目前为止,这是我的节点.

But how do I write a tree that: Takes in a feature enum that makes the subject of the root match the enum, and all of the different statuses of the enum the children? For example, if soundstatus is the root, it should make four children that are Normal, Howl, Screech, and Click. That way I can match the Example sounds with the children sounds. This is my node so far.

public class Node 
{

    ArrayList<Node> children;


    /* Constructor*/
    public Node(ArrayList<Node> ExampleList) 
    { 
        this.ExampleList = ExampleList;
        this.parent = parent;
        this.children = children; 
    }

    public ArrayList<Node> getChildren() 
    { 
        return children; 
    }

    public void addChild(Node n) 
    { 
        children.add(n);
    }

    private ArrayList<Node> children;

    Enum phrase;

    private boolean isUsed;

    Node parent;

    public void setUsed(boolean isUsed) 
    {
        this.isUsed = isUsed;
    }

    public boolean isUsed() 
    {
        return isUsed;
    }
    //This method states if the node is a leaf 
    public boolean isLeaf()
    {
        if (this.getChildren() == null)
        return true;

        else
        return false;
    }

}

推荐答案

我遇到了类似的问题,即构建枚举层次结构.但就我而言,类的层次结构也可以解决问题.如果你有兴趣这里是我的帖子:java - 如何使用枚举或任何其他方式在java中构建类别的层次结构树?

I had a similar problem, building an hierarchy of enums. But in my case, an hierarchy of classes could also do the trick. In case you are interested here is my post: How to build an hierarchy tree of categories in java using enums or any other way?

现在,仅涉及枚举层次结构,正如您在我的帖子中所见,我发现这可能对您有用:

Now, concerning only enum hierarchy, as you can see on my post, I found this that may work for you:

http://alexradzin.blogspot.hk/2010/10/hierarchical-structures-with-java-enums_05.html

特别是:

public enum OsType {
OS(null),
    Windows(OS),
        WindowsNT(Windows),
            WindowsNTWorkstation(WindowsNT),
            WindowsNTServer(WindowsNT),
        Windows2000(Windows),
            Windows2000Server(Windows2000),
            Windows2000Workstation(Windows2000),
        WindowsXp(Windows),
        WindowsVista(Windows),
        Windows7(Windows),
        Windows95(Windows),
        Windows98(Windows),
    Unix(OS) {
            @Override
            public boolean supportsXWindows() {
                return true;
            }
        },
        Linux(Unix),
        AIX(Unix),
        HpUx(Unix),
        SunOs(Unix),
;
private OsType parent = null;

private OsType(OsType parent) {
    this.parent = parent;
}

希望能帮到你!

这篇关于ID3 Java 枚举树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆