Advertisement
ns2349

lab 12

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