📄 sugaragent.java
字号:
import swarm.Globals;
import swarm.gui.Raster;
import swarm.space.Grid2d;
import swarm.space.Grid2dImpl;
import swarm.defobj.Zone;
import swarm.objectbase.SwarmObjectImpl;
import java.util.ArrayList;
public class SugarAgent extends SwarmObjectImpl{
public int currentsugar; //unhappiness;
public int metabolism; //temperature;
public int vision;
public int age;
public int xPos, yPos; //position
public int deathAge;
public int ID;
public int worldXSize, worldYSize;
public double randomMoveProbability;
public byte AgentColor;
protected Grid2dImpl _world;
protected ModelSwarm _modelswarm;
protected SugarSpace _sugarspace;
//constructor
public SugarAgent(Zone aZone, SugarSpace fSpace, Grid2dImpl aSpace,
int X, int Y, int bNum)
{
super(aZone);
_sugarspace = fSpace; //foodspace
_world = aSpace;
worldXSize = _sugarspace.getSizeX();
worldYSize = _sugarspace.getSizeY();
xPos = X;
yPos = Y;
ID = bNum;
System.out.println("sugarAgent Num" + ID + "at position" + xPos + "," +
yPos);
}
public SugarAgent(Grid2dImpl w, SugarSpace h)
{
if (_world != null || _sugarspace != null)
System.err.println ("You can only set the world/sugarspace " +
"of a sugarAgent at creation time");
_world = w;
_sugarspace= h;
// make sure the user set up world and heat.
if (_world == null || _sugarspace == null)
System.err.println ("SugarAgent was created without a world or sugarspace");
// Cache the worldSize for speed of later access. Note how we
// do this in createEnd - it could also have been done when
// setWorld:Heat: was called, but this is a good place to do
// it, too. If an object needed to allocate extra memory, this
// is the right place to do it.
worldXSize = _world.getSizeX ();
worldYSize = _world.getSizeY ();
xPos=0;
yPos=0;
}
//implements
public synchronized Object step()// The agent moves to a nearby spot.
{
moveToBestOpenSpot();
currentsugar +=_sugarspace.takeSugarAtX$Y(xPos, yPos);
currentsugar -= metabolism;
if (currentsugar <= 0 || age >= deathAge) {
_sugarspace.removeAgent(this);
_modelswarm.agentDeath(this);
}
return this;
}
//rule M: Briefly: look around for the closest, empty spot with the most food
public Object moveToBestOpenSpot()
{
// Aspects of an agents lifecycle - eating, metabolism, death.
int xLook, yLook;
int bestSugar;
int bestDistance;
int goodSpots;
int goodX[]=new int[16];
int goodY[]=new int[16];
int chosenSpot, newX, newY;
bestSugar = -1;
goodSpots = 0;
bestDistance = 999999;
yLook = yPos;
for (xLook = xPos- vision; xLook <= xPos+ vision; xLook++)
{
if(_sugarspace.getAgentAtX$Y(xLook,yLook)==null){
if(_sugarspace.getSugarAtX$Y(xLook, yLook) > bestSugar){
bestSugar = _sugarspace.getSugarAtX$Y(xLook,yLook);
bestDistance=Math.abs(xPos-xLook);
goodSpots = 0;
goodX[0] = xLook;
goodY[0] = yLook;
goodSpots++ ;
}
else if(_sugarspace.getSugarAtX$Y(xLook,yLook)==bestSugar){
if(Math.abs(xPos-xLook)<bestDistance){
bestDistance=Math.abs(xPos - xLook);
goodSpots = 0;
goodX[0] = xLook;
goodY[0] = yLook;
goodSpots++;
}
else if(Math.abs(xPos-xLook)==bestDistance){
goodX[goodSpots] = xLook;
goodY[goodSpots] = yLook;
goodSpots++;
}
}
}
}
xLook = xPos;
for (yLook = yPos- vision; yLook <= yPos+ vision; yLook++) {
if(_sugarspace.getAgentAtX$Y(xLook,yLook)==null)
if(_sugarspace.getSugarAtX$Y(xLook,yLook)>bestSugar){
bestSugar = _sugarspace.getSugarAtX$Y(xLook,yLook);
bestDistance = Math.abs(yPos-yLook);
goodSpots = 0;
goodX[0] = xLook;
goodY[0] = yLook;
goodSpots++;
}
else if (_sugarspace.getSugarAtX$Y(xLook,yLook)==bestSugar){
if(Math.abs(yPos-yLook)<bestDistance){
bestDistance = Math.abs(yPos-yLook);
goodSpots = 0;
goodX[0] = xLook;
goodY[0] = yLook;
goodSpots++;
}
else if(Math.abs(yPos -yLook)==bestDistance) {
goodX[goodSpots] = xLook;
goodY[goodSpots] = yLook;
goodSpots++;
}
}
}
if (goodSpots == 0) ;//do not move
else{
if (goodSpots == 1)
chosenSpot = 0;
else
chosenSpot = Globals.env.uniformIntRand.getIntegerWithMin$withMax
(0, (goodSpots - 1));
newX = goodX[chosenSpot]; // get the coordinate
newY = goodY[chosenSpot];
_sugarspace.moveAgent(this, newX, newY);
}
return this;
}
//how the program works
public int getcurrentsugar() {return currentsugar;}
public Object setcurrentsugar(int currentsugar)
{
this.currentsugar=currentsugar;
return this;
}
public Object setMetabolism(int metabolism)
{
this.metabolism = metabolism;
return this;
}
public int getMetabolism(){return metabolism;}
public int getVision(){return vision;}
public void setVision(int v) {vision = v;}
public int getAge() { return age;}
public void setDeathAge(int a) {deathAge = a;}
public void setRandomMoveProbability(double randomMoveProbability)
{ this.randomMoveProbability = randomMoveProbability; }
public void setAgentColor(byte AgentColor)
{ this.AgentColor = AgentColor; }
public int getX(){ return xPos;}
public int getY(){ return yPos;}
public Object setModelSwarm(ModelSwarm s)
{
_modelswarm =s;
_sugarspace=s.getSugarSpace();
return this;
}
public Object drawSelfOn(Raster r)
{
r.drawPointX$Y$Color(xPos, yPos, AgentColor);
return this;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -