Example #1
0
    def test_populate_with_organisms_not_existing_organisms_provided(self):
        # if more organisms occupy same element, only lucky one will live on
        organism_l = [Organism(100, -20, 1), Organism(1, -10, 2)]
        self.world.organism_l = organism_l

        with self.assertRaises(WorldInternalError):
            self.world.populate_initial_organisms()
Example #2
0
    def test_write_state_to_xml_file_does_not_exist(self):
        state = State(5, 4, 3, [Organism(3, 2, 1), Organism(3, 1, 2)])

        output_file = '/note/existing/nofile.xml'

        with self.assertRaises(XMLFileError):
            self.xml_handler.write_state_to_xml(output_file, state, 1)
Example #3
0
    def test_start_not_existing_organisms_provided(self):
        self.initial_state.organism_l = [
            Organism(100, -20, 1), Organism(1, -10, 2)
        ]
        self.game = Game(self.io_handler, self.initial_state)

        with self.assertRaises(GameRuntimeError):
            self.game.start()
Example #4
0
    def test_populate_with_organisms_two_occupy_same_element(self):
        # if two organisms occupy same element, one of them must die
        organism_l = [Organism(1, 1, 1), Organism(1, 1, 2), Organism(2, 2, 2)]
        self.world.organism_l = organism_l
        initial_conflict = self.world.populate_initial_organisms()

        self.assertTrue(initial_conflict)
        self.assertEqual(len(self.world.organism_l), 2)
        self.assertEqual(len(self.world._get_all_organisms()), 2)
Example #5
0
    def test_write_state_to_xml_success(self):
        original_state = State(5, 4, 3, [Organism(3, 2, 1), Organism(3, 1, 2)])

        # test with temp file should be enough
        with NamedTemporaryFile(delete=False) as test_write_file:
            self.xml_handler.write_state_to_xml(test_write_file,
                                                original_state, 3)

        with open(test_write_file.name, 'r') as test_read_file:
            state = self.xml_handler.read_state_from_xml(test_read_file)

            self.assertEqual(state.cells_cnt, original_state.cells_cnt)
            self.assertEqual(state.species_cnt, original_state.species_cnt)
            self.assertEqual(state.iterations_cnt,
                             original_state.iterations_cnt)
Example #6
0
    def setUp(self):
        self.xml_string = """<?xml version="1.0" encoding="UTF-8"?><life><world><cells>5</cells>
            <species>3</species><iterations>3</iterations></world><organisms><organism><x_pos>0</x_pos>
            <y_pos>0</y_pos><species>2</species></organism><organism><x_pos>1</x_pos><y_pos>0</y_pos>
            <species>2</species></organism><organism><x_pos>4</x_pos><y_pos>0</y_pos><species>1</species>
            </organism><organism><x_pos>0</x_pos><y_pos>1</y_pos><species>2</species></organism><organism>
            <x_pos>2</x_pos><y_pos>1</y_pos><species>1</species></organism><organism><x_pos>3</x_pos>
            <y_pos>1</y_pos><species>1</species></organism></organisms></life>"""
        self.input_file = StringIO(self.xml_string)

        self.io_handler = GameIOHandler(self.input_file,
                                        self.OUT_FILE,
                                        keep_out_file_open=False)

        self.original_state = State(
            5, 4, 3, [Organism(3, 2, 1), Organism(3, 1, 2)])
    def setUp(self):
        self.species_occurrence_d = {1: 2, 2: 3, 3: 1, 4: 4, 5: 6}
        self.original_organism = Organism(0, 1, 1)

        self.survival_rule = EvolutionSurvivalRule()
        self.isolation_rule = EvolutionIsolationRule()
        self.overcrowding_rule = EvolutionOvercrowdingRule()
        self.birth_rule = EvolutionBirthRule()
Example #8
0
    def setUp(self):
        world_grid = WorldGrid(5, 5)
        self.original_organism_l = [
            Organism(2, 0, 1),
            Organism(1, 1, 1),
            Organism(2, 1, 1),
            Organism(3, 1, 2),
            Organism(1, 2, 2),
            Organism(2, 2, 2),
            Organism(3, 2, 2),
            Organism(0, 3, 2),
            Organism(1, 3, 2),
            Organism(3, 3, 2)
        ]

        rules_engine = EvolutionRulesEngine()

        self.world = World(world_grid, self.original_organism_l, rules_engine)
