Skip to content

Commit

Permalink
cashes fitness values for plants vastly increasing performance
Browse files Browse the repository at this point in the history
  • Loading branch information
william-index committed Nov 25, 2016
1 parent 27b803f commit d590181
Show file tree
Hide file tree
Showing 18 changed files with 47 additions and 19 deletions.
3 changes: 2 additions & 1 deletion Artist/PixelArtist.py
Expand Up @@ -2,6 +2,7 @@
from PIL import Image, ImageDraw, ImageOps, ImageFont
from GridSystem import GridSystem
from Settings import colors
from Life import Plant

@attr.s
class PixelArtist(object):
Expand All @@ -12,7 +13,7 @@ def drawPlantGeneration(self, lifeforms, filename, columns):

gridedForms = []
for lifeform in lifeforms:
gridedForms.append(self.grids.arrayToGrid(lifeform, columns))
gridedForms.append(self.grids.arrayToGrid(lifeform.dna, columns))

lifeFormWidth = len(gridedForms[0][0])
lifeFormHeight = len(gridedForms[0])
Expand Down
43 changes: 27 additions & 16 deletions God/God.py
@@ -1,6 +1,7 @@
import attr
from random import randint, shuffle
from Artist import GridSystem
from Life import Plant

@attr.s
class God(object):
Expand Down Expand Up @@ -56,7 +57,7 @@ def breed(self, lifeforms):
print "Breeding..."
nextGen = []

segments = self.pickRandomDivisor_(len(lifeforms[0]))
segments = self.pickRandomDivisor_(len(lifeforms[0].dna))
print "DNA Splice length: ", segments

segmentedPlants = self.sliceToSegments_(lifeforms, segments)
Expand All @@ -69,13 +70,14 @@ def breed(self, lifeforms):
parent2 = segmentedPlants[randint(0, len(segmentedPlants)-1)]#segmentedPlants[(i+1) % len(segmentedPlants)]
breeders = [parent1, parent2]

newPlant = []
newPlantDNA = []

for j in range(0, len(segmentedPlants[i])):
side = randint(0, 1)
newPlant += breeders[side][j]
newPlantDNA += breeders[side][j]

nextGen.append(newPlant)
plant = Plant(newPlantDNA)
nextGen.append(plant)

print len(nextGen), " offspring produced."
return nextGen
Expand All @@ -85,8 +87,8 @@ def sliceToSegments_(self, lifeforms, segments):

for plant in lifeforms:
plantSegments = []
for i in range(0, len(plant), segments):
plantSegments.append(plant[i:i + segments])
for i in range(0, len(plant.dna), segments):
plantSegments.append(plant.dna[i:i + segments])
segmentedPlants.append(plantSegments)

return segmentedPlants
Expand Down Expand Up @@ -120,7 +122,7 @@ def mutate(self, lifeforms):
else:
for i in range(0, numMutations):
newVal = hex(randint(0, 15) )[2:]
lifeform[randint(0, len(lifeform))-1] = newVal
lifeform.dna[randint(0, len(lifeform.dna))-1] = newVal

mutateLifeforms.append(lifeform)

Expand All @@ -132,7 +134,7 @@ def trimDeadCells(self, lifeforms):

for lifeform in lifeforms:
fixedLifeform = []
lifeFormGrid = self.grids.arrayToGrid(lifeform, self.lifeFormWidth)
lifeFormGrid = self.grids.arrayToGrid(lifeform.dna, self.lifeFormWidth)

# All energy is calculated by checking all cells
for y in range(0, self.lifeFormHeight):
Expand All @@ -144,20 +146,25 @@ def trimDeadCells(self, lifeforms):
for row in lifeFormGrid:
fixedLifeform += row

trimmedLifeforms.append(fixedLifeform)
plant = Plant(fixedLifeform)

trimmedLifeforms.append(plant)

return trimmedLifeforms

"""
Calculates the score for a lifeform.
Args:
lifeFormArray (Array) : lifeform DNA
lifeform (Plant) : Plant lifeform
Returns:
int: score for how successful a lifeform is
"""
def judgeLifeform(self, lifeFormArray):
lifeFormGrid = self.grids.arrayToGrid(lifeFormArray, self.lifeFormWidth)
def judgeLifeform(self, lifeform):
if lifeform.fitness:
return lifeform.fitness

lifeFormGrid = self.grids.arrayToGrid(lifeform.dna, self.lifeFormWidth)

# Base values of resources needed for any lifeform
energyNeeded = 100
Expand Down Expand Up @@ -188,6 +195,8 @@ def judgeLifeform(self, lifeFormArray):
# bonus for living cells
score += self.countRealCells_(lifeFormGrid)

lifeform.fitness = score

return score


Expand Down Expand Up @@ -348,13 +357,15 @@ def createLife(self):


def createRandomLifeform(self, size):
lifeForm = []
lifeFormDNA = []

for i in range(0, size[0]*size[1]):
# Favors some empty space for starting generation
if randint(0, 1) > 0:
lifeForm.append('0')
lifeFormDNA.append('0')
else:
lifeForm.append(hex(randint(0, 15) )[2:])
lifeFormDNA.append(hex(randint(0, 15) )[2:])

plant = Plant(lifeFormDNA)

return lifeForm
return plant
15 changes: 15 additions & 0 deletions Life/Plant.py
@@ -0,0 +1,15 @@
import attr

@attr.s
class Plant(object):
dna = attr.ib()
fitness = attr.ib()
_fitness = False

@property
def fitness(self):
return self._fitness

@fitness.setter
def fitness(self, newFitness):
self._fitness = newFitness
1 change: 1 addition & 0 deletions Life/__init__.py
@@ -0,0 +1 @@
from Plant import Plant
Binary file modified art/rendered/.DS_Store
Binary file not shown.
Binary file modified art/rendered/untouched_parents_squared_success_gen_0.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified art/rendered/untouched_parents_squared_success_gen_10.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified art/rendered/untouched_parents_squared_success_gen_19.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions main.py
Expand Up @@ -16,8 +16,8 @@

# Default settings
generations = 20
initialPopSize = 20
survivalSize = 8
initialPopSize = 50
survivalSize = 10
plantWidth = 16
plantHeight = 32
rootStart = 0.8
Expand Down

0 comments on commit d590181

Please sign in to comment.