Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Image, ImageDraw,math, ImageFilter, random
- # needs PIL to run
- # WIP
- class Ant:
- def __init__(self):
- self.pos = (50,50)
- # potentialEnergy is used to calculate how far it should move
- self.potentialEnergy = 10.0
- # angle is for the current directional heading
- self.angle=45.0
- # size is the radius of the ant (used for sensing pheromones
- self.size = 3
- # used for determining when to turn significantly.
- # simulates a level of pheromones in an ants nose such that
- # if it hasn't smelled any recently, it should turn.
- self.pheromone_levels = 20
- #this method does not nessecarily go to the destination
- # instead it goes in the direction of the destination
- # based off of how much potentialEnergy is left
- def head_to(self, destination):
- # find a direction to head
- angle = math.tan( destination[1]-pos[1]/destination[0]-pos[0])
- self.ydifference = math.sin(angle)*potentialEnergy
- self.xdifference = math.cos(angle)*potentialEnergy
- pos = (round(pos[0]+self.xdifference),round(pos[1]+self.ydifference))
- return
- # returns a touple of values at the sensor locations (left,right)
- def sense(self):
- # set up the sensors as 15 degrees off of the angle at a given distance
- self.Langle = self.angle + math.pi/12.0
- self.Rangle = self.angle - math.pi/12.0
- self.Lpos = (math.cos(self.Langle)*self.size,math.sin(self.Langle)*self.size)
- self.Rpos = (math.cos(self.Rangle)*self.size,math.sin(self.Rangle)*self.size)
- self.leftResult = self.sVal(self.Lpos)
- self.rightResult = self.sVal(self.Rpos)
- self.pheromone_levels += self.leftResult + self.rightResult
- return (self.leftResult,self.rightResult)
- # used in the above method, to return a decent value from a location
- def sVal(self,where):
- self.x = round(where[0])
- self.y = round(where[1])
- try:
- return play.im.getpixel((self.x,self.y))
- except:
- return 255
- #similar to head_to, but instead moves in the direction of the current angle
- # uses potentialEnergy to determine distance
- def move_in(self,heading):
- self.ydifference = math.sin(heading)*self.potentialEnergy
- self.xdifference = math.cos(heading)*self.potentialEnergy
- playing.draw.line((self.pos,(self.pos[0]+self.xdifference,self.pos[1]+self.ydifference)),width=1)
- self.pos = (round(self.pos[0]+self.xdifference),round(self.pos[1]+self.ydifference))
- return
- def blur(self,howMuch):
- for i in int(howMuch):
- print "blurred in ant class"
- im = im.filter(ImageFilter.BLUR)
- # works in degrees
- def rotate(self,heading):
- heading = heading * 0.017453
- self.angle=heading+self.angle
- def wander(self, num_steps):
- for i in range(0,num_steps):
- self.angle_towards_p()
- if self.pheromone_levels <= 10:
- self.try_a_significantly_new_angle()
- elif i % 3 == 0 and i != 0:
- self.rotate(random.randint(-20,20))
- self.move_in(self.angle)
- self.pheromone_levels = self.pheromone_levels / 1.3
- def turn_this_ant_around(self):
- # right this minute, and I mean it this time
- self.angle = self.angle+math.pi
- def try_a_significantly_new_angle(self):
- self.rotate(random.randint(-120,120))
- # this function steers the ant towards the pheromone trail
- # currently it is a binary steering operation
- # see if elif state ment, have the ant turn based on a gradient
- # might work much better.
- def angle_towards_p(self):
- sensing_result = self.sense()
- # the signs are the way they are cause we are going towards darkness (0) away from light (255)
- if sensing_result[0]<sensing_result[1] - 3:
- self.rotate(self.how_much_to_turn(sensing_result))
- elif sensing_result[0]>sensing_result[1] + 3:
- self.rotate(35)
- #def handle_edge(self):
- # if self.pos[0]>worldSize[0]
- class Player:
- def __init__(self):
- self.number_of_ants=25
- self.number_of_turns = 200
- self.number_of_food = 10
- self.ant_list = []
- self.food_list = []
- self.food_size = 5
- self.worldSize = (512,512)
- self.im = Image.new("L",self.worldSize,"white")
- self.draw = ImageDraw.Draw(self.im)
- for k in range(0,self.number_of_ants):
- self.ant_list.append(Ant())
- for f in range(0,self.number_of_food):
- self.food_list.append((random.randint(0,self.worldSize[0]),random.randint(0,self.worldSize[0])))
- self.food_list.sort()
- print self.number_of_turns
- def play(self):
- for self.NT in range(0,int(self.number_of_turns)):
- # there is something weird about this modulo behavior
- # it seems to be triggering to often.
- self.drawFood()
- for j in range(0,self.number_of_ants):
- #print "wander"
- self.ant_list[j].wander(6)
- if self.NT % 50 == 0:
- self.ant_list[j].try_a_significantly_new_angle()
- if self.NT % 16 == 0 and self.NT != 0:
- print "blurred"
- #self.im.show("pre-blur")
- self.im = self.im.filter(ImageFilter.GaussianBlur(radius = 2.0))
- def drawFood(self):
- #print "draw food"
- for fo in range(0,len(self.food_list)):
- other_side = (self.food_list[fo][0]+self.food_size,self.food_list[fo][1]+self.food_size)
- self.draw.ellipse((self.food_list[fo],other_side),fill="black")
- #print im.getpixel((21,21))
- #print math.pi
- #draw.line(((0,0),(50,50)),width=2)
- #ant = Ant()
- playing = Player()
- playing.play()
- playing.im.show()
- print playing.food_list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement