Esempio n. 1
0
    def test_generate_population(self):
        """Test generating of initial population."""
        df_families = get_df_families(100, 20)
        population_sizes = (0, 1, 2, 100)
        for population_size in population_sizes:
            ais = ArtificialImmuneSystem(
                df_families=df_families, clonator=None, mutator=None,
                selector=None, population_size=population_size, n_generations=0
            )

            population = ais.generate_population()
            self.assertEqual(
                len(population),
                population_size,
                msg=f'Population size is `{len(population)}, expected '
                    f'`{population_size}`.'
            )

            population_size += 1
            population = ais.generate_population(n=population_size)
            self.assertEqual(
                len(population),
                population_size,
                msg=f'Population size from parameter `n` is '
                    f'`{len(population)}, expected `{population_size}`.'
            )
    def test_fitness(self):
        """Test fitness computation."""
        family_size = 126
        n_days = 11
        df_families = get_df_families(n_days, family_size)
        df_families.iloc[0]['n_people'] += 1
        families = np.array([i for i in range(1, n_days + 1)])
        days = {i: family_size for i in range(n_days)}
        days[0] += 1

        expected_fitness = 50 * 2 + 100 + 200 * 2 + 300 * 2 + 400 + 500 * 2
        expected_fitness += (9 * 3 + 18 * 2 + 36 * 4 + 199 + 398) * family_size
        expected_fitness += 1 / 400. * family_size ** (1 / 2.) * (n_days - 1)
        expected_fitness += 2 / 400. * (family_size + 1) ** (1 / 2. + 1 / 50.)

        antibody = Antibody(families=families, days=days)
        antibody.fitness(df_families)

        self.assertAlmostEqual(
            antibody.fitness_value,
            expected_fitness,
            places=7,
            msg=f'Fitness of antibody is `{antibody.fitness_value}`, expected '
                f'`{expected_fitness}`.'
        )
    def test_generate_solution(self):
        """Test random solution generating."""
        expected_min_day_size = 125
        expected_max_day_size = 300
        real_data_path = 'data/family_data.csv'

        dfs = [get_df_families(1000, 20)]
        if os.path.isfile(real_data_path):
            dfs.append(pd.read_csv(real_data_path))

        for df_families in dfs:
            antibody = Antibody()
            antibody.generate_solution(df_families)

            day_sizes = {}
            for i, day in enumerate(antibody.families):
                value = day_sizes.get(day, 0)
                day_sizes[day] = value + df_families.iloc[i]['n_people']

            for key, size in antibody.days.items():
                expected_day_size = day_sizes[key]
                self.assertTrue(
                    expected_min_day_size <= size <= expected_max_day_size,
                    msg=f'Number of people scheduled for `{key + 1}th` day is '
                        f'`{size}`, expected to be '
                        f'in `<{expected_min_day_size}, '
                        f'{expected_max_day_size}>`.'
                )
                self.assertEqual(
                    size,
                    expected_day_size,
                    msg=f'Number of people scheduled for `{key + 1}th` day is '
                        f'{size}, expected `{expected_day_size}`.'
                )
Esempio 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}`.')
Esempio n. 5
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}`.')
Esempio n. 6
0
    def test_fitness(self):
        """Test fitness computation."""
        n_families, family_size = 3, 125
        df_families = get_df_families(n_families, family_size)
        days = {i: family_size for i in range(n_families)}
        population = (
            Antibody(families=np.array([1, 2, 3]), days=days),
            Antibody(families=np.array([3, 3, 3]), days=days),
            Antibody(families=np.array([1, 1, 2]), days=days),
        )
        fitnesses = (
            50 * 2 + 9 * family_size, (50 + 9 * family_size) * n_families, 50
        )
        expected_fitness_min = np.min(fitnesses)
        expected_fitness_avg = np.mean(fitnesses)

        ais = ArtificialImmuneSystem(
            df_families=df_families, clonator=None, mutator=None,
            selector=None, population_size=0, n_generations=0, n_cpu=7
        )
        _, best_antibody, fitness_avg = ais.fitness(population)
        fitness_min = best_antibody.fitness_value

        self.assertAlmostEqual(
            fitness_min,
            expected_fitness_min,
            places=7,
            msg=f'Minimum fitness is `{fitness_min}`, expected '
                f'`{expected_fitness_min}`.'
        )
        self.assertAlmostEqual(
            fitness_avg,
            expected_fitness_avg,
            places=7,
            msg=f'Average fitness is `{fitness_avg}`, expected '
                f'`{expected_fitness_avg}`.'
        )