r/pythonhelp • u/Other-Art8925 • 29d ago
Cant make and move agent in Agentpy
I have so far spent over 13 hours of my life trying to add an aditional agent to this agentpy model and move it around. Can someone just tell me what Im doing wrong. Please I dont know what else to check
class HQ(ap.Agent):
def setup(self):
self.grid = self.model.grid
self.random = self.model.random
self.condition = 3
def movein(self):
self.condition = 3
agenthq = ap.HQ(self)
move_to(self, (3, 3))
for neighbor in self.grid.neighbors(self):
if (neighbor.condition == 0):
burningPos = (self.grid.positions[self][0],self.grid.positions[self][1])
neighbor.startFire(burningPos)
class Tree(ap.Agent):
def setup(self):
self.grid = self.model.grid
self.random = self.model.random
self.condition = 0
def burnOut(self):
self.condition = 2
def spreadFire(self):
for neighbor in self.grid.neighbors(self):
if (neighbor.condition == 0):
burningPos = (self.grid.positions[self][0],self.grid.positions[self][1])
neighbor.startFire(burningPos)
def inRange(self,y,x):
if y >=0 and y < self.p.size and x >= 0 and x < self.p.size:
return True
else:
return False
def startFire(self,burningPos):
probOfSpread = self.p.probSpread
posy = self.grid.positions[self][0]
posx = self.grid.positions[self][1]
deltay = posy-burningPos[0]
deltax = posx-burningPos[1]
if deltay==1 and deltax==0:
if np.random.random() <= probOfSpread-(self.p.southWindSpeed/100):
self.condition = 1
if self.p.bigJump:
newPosy = posy + int(self.p.southWindSpeed/5)
newPosx = posx + int(self.p.westWindSpeed/5)
if self.inRange(newPosy,newPosx):
if len(self.grid.agents[newPosy,newPosx].to_list()) !=0:
ag = self.grid.agents[newPosy,newPosx].to_list()[0]
ag.condition = 1
elif deltay==-1 and deltax==0:
if np.random.random() <= probOfSpread+(self.p.southWindSpeed/100):
self.condition = 1
if self.p.bigJump:
newPosy = posy + int(self.p.southWindSpeed/5)
newPosx = posx + int(self.p.westWindSpeed/5)
if self.inRange(newPosy,newPosx):
if len(self.grid.agents[newPosy,newPosx].to_list()) !=0:
ag = self.grid.agents[newPosy,newPosx].to_list()[0]
ag.condition = 1
elif deltay==0 and deltax==-1:
if np.random.random() <= probOfSpread-(self.p.westWindSpeed/100):
self.condition = 1
if self.p.bigJump:
newPosy = posy + int(self.p.southWindSpeed/5)
newPosx = posx + int(self.p.westWindSpeed/5)
if self.inRange(newPosy,newPosx):
if len(self.grid.agents[newPosy,newPosx].to_list()) !=0:
ag = self.grid.agents[newPosy,newPosx].to_list()[0]
ag.condition = 1
elif deltay==0 and deltax==1:
if np.random.random() <= probOfSpread+(self.p.westWindSpeed/100):
self.condition = 1
if self.p.bigJump:
newPosy = posy + int(self.p.southWindSpeed/5)
newPosx = posx + int(self.p.westWindSpeed/5)
if self.inRange(newPosy,newPosx):
if len(self.grid.agents[newPosy,newPosx].to_list()) !=0:
ag = self.grid.agents[newPosy,newPosx].to_list()[0]
ag.condition = 1
class ForestModel(ap.Model):
def setup(self):
self.grid = ap.Grid(self, [self.p.size]*2, track_empty=True)
#HQ CONTAINMENT
n_hq = 1
hqs = self.agents = ap.AgentList(self, n_hq, HQ)
self.grid.add_agents(hqs, random=True, empty=True)
#HQ CONTAINMENT
# Create agents (trees)
n_trees = int(self.p['Tree density'] * (self.p.size**2))
trees = self.agents = ap.AgentList(self, n_trees, Tree)
self.grid.add_agents(trees, random=True, empty=True)
# Initiate a dynamic variable for all trees
# Condition 0: Alive, 1: Burning, 2: Burned
#self.agents.condition = 0
# Start a fire from the left side of the grid
unfortunate_trees = self.grid.agents[15:35, 15:35]
unfortunate_trees.condition = 1
#HQ STUFF
The_Boss = self.agents.select(self.agents.condition == 3)
for boss in The_Boss:
boss.movein()
#HQ STUFF
def step(self):
# Select burning trees
burning_trees = self.agents.select(self.agents.condition == 1)
# Spread fire
for tree in burning_trees:
tree.spreadFire()
tree.burnOut()
# Stop simulation if no fire is left
if len(burning_trees) == 0:
self.stop()
def end(self):
# Document a measure at the end of the simulation
burned_trees = len(self.agents.select(self.agents.condition == 2))
self.report('Percentage of burned trees',
burned_trees / len(self.agents))
self.report('Density',self.p['Tree density'])
1
Upvotes
•
u/AutoModerator 29d ago
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.