1

ツリー クラスを作成し、コンパレータを実装しました。

class SuitComp implements Comparator<Tree> {

@Override
public int compare(Tree o1, Tree o2) {
    return Double.compare(o1.getSuit(), o2.getSuit());
    }   
}

class Tree {
    private ContinuousSpace<Object> space;
    private Grid<Object> grid;
    public double suitability;
    public int id;
    public static int count = 1;

public Tree(ContinuousSpace<Object> space, Grid<Object> grid, double suitability, int id) {
    this.space = space;
    this.grid = grid;
    this.id = count;
    count++;    
}

public double getSuit() {
    return suitability;
}

public void setSuit(double suitability) {
    this.suitability = suitability;
}

@Override
public String toString() {
    return "Tree " + id + " is the most suitable, with a value of: " + suitability;
}

public void measureSuit() {
    System.out.println("Tree number " + id + " has a suitability of " + suitability);
}

}

そして、それらを Respast Simphony を使用して (グリッド上の地理空間で) コンテキストに入れました。

public class TreeBuilder implements ContextBuilder<Object> {

    @Override
    public Context build(Context<Object> context) {
        context.setId("taylor");
        ContinuousSpaceFactory spaceFactory = 
        ContinuousSpaceFactoryFinder.createContinuousSpaceFactory(null);
        ContinuousSpace<Object> space = 
        spaceFactory.createContinuousSpace("space", context, 
                new RandomCartesianAdder<Object>(), 
                new repast.simphony.space.continuous.WrapAroundBorders(), 
                50, 50);


        GridFactory gridFactory = GridFactoryFinder.createGridFactory(null);
        Grid<Object> grid = gridFactory.createGrid("grid", context, 
                new GridBuilderParameters<Object>(new WrapAroundBorders(), 
                new SimpleGridAdder<Object>(), 
                true, 50, 50));

私が正しければ、以下のコードでは、10 個のツリー オブジェクトを作成し、それらをコンテキストに追加してから、10 個の新しい個別のツリー オブジェクトを作成し、それらを ArrayList に追加しています。

        ArrayList<Tree> trees = new ArrayList<Tree>();

        int treeCount = 10;
        for (int i = 0; i < treeCount; i++) {
            double suitability = Math.random();
            int id = i;
            context.add(new Tree(space, grid, suitability, id));

        Tree tree;

        tree = new Tree(space, grid, suitability, id);
        trees.add(tree);

次に、適合値と最大適合値を出力します。

        tree.measureSuit();


        Tree maxTree = Collections.max(trees, new SuitComp());
        System.out.println(maxTree);

        }


        for (Object obj : context) {
            NdPoint pt = space.getLocation(obj);
            grid.moveTo(obj, (int)pt.getX(), (int)pt.getY());

        }

        return context;

    }

}   

私の質問は: 10 個のツリー オブジェクトをコンテキストに追加し、同じオブジェクトをリストに追加することは可能ですか?

4

1 に答える 1

1

明らかなことを試しましたか?つまり、次のようなものです。

        ArrayList<Tree> trees = new ArrayList<Tree>();

        int treeCount = 10;
        for (int i = 0; i < treeCount; i++) {
            double suitability = Math.random();
            int id = i;
            // We create the Tree...
            Tree tree = new Tree(space, grid, suitability, id);
            // ... then add it to the context
            context.add(tree);
            // ... then add it to the list
            trees.add(tree);

うまくいかない理由がわかりません。しかし、私はRepastSimphonyの経験がないので、間違っている可能性が非常に高いです:(

于 2015-01-03T02:23:33.353 に答える