Java 梅森找到摩尔的邻居

Java 梅森找到摩尔的邻居,java,agent-based-modeling,mason-abm,Java,Agent Based Modeling,Mason Abm,我正在和梅森做一个模拟。我有一个SparseGrid2D对象,我在我的主模拟类中按如下方式填充该对象: protonLayer = new SparseGrid2D(HEIGHT, WIDTH); MersenneTwisterFast g = new MersenneTwisterFast(); for(int i = 0; i < NUM_PROTONS; i++) { // Creating the proton

我正在和梅森做一个模拟。我有一个SparseGrid2D对象,我在我的主模拟类中按如下方式填充该对象:

protonLayer = new SparseGrid2D(HEIGHT, WIDTH);

        MersenneTwisterFast g = new MersenneTwisterFast();

        for(int i = 0; i < NUM_PROTONS; i++) {

            // Creating the proton
            Proton p = new Proton(new Int2D(g.nextInt(WIDTH), g.nextInt(HEIGHT)), this);

            // Adding it to schedule
            schedule.scheduleRepeating(p);

        }
但是,出于某种原因,neight.size()会一直返回一个单元格,而不是返回所有相邻的单元格。我认为GetMooreNeights排除了空的相邻单元格,因此我添加了:

// Moore locations doesn't return empty grids .-.
        for(int i = 0; i < WIDTH; i++) {

            for(int j = 0; j < HEIGHT; j++) {

                protonLayer.setObjectLocation(new String("a"), i, j);

            }


        }
//摩尔位置不会返回空网格。-。
对于(int i=0;i
到我的主类尝试解决这个问题,但即使每个单元格现在都至少填充了一个对象,它也没有起作用

有什么想法吗

代码的完整版本:

protonest.java

package protontest;

import ec.util.MersenneTwisterFast;
import sim.engine.SimState;
import sim.field.grid.SparseGrid2D;
import sim.util.Int2D;

public class ProtonTest extends SimState{

    public SparseGrid2D protonLayer;
    public final int HEIGHT = 100;
    public final int WIDTH = 100;
    public final int NUM_PROTONS = 1;

    public ProtonTest(long seed) {

        super(seed);

    }

    public void start() {

        protonLayer = new SparseGrid2D(HEIGHT, WIDTH);

        MersenneTwisterFast g = new MersenneTwisterFast();

        for(int i = 0; i < NUM_PROTONS; i++) {

            // Creating the proton
            Proton p = new Proton(new Int2D(g.nextInt(WIDTH), g.nextInt(HEIGHT)), this);

            // Adding it to schedule
            schedule.scheduleRepeating(p);

        }

        // Moore locations doesn't return empty grids .-.
        for(int i = 0; i < WIDTH; i++) {

            for(int j = 0; j < HEIGHT; j++) {

                protonLayer.setObjectLocation(new String("a"), i, j);

            }

        }

    }

    public static void main(String[] args) {

        doLoop(ProtonTest.class, args);
        System.exit(0);

    }

}
package协议测试;
导入ec.util.mersennetwesterfast;
导入sim.engine.SimState;
导入sim.field.grid.SparseGrid2D;
导入sim.util.Int2D;
公共类协议测试扩展了SimState{
公共SparseGrid2D原型层;
公共最终内部高度=100;
公共最终整数宽度=100;
公共最终整数质子=1;
公共协议测试(长种子){
超级(种子);
}
公开作废开始(){
protonLayer=新SparseGrid2D(高度、宽度);
MersenneTwisterFast g=新的MersenneTwisterFast();
对于(int i=0;i
Proton.java

package protontest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.lowagie.text.pdf.hyphenation.TernaryTree.Iterator;

import ec.util.MersenneTwisterFast;
import sim.engine.SimState;
import sim.engine.Steppable;
import sim.util.Bag;
import sim.util.Int2D;
import sim.util.IntBag;

public class Proton implements Steppable {

    public Int2D location;

    // Building the proton and adding it to a random location
    public Proton(SimState state) {

        ProtonTest s  = (ProtonTest) state;

        MersenneTwisterFast g = new MersenneTwisterFast();

        // Random location
        int xloc = g.nextInt(s.WIDTH);
        int yloc = g.nextInt(s.HEIGHT);

        location = new Int2D(xloc, yloc);

        // Add object
        s.protonLayer.setObjectLocation(this, location);


    }

    // Building the proton and setting it at a given location
    public Proton(Int2D location, SimState state) {

        this.location = location;

        ProtonTest s = (ProtonTest) state;

        s.protonLayer.setObjectLocation(this, location);


    }

    // Same as constructor above, but takes two ints instead of an Int2D object
    public Proton(int xloc, int yloc, SimState state) {

        this(new Int2D(xloc, yloc), state);

    }

    // Stepping about
    public void step(SimState state) {

        ProtonTest s = (ProtonTest) state;

        MersenneTwisterFast g = new MersenneTwisterFast();

        // First thing first, does the proton stabilize independently?

        int p = 100; // Proton will stabilize (hence be removed from the simulation) with probability of 1/p

        int rand = g.nextInt(p);

        // If rand = 0 then it met the 1/p probability fo being removed. 
        if(rand == 0) {

            s.protonLayer.remove(this);
            return;

        }

        // Get moore neighbors 
        Bag neigh = s.protonLayer.getMooreNeighbors(location.getX(), location.getY(), 1, 1, new Bag(), new IntBag(), new IntBag());



        // Now we make a map that maps a location to an int. The int will be increased for every proton found at such location in the neighborhood.
        Map<Int2D, Integer> m = new HashMap<Int2D, Integer>();
        ArrayList<Int2D> a = new ArrayList<Int2D>();


        // Looping through neighbors
        for(Object o : neigh) {

            // The "Proton Neighbor"
            Proton pN = (Proton) o;

            // Location of current element
            Int2D thisLocation = pN.location;

            if(!m.containsKey(location)) {

                m.put(location, 1);
                // There probably is a better way of iterating through maps
                a.add(location);

            } else {

                // Everybody loves casting
                int newCounter = ((int) m.get(location)) + 1;

                m.put(location, (Integer) newCounter);


            }           

        }

        // Wait, we also want to know how many protons in the current cell
        int pAtLocation = s.protonLayer.numObjectsAtLocationOfObject(this);

        // Get minimum value from map (ie location where there are fewer protons in neighborhood)

        int currentMin = pAtLocation;
        Int2D location = this.location;

        // For each neighbor
        for(Int2D loc : a) {

            // Get number of protons
            int currentM = (int) m.get(loc);

            // If there are fewer then the proton will migrate there, unless a better locationis found
            if(currentM <= currentMin) {

                currentMin = currentM;

                location = loc;


            }


        }

        // Move the proton to the new location aaaaaand done
        s.protonLayer.setObjectLocation(this, location);
        System.out.println(neigh.size());


    }

}
package协议测试;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.Map;
导入com.lowagie.text.pdf.hyphenation.TernaryTree.Iterator;
导入ec.util.mersennetwesterfast;
导入sim.engine.SimState;
输入sim.engine.Steppable;
进口sim.util.Bag;
导入sim.util.Int2D;
导入sim.util.IntBag;
公共类实现可步进{
公共场所;
//构建质子并将其添加到随机位置
公共质子(模拟州){
ProtonTest s=(ProtonTest)状态;
MersenneTwisterFast g=新的MersenneTwisterFast();
//随机位置
int xloc=g.nextInt(s.WIDTH);
int yloc=g.nextInt(s.HEIGHT);
位置=新的Int2D(xloc、yloc);
//添加对象
s、 setObjectLocation(这个,位置);
}
//构建质子并将其设置在给定位置
公共质子(Int2D位置,模拟州){
这个位置=位置;
ProtonTest s=(ProtonTest)状态;
s、 setObjectLocation(这个,位置);
}
//与上面的构造函数相同,但接受两个int而不是一个Int2D对象
公共质子(int xloc、int yloc、SimState州){
这(新的Int2D(xloc,yloc),州);
}
//走来走去
公共无效步骤(模拟状态状态){
ProtonTest s=(ProtonTest)状态;
MersenneTwisterFast g=新的MersenneTwisterFast();
//首先,质子是否独立稳定?
int p=100;//质子将以1/p的概率稳定(因此从模拟中删除)
int rand=g.nextInt(p);
//如果rand=0,则它符合被删除的1/p概率。
如果(rand==0){
s、 protonLayer.remove(这个);
返回;
}
//找摩尔的邻居
Bag neigh=s.protonLayer.getmooreneights(location.getX(),location.getY(),1,1,new Bag(),new IntBag(),new IntBag());
//现在我们制作一张地图,将一个位置映射到一个整数。对于在附近这样的位置发现的每一个质子,整数都会增加。
Map m=新的HashMap();
ArrayList a=新的ArrayList();
//周游邻国
用于(对象o:嘶鸣){
//“质子邻居”
质子pN=(质子)o;
//当前元素的位置
Int2D thisLocation=pN.location;
如果(!m.containsKey(位置)){
m、 放置(位置,1);
//也许有一种更好的方法可以遍历地图
a、 添加(位置);
}否则{
//每个人都喜欢演员
int newCounter=((int)m.get(location))+1;
m、 put(位置,(整数)newCounter);
}           
}
//等等,我们还想知道当前细胞中有多少质子
int pAtLocation=s.protonLayer.numObjectsAtLocationOfObject(this);
//从地图上获取最小值(即附近质子较少的位置)
int currentMin=pAtLocation;
Int2D位置=此位置;
//每个邻居
对于(Int2D loc:a){
//得到质子的数目
int currentM=(int)m.get(loc);
//如果数量较少,质子就会迁移到那里,除非找到更好的位置
如果(当前m
IntBag xneights=新的IntBag(8);
IntBag yNeighbors=新的IntBag(8);
//找摩尔的邻居
Bag neigh=s.protonLayer.getmooreneights(location.getX(),location.getY(),1,Grid2D.BOUNDED,new Bag(),xneights,yNeighbors);
对于(int i=0;iSystem.out.println(“:y=“+yNeighbors.get(i));}
您是否尝试过其他模式:Grid2D.BOUNDED?
package protontest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.lowagie.text.pdf.hyphenation.TernaryTree.Iterator;

import ec.util.MersenneTwisterFast;
import sim.engine.SimState;
import sim.engine.Steppable;
import sim.util.Bag;
import sim.util.Int2D;
import sim.util.IntBag;

public class Proton implements Steppable {

    public Int2D location;

    // Building the proton and adding it to a random location
    public Proton(SimState state) {

        ProtonTest s  = (ProtonTest) state;

        MersenneTwisterFast g = new MersenneTwisterFast();

        // Random location
        int xloc = g.nextInt(s.WIDTH);
        int yloc = g.nextInt(s.HEIGHT);

        location = new Int2D(xloc, yloc);

        // Add object
        s.protonLayer.setObjectLocation(this, location);


    }

    // Building the proton and setting it at a given location
    public Proton(Int2D location, SimState state) {

        this.location = location;

        ProtonTest s = (ProtonTest) state;

        s.protonLayer.setObjectLocation(this, location);


    }

    // Same as constructor above, but takes two ints instead of an Int2D object
    public Proton(int xloc, int yloc, SimState state) {

        this(new Int2D(xloc, yloc), state);

    }

    // Stepping about
    public void step(SimState state) {

        ProtonTest s = (ProtonTest) state;

        MersenneTwisterFast g = new MersenneTwisterFast();

        // First thing first, does the proton stabilize independently?

        int p = 100; // Proton will stabilize (hence be removed from the simulation) with probability of 1/p

        int rand = g.nextInt(p);

        // If rand = 0 then it met the 1/p probability fo being removed. 
        if(rand == 0) {

            s.protonLayer.remove(this);
            return;

        }

        // Get moore neighbors 
        Bag neigh = s.protonLayer.getMooreNeighbors(location.getX(), location.getY(), 1, 1, new Bag(), new IntBag(), new IntBag());



        // Now we make a map that maps a location to an int. The int will be increased for every proton found at such location in the neighborhood.
        Map<Int2D, Integer> m = new HashMap<Int2D, Integer>();
        ArrayList<Int2D> a = new ArrayList<Int2D>();


        // Looping through neighbors
        for(Object o : neigh) {

            // The "Proton Neighbor"
            Proton pN = (Proton) o;

            // Location of current element
            Int2D thisLocation = pN.location;

            if(!m.containsKey(location)) {

                m.put(location, 1);
                // There probably is a better way of iterating through maps
                a.add(location);

            } else {

                // Everybody loves casting
                int newCounter = ((int) m.get(location)) + 1;

                m.put(location, (Integer) newCounter);


            }           

        }

        // Wait, we also want to know how many protons in the current cell
        int pAtLocation = s.protonLayer.numObjectsAtLocationOfObject(this);

        // Get minimum value from map (ie location where there are fewer protons in neighborhood)

        int currentMin = pAtLocation;
        Int2D location = this.location;

        // For each neighbor
        for(Int2D loc : a) {

            // Get number of protons
            int currentM = (int) m.get(loc);

            // If there are fewer then the proton will migrate there, unless a better locationis found
            if(currentM <= currentMin) {

                currentMin = currentM;

                location = loc;


            }


        }

        // Move the proton to the new location aaaaaand done
        s.protonLayer.setObjectLocation(this, location);
        System.out.println(neigh.size());


    }

}