Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 KB | None | 0 0
  1. import numpy
  2. width = 101
  3. height = 101
  4.  
  5. class ReplicatingProbe():
  6. #class to keep track of a probe's properties and what it can do
  7.     global width , height
  8.     def __init__(self,Location =[0,0]):
  9.     #our probe starts in the center of our galaxy
  10.         self.location = Location
  11.         self.StepsTaken = 0
  12.         self.distance = 0
  13.         self.locationlist = []
  14.  
  15.     def move(self):
  16.     #moves our probe by picking a random number 1-4, representing each possible direction
  17.     #1 represents up, 2 represents right, 3 represents down, 4 represents left
  18.         direction = numpy.random.choice([1,2,3,4])
  19.         if direction == 1:
  20.             if self.location[1]==height:
  21.                 self.move()
  22.             else:
  23.                 self.location[1] +=1
  24.         if direction == 3:
  25.             if self.location[1]==-height:
  26.                 self.move()
  27.             else:
  28.                 self.location[1] -= 1
  29.         if direction == 2:
  30.             if self.location[0] == width:
  31.                 self.move()
  32.             else:
  33.                 self.location[0] += 1
  34.         if direction == 4:
  35.             if  self.location[0]==-width:
  36.                 self.move()
  37.             else:
  38.                 self.location[0] -= 1
  39.  
  40.         self.StepsTaken += 1
  41.         #keeps track of how many steps our probe has taken
  42.         self.distance = (self.location[0]**2 + self.location[1]**2)**.5
  43.         self.locationlist.append([self.location[0],self.location[1]])
  44.  
  45.     def replicate(self,probelist):
  46.         probelist.append(ReplicatingProbe(self.location))
  47.         probelist.append(ReplicatingProbe(self.location))
  48.         #create two new probes
  49.         probelist.remove(self)
  50.         #probe is removed from list, which represents the probe being consumed
  51.  
  52. class Star():
  53. #keeps track of the information about each star, how many times it has been visited, by what, etc.
  54.     def __init__(self,UsefulFlag):
  55.         self.visits = 0
  56.  
  57.         if UsefulFlag==0:
  58.             self.useful = False
  59.  
  60.         else:
  61.             self.useful = True
  62.         #useful represents whether the star has the resources necessary for the probe to replicate. we give it a random
  63.         #value, 0 or 1, that determines whether or not the star is useful.
  64.  
  65.     def visited(self):
  66.         self.visits +=1
  67.         self.useful = False
  68.  
  69. def initializestars(probability):
  70. #creates a list of lists, each containing a star object
  71. #the first index of the list indicates x position, and the second index represents y position
  72. #index - 101 = coordinate
  73.     global width , height
  74.     starlist = []
  75.     for j in range(0,2 * width + 1 ):
  76.         stars = []
  77.         for i in range(0,2 * height + 1 ):
  78.             stars.append(Star(numpy.random.choice([0,1],p = [1-probability,probability])))
  79.         starlist.append(stars)
  80.     return starlist
  81.  
  82. def initializeprobe():
  83. #makes the first probe object
  84.     probelist = [ReplicatingProbe()]
  85.     return probelist
  86.  
  87. def replicatingexploration(probability,N_steps):
  88.     probelist = initializeprobe()
  89.     starlist = initializestars(probability)
  90.     count = 0
  91.     #this count will keep track of how many total steps have been taken
  92.     while count< N_steps:
  93.         for k in probelist:
  94.             k.move()
  95.             xcoordinate = k.location[0]
  96.             ycoordinate = k.location[1]
  97.             if starlist[xcoordinate + width][ycoordinate + height].useful:
  98.                 k.replicate(probelist)
  99.             starlist[xcoordinate + width][ycoordinate + height].visited()
  100.             #update the number of times the star the probe traveled to has been visited
  101.         count+=1
  102.  
  103.     return probelist , starlist
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement