logo资料库

Repast 中文教程.pdf

第1页 / 共13页
第2页 / 共13页
第3页 / 共13页
第4页 / 共13页
第5页 / 共13页
第6页 / 共13页
第7页 / 共13页
第8页 / 共13页
资料共13页,剩余部分请下载后查看
3.How to Build a Repast Model - 1 如何构建Repast模型-1 Contents 内容 Overview 概览 SimpleModel 简单模型 Parameters 参数 Naming your Model 命名你的模型 SimpleModel Methods and Instance Variables 简单模型方法和实例变量 Adding Displays etc. 增加显示等 This document describes how to build a Repast model using the SimpleModel class. 这个文档描述如何使用简单模型类来构建一个Repast模型 It is intended as an introduction to writing agent-based models with Repast. 它以介绍用Repast编写基于智体模型为目标 If you have some experience writing agent-based models, you may want to see How to Build a Repast Model - 2 as well. 如果你有过编写基于智体模型的经验,那么你可能也想参考如何构建Repast模型-2 Note: the demonstration model "Life" in repast/demo/life is an example of how to build a Repast model using the SimpleModel class. 注意:在repast/demo/life 中的示范模型“生命”是如何用简单模型类来构建Repast模型的 一个实例 Overview 概述 An agent-based simulation typically proceeds in two stages. 一个基于智体仿真的开始通常有两种方式来 The first is a setup stage that prepares the simulation for running, and the second is the actual running of the simulation. 第一种是设定方式,它为运行仿真做好准备,第二种是实际运行仿真
In Repast simulations the running of the simulation is divided into time steps or "ticks." 在Repast仿真中仿真运行被分为不同的节拍 Each tick some action occurs using the results of previous actions as its basis. 每个节拍在之前行为基础上进一步生成一些行为。 So, for example, if we were building a prisoner's dilemma type simulation with two players, setup would create the two players, and provide each with an initial strategy (tit-for-tat, etc.). 因此,举例来说,如果我们构建包含两个博弈方的一个穷途困境型仿真,设定将创建两 个博弈方,并为每一方提供一个初始战略。 Each tick or time step, each player would play the game (cooperate or defect) where their current play is dependent on their strategy and perhaps on the results of previous play. 在每一节拍,每一方将按照他们的战略和之前博弈结果来进行博弈 What you need to do then to build a simple Repast model is describe what happens during setup, and what occurs every tick. 在构建一个简单Repast模型时,你要做的是描述在设定时要设定什么,和每一节拍产生 什么。 The SimpleModel class provides placeholders for you to do this. 要做这些,简单模型类为你提供了占位符 SimpleModel 简单模型 Repast simulations typically have at least two classes. Repast仿真通常至少有两种“类” An agent class that describes the behavior of your agents (e.g. play a game by cooperating or defecting) and a model class that coordinates the setup and running of the model. 一个“智体类”,用来描述你的智体的行为(如,协作博弈或对抗博弈);另一个“模型 类”,用来协调设定与模型运行 You use SimpleModel as the basis of your model class, specializing it to suit your purposes. 你可以把简单模型作为你的模型类的基础,特化以适合你的目的。 In Java such specialization is typically done via inheritance such that SimpleModel becomes the super-class of your model class. 在Java中,这种特化通常借助继承方式来实现,这样简单模型就别成了你的模型类的超 类(父类、基类)。 For example, 例如
import uchicago.src.sim.engine.SimpleModel; public class MyModel extends SimpleModel { ... } In the first line we import SimpleModel and in the next we extend SimpleModel with our own model class. 在第一行,我们导入简单模型,第二行,我们扩展简单模型为我们自己的模型类。 In writing our simulation, as mentioned above, we want to describe how to setup our model, and then describe what happens every time step. 如上面提到的,在编写我们仿真时,我们需要先描述如何设定我们的模型,然后描述每 一节拍发生什么。 SimpleModel provides methods that we override in our own model to do the setup specific to our model. 简易模型提供这样的方法,即在我们自己的模型中用重置来把设定特化到我们的模型 These two methods are setup() and buildModel(). 这两种方法是 setup() 和 buildModel(). We use them as follows: 使用如下 import uchicago.src.sim.engine.SimpleModel; public class MyModel extends SimpleModel { public static final int TIT_FOR_TAT = 0; public static final int ALWAYS_DEFECT = 1; private int p1Strategy = TIT_FOR_TAT; private int p2Strategy = ALWAYS_DEFECT; ... public void setup() { super.setup(); p1Strategy = TIT_FOR_TAT; p2Strategy = ALWAYS_DEFECT; } public void buildModel() { Player p1 = new Player(p1Strategy); Player p2 = new Player(p2Strategy); p1.setOtherPlayer(p2); p2.setOtherPlayer(p1); agentList.add(p1);
agentList.add(p2); } } Here we use setup() and buildModel() to specialize SimpleModel for our own purposes. 这里我们用 setup() 和 buildModel()来特化简易模型。 In setup() we first call super.setup() to insure that SimpleModel does any necessary setup of its own, and then we set the player strategies. 在setup()中,我们先调用super.setup() 来确保简易模型完成一些必要的自我设定,然后 我们设定博弈方的策略。 The assumption here is that the strategies may have changed from their default values either through user interaction or perhaps during the course of a previous simulation run. 这里假设从默认值开始,策略按照与用户的交互或仿真前面的运行来变化。 We use setup() as the opportunity to set our model variables back to some reasonable default. 我们以使用setup()为契机,来把我们的模型变量设定回一些合理默认值 In general, setup() should "tear down" the model in preparation for the next run. 通常, setup() 应回归模型运行来为后面运行作准备。 setup() is called when the simulation is first started, and more frequently, whenever the setup button is clicked. 仿真刚启动会调用setup(),更多是每当设定按钮被点击。 Where setup() "tears downs" the simulation, we use buildModel() to create the objects that our simulation uses. 当setup() 回归仿真,我们用 buildModel() 来创建仿真所用的对象。 So, it is here where your agents should be created, and added to the master list of agents, agentList. 因此,正是在这里你的智体应该被创建,并被加入到智体管理列表中,即agentList。 agentList is an ArrayList provided by SimpleModel for this purpose. agentList是由简易模型提供的一个数组 The assumption here is that our Player class is constructed with a reference to the initial strategy and a reference to the other Player to play. 这里假设我们的博弈选手类按照初始策略和其他博弈方策略来博弈而构建。 The order of execution is that setup() is called first and then buildModel(). 执行顺序是,先调用setup() ,然后是buildModel()。
However, as mentioned above, setup() is called when the simulation is first started, and whenever the setup button is pressed. 当然正如上述所述,仿真刚启动和每当点击设定按钮时,setup()就会被调用 buildModel() is not called until the simulation is run, that is, it is not called until either the run, step or initialize buttons are pressed. buildModel() 只有当仿真运行时才被调用,也就是运行按钮、单步按钮或初始化按钮被 点击时才调用。 (See below for more on what code is executed by what buttons.) (更多按钮执行代码段的对应见下面) This provides an opportunity for the user to change any parameters via the gui. 这就为用户提供一个借助图形用户界面来改变参数的机会。 Now that setup is finished, we need to define what occurs each timestep of the simulation. 既然,完成了设定,我们需要明确仿真中的每个节拍发生什么。 SimpleModel provides three methods for this. 简易模型对此提供了三种方法 They are preStep(), step() and postStep(). 他们是preStep()、 step() 和 postStep(). Each tick they are executed in that order, first preStep(), then step(), and lastly postStep(). 每个节拍的执行顺序是,先 preStep(), 然后 step(), 最后postStep(). The intention here is to separate the core behavior in step() from any necessary pre- or post- processing. 这里的目的是要把step() 中的核心行为从任何必要的前续或后续处理中区分开 In our example, we have no such need for any pre- or post- processing, so we only use the step() method. 在我们的例子中,我们不需要任何前续或后续处理,因此,我们仅仅使用 step() 方法 import uchicago.src.sim.engine.SimpleModel; public class MyModel extends SimpleModel { public static final int TIT_FOR_TAT = 0; public static final int ALWAYS_DEFECT = 1; private int p1Strategy = TIT_FOR_TAT; private int p2Strategy = ALWAYS_DEFECT; ... public void setup() {
super.setup(); p1Strategy = TIT_FOR_TAT; p2Strategy = ALWAYS_DEFECT; } public void buildModel() { Player p1 = new Player(p1Strategy); Player p2 = new Player(p2Strategy); p1.setOtherPlayer(p2); p2.setOtherPlayer(p1); agentList.add(p1); agentList.add(p2); } public void step() { int size = agentList.size(); for (int i = 0; i < size; i++) { Player p = (Player)agentList.get(i); p.play(); } } } Here in the step() method we pull each Player out of our master agentList and call the play() method on each one. 这里,在 step() 方法中,我们把每个博弈方拉出智体管理列表,并在逐个调用 play() 方 法。 The assumption here is that a Player plays another Player when we call play(). 这里假设当我们调用 play() ,一个博弈方即会和另一个博弈方博弈。 It is typical in the step() method to iterate through all your agents and call whatever method executes their behavoir on each one. 在step()方法中通常迭代所有智体,并调用方法来执行每个智体的行为。 When the model runs it is this step() method that will execute each time step. 当模型运行时,正是这个step()方法执行每一个节拍。 A different game might require a pre- or postStep() method. 一个不同博弈可能需要 前续或后续步方法。 For example, assume something like a cooperation game with many players where after each player has played its neighbors and received a payoff, each player then polls its neighborhood
for the best strategy, and sets its own strategy accordingly. 例如,假设在一个含有很多博弈方的协作博弈中,当每个博弈方与其邻居博弈,并得到 一个收益,然后每个博弈参与者把邻居投票为最优策略,并把其设为它自己的策略。 In this case, the actual play would be defined as above in the step method, and the neighborhood polling and strategy setting would occur in a postStep() method. 在这个例子中,在step方法中定义实际博弈,而在poststep方法中确定邻居投票和策略 设定。 There is an alternative to using step() in your model. 这里有另一种方法来在你的模型中使用step() You can use the auto step mechanism. 你可以使用自动step机制 If you choose do this, SimpleModel will iterate through all your agents and call the step method defined in your agents. 如果你这样做,简易模型将迭代所有智体,并且调用智体中定义的step方法。 In order for this to work, your agents must implement the Stepable interface. 要实现这一目标,你的智体必须执行可逐步接口。 This interface has a single method, step(). 这个接口只有一个方法,即step()。 Note that step() here is a method in your agent class not your model class. 注意,这里的step(). 是你智体类中的一个方法,而不是你的模型类。 This autostep takes the place of your model's step method described above. 这种自动步将代替你模型中前面描述的step方法。 To use the auto step mechanism, you set the autoStep instance variable to true in the constructor of your model. 要使用这种自动step机制,需要在你的模型构造器中把 autoStep 实例变量设定为真。 You can also set the shuffle instance variable to true or false depending on whether you wish to shuffle the agentList before auto-stepping through it. 你也可以把实例变量变换为真或假,这可根据在自动步开始访问智体表前,你是否想变 换智体表。 preStep() and postStep() work as above, being called before and after the autoStep, respectively. 如上,preStep() 和 postStep()分别在autoStep前或后被调用。
Regardeless of whether we use preStep(), postStep(), both, or auto step, this is all that is required to create a simulation with SimpleModel: fill in setup(), buildModel() and step() (or auto step), as described above. 抛开我们是否使用preStep(), 或postStep(), 或两者, 或 auto step,要使用SimpleModel来 构建仿真所必须的是:如前面描述的,填写setup(), buildModel()和step() (或 auto step)。 Note that much more complicated scheduling of agent behavior, model events, etc, including dynamic scheduling, is possible with Repast. 注意用Repast来完成更为复杂的智体行为、模型事件等调度是可能,包括动态调度 SimpleModel is intended to simplify scheduling and the discussion above reflects this. SimpleModel有意简化了调度,上述讨论正说明了这一点。 However, you do have access to the scheduler from within a SimpleModel model via the schedule instance variable. 然而,借助在SimpleModel模型中的调度实例变量,你确实可以使用调度。 See How to Use a Schedule for more information on scheduling. 更多关于调度信息见如何使用一个调度 The relationship between the various tool bar buttons and the actual execution of code is as follows. 工具条上的各种按钮和其相应要执行的代码间关系如下 When the setup button is clicked, the code in the setup() method is executed. 当点击设定按钮,在step()方法中的代码被执行。 When the initialize button is clicked, the code in buildModel() is executed. 当初始化按钮被点击,在buildModel() 中的代码被执行 When the step button is clicked, the code in buildModel() is executed and the preStep(), step(), and postStep() sequence is executed once. 当单步按钮被点击, buildModel() 中代码被执行,并且依次执行preStep(), step(), and postStep() 中的代码一次。 When the start button is clicked, buildModel() is executed, and the preStep(), step(), and postStep() sequence is executed repeatedly until the user clicks the stop or pause button. 当开始按钮被点击,buildModel() 被执行,并且依次反复执行preStep(), step(), and postStep() 中的代码,直到用户点击停止或暂停按钮。 Parameters 参数 While setup(), buildModel() and step() are all we need to create a simulation, such a simulation is not very interesting.
分享到:
收藏