一步一步学Repast 第四章——分析SimpleModel

标签:, , , , , , , ,

前面我们写的Model都是基于SimModelImpl 的,其实可以有一个更好的父类可以让我们使用,那就是SimpleModel,它也是继承自SimModelImpl,我们来分析下它的源代码

  1. package wxy;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5.  
  6. import cern.jet.random.Uniform;
  7. import cern.jet.random.engine.MersenneTwister;
  8.  
  9. import uchicago.src.sim.engine.BasicAction;
  10. import uchicago.src.sim.engine.Schedule;
  11. import uchicago.src.sim.engine.SimModelImpl;
  12. import uchicago.src.sim.engine.Stepable;
  13. import uchicago.src.sim.util.Random;
  14. import uchicago.src.sim.util.SimUtilities;
  15.  
  16.  
  17. public class SimpleModel extends SimModelImpl ...{
  18.  
  19.   protected Schedule schedule;
  20.   protected ArrayList agentList = new ArrayList();
  21.   protected String name = "A Repast Model";
  22.   protected String[] params = ...{""};
  23.   private double stoppingTime = Double.POSITIVE_INFINITY;
  24.   private BasicAction stoppingAction;
  25.   protected boolean autoStep = false;
  26.   protected boolean shuffle = false;
  27.   protected long seed = 1;
  28.   protected boolean isGui;
  29.   protected long startAt = 1;
  30.  
  31. /**//*下面这些方法我们可以不用管它*/
  32.   public void setStoppingTime(long time) ...{
  33.     setStoppingTime((double) time);
  34.   }
  35.  
  36.   public void setStoppingTime(double time) ...{
  37.     stoppingTime = time;
  38.     if (stoppingAction != null) schedule.removeAction(stoppingAction);
  39.     if (schedule != null) setStopAction();
  40.   }
  41.  
  42.   private void setStopAction() ...{
  43.     stoppingAction = schedule.scheduleActionAt(stoppingTime,
  44.                            this, "stop", Schedule.LAST);
  45.   }
  46.  
  47.   public void setRngSeed(long seed) ...{
  48.         this.seed = seed;
  49.         super.setRngSeed(seed);
  50.         Random.createUniform();
  51.   }
  52.   /**//*上面这些方法我们可以不用管它*/ 
  53.  
  54.   //下面2个是产生随机数的函数,由于模拟对随机要求比较高,RePast是用 colt 里的包来处理相关
  55.   //数字计算的 
  56.   public int getNextIntFromTo(int from, int to) ...{
  57.         return Random.uniform.nextIntFromTo(from, to);
  58.   }
  59.  
  60.  
  61.  public double getNextDoubleFromTo(double from, double to) ...{
  62.         return Random.uniform.nextDoubleFromTo(from, to);
  63.  }
  64.  
  65.   public void setup() ...{
  66.     isGui = !(getController().isBatch());
  67.     stoppingTime = Double.POSITIVE_INFINITY;
  68.     stoppingAction = null;
  69.     schedule = new Schedule();
  70.     agentList = new ArrayList();
  71.     setRngSeed(seed);
  72.     Random.createUniform();
  73.   }
  74.  
  75. //Random.createUniform();实际上就是产生全局随机种子的方法。查看Random的源代码,可以发现下面的代码片段
  76.  
  77. //  public static void createUniform() {
  78. //        uniform = new Uniform(generator);
  79. //  }
  80. //  static {
  81. //        Date d = new Date();
  82. //        rngSeed = d.getTime();
  83. //        generator = new MersenneTwister(d);
  84. //  }
  85. // 
  86. //  这些我们也不用去管,直接使用SimpleModelget.NextIntFromTo()就可以了。
  87.  
  88.  
  89.   public void begin() ...{
  90.     buildModel();
  91.     buildSchedule();
  92.   }
  93.  
  94.   public void buildModel() ...{} //需要在子类里重写
  95.  
  96.   public void buildSchedule() ...{
  97.     if (autoStep) schedule.scheduleActionBeginning(startAt, this,
  98.                            "runAutoStep");
  99.     else schedule.scheduleActionBeginning(startAt, this, "run");
  100.     schedule.scheduleActionAtEnd(this, "atEnd");
  101.     schedule.scheduleActionAtPause(this, "atPause");
  102.     setStopAction();
  103.   } 
  104.  
  105.   //这个方法,子类用不用super.buildSchedule都无所谓,就看你需不需要在按下暂停和开始停止之类的事件里做文章了
  106.  
  107.  
  108.  //下面这些方法我们也可以不用管
  109.  
  110.   public String getName() ...{
  111.     return name;
  112.   }
  113.  
  114.   public Schedule getSchedule() ...{
  115.     return schedule;
  116.   }
  117.  
  118.   public String[] getInitParam() ...{
  119.     return params;
  120.   }
  121.  
  122.  
  123.  
  124.   public void atPause() ...{}
  125.   public void atEnd() ...{}
  126.  
  127.   public void runAutoStep() ...{
  128.     preStep();
  129.     autoStep();
  130.     postStep();
  131.   }
  132.    
  133.   public void run() ...{
  134.     preStep();
  135.     step();
  136.     postStep();
  137.   }
  138.  
  139.   private void autoStep() ...{
  140.     if (shuffle) SimUtilities.shuffle(agentList);
  141.    
  142.     int size = agentList.size();
  143.     for (int i = 0;i < size; i++) ...{
  144.       Stepable agent = (Stepable)agentList.get(i);
  145.       agent.step();
  146.     }
  147.   }  
  148.  
  149.   protected void preStep() ...{}
  150.   protected void step() ...{}
  151.   protected void postStep() ...{}
  152.  
  153. /**//* 综上所分析,我们写一个子类去继承这个父类后,其实只要做3件事情
  154. * 1、buildModel()
  155. * 2、buildSchedule()
  156. * 3、main函数
  157. * 其它事情,在这个父类里已经都完成了,不需要再理会
  158. *
  159. */
  160.    
  161. }






留言