[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Heatbug.java continued..
From: |
Miles Parker |
Subject: |
Heatbug.java continued.. |
Date: |
Thu, 13 Jan 2000 19:27:13 -0500 |
Miles T. Parker
Software Engineer
The Brookings Institution 1775 Mass. Ave. NW Washington, DC 20036
http://www.brook.edu/es/dynamics/models/ascape
mailto:address@hidden voice 202.797.6136 fax 202.797.6319
>>> Benedikt Stefansson <address@hidden> 01/13/00 06:17PM >>> wrote:
>>In the original Objective-C heatbugs, for better or for worse, there were no
>>'coordinate'
>>objects used, so to set an (x,y) coordinate one passed two method variables
>>by reference. In
>>the Java code the implementation is more OO oriented, a coordinate object is
>>passed and its
>>vars are set by the callee.
To extrapolate further, as an example from Ascape of the beneifts of stronger O
Orientation, an agent's relationship to space can be further abstracted, so we
replace:
[Swarm JHeatbugs extended comment removed...]
newX = x;
newY = y;
{
HeatCell heatCell = new HeatCell (newX, newY);
heat.findExtremeType$X$Y (((heatHere < idealTemperature)
? HeatSpace.hot
: HeatSpace.cold),
heatCell);
newX = heatCell.x;
newY = heatCell.y;
}
if ((Globals.env.uniformDblRand.getDoubleWithMin$withMax (0.0, 1.0))
< randomMoveProbability)
{
// pick a random spot
newX =
x + Globals.env.uniformIntRand.getIntegerWithMin$withMax (-1, 1);
newY =
y + Globals.env.uniformIntRand.getIntegerWithMin$withMax (-1, 1);
// normalize coords
newX = (newX + worldXSize) % worldXSize;
newY = (newY + worldYSize) % worldYSize;
}
if (unhappiness == 0)
{
// only update heat - don't move at all if no unhappiness
heat.addHeat$X$Y (outputHeat, x, y);
}
else {
tries = 0;
// only search if the current cell is neither the optimum
// or randomly chosen location - else don't bother
if ( (newX != x || newY != y) )
{
while ( (world.getObjectAtX$Y (newX, newY) != null)
&& (tries < 10) )
{
int location, xm1, xp1, ym1, yp1;
// choose randomly from the nine possible
// random locations to move to
location =
Globals.env.uniformIntRand.getIntegerWithMin$withMax (1,8);
xm1 = (x + worldXSize - 1) % worldXSize;
xp1 = (x + 1) % worldXSize;
ym1 = (y + worldYSize - 1) % worldYSize;
yp1 = (y + 1) % worldYSize;
switch (location)
{
case 1:
newX = xm1; newY = ym1; // NW
break;
case 2:
newX = x ; newY = ym1; // N
break;
case 3:
newX = xp1 ; newY = ym1; // NE
break;
case 4:
newX = xm1 ; newY = y; // W
break;
case 5:
newX = xp1 ; newY = y; // E
break;
case 6:
newX = xm1 ; newY = yp1; // SW
break;
case 7:
newX = x ; newY = yp1; // S
break;
case 8:
newX = xp1 ; newY = yp1; // SE
default:
break;
}
tries++; // don't try too hard.
}
if (tries == 10)
{
// no nearby clear spot, so just don't move.
newX = x;
newY = y;
}
}
.....
world.putObject$atX$Y (null, x, y);
x = newX;
y = newY;
world.putObject$atX$Y (this, newX, newY);
With:
[Ascape Heatbugs]
DataPoint maximizeFor = (((HeatCell) getHostCell()).getHeat() <
idealTemperature)
? HeatCell.MAXIMUM_HEAT_POINT :
HeatCell.MINIMUM_HEAT_POINT;
Cell bestLocation = getHostCell().findMaximumWithin(maximizeFor, 1,
true);
if (!bestLocation.isAvailable() && bestLocation != oldHost) {
randomWalkAvailable();
}
else if (bestLocation != oldHost) {
moveTo((HostCell) bestLocation);
}
//This is slightly different then the swarm implementation..
if (getRandom().nextFloat() < ((HeatbugModel)
getRoot()).getRandomMoveProbability()) {
randomWalk();
}
Now, of course, all the code above still has to exist somewhere, but the point
is that you don't have to write it and/or copy it each time! Also, if we want
to run it under 1D, or (eventually!) 3D or network, we don't have to rewrite
any of the Heatbugs code. Finally, from a pedagogical standpoint, its just
clearer what the code is doing. (Sorry for the rather obvious horn tooting, but
I think that this kind of move would be extremely useful for future
implementations of Swarm as well, as I'm sure you'all would agree.)
-Miles
==================================
Swarm-Support is for discussion of the technical details of the day
to day usage of Swarm. For list administration needs (esp.
[un]subscribing), please send a message to <address@hidden>
with "help" in the body of the message.
==================================
Swarm-Support is for discussion of the technical details of the day
to day usage of Swarm. For list administration needs (esp.
[un]subscribing), please send a message to <address@hidden>
with "help" in the body of the message.
- Heatbug.java continued..,
Miles Parker <=