Print

Print


Hi Sean, I realized after I left work that I should have attached the
file, but here is a slightly older version that does exactly the same
thing (but doesn't filter to remove duplicates) with some prints to
show the results... and yes, SparseGrid2D is being used.


    protected void determineNeighbors(int range) {
        neighbors.clear();

        // Preallocate bag sizes to try and speed up performance...
        Bag neighborBag = new Bag( (int) Math.pow(2 * (range + 1), 2) );
        IntBag xs = new IntBag( (int) Math.pow(2 * (range + 1), 2) );
        IntBag ys = new IntBag( (int) Math.pow(2 * (range + 1), 2) );

        state.agentscape.getNeighborsMaxDistance(
                this.getLocation().x,
                this.getLocation().y,
                range, true, neighborBag, xs, ys);

        System.out.print(neighborBag.numObjs + ": ");
        for(Object o : neighborBag) {
            System.out.print(((SimpleFirmAgent) o).getId() + ", ");
        }
        System.out.println();

        // Randomize the order of the neighbors to ensure that interactions
        // aren't predictable.
        neighborBag.shuffle(state.random);

        // Cast the bag to a list of agents and ensure that an agent it not its
        // own neighbor. This casting makes it a lot easier to do other things.
        for(Object o : neighborBag) {
            if((SimpleFirmAgent) o != this)   // don't add THIS agent
                neighbors.add(new Neighbor((SimpleFirmAgent) o));
        }

        System.out.println("Agent ID " + this.id + ": " +
neighbors.size() + ";; " + neighbors);  // Neighbor.toString prints
the neighbor agent's ID
    }


And this is the output:


45: 9, 9, 9, 7, 7, 3, 3, 2, 5, 2, 5, 8, 8, 0, 1, 6, 0, 1, 6, 4, 4, 9,
9, 9, 7, 7, 3, 3, 2, 5, 2, 5, 8, 8, 0, 1, 6, 0, 1, 6, 4, 4, 9, 9, 9,
Agent ID 9: 36;; [8, 4, 5, 0, 4, 6, 4, 0, 3, 3, 4, 2, 2, 1, 7, 7, 6,
2, 8, 8, 7, 1, 8, 3, 5, 2, 0, 1, 7, 0, 3, 6, 1, 5, 6, 5]
49: 9, 4, 9, 4, 9, 7, 7, 3, 3, 2, 5, 2, 5, 2, 8, 8, 0, 1, 6, 0, 1, 6,
9, 4, 9, 4, 9, 7, 7, 3, 3, 2, 5, 2, 5, 2, 8, 8, 0,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eighborsMaxDistance is doing something a bit odd...
In a world with 10 agents, they can have 40-50 neighbors?




On Thu, Aug 9, 2012 at 7:38 PM, Sean Luke <[log in to unmask]> wrote:
> Christopher, I'd need a concrete and simple example of this.  I presume you're referring to SparseGrid2D.
>
> getNeighborsMaxDistance(x, y, dist, toroidal, result, xPos, yPos) just calls getNeighborsMaxDistance(x, y, dist, toroidal, xPos, yPos), which returns all the <X,Y> locations that are [dist] away from <x,y>.  Then it just looks up the hash for each location and returns the objects in that hash.  It's a pretty straightforwrd function.  The only ways it could possibly return multiple objects would be:
>
> - A given <X,Y> location is being returned multiple times.  This might be possible in the toroidal code -- are you doing toroidal? -- but it seems unlikely in the non-toroidal code on first glance.
>
> - The hash is broken (unlikely).
>
> Sean
>
> On Aug 9, 2012, at 7:08 PM, Chris Hollander wrote:
>
>> So, I'm not sure if this is a bug or intended... and it's been around
>> for a LONG time, so it might be fixed already on the SVN... but when
>> you call getNeighborsMaxDistance with a sufficiently large distance
>> (say, the size of the grid width or height), you get duplicates in the
>> returned bag.
>>
>> So if I have 10 agents with a unique ID 1, 2, .. 10 on a 10x10 grid
>> and I call getNeighborsMaxDistance with a radius of 10, I'll get back
>> a bag that has multiple copies of each agent... which means I can't
>> exactly iterate over the raw bag. Of course, this is easy enough to
>> handle by simply dumping all the elements in the bag in to a set, but
>> should that have to be done? Shouldn't the returned list of neighbors
>> be guaranteed to not contain duplicates?
>>
>>
>> Christopher Hollander
>> University of Central Florida