def test_generation_enfant_non_hybride(self):
     pere = generate_scorpion()
     mere = generate_scorpion()
     enfant = generation_enfant(pere, mere, 0)
     assert len(enfant) == 10
     assert scorpion_identique(enfant, pere) | scorpion_identique(
         enfant, mere)
Esempio n. 2
0
def selection_parents_tournoi(info_scorpion_energie_portee_note):
    """
        Fonction selectionnant les parents à l'aide d'un algorithme de selection par tournoi.

    :param info_scorpion_energie_portee_note: liste d'info scorpion de longueur n
    :type info_scorpion_energie_portee_note: liste_info[scorpion, energie, portee, note]
    :return: scorpions parents
    :rtype: scorpion[n]

    .. seealso:: :func:`src.Metier.genetique.duel`
    """
    population_parent = []

    while len(population_parent) < len(info_scorpion_energie_portee_note):

        scorpion_note_1 = choice(info_scorpion_energie_portee_note)
        scorpion_note_2 = generate_scorpion_null()

        continuer = 1
        while continuer:
            scorpion_note_2 = choice(info_scorpion_energie_portee_note)
            if scorpion_identique(scorpion_note_1[0], scorpion_note_2[0]) == 0:
                continuer = 0

        winner = duel(scorpion_note_1, scorpion_note_2)
        population_parent.append(winner)

    return population_parent
    def test_duel_scorpion2_meilleur(self):
        liste_info_scorpion1 = [self.scorpion1, 0, 0, 0]
        liste_info_scorpion2 = [self.scorpion2, 0, 0, 1]

        scorpion_gagnant = duel(liste_info_scorpion1, liste_info_scorpion2)
        assert len(scorpion_gagnant) == 10
        assert scorpion_identique(scorpion_gagnant, self.scorpion2)
Esempio n. 4
0
def generer_generation_suivante(info_scorpion_energie_portee_note,
                                chance_to_hybrid, chance_to_mutate):
    """
        Fonction qui genere la generation suivante de scorpion. A partir d'une liste de longueur n d'info scorpion de la
        forme [scorpion, energie, portee, note]. On va sélectionner n parent grâce à un algorithme de selection par tournoi
        à 1 étage. Puis on formera des couples qui genereront 2 enfants avec une chance d'hybridation. Chaque enfant aura ensuite
        une chance de muter.

    :param info_scorpion_energie_portee_note: tableau d'info
    :type info_scorpion_energie_portee_note: liste[scorpion, energie, portee, note]
    :param chance_to_hybrid: coefficient d'hybridation
    :type chance_to_hybrid: float
    :param chance_to_mutate: coefficient de mutation
    :return: tableau de scorpion
    :rtype: scorpion[n]

    .. seealso:: :func:`src.Tools.selection_parents_tournoi`
    .. seealso:: :func:`src.Tools.scorpion_identique`
    .. seealso:: :func:`src.Tools.generer_enfants`
    .. seealso:: :func:`src.Tools.mutation_individu`
    """
    population_parent = selection_parents_tournoi(
        info_scorpion_energie_portee_note)
    population_enfant = []
    population_count = len(population_parent)
    while len(population_enfant) < population_count:

        id_parent1 = randrange(0, population_count - 1, 1)
        id_parent2 = 0

        continuer = 1
        while continuer:
            id_parent2 = randrange(0, population_count - 1, 1)
            if id_parent2 != id_parent1:
                identique = scorpion_identique(population_parent[id_parent1],
                                               population_parent[id_parent2])
                if identique == 0:
                    continuer = 0

        enfant1, enfant2 = generer_enfants(population_parent[id_parent1],
                                           population_parent[id_parent2],
                                           chance_to_hybrid)
        enfant1 = mutation_individu(enfant1, chance_to_mutate)
        enfant2 = mutation_individu(enfant2, chance_to_mutate)
        population_enfant.append(enfant1)
        population_enfant.append(enfant2)

    return population_enfant
Esempio n. 5
0
 def test_generate_scorpion_null(self):
     scorpion_null = TestScorpion.generate_scorpion_null()
     scorpion_null2 = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
     assert len(scorpion_null) == 10
     assert len(scorpion_null2) == 10
     assert scorpion_identique(scorpion_null, scorpion_null2) == 1
Esempio n. 6
0
 def test_scorpions_identique(self):
     scorpion2 = TestScorpion.generate_scorpion_null()
     assert scorpion_identique(self.scorpion, self.scorpion) == 1
     assert scorpion_identique(scorpion2, self.scorpion) == 0