Exemplo n.º 1
0
    def StartEvolution(
        self,
        name,
        date,
        type,
        description,
        flag,
        num_population,
        num_robots,
        num_levels,
        num_gen,
        block_size,
        gen_threshold,
        actual_gen,
        player,
        mutation,
        cross,
        wrld,
    ):

        # ATENCAO!! Deve existir pelo menos 1 GameWorld
        # depois arrumo essa verificacao no script do banco, mas agora PRECISA ser assim

        # get World config
        gameworld = GameWorld.objects.get(id=wrld)
        my_world = World(gameworld.m, gameworld.n)
        my_world.MaxArea(gameworld.max_areas)
        my_world.MaxUnits(gameworld.max_units)
        my_world.Production(gameworld.prod_unit0, gameworld.prod_unit1)
        my_world.Costs(gameworld.cost_gateway, gameworld.cost_unit0, gameworld.cost_unit1)

        for area in Area.objects.all().filter(world=gameworld.id):
            my_world.CreateArea(area.x, area.y, area.length)

        # declare Genetic Algorithm for the problem
        ga = GA(my_world, cross, mutation, num_gen, num_population)
        pareto = tools.ParetoFront()
        pop = []

        # create a new exp
        now = datetime.datetime.now()
        exp = Experiment(
            world=gameworld,
            name=name,
            date=date,
            block_size=block_size,
            start=now,
            type=type,
            description=description,
            flag=flag,
            actual_gen=actual_gen,
            CXPB=cross,
            MUTPB=mutation,
            NGEN=num_gen,
            NPOP=num_population,
            num_robots=num_robots,
            numLevels=num_levels,
            gen_threshold=gen_threshold,
            numMinPlayers=player,
            time_elapsed_end=0,
        )
        exp.save()

        # set NEW Population
        pop = ga.SetPopulation()

        # set block for the generation block table
        num_block = 1

        # set FITNESS to the entire population
        fitnesses = ga.GetFitness(pop)
        ga.AttachFitness(pop, fitnesses)
        # assign the crowding distance to the individuals
        ga.SetCrowdingDistance(pop)
        # discover again the LAST Pareto
        pareto.update(pop)

        # loop for BLOCK generations
        for i in range(1, exp.block_size):

            # Select the next generation individuals
            # ga.Selection(pop, my_world)
            ga.NSGASelection(pop, my_world)
            pareto.update(pop)

        # update actual generations with block_size
        exp.actual_gen += exp.block_size

        # save Generations Block
        gen = Generation(experiment=exp, block=num_block, comparisons="to be collected")
        gen.save()

        # save population and Front into database
        with transaction.atomic():
            count = 1
            for ind in pop:
                population = Population(generation=gen, chromosome=str(ind[0]), index=count)
                population.save()
                count += 1
            count = 1
            for ind in pareto:
                pfront = PFront(generation=gen, chromosome=str(ind[0]), index=count)
                pfront.save()
                count += 1

        # set the next status according the number of generations
        if exp.actual_gen >= exp.NGEN:
            exp.flag = "F"
        else:
            exp.flag = "R"

        exp.save()

        # run robots
        self.RunRobots(exp, num_block, ga, my_world)

        return exp
Exemplo n.º 2
0
    def ContinueEvolution(self):

        # get last ID of the Experiment
        exp = Experiment.objects.latest("id")

        # ATENCAO!! Deve existir pelo menos 1 GameWorld
        # depois arrumo essa verificacao no script do banco, mas agora PRECISA ser assim
        # inclusive fixei HARDCODE que sera esse mundo apenas

        # get World config
        gameworld = GameWorld.objects.get(id=1)  ###HARDCODE
        my_world = World(gameworld.m, gameworld.n)
        my_world.MaxArea(gameworld.max_areas)
        my_world.MaxUnits(gameworld.max_units)
        my_world.Production(gameworld.prod_unit0, gameworld.prod_unit1)
        my_world.Costs(gameworld.cost_gateway, gameworld.cost_unit0, gameworld.cost_unit1)

        for area in Area.objects.all().filter(world=gameworld.id):
            my_world.CreateArea(area.x, area.y, area.length)

        # declare Genetic Algorithm for the problem
        ga = GA(my_world, exp.CXPB, exp.MUTPB, exp.NGEN, exp.NPOP)
        pareto = tools.ParetoFront()
        pop = []

        # check if it is START or CONTINUE evolution - must know if I will create a new experiment or use the one received
        # if F then start NEW
        # if exp.flag=='F':
        #
        #     #create a new exp
        #     now=datetime.datetime.now()
        #     exp = Experiment(world=gameworld, name='Bangkok experimento ' + now.strftime("%Y-%m-%d %H:%M:%S"), date=now, block_size=10,
        #                      flag='W', actual_gen=0, keep_interaction=True, CXPB=0.3, MUTPB=0.1, NGEN=200, NPOP=300)
        #     exp.save()
        #
        #
        #     #set NEW Population
        #     pop=ga.SetPopulation()
        #
        #
        #     #set block for the generation block table
        #     num_block=1
        #
        # else:

        # number of the next block of generations
        gen = Generation.objects.latest("id")
        num_block = gen.block + 1

        # fake population
        pop = ga.SetPopulationFake(my_world)
        # get the LAST Population
        count = 0
        for ind in Population.objects.all().filter(generation=gen.id):
            pop[count][0] = literal_eval(ind.chromosome)
            count += 1

        ###### EM ########

        # set FITNESS to the entire population
        fitnesses = ga.GetFitness(pop)
        ga.AttachFitness(pop, fitnesses)
        # assign the crowding distance to the individuals
        ga.SetCrowdingDistance(pop)
        # discover again the LAST Pareto
        pareto.update(pop)

        #
        if exp.actual_gen + exp.block_size <= exp.gen_threshold and num_block <= exp.numLevels:
            loop = exp.block_size
        else:
            loop = exp.NGEN - exp.actual_gen

        # loop for BLOCK generations
        for i in range(1, loop):

            # Select the next generation individuals
            # ga.Selection(pop, my_world)
            ga.NSGASelection(pop, my_world)
            pareto.update(pop)

        # update actual generations with block_size
        exp.actual_gen += loop

        # save Generations Block
        gen = Generation(experiment=exp, block=num_block, comparisons="to be collected")
        gen.save()

        # save population and Front into database
        with transaction.atomic():
            count = 1
            for ind in pop:
                population = Population(generation=gen, chromosome=str(ind[0]), index=count)
                population.save()
                count += 1
            count = 1
            for ind in pareto:
                pfront = PFront(generation=gen, chromosome=str(ind[0]), index=count)
                pfront.save()
                count += 1

        # set the next status according the number of generations
        if exp.actual_gen >= exp.NGEN:
            exp.flag = "F"
        else:
            exp.flag = "R"

        exp.save()

        # run robots
        self.RunRobots(exp, num_block, ga, my_world)