Exemple #1
0
def cross(self, indiv1, indiv2):
    """Perform the crossover (One crossover by default)

    Parameters
    ----------
    self : OptiGenNsga2Deap
        Optimization solver

    indiv1 : individual
        first individual to modify
    indiv2 : individual
        second individual to modify

    Returns
    -------
    is_cross : bool
        True if the crossover append
    """

    if random.random() < self.p_cross:
        if self.crossover == None:
            if cxOnePoint == None:
                raise ImportError("deap module is needed.")
            else:
                cxOnePoint(indiv1, indiv2)
        else:
            self.crossover(indiv1, indiv2)

        return True
    else:
        return False
Exemple #2
0
def makenewinds(inds, select, nb_inds, nb_args):
    """
    parameters:
        inds: list of individu
        select: list of boolean selector
    """
    selectioned = [ind for ind,sel in zip(inds,select) if sel == 1]

    ind1 = ind[0][0]
    print "first individual"
    print ind1

    ind2 = ind[0][1]
    print "second individual"
    print ind2

    filho = tools.cxOnePoint(ind1,ind2)
    print "FILHO"
    for f in filho:
        print f   

    arguments = []
    selectioned = zip(*selectioned)
    print len(selectioned)
    for selection in selectioned:
        print len(selection)
        for i in range(len(selection)):
            print selection[i][0]
Exemple #3
0
def mut_suite(individual, indpb):
    # shuffle seq
    individual, = tools.mutShuffleIndexes(individual, indpb)

    # crossover inside the suite
    for i in range(1, len(individual), 2):
        if random.random() < settings.MUTPB:
            if len(individual[i - 1]) <= 2:
                print "\n\n### Indi Length =", len(individual[i - 1]), " ith = ", i - 1, individual[i - 1]
                continue  # sys.exit(1)
            if len(individual[i]) <= 2:
                print "\n\n### Indi Length =", len(individual[i]), "ith = ", i, individual[i]
                continue  # sys.exit(1)

            individual[i - 1], individual[i] = tools.cxOnePoint(individual[i - 1], individual[i])
            # individual[i - 1], individual[i] = tools.cxUniform(individual[i - 1], individual[i], indpb=0.5)

    # shuffle events
    for i in range(len(individual)):
        if random.random() < settings.MUTPB:
            if len(individual[i]) <= 2:
                print "\n\n### Indi Length =", len(individual[i]), "ith = ", i, individual[i]
                continue  # sys.exit(1)
            individual[i], = tools.mutShuffleIndexes(individual[i], indpb)

    return individual,
Exemple #4
0
def shap_point_crossover(ind1, ind2, p=0.25):
    """Apply one--point crossover on pairs of shapelets from the sets"""
    new_ind1, new_ind2 = [], []
    np.random.shuffle(ind1)
    np.random.shuffle(ind2)

    for shap1, shap2 in zip(ind1, ind2):

        if len(shap1) > 4 and len(shap2) > 4 and np.random.random() < p:

            # plt.figure()
            # plt.plot(shap1)
            # plt.plot(shap2)
            # plt.title('Before Point CX')
            # plt.show()
            shap1, shap2 = tools.cxOnePoint(list(shap1), list(shap2))

            # plt.figure()
            # plt.plot(shap1)
            # plt.plot(shap2)
            # plt.title('After Point CX')
            # plt.show()
            # input()

        new_ind1.append(shap1)
        new_ind2.append(shap2)

    if len(ind1) < len(ind2):
        new_ind2 += ind2[len(ind1):]
    else:
        new_ind1 += ind1[len(ind2):]

    return new_ind1, new_ind2
def crossover(ind1, ind2):
    i1 = dec2bin_ind(ind1)
    i2 = dec2bin_ind(ind2)
    i1, i2 = tools.cxOnePoint(i1,i2)
    ind1[:] = bin2dec_ind(i1)
    ind2[:] = bin2dec_ind(i2)
    return ind1, ind2
Exemple #6
0
def mut_suite(individual, indpb):
	# shuffle seq
	individual, = tools.mutShuffleIndexes(individual, indpb)

	# crossover inside the suite
	for i in range(1, len(individual), 2):
		if random.random() < settings.MUTPB:
			if len(individual[i - 1]) <= 2:
				print "\n\n### Indi Length =", len(individual[i - 1]), " ith = ", i - 1, individual[i - 1]
				continue  # sys.exit(1)
			if len(individual[i]) <= 2:
				print "\n\n### Indi Length =", len(individual[i]), "ith = ", i, individual[i]
				continue  # sys.exit(1)

			individual[i - 1], individual[i] = tools.cxOnePoint(individual[i - 1], individual[i])

	# shuffle events
	for i in range(len(individual)):
		if random.random() < settings.MUTPB:
			if len(individual[i]) <= 2:
				print "\n\n### Indi Length =", len(individual[i]), "ith = ", i, individual[i]
				continue  # sys.exit(1)
			individual[i], = tools.mutShuffleIndexes(individual[i], indpb)

	return individual,
Exemple #7
0
def default_config(wf, rm, estimator):
    selector = lambda env, pop: tools.selTournament(pop, len(pop), 4)
    return {
        "interact_individuals_count": 22,
        "generations": 5,
        "env": Env(wf, rm, estimator),
        "species": [Specie(name=MAPPING_SPECIE, pop_size=10,
                           cxb=0.8, mb=0.5,
                           mate=lambda env, child1, child2: tools.cxOnePoint(child1, child2),
                           mutate=mapping_default_mutate,
                           select=selector,
                           initialize=mapping_default_initialize
                    ),
                    Specie(name=ORDERING_SPECIE, pop_size=10,
                           cxb=0.8, mb=0.5,
                           mate=ordering_default_crossover,
                           mutate=ordering_default_mutate,
                           select=selector,
                           initialize=ordering_default_initialize,
                    )
        ],

        "operators": {
            # "choose": default_choose,
            "build_solutions": default_build_solutions,
            "fitness": fitness_mapping_and_ordering,
            "assign_credits": default_assign_credits
        }
    }
