Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
def learn_DNF__recombination():
    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()

    recomb_rate = 0.176
    natural_process = RecombinationProcess(recomb_rate)

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

    recombinator = Recombinator(neighborhood, performance_oracle, tolerance,
                                epsilon)
    algorithm = ConstantPopulationMutationConjunctionAlgorithm(
        recombinator, length, epsilon, performance_oracle, 75)

    print algorithm.learn_ideal_function_until_match()
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
 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)