diff --git a/agent.py b/agent.py index cb160af..4a5f67e 100644 --- a/agent.py +++ b/agent.py @@ -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 @@ -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)) @@ -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 diff --git a/environment.py b/environment.py index 5773128..4fb664c 100644 --- a/environment.py +++ b/environment.py @@ -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)): @@ -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) @@ -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) diff --git a/sugarscape.py b/sugarscape.py index bd25e16..1b4f591 100644 --- a/sugarscape.py +++ b/sugarscape.py @@ -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) @@ -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 diff --git a/wdgAgent.py b/wdgAgent.py index c92993e..c0ff80b 100644 --- a/wdgAgent.py +++ b/wdgAgent.py @@ -3,7 +3,7 @@ @author: rv ''' -from Tkinter import * +from tkinter import * class WdgAgent(): ''' diff --git a/wdgPopulation.py b/wdgPopulation.py index 1f3653f..f39c398 100644 --- a/wdgPopulation.py +++ b/wdgPopulation.py @@ -4,7 +4,7 @@ @author: rv ''' -from Tkinter import * +from tkinter import * class WdgPopulation(): ''' diff --git a/wdgWealth.py b/wdgWealth.py index 9bb0ba0..39fff2a 100644 --- a/wdgWealth.py +++ b/wdgWealth.py @@ -3,7 +3,7 @@ @author: rv ''' -from Tkinter import * +from tkinter import * class WdgWealth(): '''