Exemple #8
0
    def sapienz_mut_suite(self, individual: Individual,
                          indpb: float) -> Tuple[Individual]:
        # shuffle seq
        individual, = tools.mutShuffleIndexes(individual, indpb)

        # crossover inside the suite
        # perform one point crossover between odd and even pair positions of test cases
        for i in range(1, len(individual), 2):
            if random.random() < settings.MUTPB:
                if len(individual[i - 1]) <= 2:
                    continue
                if len(individual[i]) <= 2:
                    continue

                individual[i - 1], individual[i] = tools.cxOnePoint(
                    individual[i - 1], individual[i])

        # shuffle events inside each test case
        for i in range(len(individual)):
            if random.random() < settings.MUTPB:
                if len(individual[i]) <= 2:
                    continue
                individual[i], = tools.mutShuffleIndexes(individual[i], indpb)

        return individual,
Exemple #9
0
        def point_crossover(ind1, ind2):
            """Apply one- or two-point crossover on the shapelet sets"""
            if len(ind1) > 1 and len(ind2) > 1:
                if np.random.random() < 0.5:
                    ind1, ind2 = tools.cxOnePoint(list(ind1), list(ind2))
                else:
                    ind1, ind2 = tools.cxTwoPoint(list(ind1), list(ind2))

            return ind1, ind2
Exemple #10
0
        def mate(x, y):

            x_ = x.terms.copy()
            y_ = y.terms.copy()
            n, m = x_.shape
            nm = n * m

            a = x_.reshape((1, nm)).tolist()[0]
            b = y_.reshape((1, nm)).tolist()[0]
            c = np.array(tools.cxOnePoint(a, b))
            a = creator.pt_individual(c[0, :].reshape((n, m)))
            b = creator.pt_individual(c[1, :].reshape((n, m)))
            return (a, b)
Exemple #11
0
        def mate(x, y):

            x_ = x.terms.copy()
            y_ = y.terms.copy()
            n, m = x_.shape
            nm = n * m

            a = x_.reshape((1, nm)).tolist()[0]
            b = y_.reshape((1, nm)).tolist()[0]
            c = np.array(tools.cxOnePoint(a, b))
            a = creator.pt_individual(c[0, :].reshape((n, m)))
            b = creator.pt_individual(c[1, :].reshape((n, m)))
            return (a, b)
Exemple #12
0
def crossover_gus(indv1, indv2):

    genes1 = indv1.vector_weights
    genes2 = indv2.vector_weights
    cross1, cross2 = tools.cxOnePoint(genes1, genes2)

    indv1.vector_weights = cross1
    indv2.vector_weights = cross2

    indv1.set_mat_from_vect()
    indv2.set_mat_from_vect()

    return indv1, indv2
Exemple #13
0
def validCrossover(ind1, ind2, servers, knapSackList):
    """
    Objectif : faire un crossover entre deux individus tout en conservant la validité du knapsack
    :param ind1:
    :param ind2:
    :param servers:
    :param knapSackList:
    :return: offspring Individu
    """
    offspring1, offspring2 = tools.cxOnePoint(ind1, ind2)
    offspring1 = deleteOverflow(offspring1, servers, knapSackList)
    offspring2 = deleteOverflow(offspring2, servers, knapSackList)
    return offspring1, offspring2
Exemple #14
0
def main():
    # 杂交函数测试
    ind1 = [1, 1, 1, 1, 1]
    ind2 = [0, 0, 0, 0, 0]
    print(ind1, ind2)
    """交换从任意位置开始并且到结尾的一段基因"""
    n1, n2 = tools.cxOnePoint(ind1, ind2)
    print(n1, n2)
    """交换连续的一段长度随机,位置随机的基因"""
    n3, n4 = tools.cxTwoPoint(ind1, ind2)
    print(n3, n4)
    """进行min(len(ind1), len(ind2))次杂交,每轮交换前产生一个随机数a,若a小于indpb则交换该轮次位置的两个基因,否则不执行交换"""
    n5, n6 = tools.cxUniform(ind1, ind2, indpb=0.5)
    print(n5, n6)
def makenewinds(inds, select, nb_inds, nb_args):
    """
    parameters:
        inds: list of individu
        select: list of boolean selector
    """
    selectioned = [ind for ind, sel in zip(inds, select) if sel == 1]

    newinds = []

    for nb_arg in range(nb_args):
        args = [sel[nb_arg] for sel in selectioned]
        while len(args) < nb_inds:
            for father, mother in zip(args[:-1], args[1:]):
                print len(father)
                args.append(mother)
                args.append(father)
                son1 = copy.deepcopy(father)
                son2 = copy.deepcopy(mother)
                tools.cxOnePoint(son1, son2)
                print "Father"
                print father
                print gp.compile(father, pset)
                print "Mother"
                print mother
                print gp.compile(mother, pset)
                print son1
                print gp.compile(son1, pset)
                print son2
                print gp.compile(son2, pset)
                args.append(son1)
                args.append(son2)

        args = args[:nb_inds]  # select only nb_ind individus
        newinds.append(args)
    return newinds
