Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def __init__(self, env, x = 0, y = 0, metabolism = 4, vision = 6, endowment = 25
def getEnv(self):
return self.env

def setLocation(self, (x, y)):
def setLocation(self, t):
(x, y) = t
self.x = x
self.y = y

Expand Down Expand Up @@ -81,7 +82,8 @@ def getSexe(self):
def setFertility(self, fertility):
self.fertility = fertility

def setTags(self, (tags, tagsLength)):
def setTags(self, t):
(tags, tagsLength) = t
self.tags = tags
self.tagsLength = tagsLength
self.tribe = round(float(bin(tags).count('1')) / float(tagsLength))
Expand Down Expand Up @@ -211,7 +213,8 @@ def findFreeLocationAround(self, x, y):
return None

# Cross-over offspring from two parents: parent1 and parent2
def createChild(self, parent, (x, y)):
def createChild(self, parent, t):
(x, y) = t
# cross-over parents genetics
genitors = [self, parent]
metabolism = genitors[random.randint(0,1)].metabolism
Expand Down
33 changes: 22 additions & 11 deletions environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,30 @@ class Environment:
classdocs
'''

def __init__(self, (width, height)):
def __init__(self, t):
'''
Constructor
'''
(width, height) = t
self.gridWidth = width
self.gridHeight = height
self.grid = [[[0, 0, None] for i in range(width)] for j in range(height)]

def setCapacity(self, (i, j), value):
def setCapacity(self, t, value):
(i, j) = t
self.grid[i][j][0] = value

def getCapacity(self, (i, j)):
def getCapacity(self, t):
(i, j) = t
return int(self.grid[i][j][0])

def decCapacity(self, (i,j), value):
def decCapacity(self, t, value):
(i, j) = t
self.grid[i][j][0] = max(0, self.grid[i][j][0] - value)

def addFoodSite(self, (si, sj, r), maxCapacity):
def addFoodSite(self, t, maxCapacity):
# calculate radial dispersion of capacity from maxCapacity to 0
(si, sj, r) = t
distance = lambda di, dj : sqrt(di*di + dj*dj)
D = distance(max(si, self.gridWidth - si), max(sj, self.gridHeight - sj)) * (r/float(self.gridWidth))
for i,j in product(range(self.gridWidth), range(self.gridHeight)):
Expand All @@ -43,8 +48,9 @@ def grow(self, alpha):
for i,j in product(range(self.gridWidth), range(self.gridHeight)):
self.grid[i][j][0] = min(self.grid[i][j][0] + alpha, self.grid[i][j][1])

def growRegion(self, (imin, jmin, imax, jmax), alpha):
def growRegion(self, t, alpha):
# grow region to maxCapacity with alpha
(imin, jmin, imax, jmax) = t
imin = max(imin, 0)
jmin = max(jmin, 0)
imax = min(imax + 1, self.gridWidth)
Expand All @@ -53,19 +59,24 @@ def growRegion(self, (imin, jmin, imax, jmax), alpha):
for i in range (imin, imax):
self.grid[i][j][0] = min(self.grid[i][j][0] + alpha, self.grid[i][j][1])

def setAgent(self, (i, j), agent):
def setAgent(self, t, agent):
(i, j) = t
self.grid[i][j][2] = agent

def getAgent(self, (i, j)):
def getAgent(self, t):
(i, j) = t
return self.grid[i][j][2]

def isLocationValid(self, (i, j)):
def isLocationValid(self, t):
(i, j) = t
return (i >= 0 and i < self.gridWidth and j >= 0 and j < self.gridHeight)

def isLocationFree(self, (i, j)):
def isLocationFree(self, t):
(i, j) = t
return (self.grid[i][j][2] == None)

def getRandomFreeLocation(self,(xmin, xmax, ymin, ymax)):
def getRandomFreeLocation(self, t):
(xmin, xmax, ymin, ymax) = t
# build a list of free locations i.e. where env.getAgent(x,y) == None
# we don't use a global list and we re-build the list each time
# because init a new agent is much less frequent than updating agent's position (that would require append / remove to the global list)
Expand Down
4 changes: 2 additions & 2 deletions sugarscape.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def removeAgent(self, agent):
if initAgent(agent, tags, self.findDistribution(tags)):
self.env.setAgent(agent.getLocation(), agent)
else:
print "initAgent failed!"
print("initAgent failed!")
self.agents.remove(agent)
else:
self.agents.remove(agent)
Expand Down Expand Up @@ -478,7 +478,7 @@ def mainLoop(self):

# display infos
if update:
print "Iteration = ", self.iteration, "; fps = ", framerate, "; Seasons (N,S) = ", self.season, "; Population = ", len(self.agents), " - press F12 to pause."
print("Iteration = ", self.iteration, "; fps = ", framerate, "; Seasons (N,S) = ", self.season, "; Population = ", len(self.agents), " - press F12 to pause.")

'''
Main
Expand Down
2 changes: 1 addition & 1 deletion wdgAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

@author: rv
'''
from Tkinter import *
from tkinter import *

class WdgAgent():
'''
Expand Down
2 changes: 1 addition & 1 deletion wdgPopulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@author: rv
'''

from Tkinter import *
from tkinter import *

class WdgPopulation():
'''
Expand Down
2 changes: 1 addition & 1 deletion wdgWealth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

@author: rv
'''
from Tkinter import *
from tkinter import *

class WdgWealth():
'''
Expand Down