Python Forum
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Classes ?
#1
Hello, I have to do a model of a disease spreading in a population. As a beginner in python, my teacher gave us most of the code. I can't understand the full code but I could get some keys,  their are mobile agents that can be "infectious" or "sane". They are created by a class named 'Agent' , and put in a list called 'agents'. Their are displayed in an area of 500 of height and 800 of width.
So even if I could (harshly) make them move, I am blocked as I have to make one sane agent  infected by one of its neighbour. As each change in their state is coded in the 'agent' class, I can' understand how I could change the state of a neighbour. Here are a piece of what I tried, just for a neighbour at the right. If anyone have an piece of advice, I would be very grateful !  Blush
  
       elif self.state == 'I':# infectious
           for x in range(len(agents)):
               if agents[x]== Agent(self.x+1,self.y,'S'):
                   agents[x].state='I'
Reply
#2
That looks like it would work, I'm not sure what your question is. One thing is that it would be better to loop directly over the list. It's simpler, and more simple is less errors:
for agent in agents:
    if agent = Agent(self.x + 1, self.y, 'S')
        agent.state = 'I'
If I were to do this, I would have some way to get the neighborhood of an agent: a list of the (x, y) coordinates that were adjacent to that agent. This could be function taking an agent as a parameter, a method of the agent, or an attribute of the agent that is reset every time it moves. Since you don't seem to have movement yet, let's assume it's a method of the agent:
elif self.state == 'I':
    neighborhood = self.neighborhood()
    for agent in agents:
        if agent.state == 'S' and (agent.x, agent.y) in neighborhood:
            agent.state = 'I'
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
It worked ! Thanks a lot and sorry for my poor english and python !
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020