Ejemplo n.º 1
0
    def particle_generation(self):
        particle = tools.initIterate(creator.Particle, self.generate)
        particle.speed = np.random.uniform(self.smin, self.smax,
                                           self.dimension)
        particle.smin = self.smin
        particle.smax = self.smax

        return particle
Ejemplo n.º 2
0
    def init_algorithm(self, params):

        toolbox = base.Toolbox()
        ind_size = self.problem.ind_size
        ngen = params['generations']
        nind = params['num_individuals']
        cxpb, mutpb, lb, ub, mu, lam = 0.7, 0.2, -1.0, 1.0, nind, nind

        if hasattr(creator, 'FitnessMin') is False:
            creator.create('FitnessMin', base.Fitness, weights=(-1.0, ))

        if hasattr(creator, 'Individual') is False:
            kw0 = {'typecode': 'd', 'fitness': creator.FitnessMin}
            creator.create('Individual', array.array, **kw0)

        atr = lambda: [random.uniform(lb, ub) for _ in range(ind_size)]
        ind = lambda: tools.initIterate(creator.Individual, atr)
        population = [ind() for _ in range(nind)]

        kw1 = {'low': lb, 'up': ub, 'eta': 20.0, 'indpb': 1.0 / ind_size}
        mut = lambda xs: tools.mutPolynomialBounded(xs, **kw1)
        kw2 = {'low': lb, 'up': ub, 'eta': 20.0}
        crs = lambda i1, i2: tools.cxSimulatedBinaryBounded(i1, i2, **kw2)
        sel = lambda p, n: tools.selTournament(p, n, tournsize=3)

        toolbox.register('evaluate', self.problem.objective_function)
        toolbox.register('mate', crs)
        toolbox.register('mutate', mut)
        toolbox.register('select', sel)

        stats = tools.Statistics(lambda ind: ind.fitness.values)
        stats.register('min', np.min)
        self.hof = tools.HallOfFame(5)

        args = (population, toolbox, mu, lam, cxpb, mutpb, ngen)
        kw3 = {'stats': stats, 'halloffame': self.hof, 'verbose': True}
        self.algorithm = lambda: algorithms.eaMuPlusLambda(*args, **kw3)
Ejemplo n.º 3
0
 def test_statistics_compile(self):
     l = 10
     gen_idx = partial(random.sample, list(range(l)), l)
     i = tools.initIterate(list, gen_idx)
     self.assertSetEqual(set(i), set(range(l)))
Ejemplo n.º 4
0
 def test_statistics_compile(self):
     l = 10
     gen_idx = partial(random.sample, range(l), l)
     i = tools.initIterate(list, gen_idx)
     self.assertSetEqual(set(i), set(range(l)))
Ejemplo n.º 5
0
data = [np.ones((2, 2)), np.ones((2, 2))]

#toolbox = get_toolbox(data)
#toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=1)
#toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
#toolbox.register("population", tools.initRepeat, list, toolbox.individual)

gen_idx = partial(random.sample, list(range(10)), 10)

population = [0, 1, 2, 3]
weights = [0.0, 1, 0.0, 0.0]
n = 10

toolbox = base.Toolbox()
toolbox.register("individual", tools.initIterate, wrap, population, weights, n)
b = toolbox.individual()
print(b)

diff_idx = partial(wrap, population, weights, n)
print(gen_idx)
print(diff_idx)
result = initIterate(list, diff_idx)
print(result)

result = initIterate(list, gen_idx)
print(type(result))

