Print

Print


Another solution to the shuffle is to use a class that wraps a
MersenneTwisterFast and extends Random. Like so:

class MTFWrapper extends Random
    {
    MersenneTwisterFast rng;

    public MTFWrapper(MersenneTwisterFast r) { rng = r; }
    public synchronized void setSeed(long seed) { rng.setSeed(seed); }
    public synchronized int nextInt() {  return rng.nextInt(); }
    public synchronized int nextInt(int n) { return rng.nextInt(n); }
    public synchronized long nextLong() { return rng.nextLong(); }
    public synchronized boolean nextBoolean() { return rng.nextBoolean(); }
    public synchronized float nextFloat() { return rng.nextFloat(); }
    public synchronized double nextDouble() { return rng.nextDouble(); }
    public synchronized void nextBytes(byte[] bytes) {
rng.nextBytes(bytes); }
    public synchronized double nextGaussian()  { return rng.nextGaussian();
}
    }

Then you can shuffle any collection you want:
Collections.shuffle(agentList, new MTFWrapper(state.random));

Another method still is to write your own shuffle function:

    public static void shuffle(List list, MersenneTwisterFast mtf)
        {
        int numObjs = list.size();
        Object obj;
        int rand;

        for(int x=numObjs-1; x >= 1 ; x--)
            {
            rand = mtf.nextInt(x+1);
            obj = list.get(x);
            list.set(x, list.get(rand));
            list.set(rand, obj);
            }
        }

Note: I'm nearly certain Sean wrote this code.

Good luck,
Joey


On Tue, Oct 27, 2015 at 7:29 PM, Joey Harrison <[log in to unmask]>
wrote:

> Another solution to the shuffle is to use a class that wraps a
> MersenneTwisterFast and extends Random. Like so:
>
> class MTFWrapper extends Random
>     {
>     MersenneTwisterFast rng;
>
>     public MTFWrapper(MersenneTwisterFast r) { rng = r; }
>     public synchronized void setSeed(long seed) { rng.setSeed(seed); }
>     public synchronized int nextInt() {  return rng.nextInt(); }
>     public synchronized int nextInt(int n) { return rng.nextInt(n); }
>     public synchronized long nextLong() { return rng.nextLong(); }
>     public synchronized boolean nextBoolean() { return rng.nextBoolean(); }
>     public synchronized float nextFloat() { return rng.nextFloat(); }
>     public synchronized double nextDouble() { return rng.nextDouble(); }
>     public synchronized void nextBytes(byte[] bytes) {
> rng.nextBytes(bytes); }
>     public synchronized double nextGaussian()  { return
> rng.nextGaussian(); }
>     }
>
> Then you can shuffle any collection you want:
> Collections.shuffle(agentList, new MTFWrapper(state.random));
>
> Another method still is to write your own shuffle function:
>
>     public static void shuffle(List list, MersenneTwisterFast mtf)
>         {
>         int numObjs = list.size();
>         Object obj;
>         int rand;
>
>         for(int x=numObjs-1; x >= 1 ; x--)
>             {
>             rand = mtf.nextInt(x+1);
>             obj = list.get(x);
>             list.set(x, list.get(rand));
>             list.set(rand, obj);
>             }
>         }
>
> Note: I'm nearly certain Sean wrote this code.
>
> Good luck,
> Joey
>
> On Sun, Oct 25, 2015 at 12:12 AM, Russell Thomas <
> [log in to unmask]> wrote:
>
>> I have accomplished this by having an łagents" Bag variable in the main
>> class (SimState), and then every time I create a new object, I add it to
>> the łagents" bag.  Then I iterate over all agents simply by looping using
>> get() method.  If your agents ever die, then you also need to remove them
>> from the łagents˛ bag.  This, of course, requires passing the SimState
>> object to what ever object is iterating over all agents.  But, in my case,
>> these objects involved UI, data collection, debug, and other
>> administrative functions, and not objects within the model Iąm simulating.
>>
>> With this method, you never need to be concerned with where they might
>> reside in any other data structure, including ObjectGrid or SparseGrid.
>>
>> Russ
>>
>> On 10/24/15, 9:56 PM, "MASON Multiagent Simulation Toolkit on behalf of
>> Sean Luke" <[log in to unmask] on behalf of
>> [log in to unmask]> wrote:
>>
>> >Iterating over all the objects in an ObjectGrid2D means iterating over
>> >every single
>> >cell in the array.  If you don't have a lot of objects relative to the
>> >cells, this can
>> >be very slow, and it would make more sense to use a SparseGrid2D instead.
>> >
>> >You could use a double for-loop to go through all the objects.  But
>> >elements() isn't too slow, and it's O(1).
>> >
>> >Sean
>> >
>> >On Oct 24, 2015, at 10:37 AM, Axel Kowald <[log in to unmask]> wrote:
>> >
>> >> Hello Together,
>> >>
>> >> I just wonder what the best way is to iterate over all the agent on my
>> >> ObjectGrid2D ?
>> >> Currently I'm extracting a Bag with all agents by using the elements()
>> >> method. It works, but I'm not sure how fast/slow it is. Would it be
>> >> worth to maintain my own Bag and do the bookkeeping by
>> >> inserting/removing agents when needed?
>> >>
>> >> thanx,
>> >>  Axel
>>
>
>