Skip to content

Commit

Permalink
Cell type assignment/checking now done in Cell class
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Connelly committed Aug 25, 2011
1 parent 9cd7eec commit 08e3964
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 48 deletions.
22 changes: 19 additions & 3 deletions seeds/Cell.py
Expand Up @@ -8,6 +8,10 @@
__author__ = "Brian Connelly <bdc@msu.edu>"
__credits__ = "Brian Connelly"

import random

from seeds.SEEDSError import *


class Cell(object):
"""
Expand Down Expand Up @@ -51,7 +55,11 @@ class Cell(object):
"""

def __init__(self, experiment, population, node, name=None, label=None):
types = []
type_colors = []
max_types = 0

def __init__(self, experiment, population, node, type=None, name=None, label=None):
"""Initialize a Cell object
Parameters:
Expand All @@ -63,6 +71,8 @@ def __init__(self, experiment, population, node, name=None, label=None):
*node*
The ID of the node of the population topology graph on which this
Cell resides
*type*
The type of Cell this is (specific to the Cell class used)
*name*
The name of the Cell type
*label*
Expand All @@ -76,10 +86,16 @@ def __init__(self, experiment, population, node, name=None, label=None):
self.node = node
self.name = name
self.label = label
self.type = None
self.max_types = 0
self.type_colors = ['r','g','b','y','c', 'm', 'k']

if type:
if type not in range(len(self.types)):
raise CellTypeError(type)
else:
self.type = type
else:
self.type = random.randint(0, len(self.types))

if self.label:
self.config_section = "%s:%s" % (self.name, self.label)
else:
Expand Down
18 changes: 16 additions & 2 deletions seeds/SEEDSError.py
Expand Up @@ -72,7 +72,22 @@ def __init__(self, cell):
self.cell = cell

def __str__(self):
return "Cell type '%s' not found" % (self.cell)
return "Cell plugin '%s' not found" % (self.cell)

class CellTypeError(SEEDSError):
"""Error to be raised when an invalid Cell type is used
Attributes:
*celltype*
The attempted cell type ID
"""

def __init__(self, celltype):
self.celltype = celltype

def __str__(self):
return "Cell type '%s' not found" % (self.celltype)


class ResourceCellPluginNotFoundError(PluginNotFoundError):
Expand Down Expand Up @@ -174,4 +189,3 @@ def __init__(self, message):

def __str__(self):
return self.message

12 changes: 3 additions & 9 deletions seeds/plugins/cell/GameOfLifeCell.py
Expand Up @@ -36,7 +36,7 @@ class GameOfLifeCell(Cell):
ALIVE = 0
DEAD = 1

def __init__(self, experiment, population, node, type=-1, name="GameOfLifeCell", label=None):
def __init__(self, experiment, population, node, type=None, name="GameOfLifeCell", label=None):
"""Initialize a GameOfLifeCell object
The type for the cell is selected at random.
Expand All @@ -50,21 +50,15 @@ def __init__(self, experiment, population, node, type=-1, name="GameOfLifeCell",
*node*
The ID of the node on which this Cell resides
*type*
The type of cell to initialize (-1 for random)
The type of cell to initialize (If none provided, type randomly assigned)
*name*
The name of this Cell type
*label*
A unique label for configuring this Cell type
"""

super(GameOfLifeCell, self).__init__(experiment, population, node=node, name=name, label=label)

if type == -1:
self.type = random.randint(0, len(self.types)-1)
else:
self.type = type

super(GameOfLifeCell, self).__init__(experiment, population, node=node, type=type, name=name, label=label)
self.population.increment_type_count(self.type)

def __str__(self):
Expand Down
12 changes: 3 additions & 9 deletions seeds/plugins/cell/Kerr07Cell.py
Expand Up @@ -73,7 +73,7 @@ class Kerr07Cell(Cell):
RESISTANT = 2
PRODUCER = 3

def __init__(self, experiment, population, node, type=-1, name="Kerr07Cell", label=None):
def __init__(self, experiment, population, node, type=None, name="Kerr07Cell", label=None):
"""Initialize a Kerr07Cell object
The type for the cell is selected at random.
Expand All @@ -87,21 +87,15 @@ def __init__(self, experiment, population, node, type=-1, name="Kerr07Cell", lab
*node*
The ID of the node on which this Cell resides
*type*
The type of cell to initialize (-1 for random)
The type of cell to initialize (randomly chosen if not provided)
*name*
The name of this Cell type
*label*
A unique label for configuring this Cell type
"""

super(Kerr07Cell, self).__init__(experiment, population, node=node, name=name, label=label)

if type == -1:
self.type = random.randint(0,len(self.types)-1)
else:
self.type = type

super(Kerr07Cell, self).__init__(experiment, population, node=node, type=type, name=name, label=label)
self.population.increment_type_count(self.type)

self.ds = self.experiment.config.getfloat(self.config_section, 'death_sensitive')
Expand Down
6 changes: 3 additions & 3 deletions seeds/plugins/cell/QuasispeciesCell.py
Expand Up @@ -73,7 +73,7 @@ class QuasispeciesCell(Cell):
NARROW = 1
WIDE = 2

def __init__(self, experiment, population, node, type=-1, name="QuasispeciesCell", label=None):
def __init__(self, experiment, population, node, type=None, name="QuasispeciesCell", label=None):
"""Initialize a QuasispeciesCell object
The type for the cell is selected at random.
Expand All @@ -87,15 +87,15 @@ def __init__(self, experiment, population, node, type=-1, name="QuasispeciesCell
*node*
The ID of the node on which this Cell resides
*type*
The type of cell to initialize (-1 for random)
The type of cell to initialize (randomly chosen if not provided)
*name*
The name of this Cell type
*label*
A unique label for configuring this Cell type
"""

super(QuasispeciesCell, self).__init__(experiment, population, node=node, name=name, label=label)
super(QuasispeciesCell, self).__init__(experiment, population, node=node, type=type, name=name, label=label)

self.death_rate = self.experiment.config.getfloat(self.config_section, 'death_rate')
self.genotype_length = self.experiment.config.getint(self.config_section, 'genotype_length')
Expand Down
17 changes: 6 additions & 11 deletions seeds/plugins/cell/RPSCell.py
Expand Up @@ -48,7 +48,7 @@ class RPSCell(Cell):
PAPER = 1
SCISSORS = 2

def __init__(self, experiment, population, node, type=-1, name="RPSCell", label=None):
def __init__(self, experiment, population, node, type=None, name="RPSCell", label=None):
"""Initialize a RPSCell object
The type for the cell is selected at random.
Expand All @@ -62,21 +62,15 @@ def __init__(self, experiment, population, node, type=-1, name="RPSCell", label=
*node*
The ID of the node on which this Cell resides
*type*
The type of cell to initialize (-1 for random)
The type of cell to initialize (assigned randomly if not provided)
*name*
The name of this Cell type
*label*
A unique label for configuring this Cell type
"""

super(RPSCell, self).__init__(experiment, population, node=node, name=name, label=label)

if type == -1:
self.type = random.randint(0, len(self.types)-1)
else:
self.type = type

super(RPSCell, self).__init__(experiment, population, node=node, type=type, name=name, label=label)
self.population.increment_type_count(self.type)

self.distance_dependent = self.experiment.config.getboolean(section=self.config_section,
Expand Down Expand Up @@ -127,11 +121,12 @@ def update(self):
if self.type == self.ROCK and competitor.type == self.PAPER:
self.type = self.PAPER
self.population.update_type_count(self.ROCK, self.type)
self.id = self.population.get_cell_id()
elif self.type == self.PAPER and competitor.type == self.SCISSORS:
self.type = self.SCISSORS
self.population.update_type_count(self.PAPER, self.type)
self.id = self.population.get_cell_id()
elif self.type == self.SCISSORS and competitor.type == self.ROCK:
self.type = self.ROCK
self.population.update_type_count(self.SCISSORS, self.type)


self.id = self.population.get_cell_id()
20 changes: 9 additions & 11 deletions templates/Cell-Template.py
Expand Up @@ -61,21 +61,19 @@ class TODO-CellTypeName(Cell):
# during this step as well as any other properties associated with this
# Cell. This is often the stage where configuration values are read.

def __init__(self, experiment, population, id, type=-1,
def __init__(self, experiment, population, node, type=None,
name="TODO-CellTypeName", label=None):
"""
TODO: documentation
"""

# Call the Cell constructor, assigning properties common to all cells.
super(RPSCell, self).__init__(experiment, population, id, name=name, label=label)

# Assign the organism the specified type, or pick one at random
if type == -1:
self.type = random.randint(0, len(self.types)-1)
else:
self.type = type

# The type will be assigned by Cell's constructor and stored in self.type

super(TODO-CellTypeName, self).__init__(experiment, population,
node=node, type=type,
name=name, label=label)

# Keep track of how many organisms there are of this type. You will
# probably want to keep this code.
self.population.increment_type_count(self.type)
Expand Down Expand Up @@ -107,8 +105,8 @@ def update(self):
"""

# If state is updated based on the composition of the neighborhood,
# this code gets a list of neighboring Cell objects.
neighbors = self.get_neighbors()
# this code updates the list of neighboring Cell objects.
self.neighbors = self.get_neighbors()

# If a Cell's state depends on the level of some resource, at that
# point in space, the following sample code gets that resource and sets
Expand Down

0 comments on commit 08e3964

Please sign in to comment.