Exemple #16
0
        def shap_point_crossover(ind1, ind2):
            new_ind1, new_ind2 = [], []
            np.random.shuffle(ind1)
            np.random.shuffle(ind2)

            for shap1, shap2 in zip(ind1, ind2):
                if len(shap1) > 4 and len(shap2) > 4:
                    shap1, shap2 = tools.cxOnePoint(list(shap1), list(shap2))
                new_ind1.append(shap1)
                new_ind2.append(shap2)

            if len(ind1) < len(ind2):
                new_ind2 += ind2[len(ind1):]
            else:
                new_ind1 += ind1[len(ind2):]

            return new_ind1, new_ind2
Exemple #17
0
def crossOver(ind1, ind2):
    child1, child2 = [toolbox.clone(ind) for ind in (ind1, ind2)]
    del child1.fitness.values
    del child2.fitness.values
    l1 = list(split(child1, N_NUM_ACTUATORS))
    l2 = list(split(child2, N_NUM_ACTUATORS))
    crossedTotalList = []
    new1 = toolbox.individual()
    new2 = toolbox.individual()
    del new1[:]
    del new2[:]
    for i in range(N_NUM_ACTUATORS):
        crossedTotalList.append(tools.cxOnePoint(l1[i], l2[i]))
    for sublist in crossedTotalList:
        new1.extend(sublist[0])
        new2.extend(sublist[1])
    return new1, new2,
Exemple #18
0
def get_child_reward_network(
        mom: Agent,
        dad: Agent,
        mut_sigma: float = 0.3,
        mut_p: float = 0.05) -> Tuple[List[np.ndarray], List[np.ndarray]]:
    """
    Takes as input a pair of agents, and constructs the child's reward network.

    Parameters
    ----------
    mom : ``Agent``.
        First parent agent.
    dad : ``Agent``.
        First parent agent.
    mut_sigma : ``float``, optional.
        Standard deviation of mutation operation on reward vectors.
    mut_p : ``float``, optional.
        Probability of mutation on reward vectors.

    Returns
    -------
    childs_reward_weights : ``List[np.ndarray]``.
        Weights of the new agent's reward network.
    childs_reward_biases : ``List[np.ndarray]``.
        Biases of the new agent's reward network.

    """
    # Get parents' DNA.
    moms_dna = reward_to_DNA(mom.reward_weights, mom.reward_biases)
    dads_dna = reward_to_DNA(dad.reward_weights, dad.reward_biases)

    # Crossover.
    childs_dna, _ = cxOnePoint(moms_dna, dads_dna)

    # Mutation.
    # HARDCODE: ``mu`` and grabbing first and only tuple element.
    childs_dna = mutGaussian(childs_dna, 0.0, mut_sigma, mut_p)[0]

    # HARDCODE: using ``mom``'s reward hyperparams for child.
    childs_reward_weights, childs_reward_biases = DNA_to_reward(
        childs_dna, mom.n_layers, mom.input_dim, mom.hidden_dim)

    return childs_reward_weights, childs_reward_biases
def do_exp():
    config = {
        "interact_individuals_count": 500,
        "generations": 1000,
        "env": Env(_wf, rm, estimator),
        "species": [Specie(name=MAPPING_SPECIE, pop_size=500,
                           cxb=0.9, mb=0.9,
                           mate=lambda env, child1, child2: tools.cxOnePoint(child1, child2),
                           # mutate=mapping_default_mutate,
                           # mutate=lambda ctx, mutant: mapping_k_mutate(ctx, 3, mutant)
                           mutate=mapping_all_mutate,
                           # mutate=mapping_improving_mutation,
                           select=selector,
                           initialize=mapping_default_initialize,
                           # initialize=lambda ctx, pop: mapping_heft_based_initialize(ctx, pop, heft_mapping, 3),
                           stat=lambda pop: {"hamming_distances": hamming_distances([to_seq(p) for p in pop], to_seq(ms_ideal_ind)),
                                             "unique_inds_count": unique_individuals(pop),
                                             "pcm": pcm(pop),
                                             "gdm": gdm(pop)}

        ),
                    Specie(name=ORDERING_SPECIE, fixed=True,
                           representative_individual=ListBasedIndividual(os_representative))
        ],

        "solstat": lambda sols: {"best_components": hamming_for_best_components(sols, ms_ideal_ind, os_ideal_ind),
                                 "best_components_itself": best_components_itself(sols),
                                 "best": -1*Utility.makespan(build_schedule(_wf, estimator, rm, max(sols, key=lambda x: x.fitness)))
                                 },

        "operators": {
            # "choose": default_choose,
            "build_solutions": default_build_solutions,
            # "fitness": fitness_mapping_and_ordering,
            "fitness": overhead_fitness_mapping_and_ordering,
            # "assign_credits": default_assign_credits
            # "assign_credits": max_assign_credits
            "assign_credits": assign_from_transfer_overhead
        }
    }
    return do_experiment(saver, config, _wf, rm, estimator)
def main():
    nb_of_inputs, nb_of_outputs = get_nb_of_inputs_and_outputs_of_env(env_name)

    model = tf.keras.Sequential([
        tf.keras.layers.Dense(2 ** 8, input_shape=(nb_of_inputs,), activation='tanh', use_bias=True, name='dense_1'),
        # tf.keras.layers.Dense(2**8, activation='tanh', name='dense_2', use_bias=True),
        tf.keras.layers.Dense(nb_of_outputs, activation='tanh', name='output'),
    ])

    nb_of_weights_per_layer = extract_nb_of_weights_per_layer(model)
    brain = Brain(model, nb_of_weights_per_layer)
    genome_model = Genome(brain)

    crossover = lambda p1, p2: tools.cxOnePoint(p1, p2)

    mutations = [
        lambda v: mut_scalar_add_uniform(v, 1., 0.2),
        lambda v: mut_scalar_add_uniform(v, 1, 0.01),
        lambda v: mut_random_value(v, 1, 0.02),
    ]

    mutation_manager = MutationManager(mutations)

    population = Population(
        env_name=env_name,
        genome_model=genome_model,
        crossover=crossover,
        mutation_manager=mutation_manager,
        nb_of_genomes=nb_of_genomes)

    with population as population:
        population.init_genomes()
        population.init_environments()

        for i in range(nb_of_generations):
            global g_mut_scale

            population.evaluate()
            population.evolve()

            g_mut_scale = (1. / 1.3) ** i
