0

ボックスごとに 1 つのオブジェクト (num_objects) のカウントを持つ 6 つのボックスのリスト (boxlist) があります。各ボックスは地理コンテキストに存在します (特定の緯度と経度の座標にあります)。

    //iterate through each of the boxes in the list (6)
    for (int i = 0; i < boxlist.size(); i++){
        //get the first box... then second box... etc (call it target box)
        Box targetbox = boxlist.get(i);
        Context context = ContextUtils.getContext(targetbox);
        Geography<Object> geography = (Geography)context.getProjection("Geography");
        Geometry geom = geography.getGeometry(targetbox);
        //get the coordinates of the target box
        Coordinate coord = geom.getCoordinates()[0];

        //for each of the 6 boxes, get the number of objects in the target box
        double num = targetbox.getNum_objects();
        // print the number of objects in each box (1)
        System.out.println(num);

        //create random utility
        Random random = new Random();
        // create the same number of BoxObjects as the num_objects in the box and place them in the geography at the same location as the box they are in (take them out of the box)
        for (int j = 0; j < num; j++) {
            boolean randomBoolean = random.nextBoolean();
            boolean anotherBoolean = false;
            BoxObject obj = new BoxObject(context, geography, randomBoolean, anotherBoolean);
            context.add(obj);
            // move to the object to the same coordinates of its box
            geography.move(obj, new GeometryFactory().createPoint(coord));
        }   

  } 

私のループは、6 つのオブジェクトを作成する必要があるボックス内に 1 つのオブジェクトを含む 6 つのボックスがあることを正しくカウントしていますが、12 個のオブジェクトを作成しています。どうすればこれを修正できますか?

注: 実際のシミュレーションでは、ボックスに複数のオブジェクトが含まれる場合があります。

4

2 に答える 2

1

私の問題は、このコードが Agent クラスにネストされていたことです。私は 2 つのエージェントを持っていて、エージェントごとに 1 回ずつ、メソッドを 2 回実行していました (2 倍のオブジェクトを生成します)。このコードをメイン モデル クラスに移動したところ (技術的にはエージェントとは関係がないため)、完全に機能しました。

于 2015-02-26T21:43:24.360 に答える
0

オブジェクトが入ったターゲットボックスがあります。ボックス targetbox = boxlist.get(i);

また、オブジェクトを含む targetbox のコンテキストも取得します。コンテキスト context = ContextUtils.getContext(targetbox);

次に、同じコンテキストに別のオブジェクトを追加します。BoxObject が何をするにしても、コンテキストを渡すと、以前のオブジェクトをコンテキストから削除して、新しく作成されたオブジェクトのみが結果に含まれるようにする必要があります。

于 2015-02-26T19:45:42.607 に答える