def test(self): mutation1 = RandomAdditiveMutation(GaussianRn(2, 1.0)) random.seed(42) nso = ConstantSearch([1.0, 2.0]) es1 = EvolutionStrategy(mu_param=1, lambda_param=1, mode="(mu+lambda)", nso=nso, uso=mutation1, term=MaxSteps(100)) mutation2 = RandomAdditiveMutation(GaussianESMutation(2, 1.0, 1.0, 5)) es2 = AdaptiveEvolutionStrategy(mu_param=1, lambda_param=1, mode="(mu+lambda)", nso=nso, uso=mutation2, term=MaxSteps(100)) def f(x): return x[0]**2 + x[1]**2 x1 = es1.solve(f) random.seed(42) x2 = es2.solve(f) self.assertAlmostEqual(x1.g[0], x2.g[0]) self.assertAlmostEqual(x1.g[1], x2.g[1])
def test2(self): def f(x): return math.exp(x) + math.sin(x) xs = np.linspace(0.0, 1.0, 50) ys = np.array([f(x) for x in xs]) unireg = UnivariateRegressionProblem(xs, ys) random.seed(42) rand_node = SymbolicNodeGenerator(['x'], []) mutation = NodeSingleSwap(rand_node) nso = GrowTree(3, 'float', rand_node) es1 = EvolutionStrategy(mu_param=50, lambda_param=50, mode="(mu+lambda)", nso=nso, uso=mutation, term=MaxSteps(20)) expr = es1.solve(unireg) self.assertTrue(expr.y < 5.0) evaluator = SimplifyingEvaluator(1.0, 0.5) es2 = EvolutionStrategy(mu_param=50, lambda_param=50, mode="(mu+lambda)", nso=nso, uso=mutation, term=MaxSteps(20), evaluator=evaluator) expr2 = es1.solve(unireg) self.assertTrue(expr2.y < 5.0)
def test(self): random.seed(42) box_constr = BoxConstraint([-2.0, -2.0], [2.0, 2.0]) ram = RandomAdditiveMutation(GaussianRn(2, 0.1), cond=box_constr.get_checker()) search = HillClimbing(ConstantSearch([-1.0, 1.0]), ram, MaxSteps(100)) x = search.solve(lambda t: (t[0] - 0.25)**2) self.assertAlmostEqual(x.g[0], 0.25, places=2)
def test(self): nso = ConstantSearch(np.array([1.0, 2.0])) sigma = 0.05 uso = RandomAdditiveMutation(GaussianRn(2, sigma)) term = MaxSteps(50) rw = RandomWalk(nso, uso, term) random.seed(42) el = EvaluationLogger("test_fun") fun = logme(el)(lambda x: x[0]**2 + x[1]**2) rw.solve(fun) for i in range(len(el.evals) - 1): diff = el.evals[i][0][0] - el.evals[i + 1][0][0] self.assertTrue(np.linalg.norm(diff) < 6 * sigma)
def test(self): n_vars = 5 n_conj = 4 n_disj = 3 nso_rand = RandomBitSequence(n_vars) random.seed(42) msat = MaxSAT(n_vars, n_conj, n_disj) tabu_max_len = 30 # the result is very sensitive to this parameter neighbourhood_search = SimpleTabuSearchNeighbourhood(sat_neighbourhood) search = TabuSearch(nso_rand, neighbourhood_search, MaxSteps(100), tabu_max_len) xts = search.solve(msat) self.assertTrue(len(xts.g) == n_vars)
def test(self): random.seed(42) box_constr = BoxConstraint([-2.0, -2.0], [2.0, 2.0]) strength = 0.2 tso = DifferentialRecombination(strength, box_constr.get_fixer()) nso = RandomUniformSearch(box_constr.ai, box_constr.bi) search = DifferentialEvolution(nso=nso, n0=5, tso=tso, term=MaxSteps(100)) def f(t): return (t[0] - 0.25)**2 x = search.solve(f) self.assertAlmostEqual(x.g[0], 0.25, places=2)
def test(self): n_vars = 10 max_time = 1 max_steps = 1000 nso_const = ConstantSearch([True for _ in range(n_vars)]) uso = BitFlips(2) schedules = [{ "label": "exponential", "schedule": ExponentialSchedule(1.0, 0.05) }, { "label": "logarithmic", "schedule": LogarithmicSchedule(1.0) }, { "label": "polynomial", "schedule": PolynomialSchedule(1.0, 2, 500) }] msat = MaxSAT(n_vars, 5, 3) termination = TimeLimit(max_time) | MaxSteps(max_steps) for s in schedules: search = SimulatedAnnealing(nso_const, uso, termination, s["schedule"]) xsa = search.solve(msat) self.assertTrue(msat(xsa.g) <= msat(nso_const()))
def test(self): search = RandomSampling(RandomSequenceSearch(range(10)), MaxSteps(20)) x = search.solve(lambda i: (i - 5)**2) self.assertIn(x.g, range(10))