func = register()
ups = initRepeat(list, initIterate(list, diff_idx), n=2)
print(ups)
Ejemplo n.º 6
0
    def _actual_ga_run(self):
        ## TODO: correct this.
        heft_initial = self.schedule_to_chromosome_converter(self.back_cmp.current_schedule)
        heft_initial = self._clean_chromosome(heft_initial, self.back_cmp.event, self.back_cmp.fixed_schedule)

         ##=====================================
        ##Regular GA
        ##=====================================
        # print("GaHeft WITH NEW POP: ")
        # ga = self._get_ga_alg()
        # ((best_r, pop_r, schedule_r, stopped_iteration_r), logbook_r) = ga(ga_calc.fixed_schedule_part, ga_calc.initial_schedule, current_time)

        ## TODO: make here using of param to decide how to build initial population for regular gaheft
        ## TODO: self.mixed_init_pop doesn't exist and isn't setted anywhere
        ## TODO: need to refactor and merge with MPGA.py
        initial_pop_gaheft = None
        if self.mixed_init_pop is True:
            heft_initial = tools.initIterate(creator.Individual, lambda: heft_initial)
            ga_functions = GAFunctions2(self.workflow, self.resource_manager, self.estimator)
            heft_pop = [ga_functions.mutation(deepcopy(heft_initial)) for i in range(self.params["population"])]
            cleaned_schedule = self.back_cmp.fixed_schedule
            initial_pop_gaheft = [self._clean_chromosome(deepcopy(p), self.back_cmp.event, cleaned_schedule) for p in self.past_pop] + heft_pop


        print("GaHeft WITH NEW POP: ")
        if self.mpnewVSmpoldmode is True:
            print("using multi population gaheft...")
            ga = self.mpga_builder()
            ((best_r, pop_r, schedule_r, stopped_iteration_r), logbook_r) = ga(self.back_cmp.fixed_schedule, None, self.current_time, initial_population=None, only_new_pops=True)
        else:
            print("using single population gaheft...")
            ga = self.ga_builder()
            ((best_r, pop_r, schedule_r, stopped_iteration_r), logbook_r) = ga(self.back_cmp.fixed_schedule, None, self.current_time, initial_population=initial_pop_gaheft)



        ##=====================================
        ##Old pop GA
        ##=====================================
        print("GaHeft WITH OLD POP: ")
        cleaned_schedule = self.back_cmp.fixed_schedule
        initial_pop = [self._clean_chromosome(ind, self.back_cmp.event, cleaned_schedule) for ind in self.past_pop]


        result = self.mpga_builder()(self.back_cmp.fixed_schedule,
                                     heft_initial,
                                     self.current_time,
                                     initial_population=initial_pop)
        ((best_op, pop_op, schedule_op, stopped_iteration_op), logbook_op) = result

        self.past_pop = pop_op

        ##=====================================
        ##Save stat to stat_saver
        ##=====================================
        ## TODO: make exception here
        task_id = "" if not hasattr(self.current_event, 'task') else str(self.current_event.task.id)
        if self.stat_saver is not None:
            ## TODO: correct pop_agr later
            stat_data = {
                "wf_name": self.workflow.name,
                "event_name": self.current_event.__class__.__name__,
                "task_id": task_id,
                "with_old_pop": {
                    "iter": stopped_iteration_op,
                    "makespan": Utility.makespan(schedule_op),
                    "pop_aggr": logbook_op
                },
                "with_random": {
                    "iter": stopped_iteration_r,
                    "makespan": Utility.makespan(schedule_r),
                    "pop_aggr": logbook_r
                }
            }
            self.stat_saver(stat_data)



        self._stop_ga()
        return result
