ns2349

lab 12

Dec 14th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.55 KB | None | 0 0
  1. #Noah Segal-Gould
  2. #Keith O'Hara
  3. #Object-Oriented Programming with Robots
  4. #Bard College
  5. #11/21/14
  6.  
  7. #Lab 12: Predator-Prey Simulation
  8. #Due 14 December 2014
  9.  
  10. from Graphics import *
  11. import random
  12.  
  13. class World(object):
  14.  
  15.     def __init__(self, nR, nW):
  16.         ''' Create a simulated world with nR rabbits and nW wolves '''
  17.         self.display = True
  18.         self.nR = nR
  19.         self.nW = nW
  20.         self.animals = []
  21.  
  22.         self.win = Window("simulation", 400, 400)
  23.         self.win.setBackground(Color("white"))
  24.  
  25.         self.gwin = Window("population size", 1000, 300)
  26.  
  27.         for i in range(nR):
  28.             r = Rabbit(self)
  29.  
  30.         for i in range(nW):
  31.             w = Wolf(self)
  32.            
  33.     def addAnimal(self, w):
  34.         self.animals.append(w)
  35.        
  36.     def nearbyAnimals(self, a):
  37.         ''' find all the nearby animals within sensing range of a'''
  38.         nearby = []
  39.         for o in self.animals:
  40.             if o.alive and a.distance(o) < a.SENSING_RANGE and o != a:
  41.                 nearby.append(o)
  42.  
  43.         return nearby
  44.  
  45.     def closestNeighbor(self, a, t):
  46.         ''' find the closest animal to animal a of type t;
  47.            return None if no closest exists'''
  48.         closestDistance = 100000000
  49.         neighbor = None
  50.         for o in self.nearbyAnimals(a):
  51.             if isinstance(o, t) and a.distance(o) < closestDistance:
  52.                 neighbor = o
  53.                 closestDistance = a.distance(o)
  54.         return neighbor
  55.                
  56.     def run(self):
  57.         ''' run the simulated world '''
  58.  
  59.         t = 0
  60.         while t < self.gwin.getWidth() and self.nR > 0 and self.nW > 0:
  61.  
  62.             self.nR = 0
  63.             self.nW = 0
  64.             for a in self.animals:
  65.                 if a.alive:
  66.                     a.takeAStep()
  67.                     if isinstance(a, Rabbit):
  68.                         self.nR += 1
  69.                     elif isinstance(a, Wolf):
  70.                         self.nW += 1
  71.                 else:                
  72.                     self.animals.remove(a)
  73.                    
  74.             pr = Circle((t, self.gwin.getHeight() - self.nR), 1)
  75.             pr.color = Color("blue")
  76.             pr.draw(self.gwin)
  77.  
  78.             pw = Circle((t, self.gwin.getHeight() - self.nW), 1)
  79.             pw.color = Color("red")
  80.             pw.draw(self.gwin)
  81.  
  82.             t = t + 1
  83.    
  84.         print ("Simulation Done")
  85.  
  86. class Animal(object):
  87.     SIZE = 5
  88.     SENSING_RANGE = 30
  89.  
  90.     def __init__(self, world):
  91.         ''' Create a new Animal'''
  92.         self.world = world
  93.         self.alive = True
  94.         self.vx = 4
  95.         self.vy = 4
  96.         self.reproduction_prob = 0.02
  97.         self.x = random.uniform(0, self.world.win.getWidth())
  98.         self.y = random.uniform(0, self.world.win.getHeight())
  99.         self.size = self.SIZE
  100.         self.world.addAnimal(self)
  101.         self.setAppearance()
  102.    
  103.     def setAppearance(self):
  104.         self.appearance = Circle(Point(self.x, self.y), self.size)                                
  105.         if self.world.display:
  106.             self.appearance.draw(self.world.win)
  107.    
  108.     def eat(self):
  109.         pass
  110.  
  111.     def reproduce(self):
  112.         pass
  113.  
  114.     def die(self):
  115.         '''remove this animal from the population'''
  116.         if self.world.display:
  117.             self.appearance.undraw()
  118.         self.alive = False
  119.  
  120.     def takeAStep(self):
  121.         ''' move the animal for one timestep'''
  122.         dx = random.uniform(-self.vx, self.vx)
  123.         dy = random.uniform(-self.vy, self.vy)
  124.         if self.insideWindow(dx, dy):
  125.             self.x = self.x + dx
  126.             self.y = self.y + dy
  127.             self.appearance.move(dx,dy)
  128.  
  129.     def distance(self, other):
  130.         ''' find the distance between myself and the other animal'''
  131.         return ((self.x - other.x)**2 + (self.y - other.y)**2)**0.5
  132.  
  133.     def insideWindow(self, dx, dy):
  134.         ''' check to see if moving animal by (dx, dy) keeps it in the window'''
  135.         tx = self.x + dx
  136.         ty = self.y + dy
  137.         if self.size/2 < tx < self.world.win.getWidth() - self.size/2 and\
  138.            self.size/2 < ty < self.world.win.getHeight() - self.size/2:
  139.             return True
  140.         else:
  141.             return False
  142.                
  143.     def __str__(self):
  144.         return "Animal at (%d, %d)" % (self.x, self.y)
  145.  
  146. class Wolf(Animal):
  147.     SIZE = 6
  148.     def __init__(self, world):
  149.         Animal.__init__(self, world)
  150.         self.energy = 500
  151.        
  152.     def setAppearance(self):
  153.         self.appearance = Circle(Point(self.x, self.y), self.size)  
  154.         self.appearance.setFill(Color("red"))    
  155.         self.appearance.setOutline(Color("black"))                          
  156.         if self.world.display:
  157.             self.appearance.draw(self.world.win)      
  158.    
  159.     def takeAStep(self):
  160.         Animal.takeAStep(self)
  161.         self.eat()
  162.         self.reproduce()
  163.         self.energy = self.energy - 25
  164.         if self.energy < 1:
  165.             self.die()
  166.                                                                
  167.     def eat(self):
  168.         r = self.world.closestNeighbor(self, Rabbit)
  169.         if r != None:
  170.             self.energy = self.energy + 100
  171.             r.die()
  172.        
  173.     def reproduce(self):
  174.         self.reproduction_prob = 0.03
  175.         if self.energy > 250:
  176.             self.reproduction_prob = self.reproduction_prob + 0.005
  177.         elif self.energy < 250:
  178.             self.reproduction_prob = self.reproduction_prob - 0.003
  179.         r = self.world.closestNeighbor(self, Rabbit)
  180.         if r == None and random.random() < self.reproduction_prob:
  181.             Wolf(self.world)        
  182.                
  183. class Rabbit(Animal):
  184.     SIZE = 3
  185.     def __init__(self, world):
  186.         Animal.__init__(self, world)
  187.         self.energy = 100
  188.  
  189.     def setAppearance(self):
  190.         self.appearance = Circle(Point(self.x, self.y), self.size)  
  191.         self.appearance.setFill(Color("blue"))    
  192.         self.appearance.setOutline(Color("black"))                          
  193.         if self.world.display:
  194.             self.appearance.draw(self.world.win)      
  195.  
  196.     def takeAStep(self):
  197.         Animal.takeAStep(self)
  198.         self.reproduce()
  199.         self.energy = self.energy - 1
  200.         if self.energy < 1:
  201.             self.die()
  202.    
  203.     def reproduce(self):
  204.         self.reproduction_prob = 0.06
  205.         w = self.world.closestNeighbor(self, Wolf)
  206.         if w == None and random.random() < self.reproduction_prob:
  207.             Rabbit(self.world)        
  208.    
  209. def go():
  210.     random.seed(12)
  211.     w = World(20, 20)
  212.     w.run()
  213.  
  214. go()
Advertisement
Add Comment
Please, Sign In to add comment