Exemple #21
0
def default_config(wf, rm, estimator):
    selector = lambda env, pop: tools.selTournament(pop, len(pop), 4)
    return {
        "interact_individuals_count":
        22,
        "generations":
        5,
        "env":
        Env(wf, rm, estimator),
        "species": [
            Specie(name=MAPPING_SPECIE,
                   pop_size=10,
                   cxb=0.8,
                   mb=0.5,
                   mate=lambda env, child1, child2: tools.cxOnePoint(
                       child1, child2),
                   mutate=mapping_default_mutate,
                   select=selector,
                   initialize=mapping_default_initialize),
            Specie(
                name=ORDERING_SPECIE,
                pop_size=10,
                cxb=0.8,
                mb=0.5,
                mate=ordering_default_crossover,
                mutate=ordering_default_mutate,
                select=selector,
                initialize=ordering_default_initialize,
            )
        ],
        "operators": {
            # "choose": default_choose,
            "build_solutions": default_build_solutions,
            "fitness": fitness_mapping_and_ordering,
            "assign_credits": default_assign_credits
        }
    }
Exemple #22
0
 def _mate(self, ind1, ind2):
     if np.random.rand() < self.config.cx_prob:
         tools.cxOnePoint(ind1, ind2)
         del ind1.fitness.values
         del ind2.fitness.values
Exemple #23
0
def do_exp():
    config = {
        "interact_individuals_count":
        100,
        "generations":
        300,
        "env":
        Env(_wf, rm, estimator),
        "species": [
            Specie(
                name=MAPPING_SPECIE,
                pop_size=50,
                cxb=0.9,
                mb=0.9,
                mate=lambda env, child1, child2: tools.cxOnePoint(
                    child1, child2),
                # mutate=mapping_default_mutate,
                # mutate=lambda ctx, mutant: mapping_k_mutate(ctx, 3, mutant)
                mutate=mapping_all_mutate,
                # mutate=OnlyUniqueMutant()(mapping_all_mutate),
                select=selector,
                # initialize=mapping_default_initialize,
                initialize=lambda ctx, pop: mapping_heft_based_initialize(
                    ctx, pop, heft_mapping, 3),
                stat=lambda pop: {
                    "hamming_distances":
                    hamming_distances([to_seq(p)
                                       for p in pop], to_seq(ms_ideal_ind)),
                    "unique_inds_count":
                    unique_individuals(pop),
                    "pcm":
                    pcm(pop),
                    "gdm":
                    gdm(pop)
                }),
            Specie(name=ORDERING_SPECIE,
                   fixed=True,
                   representative_individual=ListBasedIndividual(
                       os_representative))
        ],
        "solstat":
        lambda sols: {
            "best_components":
            hamming_for_best_components(sols, ms_ideal_ind, os_ideal_ind),
            "best_components_itself":
            best_components_itself(sols),
            "best":
            -1 * Utility.makespan(
                build_schedule(_wf, estimator, rm,
                               max(sols, key=lambda x: x.fitness)))
        },
        "operators": {
            # "choose": default_choose,
            "build_solutions": default_build_solutions,
            "fitness": fitness_mapping_and_ordering,
            # "fitness": overhead_fitness_mapping_and_ordering,
            # "assign_credits": default_assign_credits
            # "assign_credits": max_assign_credits
            "assign_credits": assign_from_transfer_overhead
        }
    }
    return do_experiment(saver, config, _wf, rm, estimator)