Ejemplo n.º 7
0
    def init_algorithm(self, params):

        if params['algorithm_class'] not in ['simple', 'multiobjective']:
            raise ValueError('Non-existent algorithm class.')

        toolbox = base.Toolbox()
        ngen = params['generations']
        nind = params['num_individuals']
        cxpb = 0.5 if params['algorithm_class'] == 'simple' else 0.9
        lb, ub = -1.0, 1.0
        ind_size = self.problem.ind_size

        if nind % 4 != 0:
            raise ValueError('Number of individuals must be multiple of four')

        if hasattr(creator, 'FitnessMin') is False:
            creator.create('FitnessMin', base.Fitness, weights=(-1.0, -1.0))

        if hasattr(creator, 'Individual') is False:
            creator.create('Individual',
                           array.array,
                           typecode='d',
                           fitness=creator.FitnessMin)

        atr = lambda: [random.uniform(lb, ub) for _ in range(ind_size)]
        ind = lambda: tools.initIterate(creator.Individual, atr)
        population = [ind() for _ in range(nind)]

        if params['algorithm_class'] == 'simple':
            self.hof = tools.HallOfFame(1)
            mut = lambda xs: tools.mutGaussian(xs, mu=0, sigma=1, indpb=0.1)
            crs = tools.cxTwoPoint
            sel = lambda p, n: tools.selTournament(p, n, tournsize=3)
        else:
            self.hof = tools.ParetoFront()
            mut = lambda xs: tools.mutPolynomialBounded(
                xs, low=lb, up=ub, eta=20.0, indpb=1.0 / ind_size)
            crs = lambda ind1, ind2: tools.cxSimulatedBinaryBounded(
                ind1, ind2, low=lb, up=ub, eta=20.0)
            sel = tools.selNSGA2

        toolbox.register('evaluate', self.problem.objective_function)
        toolbox.register('mate', crs)
        toolbox.register('mutate', mut)
        toolbox.register('select', sel)

        stats = tools.Statistics(lambda ind: ind.fitness.values)
        stats.register('avg', np.mean, axis=0)
        stats.register('std', np.std, axis=0)
        stats.register('min', np.min, axis=0)
        stats.register('max', np.max, axis=0)

        args = (population, toolbox)
        kwargs = {'cxpb': cxpb, 'ngen': ngen, 'stats': stats}
        kwargs['halloffame'] = self.hof
        if params['algorithm_class'] == 'simple':
            kwargs['mutpb'] = 0.2
            kwargs['verbose'] = True
            self.algorithm = lambda: algorithms.eaSimple(*args, **kwargs)
        else:
            self.algorithm = lambda: self.multiobjective(*args, **kwargs)
