0

ID3 アルゴリズムの簡易版である非バイナリ学習ツリーを作成しようとしています。これを行うために、列挙型階層を教えている参照がいくつかあるため、列挙型を使用しようとしましたが、ツリーを作成するために必要な関数への列挙型の転送に問題があります。ツリーに必要なすべてをできる限りセットアップしましたが、ツリーの初期構築に問題があります。

まず、「main.enumname」をあちこちに記述する必要がないように、それぞれ独自のファイルを持つ 6 つの列挙型を作成しました。これらの最初の 5 つの列挙型は、車の診断を表します。

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}

次に、さらに 2 つの列挙型を作成しました。1 つはさまざまな診断結果用で、もう 1 つは車の診断のさまざまな「トピック」用です。

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

次に、さまざまな車の診断の 5 つのデータ例を作成し、ツリーで並べ替えました。

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 に入れます。これは、これらがツリーを構築するためにランダムに使用されるためです。

//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 の 4 つの子を作成する必要があります。そうすれば、例の音と子供の音を一致させることができます。これはこれまでの私のノードです。

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

}
4

2 に答える 2

0

列挙型の階層を構築するという同様の問題がありました。しかし、私の場合は、クラスの階層でもうまくいく可能性があります。ここに興味がある場合は、私の投稿があります: 列挙型またはその他の方法を使用して Java でカテゴリの階層ツリーを構築する方法は?

さて、私の投稿でわかるように、列挙階層のみに関して、私はあなたのために働くかもしれないこれを見つけました:

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

それが役立つことを願っています!

于 2013-11-25T06:41:57.113 に答える
0

機能に子クラスを追加できます。

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()));
    }
}
于 2012-04-19T18:51:30.117 に答える