Пример #1
0
def main():
    try:
        pop = Population.load_from_file(constants.res_loc("networks") + pop_name + ".pop")
    except:
        seed = random.randint(0, 1000)
        pop = Population(seed, 100)

    pool = Pool(number_of_processes)
    while True:
        worlds = []
        for net in pop.current_generation:
            nWorld = NeuronalWorld(pop.seed, net)
            worlds.append(nWorld)
            nWorld.generatePlatform()

        # evaluate all neuronal worlds
        fitnesses = pool.map(evaluate, worlds)
        # set the fitness (because multiprocessing)
        for world, fit in zip(worlds, fitnesses):
            world.nn.fitness = fit

        path = constants.res_loc("networks") + pop.name + ".pop"
        pop.save_to_file(path)
        best_fitness =  max(nn.fitness for nn in pop.current_generation)
        print("best fitness:", best_fitness)
        pop.create_next_generation()
        pop.generation_count += 1
    def __init__(self, networkContext, setContextFunc, popFileName):
        BaseContext.__init__(self, setContextFunc)
        self._networkContext = networkContext
        self._popFileName = popFileName
        self._pop = Population.load_from_file(
            constants.res_loc("networks") + popFileName)

        self._background = texturehandler.fillSurface(
            pygame.Surface(constants.screenSize),
            random.choice(texturehandler.blocks), (64, 64))

        best_fitness = max(n.fitness for n in self._pop.current_generation)
        fontObj = Font(None, 40)
        self.addElements({
            "lCaption":
            GuiLabel.createCentered(10, Font(None, 60), self._pop.name),
            "lSeed":
            GuiLabel(240, 90, fontObj, "Current seed:"),
            "tfSeed":
            GuiNumberTextfield(450,
                               87,
                               SysFont("Monospace", 24, bold=True),
                               width=140,
                               text=str(self._pop.seed)),
            "bSeed":
            GuiButton(600, 87, fontObj, "Set Seed", width=200,
                      height=32).connect(self.buttonSetSeed),
            "lSize":
            GuiLabel(
                240, 130, fontObj, "Population size: {}".format(
                    len(self._pop.current_generation))),
            "lFitness":
            GuiLabel(240, 170, fontObj,
                     "Highest fitness: {0:.2f}".format(best_fitness)),
            "lGeneration":
            GuiLabel(240, 210, fontObj,
                     "Generation: {}".format(self._pop.generation_count)),
            "bDelete":
            GuiButton(390,
                      470,
                      fontObj,
                      "Delete (hold CTRL)",
                      width=300,
                      height=40,
                      startColor=(255, 50, 50),
                      endColor=(255, 100, 100)).connect(self.buttonDelete),
            "bShowResult":
            GuiButton(240, 530, fontObj, "Show Result",
                      width=285).connect(self.buttonShowResult),
            "bResumeTraining":
            GuiButton(555, 530, fontObj, "Resume Training",
                      width=285).connect(self.buttonResumeTraining),
            "bBack":
            GuiButton(240, 600, fontObj, "Back").connect(self.buttonBack)
        })

        # enable key repeats
        pygame.key.set_repeat(500, 50)