def test_mutation_called_based_on_mutation_factor(self):
        self.mutation_factor = 1
        self.neighborhood_calc = NeighborhoodWithOtherRepresentations(
            self.number_of_activations, self.mutation_neighborhood,
            self.mutation_factor, self.natural_process)

        first_rep = (1, 3, 5)
        second_rep = (2, 4, 6)

        self.neighborhood_calc.get_neighborhood(first_rep, second_rep)

        self.assertEqual(
            self.number_of_activations, self.mutation_neighborhood.
            get_neighborhood_of_representation.call_count)
class TestNeighborhoodWithPopulation(unittest.TestCase):
    def setUp(self):
        self.number_of_activations = 2
        self.mutation_neighborhood = Mock()
        self.natural_process = Mock()

        def return_itself(*args):
            return args[0]

        self.mutation_neighborhood.get_neighborhood_of_representation.side_effect = return_itself
        self.natural_process.get_a_mutation_from_the_reps.side_effect = return_itself

    def test_no_mutation(self):
        self.mutation_factor = 0
        self.neighborhood_calc = NeighborhoodWithOtherRepresentations(
            self.number_of_activations, self.mutation_neighborhood,
            self.mutation_factor, self.natural_process)

        first_rep = (1, 3, 5)
        second_rep = (2, 4, 6)

        self.neighborhood_calc.get_neighborhood(first_rep, second_rep)

        self.assertEqual(
            0, self.mutation_neighborhood.get_neighborhood_of_representation.
            call_count)

    def test_mutation_called_based_on_mutation_factor(self):
        self.mutation_factor = 1
        self.neighborhood_calc = NeighborhoodWithOtherRepresentations(
            self.number_of_activations, self.mutation_neighborhood,
            self.mutation_factor, self.natural_process)

        first_rep = (1, 3, 5)
        second_rep = (2, 4, 6)

        self.neighborhood_calc.get_neighborhood(first_rep, second_rep)

        self.assertEqual(
            self.number_of_activations, self.mutation_neighborhood.
            get_neighborhood_of_representation.call_count)
    def test_no_mutation(self):
        self.mutation_factor = 0
        self.neighborhood_calc = NeighborhoodWithOtherRepresentations(
            self.number_of_activations, self.mutation_neighborhood, self.mutation_factor, self.natural_process
        )

        first_rep = (1, 3, 5)
        second_rep = (2, 4, 6)

        self.neighborhood_calc.get_neighborhood(first_rep, second_rep)

        self.assertEqual(0, self.mutation_neighborhood.get_neighborhood_of_representation.call_count)
class TestNeighborhoodWithPopulation(unittest.TestCase):
    def setUp(self):
        self.number_of_activations = 2
        self.mutation_neighborhood = Mock()
        self.natural_process = Mock()

        def return_itself(*args):
            return args[0]

        self.mutation_neighborhood.get_neighborhood_of_representation.side_effect = return_itself
        self.natural_process.get_a_mutation_from_the_reps.side_effect = return_itself

    def test_no_mutation(self):
        self.mutation_factor = 0
        self.neighborhood_calc = NeighborhoodWithOtherRepresentations(
            self.number_of_activations, self.mutation_neighborhood, self.mutation_factor, self.natural_process
        )

        first_rep = (1, 3, 5)
        second_rep = (2, 4, 6)

        self.neighborhood_calc.get_neighborhood(first_rep, second_rep)

        self.assertEqual(0, self.mutation_neighborhood.get_neighborhood_of_representation.call_count)

    def test_mutation_called_based_on_mutation_factor(self):
        self.mutation_factor = 1
        self.neighborhood_calc = NeighborhoodWithOtherRepresentations(
            self.number_of_activations, self.mutation_neighborhood, self.mutation_factor, self.natural_process
        )

        first_rep = (1, 3, 5)
        second_rep = (2, 4, 6)

        self.neighborhood_calc.get_neighborhood(first_rep, second_rep)

        self.assertEqual(
            self.number_of_activations, self.mutation_neighborhood.get_neighborhood_of_representation.call_count
        )
def recombination_main():
    common_classes = CommonClassesCreator()

    length, epsilon, mutation_neighborhood, tolerance = common_classes.get_common_classes(
    )

    mutation_factor = 0.1

    concept_class = MonotoneConjunction(length)
    performance = common_classes.get_perf_without_precomp(concept_class)

    recomb_process = RecombinationProcess()
    neighborhood = NeighborhoodWithOtherRepresentations(
        length, mutation_neighborhood, mutation_factor, recomb_process)
    recombinator = Recombinator(neighborhood, performance, tolerance, epsilon)
    recombination = RecombinationConjunctionAlgorithm(recombinator, length,
                                                      epsilon, concept_class)

    recombination.learn_ideal_function(concept_class)
Пример #6
0
def learn_DNF__HGT():
    dnf = DNF()
    length = 58
    epsilon = (2**-53)
    tau = (epsilon / length)**3 * log(1/epsilon)

    tolerance = ConjunctionTolerance(length, epsilon)
    conjunction_performance_oracle = OneSidedPerformanceOracleWithTolerance(dnf, tau)
    performance_oracle = DNFOneSidePerformanceOracleWithTolerance(dnf, tau, epsilon,
                                                                  conjunction_performance_oracle)
    mutation_neighborhood = MonotoneConjunctionNeighborhood()

    natural_process = HGTProcess(0.35, length)

    neighborhood = NeighborhoodWithOtherRepresentations(length, mutation_neighborhood,
                                                        0.1, natural_process)

    HGT_mutator = HGT_Mutator(neighborhood, performance_oracle, tolerance, epsilon, natural_process)
    algorithm = ConstantPopulationMutationConjunctionAlgorithm(HGT_mutator, length, epsilon, performance_oracle, 75)

    print algorithm.learn_ideal_function_until_match()
def HGT_main():
    common_classes = CommonClassesCreator()

    length, epsilon, mutation_neighborhood, tolerance = common_classes.get_common_classes(
    )
    mutation_factor = 0.1
    HGT_factor = 1

    concept_class = MonotoneConjunction(length)
    performance = common_classes.get_perf_without_precomp(concept_class)

    HGT_process = HGTProcess(HGT_factor, length)
    neighborhood = NeighborhoodWithOtherRepresentations(
        length, mutation_neighborhood, mutation_factor, HGT_process)
    HGT_mutator = HGT_Mutator(neighborhood, performance, tolerance, epsilon,
                              HGT_process)
    mutation = HGTConjunctionAlgorithm(HGT_mutator, length, epsilon,
                                       performance)

    final_population = mutation.learn_ideal_function(concept_class)

    if len(final_population) <= 30:
        print final_population
 def get_neighborhood_with_other_representations(self, mutation_factor):
     return NeighborhoodWithOtherRepresentations(self.number_of_mutations_from_mutator,
                                                 self.mutation_neighborhood,
                                                 mutation_factor, self.natural_process)