コード例 #1
0
ファイル: main.py プロジェクト: pleger/CityCA
"""
This script is used to execute the cellular automaton Chile
"""

from Automaton import Automaton
from Simulation import Simulation
from Analyzer import Analyzer
from Agent import Agent

# TODO: USE A GUI TO CONFIG THESE PARAMETERS
COLUMNS = 30
ROWS = 30
POPULATION = 100
ITERATIONS = 20

# executing the main method of the code
automaton = Automaton(ROWS, COLUMNS)
analyzer = Analyzer(automaton)
automaton.createPopulation(POPULATION, Agent.randomRangeRadiumUnif(1, 5))

simulation = Simulation(automaton, True)
simulation.start(ITERATIONS)
rankings = analyzer.getRankingOfPopulation()
print analyzer.getLinearRegressionData(False)
コード例 #2
0
ファイル: Tests.py プロジェクト: pleger/CityCA
class TestFitness(unittest.TestCase):
    def setUp(self):
        self.automaton = Automaton(ROWS, COLUMNS)
        self.simulation = Simulation(self.automaton, False)

    def test_infiniteRadium(self):

        self.automaton.reinit(ROWS, COLUMNS)
        self.automaton.createPopulation(POPULATION, Agent.infiniteRadium())
        self.simulation.start(ITERATIONS)
        self.assertTrue(self.automaton.convergence, "IT IS CONVERGENCE")
        array = self.automaton.getMatrixOfPopulation()
        # print repr(self.automaton) + " " + repr(array.max())
        self.assertEqual(POPULATION, len(self.automaton.getAgents()), "ALL AGENTS")

    def test_withZeroRadium(self):
        self.automaton.reinit(ROWS, COLUMNS)
        self.automaton.createPopulation(POPULATION, Agent.constRadium(0))
        self.simulation.start(3)
        self.assertTrue(self.automaton.convergence, "IT IS CONVERGENCE")

    def test_random(self):

        self.automaton.reinit(ROWS, COLUMNS)
        self.automaton.createPopulation(POPULATION, Agent.infiniteRadium(), Agent.randomFitness)
        self.simulation.start(ITERATIONS)
        self.assertFalse(self.automaton.convergence, " IT IS NOT CONVERGENCE")

    def test_circularRangeInf(self):
        self.automaton.reinit(ROWS, COLUMNS)
        self.automaton.enableCircularGrid()
        rr = [0, 1, 2, 3, 4, 5, 17, 18, 19]
        rc = [0, 1, 2, 3, 4, 5, 27, 28, 29]

        ranges = self.automaton.getRanges(1, 1, 4)
        ranges[0].sort()
        ranges[1].sort()

        self.assertEquals(rr, ranges[0], "Not same ranges for rows:" + repr(ranges[0]))
        self.assertEquals(rc, ranges[1], "Not same ranges for columns:" + repr(ranges[1]))

    def test_circularRangeSup(self):
        self.automaton.reinit(ROWS, COLUMNS)
        self.automaton.enableCircularGrid()
        rr = [0, 1, 2, 14, 15, 16, 17, 18, 19]
        rc = [0, 1, 23, 24, 25, 26, 27, 28, 29]

        ranges = self.automaton.getRanges(18, 27, 4)
        ranges[0].sort()
        ranges[1].sort()

        self.assertEquals(rr, ranges[0], "Not same ranges for rows:" + repr(ranges[0]))
        self.assertEquals(rc, ranges[1], "Not same ranges for columns:" + repr(ranges[1]))

    def test_circularRangeAndWithoutRandomInf(self):
        self.automaton.reinit(ROWS, COLUMNS)
        self.automaton.enableCircularGrid()
        self.automaton.disableRandomVisitingOfCells()

        rr = [17, 18, 19, 0, 1, 2, 3, 4, 5]
        rc = [27, 28, 29, 0, 1, 2, 3, 4, 5]

        ranges = self.automaton.getRanges(1, 1, 4)

        self.assertEquals(rr, ranges[0], "Not same ranges for rows:" + repr(ranges[0]))
        self.assertEquals(rc, ranges[1], "Not same ranges for columns:" + repr(ranges[1]))

    def test_circularRangeAndWithoutRandomSup(self):
        self.automaton.reinit(ROWS, COLUMNS)
        self.automaton.enableCircularGrid()
        self.automaton.disableRandomVisitingOfCells()

        rr = [14, 15, 16, 17, 18, 19, 0, 1, 2]
        rc = [23, 24, 25, 26, 27, 28, 29, 0, 1]

        ranges = self.automaton.getRanges(18, 27, 4)

        self.assertEquals(rr, ranges[0], "Not same ranges for rows:" + repr(ranges[0]))
        self.assertEquals(rc, ranges[1], "Not same ranges for columns:" + repr(ranges[1]))

    def test_rangeWithoutRandomInf(self):
        self.automaton.reinit(ROWS, COLUMNS)

        rr = [0, 1, 2, 3, 4, 5]
        rc = [0, 1, 2, 3, 4, 5]

        ranges = self.automaton.getRanges(1, 1, 4)
        ranges[0].sort()
        ranges[1].sort()

        self.assertEquals(rr, ranges[0], "Not same ranges for rows:" + repr(ranges[0]))
        self.assertEquals(rc, ranges[1], "Not same ranges for columns:" + repr(ranges[1]))

    def test_rangeWithoutRandomSup(self):
        self.automaton.reinit(ROWS, COLUMNS)

        rr = [14, 15, 16, 17, 18, 19]
        rc = [23, 24, 25, 26, 27, 28, 29]

        ranges = self.automaton.getRanges(18, 27, 4)
        ranges[0].sort()
        ranges[1].sort()

        self.assertEquals(rr, ranges[0], "Not same ranges for rows:" + repr(ranges[0]))
        self.assertEquals(rc, ranges[1], "Not same ranges for columns:" + repr(ranges[1]))