前面我们写的Model都是基于SimModelImpl 的,其实可以有一个更好的父类可以让我们使用,那就是SimpleModel,它也是继承自SimModelImpl,我们来分析下它的源代码
- package wxy;
- import java.util.ArrayList;
- import java.util.Date;
- import cern.jet.random.Uniform;
- import cern.jet.random.engine.MersenneTwister;
- import uchicago.src.sim.engine.BasicAction;
- import uchicago.src.sim.engine.Schedule;
- import uchicago.src.sim.engine.SimModelImpl;
- import uchicago.src.sim.engine.Stepable;
- import uchicago.src.sim.util.Random;
- import uchicago.src.sim.util.SimUtilities;
- public class SimpleModel extends SimModelImpl ...{
- protected Schedule schedule;
- protected ArrayList agentList = new ArrayList();
- protected String name = "A Repast Model";
- protected String[] params = ...{""};
- private double stoppingTime = Double.POSITIVE_INFINITY;
- private BasicAction stoppingAction;
- protected boolean autoStep = false;
- protected boolean shuffle = false;
- protected long seed = 1;
- protected boolean isGui;
- protected long startAt = 1;
- /**//*下面这些方法我们可以不用管它*/
- public void setStoppingTime(long time) ...{
- setStoppingTime((double) time);
- }
- public void setStoppingTime(double time) ...{
- stoppingTime = time;
- if (stoppingAction != null) schedule.removeAction(stoppingAction);
- if (schedule != null) setStopAction();
- }
- private void setStopAction() ...{
- stoppingAction = schedule.scheduleActionAt(stoppingTime,
- this, "stop", Schedule.LAST);
- }
- public void setRngSeed(long seed) ...{
- this.seed = seed;
- super.setRngSeed(seed);
- Random.createUniform();
- }
- /**//*上面这些方法我们可以不用管它*/
- //下面2个是产生随机数的函数,由于模拟对随机要求比较高,RePast是用 colt 里的包来处理相关
- //数字计算的
- public int getNextIntFromTo(int from, int to) ...{
- return Random.uniform.nextIntFromTo(from, to);
- }
- public double getNextDoubleFromTo(double from, double to) ...{
- return Random.uniform.nextDoubleFromTo(from, to);
- }
- public void setup() ...{
- isGui = !(getController().isBatch());
- stoppingTime = Double.POSITIVE_INFINITY;
- stoppingAction = null;
- schedule = new Schedule();
- agentList = new ArrayList();
- setRngSeed(seed);
- Random.createUniform();
- }
- //Random.createUniform();实际上就是产生全局随机种子的方法。查看Random的源代码,可以发现下面的代码片段
- // public static void createUniform() {
- // uniform = new Uniform(generator);
- // }
- // static {
- // Date d = new Date();
- // rngSeed = d.getTime();
- // generator = new MersenneTwister(d);
- // }
- //
- // 这些我们也不用去管,直接使用SimpleModelget.NextIntFromTo()就可以了。
- public void begin() ...{
- buildModel();
- buildSchedule();
- }
- public void buildModel() ...{} //需要在子类里重写
- public void buildSchedule() ...{
- if (autoStep) schedule.scheduleActionBeginning(startAt, this,
- "runAutoStep");
- else schedule.scheduleActionBeginning(startAt, this, "run");
- schedule.scheduleActionAtEnd(this, "atEnd");
- schedule.scheduleActionAtPause(this, "atPause");
- setStopAction();
- }
- //这个方法,子类用不用super.buildSchedule都无所谓,就看你需不需要在按下暂停和开始停止之类的事件里做文章了
- //下面这些方法我们也可以不用管
- public String getName() ...{
- return name;
- }
- public Schedule getSchedule() ...{
- return schedule;
- }
- public String[] getInitParam() ...{
- return params;
- }
- public void atPause() ...{}
- public void atEnd() ...{}
- public void runAutoStep() ...{
- preStep();
- autoStep();
- postStep();
- }
- public void run() ...{
- preStep();
- step();
- postStep();
- }
- private void autoStep() ...{
- if (shuffle) SimUtilities.shuffle(agentList);
- int size = agentList.size();
- for (int i = 0;i < size; i++) ...{
- Stepable agent = (Stepable)agentList.get(i);
- agent.step();
- }
- }
- protected void preStep() ...{}
- protected void step() ...{}
- protected void postStep() ...{}
- /**//* 综上所分析,我们写一个子类去继承这个父类后,其实只要做3件事情
- * 1、buildModel()
- * 2、buildSchedule()
- * 3、main函数
- * 其它事情,在这个父类里已经都完成了,不需要再理会
- *
- */
- }

留言