def genetic(rts, cpus):

    def func_X(a, b):
        """ length of messages transmitted from a towards b or from b towards a """
        comm_load = 0
        if "p" in a:
            for p in a["p"]:
                if p["id"] == b["id"]:
                    comm_load += p["payload"]
        # This part consideres incoming msgs from other tasks (so that task is the successor, b->a)
        # if "p" in b:
        #    for p in b["p"]:
        #        if p["id"] == a["id"]:
        #            comm_load += p["payload"]
        return comm_load

    def func_Y(i, rts):
        """ load of the communication control network that the task i produces """
        comm_load = 0
        other_tasks = [t for t in rts if t is not i]
        for j in other_tasks:
            comm_load += func_X(i, j)
        return comm_load

    def func_Xc(cpu_h, cpu_k):
        """ length of all messages (in bytes) to be transmitted between processors h and k through the network """
        summ = 0
        for task_h in cpu_h["tasks"]:
            for task_j in cpu_k["tasks"]:
                summ += func_X(task_h, task_j)
        return summ

    def func_Vp(cpus):
        """ total amount of information to be transferred over the network """
        summ = 0
        for cpu in cpus.values():
            other_cpus = [c for c in cpus.values() if c is not cpu]
            for other_cpu in other_cpus:
                summ += func_Xc(cpu, other_cpu)
        return summ

    def func_B(rts):
        """ Total amount of data to be transferred between predecessor and successors throught the network """
        summ = 0
        for task in rts:
            summ += func_Y(task, rts)
        return summ

    def func_cost_p(rts, cpus):
        return func_Vp(cpus) / func_B(rts)

    # initialization
    n_p = 128  # number of generations
    n_g = 16  # population
    pc = 0.5  # crossover probability
    pm = 0.001  # mutation probability
    m = len(rts)  # tasks
    n = len(cpus)  # processors
    B = func_B(rts) # total of data to be transferred between predecessor and successor

    W = 70  # network bandwith in bytes/slot
    Dmin = min([t["d"] for t in rts])  # minimum deadline
    WDmin = W * Dmin

    # generate first chromosome
    chromosome1 = []
    for task in rts:
        g = func_Y(task, rts) * int(random.uniform(0,1) * m)
        chromosome1.append(g)

    # the dict stores the cpus tasks lists
    chromosomes = [(chromosome1, dict())]

    # generate remain population
    for _ in range(n_g - 1):
        new_chromosome = []
        nu = max(chromosome1) / 10
        for g1 in chromosome1:
            g2 = g1 + random.uniform(-nu, nu)
            new_chromosome.append(g2)
        chromosomes.append((new_chromosome, dict()))

    # initialize the dict associated with each chromosome
    for _, cpus_alloc in chromosomes:
        for cpu_id in cpus.keys():
            cpus_alloc[cpu_id] = {"tasks":[], "uf":0}  # tasks assigned to this cpu

    # aux for reordering and other stuff
    chromosomes_stack = []

    # do generations
    for _ in range(n_p):

        chromosomes_stack.clear()

        # A stack is assembled containing the tasks ordered by the value of its allele in decreasing order.
        for item in chromosomes:
            chromosome, cpus_alloc = item[0], item[1]

            task_stack = []
            for task_id, allele in enumerate(chromosome):
                task_stack.append((allele, rts[task_id]))
            task_stack.sort(key=lambda allele: allele[0], reverse=True)

            # clear previous task assignation
            for cpu in cpus_alloc.values():
                cpu["tasks"].clear()

            # aux -- for easy sorting
            cpu_stack = [cpu for cpu in cpus_alloc.values()]

            # partition
            for _, max_task in task_stack:
                if "cpu" in max_task:
                    cpu_id = max_task["cpu"]
                    cpus_alloc[cpu_id]["tasks"].append(max_task)
                else:
                    # create auxiliary stack with all task j that communicate with i
                    aux_stack = []

                    # add the succesors
                    if "p" in max_task:
                        for p in max_task["p"]:
                            for task in rts:
                                if task["id"] == p["id"]:
                                    aux_stack.append((func_Y(task, rts), task))

                    # add other tasks that communicate with the task (the task will be the succesor)
                    #for task in [t for t in rts if t is not max_task]:
                    #    if "p" in task:
                    #        for p in task["p"]:
                    #           if p["id"] == max_task["id"]:
                    #                aux_stack.append((func_Y(task, rts), task))

                    cpu_a = None

                    # order by func_y
                    if aux_stack:
                        aux_stack.sort(key=lambda t: t[0], reverse=True)
                        aux_max_task = aux_stack[0]

                        # find the cpu at which the aux_max_task is allocated
                        for cpu in cpus_alloc.values():
                            if aux_max_task in cpu["tasks"]:
                                cpu_a = cpu

                    #if not aux_stack or cpu_a is None:
                    if cpu_a is None:
                        # update uf factors and allocate task to cpu with min uf
                        for cpu in cpus_alloc.values():
                            cpu["uf"] = sum([t["uf"] for t in cpu["tasks"]])
                        cpu_stack.sort(key=lambda c: c["uf"])

                        cpu_stack[0]["tasks"].append(max_task)
                    else:
                        cpu_a["tasks"].append(max_task)

            # apply the cost function to the chromosomes
            chromosomes_stack.append((func_cost_p(rts, cpus_alloc), chromosome))

        # evaluation, cost and fitness

        # elistist selection -- for the sake of simplicity, separate the first two chromosomes as the 'best'
        chromosomes_stack.sort(key=lambda c: c[0])  # order by ascending value
        chromosomes_stack = chromosomes_stack[2:]

        # apply roulette selection on the rest -- an apply crossover
        sum_fitness = sum([c[0] for c in chromosomes_stack])  # sum of all fitness values (cost function result)
        chromosomes_stack = [(c[0] / sum_fitness, c[1]) for c in chromosomes_stack]  # normalization

        # calculate probabilities
        sum_prob = 0
        probs = []
        for fitness, c in chromosomes_stack:
            prob = sum_prob + fitness
            probs.append(prob)
            sum_prob += fitness

        # perform crossover
        for _ in range(int(n_g / 2)):
            cs = []  # selected chromosomes
            for s in range(2):
                r = random.uniform(0,1)
                for idx, p in enumerate(probs):
                    if r < p:
                        cs.append(chromosomes_stack[idx][1])
            # uses radint() function for selecting a crossover point
            tools.cxOnePoint(cs[0], cs[1])

        # mutate
        for c in chromosomes_stack:
            tools.mutGaussian(c[1], pm, pm, pm)

    # memory constraint verification
    for idx, chromosome in enumerate(chromosomes):
        print("Chromosome {0}".format(idx))
        valid_cpu = True
        ch_cpus = chromosome[1]
        for cpuid, cpu in ch_cpus.items():
            if cpus[cpuid]["capacity"] < sum([t["r"] for t in cpu["tasks"]]):
                valid_cpu = False
        if valid_cpu:
            print_results(rts, chromosome[1])
        else:
            print(" -- Invalid assignation found.")

    # bandwidth constraint
    bw_ok = B <= WDmin

    return
Exemple #25
0
    def mate(self, individual1, individual2):
        individual1.number_centroids, individual2.number_centroids = individual2.number_centroids, individual1.number_centroids,
        tools.cxOnePoint(individual1.attribute_flags,
                         individual2.attribute_flags)

        return individual1, individual2
