def setUp(self): self.neighborhood = Mock() self.performance = Mock() self.tolerance = Mock() self.mutation_probability = Mock() self.mutation_probability.get_relational_probability.return_value = 0.2 epsilon = 1 self.selection_size = 1 self.mutator = Mutator(self.neighborhood, self.performance, self.tolerance, self.mutation_probability, epsilon) self.tolerance.get_tolerance.return_value = 2**-5
class TestMutator(unittest.TestCase): def setUp(self): self.neighborhood = Mock() self.performance = Mock() self.tolerance = Mock() self.mutation_probability = Mock() self.mutation_probability.get_relational_probability.return_value = 0.2 epsilon = 1 self.selection_size = 1 self.mutator = Mutator(self.neighborhood, self.performance, self.tolerance, self.mutation_probability, epsilon) self.tolerance.get_tolerance.return_value = 2**-5 def set_neighborhood(self): neighborhood = set() neighborhood.add(self.expected_rep) neighborhood.add((1, 1, 0, 0)) neighborhood.add((1, 0, 0, 1)) self.neighborhood.get_neighborhood_of_representation.return_value = neighborhood def test_mutator__one_in_bene(self): representation = (1, 1, 0, 1) perf_of_representation = 0.2 self.expected_rep = (1, 1, 1, 1) perf_of_expected_representation = 0.2 + self.tolerance.get_tolerance.return_value def perf_returns(rep): if rep == representation: return perf_of_representation if rep == self.expected_rep: return perf_of_expected_representation return 0 self.set_neighborhood() self.performance.get_estimated_performance.side_effect = perf_returns self.assertEqual(self.expected_rep, self.mutator.evolutionary_step(representation)) def test_mutator__two_in_neut(self): representation = (1, 1, 0, 1) self.expected_rep = set() self.expected_rep.add((1, 1, 1, 1)) self.expected_rep.add((0, 0, 0, 0)) self.performance.get_estimated_performance.return_value = 0 self.neighborhood.get_neighborhood_of_representation.return_value = self.expected_rep self.assertIn(self.mutator.evolutionary_step(representation), self.expected_rep)
def learn_single_time__classic_model(common_classes): length, epsilon, mutation_neighborhood, tolerance = common_classes.get_common_classes( ) mutation_probability = common_classes.get_mutation_probability() concept_class = MonotoneConjunction(length) performance = common_classes.get_perf_without_precomp(concept_class) mutator = Mutator(mutation_neighborhood, performance, tolerance, mutation_probability, epsilon) algorithm = ConjunctionEvolvabilityAlgorithm(mutator, length, epsilon, performance) return algorithm.learn_ideal_function_until_match()
def main(): common_classes = CommonClassesCreator(False) length, epsilon, mutation_neighborhood, tolerance = common_classes.get_common_classes() concept_class = MonotoneConjunction(length) performance = common_classes.get_perf_without_precomp(concept_class) mutation_probability = common_classes.get_mutation_probability() mutator = Mutator(mutation_neighborhood, performance, tolerance, mutation_probability, epsilon) algorithm = ConjunctionEvolvabilityAlgorithm(mutator, length, epsilon, performance) hypo = algorithm.learn_ideal_function(epsilon) print "HYPO IS: " + str(hypo)
def learn_DNF__basic_model(): 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() mutation_probability = ConjunctionMutationProbability( mutation_neighborhood) mutator = Mutator(mutation_neighborhood, performance_oracle, tolerance, mutation_probability, epsilon) algorithm = ConjunctionEvolvabilityAlgorithm(mutator, length, epsilon, performance_oracle) print algorithm.learn_ideal_function_until_match()