ID3 Java Enum Tree

随声附和 提交于 2019-12-20 06:08:37

问题


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.

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);

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);

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;
    }

}

回答1:


you can add a child class to features:

import java.util.*;
interface hasEnumChildren {
    Class clazz();
}
enum fuelstats {
    notempty,empty
}
enum lightstatus {
    Dim,Normal
}
enum scents {
    normal,gas
}
enum soundstatus {
    Normal,Howl,Screech,Click
}
enum turn {
    no,yes
}
enum problems {
    battery,starter,solenoid,outofgas,flooding
}
enum features implements hasEnumChildren {
    lightstatus(lightstatus.class),soundstatus(soundstatus.class),fuelstats(fuelstats.class),scents(scents.class),turn(turn.class),problems(problems.class);
    features(Class clazz) {
        this.clazz=clazz;
    }
    final Class clazz;
    @Override public Class clazz() {
        return clazz;
    }
}
public class So10233099 {
    public static void main(String[] args) {
        System.out.println(Arrays.asList(features.lightstatus.clazz().getEnumConstants()));
    }
}



回答2:


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

In particular:

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;
}

I hope it helps!



来源:https://stackoverflow.com/questions/10233099/id3-java-enum-tree

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!