Ejemplo n.º 8
0
        def __call__(self, fixed_schedule_part, initial_schedule, current_time=0, initial_population=None, only_new_pops=False):



            ##==========================
            ## create populations
            ##==========================


            ##TODO: remake it
            creator.create("FitnessMax", base.Fitness, weights=(1.0,))
            creator.create("Individual", dict, fitness=creator.FitnessMax)

            toolbox = base.Toolbox()
            toolbox.register("evaluate", ga_functions.build_fitness(fixed_schedule_part, current_time))
            toolbox.register("attr_bool", ga_functions.build_initial(fixed_schedule_part, current_time))
            # Structure initializers
            toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.attr_bool)
            toolbox.register("population", tools.initRepeat, list, toolbox.individual)

            ## TODO: replace it with strategy and specilized builder
            if only_new_pops is True:
                ## TODO: replace this magic number with parameter
                populations = [toolbox.population(n=POPSIZE) for i in range(3)]
            else:
                ## create populations
                old_pop = initial_population
                newpop = toolbox.population(n=POPSIZE)
                #heft_initial = GAFunctions2.schedule_to_chromosome(initial_schedule)
                # TODO: this is a stub. Rearchitect information flows and entities responsibilities as soon as you will get the first positive results.
                heft_initial = initial_schedule
                heft_initial = tools.initIterate(creator.Individual, lambda: heft_initial)
                heft_pop = [ga_functions.mutation(deepcopy(heft_initial)) for i in range(POPSIZE)]
                populations = [old_pop, newpop, heft_pop]

            ## TODO: replace this more effective implementation
            def quick_save():
                whole_pop = reduce(lambda x, y: x+y, populations)
                ## choose new old pop for the next run
                sorted_whole_pop = sorted(whole_pop, key=lambda x: x.fitness.values, reverse=True)
                best = sorted_whole_pop[0]
                new_oldpop = sorted_whole_pop[0:POPSIZE]

                ## construct final result
                ## TODO: implement constructor of final result here
                result = ((best, new_oldpop, None, None), common_logbook)
                self._save_result(result)
                return result



            ##==========================
            ## run for several migration periods
            ##==========================
            common_logbook = tools.Logbook()
            result = None
            for k in range(MIGRATIONS):
                new_pops = []
                iter_map = {}

                for pop in populations:
                    ((best, npop, schedule, stopped_iteration), logbook) = ga_alg(fixed_schedule_part,
                           None,
                           current_time=current_time,
                           initial_population=pop)
                    new_pops.append(npop)
                    for rec in logbook:
                        iter = k*GENERATIONS + rec["iter"]
                        mp = iter_map.get(iter, [])
                        mp.append({"worst": rec["worst"], "best": rec["best"], "avr": rec["avr"]})
                        iter_map[iter] = mp
                        pass
                for iter, items in iter_map.items():
                    best = max(it["best"] for it in items)
                    avr = sum(it["avr"] for it in items)/len(items)
                    worst = min(it["worst"] for it in items)
                    common_logbook.record(iter=iter, worst=worst, best=best, avr=avr)
                    pass
                populations = new_pops
                migRing(populations, migrCount, emigrant_selection)
                result = quick_save()
                pass

            # merge all populations in one and evaluate for some time
            # TODO: test this changes
            common_pop = functools.reduce(operator.add, populations, [])
            for k in range(MERGED_POP):
                ((best, npop, schedule, stopped_iteration), logbook) = ga_alg(fixed_schedule_part,
                           None,
                           current_time=current_time,
                           initial_population=common_pop)
                common_pop = npop
                for rec in logbook:
                    iter = (all_iters_count - MERGED_POP) + rec["iter"]
                    common_logbook.record(iter=iter, worst=rec["worst"], best=rec["best"], avr=rec["avr"])
                    pass
                result = quick_save()
                pass

            ((best, new_oldpop, x1, x2), x3) = result
            result = ((best, new_oldpop, ga_functions.build_schedule(best, fixed_schedule_part, current_time), None), common_logbook)
            self._save_result(result[0])
            return result
