Ejemplo n.º 1
0
    def test_select_best(self):
        """
        Test selecting of best antibodies from population and clones.
        """
        ais = ArtificialImmuneSystem(
            df_families=None, clonator=None, mutator=None,
            selector=None, population_size=0, n_generations=0
        )
        population_fitness = [25, 15, 5, 38]
        clones_fitness = [[20, 20], [16, 16], [20, 15], [10, 40]]
        min_fitnesses = [20, 15, 5, 10]
        population, clones = [], []

        for fitness in population_fitness:
            antibody = Antibody()
            antibody.fitness_value = fitness
            population.append(antibody)

        for list_of_fitnesses in clones_fitness:
            clones_list = []
            for fitness in list_of_fitnesses:
                antibody = Antibody()
                antibody.fitness_value = fitness
                clones_list.append(antibody)
            clones.append(clones_list)

        new_population = ais.select_best(population, clones)
        for antibody, expected_fitness in zip(new_population, min_fitnesses):
            self.assertEqual(
                antibody.fitness_value,
                expected_fitness,
                msg=f'Fitness of antibody is `{antibody.fitness_value}`, '
                    f'expected `{expected_fitness}`.'
            )
 def test_lower_than(self):
     """Test less than comparison between two antibodies."""
     fitnesses = ((0, 1), (5, 10))
     for f1, f2 in fitnesses:
         a1 = Antibody()
         a1.fitness_value = f1
         a2 = Antibody()
         a2.fitness_value = f2
         self.assertLess(
             a1,
             a2,
             msg=f'Antibody `{a1}` should be less than `{a2}`.'
         )
 def test_equal(self):
     """Test equal comparison between two antibodies."""
     fitnesses = (0, 5, 10)
     for fitness in fitnesses:
         a1 = Antibody()
         a1.fitness_value = fitness
         a2 = Antibody()
         a2.fitness_value = fitness
         self.assertEqual(
             a1,
             a2,
             msg=f'Antibody `{a1}` should equal to `{a2}`.'
         )
Ejemplo n.º 4
0
    def test_basic_mutate_solutions(self):
        """
        Test whether created `Antibody` was mutated by `BasicMutator`
        class.
        """
        fitness_value = 125
        n_mutations = 5
        n_performed_mutations = 0
        n_families, family_size = 1000, 20
        df_families = get_df_families(n_families, family_size)

        antibody = Antibody()
        antibody.generate_solution(df_families)
        antibody.fitness_value = fitness_value

        basic_mutator = BasicMutator()
        mutated_antibody = basic_mutator.mutate([[copy.deepcopy(antibody)]],
                                                df_families=df_families)[0][0]

        for i in range(n_families):
            if antibody.families[i] != mutated_antibody.families[i]:
                n_performed_mutations += 1

        self.assertEqual(
            n_mutations,
            n_performed_mutations,
            msg=f'Number of mutations was `{n_performed_mutations}`, expected '
            f'`{n_mutations}`.')
Ejemplo n.º 5
0
    def test_basic_clone_solutions(self):
        """
        Test number of clones created by clone method of `BasicClonator`
        class.
        """
        antibodies = []

        # Pairs of antibody fitness and expected number of clones
        fitness_clones_pair = [[128, 7], [64, 8], [256, 1], [127, 7], [254, 1]]

        for fitness, _ in fitness_clones_pair:
            antibody = Antibody()
            antibody.fitness_value = fitness
            antibodies.append(antibody)

        basic_clonator = BasicClonator()
        clones_lists = basic_clonator.clone(antibodies)

        self.assertEqual(
            len(clones_lists),
            len(fitness_clones_pair),
            msg=f'Number of lists of clones is `{len(clones_lists)}`, '
            f'expected `{len(fitness_clones_pair)}`.')

        for ((_, n_clones), clones) in zip(fitness_clones_pair, clones_lists):
            self.assertEqual(
                len(clones),
                n_clones,
                msg=f'Number of created clones is `{len(clones)}`, '
                f'expected `{n_clones}`.')
Ejemplo n.º 6
0
    def test_preference_mutate_solutions(self):
        """
        Test whether created `Antibody` was mutated by
        `PreferenceMutator` and `AdvancedPreferenceMutator` classes.
        """
        fitness_value = 125
        n_mutations = 5
        n_performed_mutations, n_advanced_performed_mutations = 0, 0
        n_families, family_size = 1000, 20
        df_families = get_df_families(n_families, family_size)

        antibody = Antibody()
        antibody.generate_solution(df_families)
        antibody.fitness_value = fitness_value

        preference_mutator = PreferenceMutator()
        mutated_antibody = preference_mutator.mutate(
            [[copy.deepcopy(antibody)]], df_families=df_families)[0][0]

        advanced_preference_mutator = AdvancedPreferenceMutator()
        advanced_mutated_antibody = advanced_preference_mutator.mutate(
            [[copy.deepcopy(antibody)]], df_families=df_families)[0][0]

        families_choices = df_families[[f'choice_{i}' for i in range(10)]] \
            .values
        for i in range(n_families):
            if antibody.families[i] != mutated_antibody.families[i]:
                n_performed_mutations += 1
                self.assertTrue(
                    mutated_antibody.families[i] in families_choices[i],
                    msg=f'Day mutated family is going to workshop is '
                    f'{mutated_antibody.families[i]}, expected one of '
                    f'{families_choices[i]}.')
            if antibody.families[i] != advanced_mutated_antibody.families[i]:
                n_advanced_performed_mutations += 1
                self.assertTrue(
                    advanced_mutated_antibody.families[i]
                    in families_choices[i],
                    msg=f'Day advanced mutated family is going to workshop is '
                    f'{advanced_mutated_antibody.families[i]}, expected '
                    f'one of {families_choices[i]}.')

        self.assertEqual(
            n_mutations,
            n_performed_mutations,
            msg=f'Number of mutations was `{n_performed_mutations}`, expected '
            f'`{n_mutations}`.')

        self.assertEqual(n_mutations,
                         n_advanced_performed_mutations,
                         msg=f'Number of advanced mutations was '
                         f'`{n_advanced_performed_mutations}`, expected '
                         f'`{n_mutations}`.')
Ejemplo n.º 7
0
    def fitness(self, population):
        """
        Compute fitness of each antibody in `population`.

        :param population: list, list of `Antibody` objects.
        :return:
            list, list of antibodies with calculated fitness values.
            Antibody, antibody with minimum fitness value.
            float, average fitness value.
        """
        sum_fitness = 0
        best_antibody = Antibody()
        best_antibody.fitness_value = 999999999999

        with multiprocessing.Pool(self.n_cpu) as pool:
            fn = partial(self._fitness, df_families=self.df_families)
            population = pool.map(fn, population)

        for antibody in population:
            sum_fitness += antibody.fitness_value
            if antibody.fitness_value < best_antibody.fitness_value:
                best_antibody = antibody

        return population, best_antibody, sum_fitness / len(population)