Example #9
0
    def setUp(self):
        self.original_organism_l = [
            Organism(2, 0, 1),
            Organism(1, 1, 1),
            Organism(2, 1, 1),
            Organism(3, 1, 2),
            Organism(1, 2, 2),
            Organism(2, 2, 2),
            Organism(3, 2, 2),
            Organism(0, 3, 2),
            Organism(1, 3, 2),
            Organism(3, 3, 2)
        ]
        self.initial_state = State(5, 2, 2, self.original_organism_l)
        # we set out file as input file in order to check final state of the game
        self.io_handler = GameIOHandler('dummy.xml', self.OUT_FILE)

        self.game = Game(self.io_handler, self.initial_state)
Example #10
0
    def setUp(self):
        self.world_grid = WorldGrid(4, 4)

        self.organism = Organism(0, 2, 1)
        self.organism_wrong_x = Organism(-1, 2, 1)
        self.organism_wrong_y = Organism(1, -3, 1)
        self.organism_wrong_xy = Organism(-1, -5, 1)
        self.organism_out_x = Organism(10, 2, 1)
        self.organism_out_y = Organism(2, 20, 1)
        self.organism_out_xy = Organism(22, 20, 1)
Example #11
0
 def setUp(self):
     self.organism_l = [
         Organism(0, 2, 1),
         Organism(-1, 2, 1),
         Organism(1, -3, 1),
         Organism(-1, -5, 1),
         Organism(10, 2, 2),
         Organism(2, 20, 2)
     ]
Example #12
0
    def _read_element_organism(self, organism):
        """Parses the element organism.

        Attributes:
            organism (etree.Element): XML element organism.

        Returns:
            organism (Organism): Parsed Organism.
        """
        x, y, species = None, None, None

        for child in organism:
            if child.tag == self.ELEMENT_X_POS:
                x = child.text
            elif child.tag == self.ELEMENT_Y_POS:
                y = child.text
            elif child.tag == self.ELEMENT_SPECIES:
                species = child.text

        return Organism(int(x), int(y), int(species))
Example #13
0
    def test_populate_with_organisms_more_occupy_same_element(self):
        # if more organisms occupy same element, only lucky one will live on
        organism_l = [
            Organism(1, 1, 1),
            Organism(1, 1, 2),
            Organism(2, 2, 2),
            Organism(2, 2, 2),
            Organism(2, 2, 2),
            Organism(2, 2, 2)
        ]
        self.world.organism_l = organism_l
        initial_conflict = self.world.populate_initial_organisms()

        self.assertTrue(initial_conflict)
        self.assertEqual(len(self.world.organism_l), 2)
        self.assertEqual(len(self.world._get_all_organisms()), 2)
Example #14
0
    def apply(self, organism, species_occurrence_d, **kwargs):
        """Overrides method derived from base class.

        Note:
        Organism will be born only if the rule was applied.

        Attributes:
            organism (Organism): Organism or (None) on which the rule will be applied.
            species_occurrence_d (dict): Occurrence dict indicating occurrence
                of species among organisms.Indicating
            **kwargs: Keyword arguments contain the cell (x|y) for organism to be born at.

        Returns:
            organism (Organism): Born organism, None otherwise.
            applied (bool): Indicates if the rule was applied.

        Raises:
            EvolutionRuleError: If organism does not exist.
        """
        if organism:
            raise EvolutionRuleError('Not applicable - organism must not exist.')

        evolved_organism = None
        applied = False
        birth_species_candidate_l = []

        if species_occurrence_d:
            for species, occurrence in species_occurrence_d.iteritems():
                if occurrence == 3:
                    birth_species_candidate_l.append(species)

        if birth_species_candidate_l:
            # if there are 3 organisms with same species, choose one and give a birth 
            random_species = random.choice(birth_species_candidate_l)
            cell = kwargs.get('cell')
            evolved_organism = Organism(cell[0], cell[1], random_species)
            applied = True

        return evolved_organism, applied
Example #15
0
    def test_state_is_valid(self):
        state = State(5, 4, 3, [Organism(3, 2, 1), Organism(3, 1, 2)])
        state_final = State(5, 4, 0, [Organism(3, 2, 1), Organism(3, 1, 2)])

        self.assertTrue(state.is_valid())
        self.assertTrue(state_final.is_valid())
Example #16
0
    def setUp(self):
        self.rules_engine = EvolutionRulesEngine()

        self.original_organism = Organism(0, 1, 1)
        self.neighboring_organism_l = [Organism(0, 2, 1), Organism(1, 2, 1), Organism(1, 1, 1)]