Exemple #26
0
 def cxComb(ind1,ind2):
    if numpy.random.randint(2)==0:
       tools.cxOnePoint(ind1,ind2)
    else:
       tools.cxBlend(ind1,ind2,0.0)
    return ind1,ind2
def crossverAll(first, second):
    return tools.cxOnePoint(first, second)
Exemple #28
0
heft_mapping = extract_mapping_from_ga_file("{0}/temp/heft_etalon_tr100_m50.json".format(__root_path__), rm)

ms_ideal_ind = build_ms_ideal_ind(_wf, rm)
os_ideal_ind = build_os_ideal_ind(_wf)


ms_str_repr = [{k: v} for k, v in ms_ideal_ind]


config = {
        "interact_individuals_count": 200,
        "generations": 10000,
        "env": Env(_wf, rm, estimator),
        "species": [Specie(name=MAPPING_SPECIE, pop_size=50,
                           cxb=0.9, mb=0.9,
                           mate=lambda env, child1, child2: tools.cxOnePoint(child1, child2),
                           mutate=mapping_all_mutate,
                           # mutate=mapping_default_mutate,
                           # mutate=MappingArchiveMutate(),
                           select=mapping_selector,
                           # initialize=mapping_default_initialize,
                           initialize=lambda ctx, pop: mapping_heft_based_initialize(ctx, pop, heft_mapping, 3),
                           stat=lambda pop: {"hamming_distances": hamming_distances([to_seq(p) for p in pop], to_seq(ms_ideal_ind)),
                                             "unique_inds_count": unique_individuals(pop),
                                             "pcm": pcm(pop),
                                             "gdm": gdm(pop)}

                    ),
                    Specie(name=ORDERING_SPECIE, pop_size=50,
                           cxb=0.8, mb=0.5,
                           mate=ordering_default_crossover,
Exemple #29
0
config = {
    "hall_of_fame_size":
    0,
    "interact_individuals_count":
    50,
    "generations":
    10,
    "env":
    Env(_wf, rm, estimator),
    "species": [
        Specie(
            name=MAPPING_SPECIE,
            pop_size=50,
            cxb=0.9,
            mb=0.9,
            mate=lambda env, child1, child2: tools.cxOnePoint(child1, child2),
            # mutate=mapping_all_mutate,
            # mutate=mapping_all_mutate_variable,
            mutate=mapping_mut_reg(mapping_all_mutate_configurable),
            # mutate=mapping_all_mutate_variable2,
            # mutate=mapping_improving_mutation,
            # mutate=mapping_default_mutate,
            # mutate=MappingArchiveMutate(),
            select=mapping_selector,
            # initialize=mapping_default_initialize,
            initialize=lambda ctx, pop: mapping_heft_based_initialize(
                ctx, pop, heft_mapping, 3),
            stat=lambda pop: {
                "hamming_distances":
                hamming_distances([to_seq(p)
                                   for p in pop], to_seq(ms_ideal_ind)),
Exemple #30
0
 def mate(ind1, ind2):
     return tools.cxOnePoint(ind1, ind2)
 def crossover_one_point(self, ind1:Individual, ind2:Individual):
     ch1, ch2 = tools.cxOnePoint(ind1.mapping, ind2.mapping)
     ind1.mapping = ch1
     ind2.mapping = ch2
     return ind1, ind2
Exemple #32
0
def defaultCx(mother, father):
    return tools.cxOnePoint(mother, father)
def crossover(ind1, ind2):
    return cxOnePoint(ind1, ind2)
Exemple #34
0
	def cxGIMME_Simple(self, ind1, ind2):

		# configs
		config1 = ind1[0]
		config2 = ind2[0]

		newConfig1 = []
		newConfig2 = []

		l1 = len(config1)
		l2 = len(config2)

		if (l1 > l2):
			maxLenConfig = config1
			maxLen = l1
			minLenConfig = config2
			minLen = l2
		else:
			maxLenConfig = config2
			maxLen = l2
			minLenConfig = config1
			minLen = l1

		cxpoints = [] 
		
		clist1 = []
		clist2 = []

		remainder1 = []
		remainder2 = []
		for i in range(minLen):
		
			parent1 = [None, None]
			parent2 = [None, None]

			parent1[0] = minLenConfig[i]
			parent1[1] = ind1[1][i].flattened()
			parent2[0] = maxLenConfig[i]
			parent2[1] = ind2[1][i].flattened()

			clist1.append(parent1)
			clist2.append(parent2)


		# breakpoint()

		for ind,clist in zip([ind1,ind2], [clist1,clist2]):


			# print("-----------[Before]-----------")
			# print(json.dumps(ind[1], default=lambda o: o.__dict__))

			randI1 = random.randint(0, len(clist1) - 1)
			randI2 = random.randint(0, len(clist1) - 1)
			

			newProfilesConfig = tools.cxOnePoint(ind1 = clist[randI1][0], ind2 = clist[randI2][0])
			newProfilesGIP = tools.cxOnePoint(ind1 = clist[randI1][1], ind2 = clist[randI2][1])
			
			ind[0][randI1] = newProfilesConfig[0]
			ind[1][randI1] = self.interactionsProfileTemplate.unflattened(newProfilesGIP[0])

			ind[0][randI2] = newProfilesConfig[1]
			ind[1][randI2] = self.interactionsProfileTemplate.unflattened(newProfilesGIP[1])

			# print("-----------[After]-----------")
			# print(json.dumps(ind[1], default=lambda o: o.__dict__))

		# breakpoint()



		del ind1.fitness.values
		del ind2.fitness.values

		return (ind1, ind2)
def genetic(rts, cpus):

    def func_X(a, b):
        """ length of messages transmitted from a towards b or from b towards a """
        comm_load = 0
        if "p" in a:
            for p in a["p"]:
                if p["id"] == b["id"]:
                    comm_load += p["payload"]
        # This part consideres incoming msgs from other tasks (so that task is the successor, b->a)
        # if "p" in b:
        #    for p in b["p"]:
        #        if p["id"] == a["id"]:
        #            comm_load += p["payload"]
        return comm_load

    def func_Xc(cpu_h, cpu_k):
        """ length of all messages (in bytes) to be transmitted between processors h and k through the network """
        summ = 0
        for task_h in cpu_h["tasks"]:
            for task_j in cpu_k["tasks"]:
                summ += func_X(task_h, task_j)
        return summ

    def func_Y(i, rts):
        """ load of the communication control network that the task i produces """
        comm_load = 0
        other_tasks = [t for t in rts if t is not i]
        for j in other_tasks:
            comm_load += func_X(i, j)
        return comm_load

    def func_Vp(cpus):
        """ total amount of information to be transferred over the network """
        summ = 0
        for cpu in cpus.values():
            other_cpus = [c for c in cpus.values() if c is not cpu]
            for other_cpu in other_cpus:
                summ += func_Xc(cpu, other_cpu)
        return summ

    def func_B(rts):
        """ Total amount of data to be transferred between predecessor and successors throught the network """
        summ = 0
        for task in rts:
            summ += func_Y(task, rts)
        return summ

    def func_cost_p(rts, cpus):
        return func_Vp(cpus) / func_B(rts)

    def get_cpu_alloc(individual):
        cpus_alloc = dict()
        for cpu_id in cpus.keys():
            cpus_alloc[cpu_id] = {"tasks": [], "uf": 0}  # tasks assigned to this cpu

        # A stack is assembled containing the tasks ordered by the value of the gene in decreasing order.
        task_stack = []
        for task_id, gene in enumerate(individual):
            task_stack.append((gene, rts[task_id]))
        task_stack.sort(key=lambda t: t[0], reverse=True)  # sort by gene value

        # clear previous task assignation
        #for cpu in cpus_alloc.values():
        #    cpu["tasks"].clear()

        # aux list  -- for easy sorting
        cpu_stack = [cpu for cpu in cpus_alloc.values()]

        # partition
        for _, max_task in task_stack:
            if "cpu" in max_task:
                cpu_id = max_task["cpu"]
                cpus_alloc[cpu_id]["tasks"].append(max_task)
            else:
                # create auxiliary stack with all task j that communicate with i
                aux_stack = []

                # add the succesors
                if "p" in max_task:
                    for p in max_task["p"]:
                        for task in rts:
                            if task["id"] == p["id"]:
                                aux_stack.append((func_Y(task, rts), task))

                # add other tasks that communicate with the task (the task will be the succesor)
                # for task in [t for t in rts if t is not max_task]:
                #    if "p" in task:
                #        for p in task["p"]:
                #           if p["id"] == max_task["id"]:
                #                aux_stack.append((func_Y(task, rts), task))

                cpu_a = None

                # order by func_y
                if aux_stack:
                    aux_stack.sort(key=lambda t: t[0], reverse=True)
                    aux_max_task = aux_stack[0]

                    # find the cpu at which the aux_max_task is allocated
                    for cpu in cpus_alloc.values():
                        if aux_max_task in cpu["tasks"]:
                            cpu_a = cpu

                # if not aux_stack or cpu_a is None:
                if cpu_a is None:
                    # update uf factors and allocate task to cpu with min uf
                    for cpu in cpus_alloc.values():
                        cpu["uf"] = sum([t["uf"] for t in cpu["tasks"]])
                    cpu_stack.sort(key=lambda c: c["uf"])
                    cpu_stack[0]["tasks"].append(max_task)
                else:
                    cpu_a["tasks"].append(max_task)

        # return the task allocation performed using the chromosome
        return cpus_alloc

    def cost(individual):
        # apply the cost function to the chromosome based in the cpu allocation produced
        return func_cost_p(rts, get_cpu_alloc(individual))

    def init_population(individual, rts, n):
        # generate initial population
        p_list = []

        # generate first chromosome
        chromosome = []
        for task in rts:
            g = func_Y(task, rts) * int(random.uniform(0, 1) * len(rts))
            chromosome.append(g)
        p_list.append(chromosome)

        # remaining chromosomes
        for _ in range(n - 1):
            new_chromosome = []
            nu = max(chromosome) / 10
            for g1 in chromosome:
                g2 = abs(g1 + int(random.uniform(-nu, nu)))
                new_chromosome.append(g2)
            p_list.append(new_chromosome)

        return [individual(c) for c in p_list]

    creator.create("FitnessMin", base.Fitness, weights=(-1.0,))

    # Defines each individual as a list
    creator.create("Individual", list, fitness=creator.FitnessMin)

    toolbox = base.Toolbox()

    # Initialize the population
    toolbox.register("population", init_population, creator.Individual, rts)

    # Applies a gaussian mutation of mean mu and standard deviation sigma on the input individual. The indpb argument
    # is the probability of each attribute to be mutated.
    toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.01)

    # Use map to pass values
    toolbox.register("evaluate", cost)

    # Generate the initial population (first generation)
    population = toolbox.population(n=6)

    # Evaluate the first generation
    fitnesses = map(toolbox.evaluate, population)
    for ind, fit in zip(population, fitnesses):
        ind.fitness.values = (fit,)

    for _ in range(120):  # generations

        # Select the k worst individuals among the input individuals.
        population_worst = tools.selWorst(population, int(len(population) / 2))

        # Perform a roulette selection and apply a crossover to the selected individuals
        for _ in range(len(population_worst)):
            pair = tools.selRoulette(population_worst, 2)  # roulette
            tools.cxOnePoint(pair[0], pair[1])  # one point crossover

        # Mutate
        for c in population_worst:
            toolbox.mutate(c)
            del c.fitness.values  # delete the fitness value

        # Evaluate again the entire population
        fitnesses = map(toolbox.evaluate, population)
        for ind, fit in zip(population, fitnesses):
            ind.fitness.values = (fit,)

    # print the final population
    print_population(population)

    # memory constraint verification
    for i, ind in enumerate(population):
        valid_cpu = True
        ch_cpus = get_cpu_alloc(ind)
        for cpuid, cpu in ch_cpus.items():
            if cpus[cpuid]["capacity"] < sum([t["r"] for t in cpu["tasks"]]):
                valid_cpu = False
        if valid_cpu:
            print_results(i, rts, ch_cpus)
        else:
            print("Chromosome {0} -- Invalid assignation found.".format(i))
Exemple #36
0
def solve(self):
    """Method to perform NSGA-II using DEAP tools
    
    Parameters
    ----------
    self : OptiGenAlgNsga2Deap
        Solver to perform NSGA-II

    Returns
    -------
    multi_output : OutputMultiOpti
        class containing the results
    """

    if self.problem == None:
        raise MissingProblem(
            "The problem has not been defined, please add a problem to OptiGenAlgNsga2Deap"
        )

    # Create the toolbox
    self.create_toolbox()

    # Add the design variable names
    self.multi_output.design_var_names = list(self.problem.design_var.keys())
    self.multi_output.design_var_names.sort()

    # Add the fitness names
    self.multi_output.fitness_names = list(self.problem.obj_func.keys())
    self.multi_output.fitness_names.sort()

    # Add the reference output to multi_output
    self.multi_output.output_ref = self.problem.output

    # Create the first population
    pop = self.toolbox.population(self.size_pop)

    # Start of the evaluation of the generation
    time_start_gen = datetime.now().strftime("%H:%M:%S")

    # Evaluate the population
    nb_error = 0
    for i in range(0, self.size_pop):
        nb_error += evaluate(self, pop[i])
        print(
            "\r{}  gen {:>5}: {:>5.2f}%, {:>4} errors.".format(
                time_start_gen, 0, (i + 1) * 100 / self.size_pop, nb_error),
            end="",
        )

    # Check the constraints violation
    nb_infeasible = 0
    if len(self.problem.constraint) > 0:
        for indiv in pop:
            nb_infeasible += check_cstr(self, indiv)
    print("\r{}  gen {:>5}: 100%, {:>4} errors,{:>4} infeasible.".format(
        time_start_gen, 0, nb_error, nb_infeasible - nb_error))

    # Add pop to OutputMultiOpt
    for indiv in pop:
        # Check that at every fitness values is different from inf
        is_valid = indiv.fitness.valid and indiv.is_simu_valid and indiv.cstr_viol == 0

        # Add the indiv to the multi_output
        self.multi_output.add_evaluation(indiv.output, is_valid, list(indiv),
                                         indiv.fitness.values, 0)

    if self.selector == None:
        pop = selNSGA2(pop, self.size_pop)
    else:
        parents = self.selector(pop, self.size_pop)

    ############################
    # LOOP FOR EACH GENERATION #
    ############################
    for ngen in range(1, self.nb_gen):
        time_start_gen = datetime.now().strftime("%H:%M:%S")
        # Extracting parents using
        parents = tournamentDCD(pop, self.size_pop)

        # Copy new indivuals
        children = []

        for indiv in parents:
            child = self.toolbox.individual()
            for i in range(len(indiv)):
                child[i] = deepcopy(indiv[i])
            child.output = Output(init_dict=indiv.output.as_dict())
            child.fitness = deepcopy(indiv.fitness)
            children.append(child)

        for indiv1, indiv2 in zip(children[::2], children[::-2]):
            # Crossover
            is_cross = False
            if random.random() < self.p_cross:
                is_cross = True
                if self.crossover == None:
                    cxOnePoint(indiv1, indiv2)

            # Mutation
            is_mutation = self.mutate(indiv1)
            if is_cross or is_mutation:
                update(indiv1)

            is_mutation = self.mutate(indiv2)
            if is_cross or is_mutation:
                update(indiv2)

        # Evaluate the children
        to_eval = []
        for indiv in children:
            if indiv.fitness.valid == False:
                to_eval.append(indiv)

        nb_error = 0
        for i in range(len(to_eval)):
            nb_error += evaluate(self, to_eval[i])
            print(
                "\r{}  gen {:>5}: {:>5.2f}%, {:>4} errors.".format(
                    time_start_gen, ngen, (i + 1) * 100 / len(to_eval),
                    nb_error),
                end="",
            )

        # Check the constraints violation
        nb_infeasible = 0
        if len(self.problem.constraint) > 0:
            for indiv in to_eval:
                nb_infeasible += check_cstr(self, indiv)
        print("\r{}  gen {:>5}: 100%, {:>4} errors,{:>4} infeasible.".format(
            time_start_gen, ngen, nb_error, nb_infeasible - nb_error))
        # Add children to OutputMultiOpti
        for indiv in children:
            # Check that at every fitness values is different from inf
            is_valid = (indiv.fitness.valid and indiv.is_simu_valid
                        and indiv.cstr_viol == 0)

            # Add the indiv to the multi_output
            self.multi_output.add_evaluation(
                indiv.output,
                is_valid,
                list(indiv),
                indiv.fitness.values,
                ngen,
            )

        # Sorting the population according to NSGA2
        if self.selector == None:
            pop = selNSGA2(pop + children, self.size_pop)
        else:
            pop = self.selector(pop, self.size_pop)

    return self.multi_output