1、8:39 上午 05/15/19DRAFTjSIMPLEBUG:a Swarm tutorial for JavaCharles P. StaelinSmith CollegeNorthampton, MAApril 2000based on OjbectiveC code and original text byChris Lanton public class StartSimpleBugpublic static void main (String args)/ The size of the bugs world and its initial position.int worldXS
2、ize = 80;int worldYSize = 80;int xPos = 40;int yPos = 40;int i;/ Swarm initialization: all Swarm apps must call this first.Globals.env.initSwarm (“SimpleBug“, “2.1“, “bug-swarmsantafe.edu“, args);/(本程序不必要)System.out.println(“I started at X = “ + xPos + “ Y = “ + yPos);/ Have the bug randomly walk ba
3、ckward (-1), forward (+1), or/ not at all (0) in first the X and then the Y direction.jSWARM tutorial /SimpleCBug- 2 -/ (The randomMove() method, defined below, returns an/ integer between -1 and +1.) Note that the bugs world is a/ torus. If the bug walks off the edge of its rectangular/ world, it i
4、s magically transported (via the modulus/ operator) to the opposite edge.for(i = 0; i application 的 Makefile 列出了 Swarms /bin 目录中的 javacswarm 脚本中的Java 源文件。/SimpleCBug/MakefileJAVA_SRC = StartSimpleBug.javaall: $(JAVA_SRC)$(SWARMHOME)/bin/javacswarm $(JAVA_SRC)clean: rm -f *.class如果在控制台输入“javaswarm St
5、artSimpleBug“,可以得到运行结果,包括bug 的初始化声明,然后是它的 100 步移动的位置。jSWARM tutorial /SimpleJavaBug- 3 -/SimpleJavaBug起点本版本的 SimpleCBug 用到了一些面向对象的方法,也介绍了一点 Swarm 的内容。SimpleBug.java 文件定义了 SimpleBug 类,使仿真中的主体客观化,然后就可以用这个类定义一个或多个 bug 在整二维空间上随机游走。每个主体都是类的实例(对象)。SimpleBug 类是 SwarmObjectImpl 类的子类,它能赋予每个 bug 一个 Zone (aZon
6、e),它是“世界”的初始化。bug 位置的初始化为(wXSize, wYSize, X and Y),还有一个 bug 的数量 (bNum)。稍后会讨论为什么 bug 是 SwarmObjectImpl 的子类,以及关于 zone 的一些内容。现在可以先把一个 zone 看做是为 Swarm 分配的内存区域,在其中可以创造 bug 以及 bug 的资源。通过 Zone 基类的构造函数来初始化。构造函数保存了 bug 的“世界”、初始位置和数量。最后输出新产生的 bug的声明。注意这里还必须引入(import)一些必须的类库。第一,swarm.Globals,这是每一个 Swarm 程序都用到的
7、类,它描述了每个 Swarm 程序都会用到的一些全局变量和方法。还有 swarm.defobj.Zone 和 swarm.objectbase.SwarmObjectImpl,分别提供了 Zone 和 SwarmObjectImpl 类的定义。在 Swarms Java AP 部分还会谈到更多的 Swarm 类的描述。模型中的 SimpleBugs 能够做两个动作:在 XY 空间上随机游走以及向控制台汇报它们的位置。第一个方法为 randomWalk(),可以使 bug 在 XY 空间上随机的前后游走 1 个单位的距离(还必须保证 bug 不能走出它的空间,如果走出,将出现在“世界”的另一端)
8、。随机数生成器 randomWalk()是 Swarm 的 Globals 环境提供的。当运行一个Swarm 程序的时候,Swarm 的 Globals 类声明一个对象,Globals.env。这个对像包含一些变量和方法,包括 uniformIntRand.getIntegerWithMin$Max()方法。Swarm的 swarm.random 空间提供了一些非常强大的随机数生成器。其中的两个使用的比较频繁,所以把它们放在了 Swarm 的 Globals 环境中:uniformIntRand.getIntegerWithMin$Max(),uniformDblRand.getDoubleW
9、ithMin$withMax()。/SimpleJavaBug/SimpleBug.java/ SimpleBug.java/ Defines the class for our SimpleBug agents.import swarm.Globals;import swarm.defobj.Zone;import swarm.objectbase.SwarmObjectImpl;public class SimpleBug extends SwarmObjectImpljSWARM tutorial /SimpleJavaBug- 4 -/ These instance variables
10、 keep track of the size of a given/ bugs world, its position within it, and its identity.int worldXSize;int worldYSize;int xPos;int yPos;int bugNumber;/ Constructor to create a SimpleBug object in Zone aZone and to/ place it at the specified X,Y location in its world. The bug/ is also given a numeri
11、c id, bNum.public SimpleBug(Zone aZone, int wXSize, int wYSize, int X, int Y,int bNum)/ Call the contructor for the bugs parent class.super(aZone);/ Record the bugs world size, its initial position and id/ number.worldXSize = wXSize;worldYSize = wYSize;xPos = X;yPos = Y;bugNumber = bNum;/ Announce t
12、he bugs presence to the console.System.out.println(“SimpleBug number “ + bugNumber + “ has been created at “ + xPos + “, “ + yPos);/ This is the method to have the bug take a random walk backward/ (-1), forward (+1), or not at all (0) in first the X and then/ the Y direction. The randomWalk method u
13、ses/ getIntegerWithMin$withMax() to return an integer between a/ minimum and maximum value, here between -1 and +1./ Globals.env.uniformRand is an instance of the class/ UniformIntegerDistImpl, instantiated by the call to/ Globals.env.initSwarm in StartSimpleBug. Note that the bugs/ world is a torus
14、. If the bug walks off the edge of its/ rectangular world, it is magically transported (via the modulus/ operator) to the opposite edge.public void randomWalk()xPos += Globals.env.uniformIntRand.getIntegerWithMin$withMax(-1, 1);yPos += Globals.env.uniformIntRand.getIntegerWithMin$withMax(-1, 1);xPos
15、 = (xPos + worldXSize) % worldXSize;yPos = (yPos + worldYSize) % worldYSize;/ Method to report the bugs location to the console.public void reportPosition()System.out.println(“Bug “ + bugNumber + “ is at “ + xPos + “, “ + yPos);jSWARM tutorial /SimpleJavaBug- 5 -现在有了 bug 类,StartSimpleBug.java 文件也得有所
16、变化,需要在其中初始化 Swarm 的环境,创造一个 SimpleBug 并且激活它。先介绍 Globals.env.initSwarm(),这是 Swarm 的 Global 环境的一个静态方法。这个方法建立了 Swarm 的全局变量和方法,每一个 Swarm 程序都以它开始。它有四个参数:仿真的类的名字、Swarm 的版本、报告 Swarm 漏洞的 email 地址以及运行程序的命令字符串。模型在 80*80 的矩形空间创建了一个 SimpleBug,然后把它放在 “世界”的中心。该 bug 在 Swarm 的 globalZone 创建。(稍后会介绍放置 bug 对象的子空间的环境)。创
17、建出 bug 之后,让它做 100 次的随机游走,通过 randomWalk() 和 reportPosition()方法报出它每一步的位置。/SimpleJavaBug/StartSimpleBug.java/ StartSimpleBug.java/ The Java SimpleBug application. import swarm.Globals;import swarm.defobj.Zone;public class StartSimpleBug / The size of the bugs world and its initial position.static int w
18、orldXSize = 80;static int worldYSize = 80;static int xPos = 40;static int yPos = 40;public static void main (String args)int i;SimpleBug abug;/ Swarm initialization: all Swarm apps must call this first.Globals.env.initSwarm (“SimpleBug“, “2.1“, “bug-swarmsantafe.edu“, args); /在这个过程中声明了 SimpleBug 用到的
19、随机生成函数/ Create an instance of a SimpleBug, abug, and place it / within its world at (xPos, yPos). The bug is created in/ Swarms globalZone and is given a “bug id“ of 1.abug = new SimpleBug(Globals.env.globalZone, worldXSize, worldYSize, xPos, yPos, 1);/ Loop our bug through a series of random walks
20、asking it to/ report its position after each one.for (i = 0; i = endTime)/ Terminate the simulation.System.out.println(“Weve reached our endTime at period “ + Globals.env.getCurrentTime();Globals.env.getCurrentSwarmActivity().terminate();elsefor (i = 0; (t = (int)Math.pow(interval, (double)i) = endT
21、ime)jSWARM tutorial /SimpleSwarmBug2- 25 -/ Terminate the simulation.System.out.println(“Weve reached our endTime at period “ + Globals.env.getCurrentTime();Globals.env.getCurrentSwarmActivity().terminate();elsefor (i = 0; (t = (int)Math.pow(interval, (double)i) = endTime; i+)if (t = Globals.env.get
22、CurrentTime()System.out.println(“The time is “ + t);return;SimpleBug 类也得有一些改变。首先,每个 bug 必须通过其构造函数与bugspace 相关联。第二,必须保证一个 bug 不能抢另一个的地盘。当它走动的时候要向空着的地方走(看 myBugSpace.getObjectAtX$Y(newX, newY)是否是 null)。如果为空,它就走过去,如果不空它就不动(注意到在仿真的每一步,bug 都是按我们定下的规则行动的,因些,一个 bug 可能因为另一个抢先它一步而不能移动)。最后,再为 SimpleBug 类添加一个功
23、能,即对 reportIfEaten()的回应。布尔变量 haveEaten 作为 SimpleBug 类的一个实例变量。每当 bug 做一个 randomWalk(),如果它吃一个食就把 haveEaten 设为真,否则设为假(以前 bug 是把这个结果返回了控制台)。新的 reportIfEaten()方法当 haveEaten 是真的时候给控制台报告一下,如果为假就什么也不做。/SimpleSwarmBug2/SimpleBug.java/ SimpleBug.java/ Defines the class for our SimpleBug agents/import swarm.Gl
24、obals;import swarm.defobj.Zone;import swarm.objectbase.SwarmObjectImpl;import swarm.space.Grid2dImpl;public class SimpleBug extends SwarmObjectImpl/ These instance variables keep track of a given bugs foodspace,/ position and identity. We also save the dimensions of the/ foodspace so that we can mak
25、e fewer calls to the getSizeX() and/ getSizeY() methods in the bugs randomWalk().FoodSpace myFoodSpace;Grid2dImpl myBugSpace;int xPos;int yPos;jSWARM tutorial /SimpleSwarmBug2- 26 -int bugNumber;int worldXSize;int worldYSize;/ haveEaten keeps track of whether the bug has eaten on its most/ recent wa
26、lk.boolean haveEaten;/ Constructor to create a SimpleBug object in Zone aZone and to/ place it in the foodspace and bugspace, fSpace and bSpace, at/ the specified X,Y location. The bug is also given a numeric id,/ bNum.public SimpleBug(Zone aZone, FoodSpace fSpace, Grid2dImpl bSpace, int X, int Y, i
27、nt bNum)/ Call the constructor for the bugs parent class.super(aZone);/ Record the bugs foodspace, bugspace, initial position and/ id number.myFoodSpace = fSpace;myBugSpace = bSpace;worldXSize = myFoodSpace.getSizeX();worldYSize = myFoodSpace.getSizeY();xPos = X;yPos = Y;bugNumber = bNum;/ Announce
28、the bugs presence to the console.System.out.println(“SimpleBug number “ + bugNumber + “ has been created at “ + xPos + “, “ + yPos);/ This is the method to have the bug take a random walk backward/ (-1), forward (+1), or not at all (0) in first the X and then/ the Y direction. The randomWalk method
29、uses/ getIntegerWithMin$withMax() to return an integer between a/ minimum and maximum value, here between -1 and +1./ Globals.env.uniformRand is an instance of the class/ UniformIntegerDistImpl, instantiated by the call to/ Globals.env.initSwarm in StartSimpleBug. Note that the bugs/ world is a toru
30、s. If the bug walks off the edge of its/ rectangular world, it is magically transported (via the modulus/ operator) to the opposite edge. If on its walk the bug finds/ food, it eats it and turns on the haveEaten flag so it can/ report its feast if asked. Note that before the bug actually/ moves, we
31、must check to see that there is no other bug at the/ destination cell. If there is, the this bug just stays put.public void randomWalk()int newX, newY;/ Turn off the haveEaten flag.haveEaten = false;/ Decide where to move.jSWARM tutorial /SimpleSwarmBug2- 27 -newX = xPos + Globals.env.uniformIntRand
32、.getIntegerWithMin$withMax(-1, 1);newY = yPos +Globals.env.uniformIntRand.getIntegerWithMin$withMax(-1, 1);newX = (newX + worldXSize) % worldXSize;newY = (newY + worldYSize) % worldYSize;/ Is there a bug at the new position already? If not, put a/ null at this bugs current position and put this bug
33、at the/ new position.if (myBugSpace.getObjectAtX$Y(newX, newY) = null)myBugSpace.putObject$atX$Y(null, xPos, yPos);xPos = newX;yPos = newY;myBugSpace.putObject$atX$Y(this, xPos, yPos);/ If there is food at this cell, eat it and set the haveEaten/ flag.if (myFoodSpace.getValueAtX$Y(xPos, yPos) = 1)my
34、FoodSpace.putValue$atX$Y(0, xPos, yPos);haveEaten = true;/ Method to report the bugs position to the console.public void reportPosition()System.out.println(“Bug “ + bugNumber + “ is at “ + xPos + “, “ + yPos);/ Method to report if the bug has eaten.public boolean reportIfEaten()if ( haveEaten )Syste
35、m.out.println(“Bug “ + bugNumber + “ has found food at “ +xPos + “, “ + yPos);return haveEaten;FoodSpace.java 文件和 StartSimplebug.java 文件以及 MakeFile 文件没有做任务变动。我们做的这些变动对 StartSimpleBug.java 没有什么影响。当 application 建立并运行的时候,我们会看到这么多的 bug 留下的建造消息。然后可以看到汇报员 bug 找到食物时返回的消息,消息会越来越少,直到foodspace 里的食物被吃完。现在我们对 endTime 不很依赖了。这个模型里的世界大小为 80*80,有差不多 640 个 bug,密度为 0.1,仿真的时间可能要长一些。