Ejemplo n.º 9
0
        def __call__(self,
                     fixed_schedule_part,
                     initial_schedule,
                     current_time=0,
                     initial_population=None,
                     only_new_pops=False):
            ##==========================
            ## create populations
            ##==========================
            ##TODO: remake it
            creator.create("FitnessMax", base.Fitness, weights=(1.0, ))
            creator.create("Individual", dict, fitness=creator.FitnessMax)

            toolbox = base.Toolbox()
            toolbox.register(
                "evaluate",
                ga_functions.build_fitness(fixed_schedule_part, current_time))
            toolbox.register(
                "attr_bool",
                ga_functions.build_initial(fixed_schedule_part, current_time))
            # Structure initializers
            toolbox.register("individual", tools.initIterate,
                             creator.Individual, toolbox.attr_bool)
            toolbox.register("population", tools.initRepeat, list,
                             toolbox.individual)

            ## TODO: replace it with strategy and specilized builder
            if only_new_pops is True:
                ## TODO: replace this magic number with parameter
                populations = [toolbox.population(n=POPSIZE) for i in range(3)]
            else:
                ## create populations
                old_pop = initial_population
                newpop = toolbox.population(n=POPSIZE)
                #heft_initial = GAFunctions2.schedule_to_chromosome(initial_schedule)
                # TODO: this is a stub. Rearchitect information flows and entities responsibilities as soon as you will get the first positive results.
                heft_initial = initial_schedule if not isinstance(
                    initial_schedule,
                    Schedule) else GAFunctions2.schedule_to_chromosome(
                        initial_schedule, fixed_schedule_part)
                heft_initial = tools.initIterate(creator.Individual,
                                                 lambda: heft_initial)
                heft_pop = [
                    ga_functions.mutation(deepcopy(heft_initial))
                    for i in range(POPSIZE)
                ]
                populations = [old_pop, newpop, heft_pop]

            ## TODO: replace this more effective implementation
            def quick_save():
                whole_pop = reduce(lambda x, y: x + y, populations)
                ## choose new old pop for the next run
                sorted_whole_pop = sorted(whole_pop,
                                          key=lambda x: x.fitness.values,
                                          reverse=True)
                best = sorted_whole_pop[0]
                new_oldpop = sorted_whole_pop[0:POPSIZE]

                ## construct final result
                ## TODO: implement constructor of final result here
                result = ((best, new_oldpop, None, None), common_logbook)
                self._save_result(result)
                return result

            ##==========================
            ## run for several migration periods
            ##==========================
            common_logbook = tools.Logbook()
            result = None
            for k in range(MIGRATIONS):
                new_pops = []
                iter_map = {}

                for pop in populations:
                    ((best, npop, schedule, stopped_iteration),
                     logbook) = ga_alg(fixed_schedule_part,
                                       None,
                                       current_time=current_time,
                                       initial_population=pop)
                    new_pops.append(npop)
                    # for rec in logbook:
                    #     iter = k*GENERATIONS + rec["iter"]
                    #     mp = iter_map.get(iter, [])
                    #     mp.append({"worst": rec["worst"], "best": rec["best"], "avr": rec["avr"]})
                    #     iter_map[iter] = mp
                    #     pass
                for iter, items in iter_map.items():
                    best = max(it["best"] for it in items)
                    avr = sum(it["avr"] for it in items) / len(items)
                    worst = min(it["worst"] for it in items)
                    common_logbook.record(iter=iter,
                                          worst=worst,
                                          best=best,
                                          avr=avr)
                    pass
                populations = new_pops
                migRing(populations, migrCount, emigrant_selection)
                result = quick_save()
                pass

            # merge all populations in one and evaluate for some time
            # TODO: test this changes
            common_pop = functools.reduce(operator.add, populations, [])
            for k in range(MERGED_POP):
                ((best, npop, schedule, stopped_iteration),
                 logbook) = ga_alg(fixed_schedule_part,
                                   None,
                                   current_time=current_time,
                                   initial_population=common_pop)
                common_pop = npop
                # for rec in logbook:
                #     iter = (all_iters_count - MERGED_POP) + rec["iter"]
                #     common_logbook.record(iter=iter, worst=rec["worst"], best=rec["best"], avr=rec["avr"])
                #     pass
                result = quick_save()
                pass

            ((best, new_oldpop, x1, x2), x3) = result
            result = ((best, new_oldpop,
                       ga_functions.build_schedule(best, fixed_schedule_part,
                                                   current_time), None),
                      common_logbook)
            self._save_result(result[0])
            return result
INDIVIDUAL_SIZE = NUMBER_OF_CITIES = 4
POPULATION_SIZE = 10
N_ITERATIONS = 20
N_MATINGS = 10
#  cities
cities = city
print(cities)
#  distancias
distances = distan
print(distances)
toolbox = base.Toolbox()

## permutaciones para individuos
toolbox.register("indices", random.sample, range(INDIVIDUAL_SIZE),
                 INDIVIDUAL_SIZE)
print(tools.initIterate(creator.Individual, toolbox.indices))
toolbox.register("individual", tools.initIterate, creator.Individual,
                 toolbox.indices)
## poblacion
toolbox.register("population", tools.initRepeat, list, toolbox.individual)


def EVALUATE(individual):
    summation = 0
    start = individual[0]
    for i in range(1, len(individual)):
        end = individual[i]
        summation += distances[start][end]
        start = end
    init = distances[end][individual[0]]
    return summation + init