Skip to content
Snippets Groups Projects
Commit 862324fb authored by phwitte's avatar phwitte
Browse files

Update ackland_31_7.py, evolution with predator and food

parent fcdef312
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,39 @@ import time
import random
import math
winWidth = 700
winHeight = 700
window = GraphWin("Window", winWidth, winHeight)
maxTime = 10000
agentNum = 80
predNum = agentNum/10
generations = 100
foodCluster = 16
clusterSize = 8
clusterradius = 20
isFood = False
isPred = True
t = 0.1
rr = 5
As = 5000 * (rr**2)
Am = 5 * (rr**2) * t
#mutate values
m_zoo_r = 5
m_zoa_r = 10
m_speed = 0.5
m_food = 0.1
m_pred = 0.1
m_noise = 0.1
class Agent:
def __init__(self, point, window ):
self.color = color_rgb(0,0,0)
......@@ -16,8 +49,7 @@ class Agent:
#evolvable
self.speed = 4
self.zor_r = 10#fix
self.zor_r = 5#fix
self.zoo_r = 20
self.zoa_r = 200
......@@ -57,7 +89,7 @@ class Predator:
self.line = Line(self.point, Point(self.point.getX() + self.Velocity_x, self.point.getY()+self.Velocity_y))
#evolvable
self.speed = 2
self.speed = 5.5
self.zor_r = 10#fix
#self.zoo_r = 20
......@@ -309,12 +341,13 @@ def checkBoundary(agent, winWidth, winHeight):
if x > 0 and y < winHeight and x < winWidth and y > 0:
agent.point = point
elif x <= 0 or x >= winWidth:
else:
if x <= 0 or x >= winWidth:
agent.Velocity_x = agent.Velocity_x * (-1)
agent.point.move(agent.Velocity_x,agent.Velocity_y)
elif y <= 0 or y >= winHeight:
if y <= 0 or y >= winHeight:
agent.Velocity_y = agent.Velocity_y * (-1)
agent.point.move(agent.Velocity_x,agent.Velocity_y)
return agent
......@@ -367,7 +400,7 @@ def update_couzin(agents,foods, predators,winWidth, winHeight, window):
#update predator
def update_predator(predator, agents, winWidth, winHeight, window):
print predator.zoa_r
#print predator.zoa_r
predator.tempV = check_prey(predator, agents)
radTurn = math.radians(predator.turn_angle)
......@@ -451,30 +484,68 @@ def generateWheel(agents):
return wheel
def pickParent(agents, wheel):
r + random.randrange(wheel[-1])
r = random.randrange(wheel[-1]) +1
def simulate():
winWidth = 500
winHeight = 500
for i in range(len(wheel)):
if r <= wheel[i]:
return agents[i]
window = GraphWin("Window", winWidth, winHeight)
def mutate(parent):
maxTime = 10000
point = Point(random.uniform(0,winWidth/2), random.uniform(0,winHeight/2))
child = Agent(point, window)
agentNum = 80
predNum = agentNum/10
child.Velocity_x = random.uniform(-1, 1)
child.Velocity_y = random.uniform(-1, 1)
foodCluster = 128
clusterSize = 8
clusterradius = 20
isFood = True
isPred = False
t = 0.1
child.zoa_r = random.uniform(parent.zoa_r - m_zoa_r, parent.zoa_r + m_zoa_r )
child.zoa_r = max (child.zoa_r, math.sqrt(As/(2*math.pi)) )
rr = 5
As = 200 * (rr**2)
Am = 5 * (rr**2) * t
child.zoo_r = random.uniform(parent.zoo_r - m_zoo_r, parent.zoo_r + m_zoo_r)
child.zoo_r = min( child.zoa_r ,max(child.zoo_r, child.zor_r))
child.speed = random.uniform(parent.speed - m_speed, parent.speed + m_speed)
child.speed = min( 5 ,max(child.speed, 1))
child.blind_angle = (360 - math.degrees(As/(child.zoa_r**2)) ) / 2
child.turn_angle = math.degrees(Am/(2*(child.speed**2)))
child.food_pref = random.uniform(parent.food_pref - m_food, parent.food_pref + m_food)
child.anti_pred = random.uniform(parent.anti_pred - m_pred, parent.anti_pred + m_pred)
child.noise = random.uniform(parent.noise - m_noise, parent.noise + m_noise)
return child
def nextGen_food(agents):
wheel = generateWheel(agents)
tempAgents = []
for i in range(agentNum):
parent = pickParent(agents, wheel)
child = mutate(parent)
tempAgents.append(child)
return tempAgents
#----------new generation for predator simulation
def pickParent_simple_random(agents):
r = random.randrange(len(agents))
return agents[r]
def nextGen_pred(agents):
tempAgents = []
for i in range(agentNum):
parent = pickParent_simple_random(agents)
child = mutate(parent)
tempAgents.append(child)
return tempAgents
def simulate():
# zones for couzin, vicsek
# radii of zones
# swarm: 10, 20, 200
......@@ -485,11 +556,14 @@ def simulate():
foods = []
predators = []
maxLife = 100
maxLife = 1000
global window
# first generation , random
for i in range (agentNum):
#p = Point(random.uniform(0,winWidth), random.uniform(0,winHeight))
agent = Agent(Point(random.uniform(0,winWidth), random.uniform(0,winHeight)) , window)
agent = Agent(Point(random.uniform(0,winWidth/2), random.uniform(0,winHeight/2)) , window)
agent.Velocity_x = random.uniform(-1, 1)
agent.Velocity_y = random.uniform(-1, 1)
......@@ -501,8 +575,8 @@ def simulate():
agent.blind_angle = (360 - math.degrees(As/(agent.zoa_r**2)) ) / 2
agent.turn_angle = math.degrees(Am/(2*(agent.speed**2)))
agent.food_pref = random.uniform(0,1)
agent.anti_pred = random.uniform(1,3)
agent.food_pref = random.uniform(0,5)
agent.anti_pred = random.uniform(0,5)
agent.noise = random.uniform(0,1)
......@@ -519,6 +593,9 @@ def simulate():
print "blind: "+str(agent.blind_angle)
print "zoa:" +str(agent.zoa_r)
"""
for j in range(generations):
if isPred:
for i in range(predNum):
predator = Predator(Point(random.uniform(0,winWidth), random.uniform(0,winHeight)) , window)
predator.Velocity_x = random.uniform(-1, 1)
......@@ -528,18 +605,65 @@ def simulate():
view_angle = 360-(predator.blind_angle*2)
predator.zoa_r = 20*math.sqrt(As/view_angle)
#predator.blind_angle = (360 - math.degrees(As/(predator.zoa_r**2)) ) / 2
predator.turn_angle = math.degrees(Am/(2*(predator.speed**2)))
predator.turn_angle = math.degrees(1.1*Am/(2*(predator.speed**2)))
predators.append(predator)
for i in range(maxTime):
#print i
print "generation " + str(j)
avgSpeed = 0
avgTurn = 0
avgFood = 0
avgZOA = 0
avgZOO = 0
avgBlind = 0
avgNoise = 0
for agent in agents:
avgSpeed += agent.speed
avgTurn += agent.turn_angle
avgFood += agent.food_pref
avgZOA += agent.zoa_r
avgZOO += agent.zoo_r
avgBlind += agent.blind_angle
avgNoise += agent.noise
avgSpeed /= agentNum
avgTurn /= agentNum
avgFood /= agentNum
avgZOA /= agentNum
avgZOO /= agentNum
avgBlind /= agentNum
avgNoise /= agentNum
print "speed: " +str(avgSpeed)
print "turn: " +str(avgTurn)
print "food pref: " +str(avgFood)
print "zoa: " +str(avgZOA)
print "zoo: " +str(avgZOO)
print "blind: " +str(avgBlind)
print "noise: "+str(avgNoise)
for agent in agents:
agent.point.undraw()
agent.point.draw(window)
#print agent.point
i = 0
widthTemp = winWidth
heightTemp = winHeight
if isFood:
widthTemp = winWidth/2
heightTemp = winHeight/2
for i in range(maxTime):
#food
if i == maxTime * 0.01 and isFood:
if i == maxTime * 0.025 and isFood:
for j in range (foodCluster):
x = random.uniform(clusterradius, winWidth-clusterradius)
y = random.uniform(clusterradius, winHeight- clusterradius)
x = random.uniform(widthTemp + clusterradius, winWidth-clusterradius)
y = random.uniform(heightTemp + clusterradius, winHeight- clusterradius)
for k in range (clusterSize):
xk = random.uniform(x - clusterradius, x + clusterradius)
yk = random.uniform(y - clusterradius, y + clusterradius)
......@@ -547,18 +671,21 @@ def simulate():
f.point.setFill("blue")
f.point.draw(window)
foods.append(f)
widthTemp = winWidth
heightTemp = winHeight
if i >= maxTime * 0.01 and isFood:
print len(foods)
print 4.0/8 * clusterSize * foodCluster
if len(foods) <= 4.0/8 * clusterSize * foodCluster:
if i >= maxTime * 0.025 and isFood:
#print len(foods)
#print 4.0/8 * clusterSize * foodCluster
if len(foods) <= 1.0/8 * clusterSize * foodCluster:
print "done eating"
break
#predator
if i >= maxTime * 0.01 and isPred:
if i >= maxTime * 0.025 and isPred:
if predators == []:
True
print "preds are done"
break
else:
if(predators[-1].hasEaten or predators[-1].lifeTime >= maxLife):
print "hasEaten"
......@@ -567,17 +694,26 @@ def simulate():
else:
#predators[-1].point.undraw()
predators[-1] = update_predator(predators[-1], agents, winWidth, winHeight, window)
print i
#print i
predators[-1].lifeTime += 1
#predators[-1].point.draw(window)
agents = update_couzin(agents, foods, predators, winWidth, winHeight, window)
agents = update_couzin(agents, foods, predators, widthTemp, heightTemp, window)
time.sleep(0.01*t)
print generateWheel(agents)
window.getMouse()
print "eating time: " + str(i)
window.close()
window = GraphWin("Window", winWidth, winHeight)
if isFood:
agents = nextGen_food(agents)
elif isPred:
agents = nextGen_pred(agents)
#print len(agents)
simulate()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment