Exemplo n.º 1
0
    def test_mimic_discrete_max():
        """Test mimic function for a discrete maximization problem"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        _, _, curve = mimic(problem, max_attempts=50, timing=True)

        assert (curve.shape[1] == 2)
Exemplo n.º 2
0
    def test_hill_climb_discrete_max():
        """Test hill_climb function for a discrete maximization problem"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        _, _, curve = hill_climb(problem, restarts=20, timing=True)

        assert (curve.shape[1] == 2)
Exemplo n.º 3
0
    def test_random():
        """Test random method"""

        problem = DiscreteOpt(5, OneMax(), maximize=True, max_val=5)

        rand = problem.random()

        assert (len(rand) == 5 and max(rand) >= 0 and min(rand) <= 4)
Exemplo n.º 4
0
    def test_eval_fitness_min():
        """Test eval_fitness method for a minimization problem"""

        problem = OptProb(5, OneMax(), maximize=False)
        x = np.array([0, 1, 2, 3, 4])
        fitness = problem.eval_fitness(x)

        assert fitness == -10
Exemplo n.º 5
0
    def test_genetic_alg_discrete_max():
        """Test genetic_alg function for a discrete maximization problem"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        best_state, best_fitness, _ = genetic_alg(problem, max_attempts=50)

        x = np.array([1, 1, 1, 1, 1])

        assert (np.array_equal(best_state, x) and best_fitness == 5)
Exemplo n.º 6
0
    def test_mimic_discrete_min():
        """Test mimic function for a discrete minimization problem"""

        problem = DiscreteOpt(5, OneMax(), maximize=False)
        best_state, best_fitness, _ = mimic(problem, max_attempts=50)

        x = np.array([0, 0, 0, 0, 0])

        assert (np.array_equal(best_state, x) and best_fitness == 0)
Exemplo n.º 7
0
    def test_hill_climb_discrete_max():
        """Test hill_climb function for a discrete maximization problem"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        best_state, best_fitness, _ = hill_climb(problem, restarts=20)

        x = np.array([1, 1, 1, 1, 1])

        assert (np.array_equal(best_state, x) and best_fitness == 5)
Exemplo n.º 8
0
    def test_hill_climb_continuous_min():
        """Test hill_climb function for a continuous minimization problem"""

        problem = ContinuousOpt(5, OneMax(), maximize=False)
        best_state, best_fitness, _ = hill_climb(problem, restarts=20)

        x = np.array([0, 0, 0, 0, 0])

        assert (np.array_equal(best_state, x) and best_fitness == 0)
Exemplo n.º 9
0
 def count_ones(self, stateCount):
     stateCount = stateCount
     initial = OneMax()
     state = self.generate_test_state(stateCount, "CO")
     problem = DiscreteOpt(length=stateCount,
                           fitness_fn=initial,
                           maximize=True)
     problem.set_state(state)
     return problem
Exemplo n.º 10
0
    def test_genetic_alg_continuous_min():
        """Test genetic_alg function for a continuous minimization problem"""

        problem = ContinuousOpt(5, OneMax(), maximize=False)
        best_state, best_fitness, _ = genetic_alg(problem, max_attempts=200)

        x = np.array([0, 0, 0, 0, 0])

        assert (np.allclose(best_state, x, atol=0.5) and best_fitness < 1)
Exemplo n.º 11
0
    def test_simulated_annealing_discrete_max():
        """Test simulated_annealing function for a discrete maximization
        problem"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        _, _, curve = simulated_annealing(problem,
                                          timing=True,
                                          max_attempts=50)

        assert (curve.shape[1] == 2)
Exemplo n.º 12
0
    def test_set_state_min():
        """Test set_state method for a minimization problem"""

        problem = OptProb(5, OneMax(), maximize=False)

        x = np.array([0, 1, 2, 3, 4])
        problem.set_state(x)

        assert (np.array_equal(problem.get_state(), x)
                and problem.get_fitness() == -10)
Exemplo n.º 13
0
    def test_find_sample_order():
        """Test find_sample_order method"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        problem.parent_nodes = np.array([2, 0, 1, 0])

        order = np.array([0, 2, 4, 1, 3])
        problem.find_sample_order()

        assert np.array_equal(np.array(problem.sample_order), order)
Exemplo n.º 14
0
    def test_reproduce_mut1_max2():
        """Test reproduce method when mutation_prob is 1 and max_val is 2"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        father = np.array([0, 0, 0, 0, 0])
        mother = np.array([1, 1, 1, 1, 1])

        child = problem.reproduce(father, mother, mutation_prob=1)

        assert (len(child) == 5 and sum(child) > 0 and sum(child) < 5)
Exemplo n.º 15
0
    def test_simulated_annealing_continuous_min():
        """Test simulated_annealing function for a continuous minimization
        problem"""

        problem = ContinuousOpt(5, OneMax(), maximize=False)
        best_state, best_fitness, _ = simulated_annealing(problem,
                                                          max_attempts=50)

        x = np.array([0, 0, 0, 0, 0])

        assert (np.array_equal(best_state, x) and best_fitness == 0)
Exemplo n.º 16
0
    def test_mimic_curve_length_max_attempts():
        """Test random_hill_climb function such that when curve is True for ma_iters
        the length of all fitness scores should be equal to max_iters"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)

        max_attempts = 10

        best_state, best_fitness, all_fitness = simulated_annealing(
            problem, max_attempts=max_attempts, curve=True)
        assert len(all_fitness) != max_attempts
Exemplo n.º 17
0
    def test_simulated_annealing_discrete_max():
        """Test simulated_annealing function for a discrete maximization
        problem"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        best_state, best_fitness, fitness_curve = simulated_annealing(
            problem, max_attempts=50, curve=True, max_iters=1000)

        x = np.array([1, 1, 1, 1, 1])

        assert (np.array_equal(best_state, x) and best_fitness == 5)
Exemplo n.º 18
0
    def test_random_neighbor_max2():
        """Test random_neighbor method when max_val is equal to 2"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)

        x = np.array([0, 0, 1, 1, 1])
        problem.set_state(x)

        neigh = problem.random_neighbor()
        sum_diff = np.sum(np.abs(x - neigh))

        assert (len(neigh) == 5 and sum_diff == 1)
Exemplo n.º 19
0
    def test_random_hill_climb_discrete_min():
        """Test random_hill_climb function for a discrete minimization
        problem"""

        problem = DiscreteOpt(5, OneMax(), maximize=False)
        best_state, best_fitness, _ = random_hill_climb(problem,
                                                        max_attempts=10,
                                                        restarts=20)

        x = np.array([0, 0, 0, 0, 0])

        assert (np.array_equal(best_state, x) and best_fitness == 0)
Exemplo n.º 20
0
    def test_random_hill_climb_continuous_max():
        """Test random_hill_climb function for a continuous maximization
        problem"""

        problem = ContinuousOpt(5, OneMax(), maximize=True)
        best_state, best_fitness, _ = random_hill_climb(problem,
                                                        max_attempts=10,
                                                        restarts=20)

        x = np.array([1, 1, 1, 1, 1])

        assert (np.array_equal(best_state, x) and best_fitness == 5)
Exemplo n.º 21
0
    def test_simulated_annealing_curve_length_max_iters():
        """Test random_hill_climb function such that when curve is True for ma_iters
        the length of all fitness scores should be equal to max_iters"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        x = np.array([0, 0, 0, 0, 0])

        max_iters = 300

        best_state, best_fitness, all_fitness = simulated_annealing(
            problem, max_iters=max_iters, init_state=x, curve=True)
        assert len(all_fitness) == max_iters
Exemplo n.º 22
0
    def test_mimic_discrete_max_fast():
        """Test mimic function for a discrete maximization problem using
        fast mimic"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        best_state, best_fitness = mimic(problem,
                                         max_attempts=50,
                                         fast_mimic=True)

        x = np.array([1, 1, 1, 1, 1])

        assert (np.array_equal(best_state, x) and best_fitness == 5)
Exemplo n.º 23
0
    def test_random_pop():
        """Test random_pop method"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        problem.random_pop(100)

        pop = problem.get_population()
        pop_fitness = problem.get_pop_fitness()

        assert (np.shape(pop)[0] == 100 and np.shape(pop)[1] == 5
                and np.sum(pop) > 0 and np.sum(pop) < 500
                and len(pop_fitness) == 100)
Exemplo n.º 24
0
    def test_hill_climb_max_iters():
        """Test hill_climb function with max_iters less than infinite"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        x = np.array([0, 0, 0, 0, 0])

        best_state, best_fitness, _ = hill_climb(problem,
                                                 max_iters=1,
                                                 restarts=0,
                                                 init_state=x)

        assert best_fitness == 1
Exemplo n.º 25
0
    def test_random():
        """Test random method"""

        problem = ContinuousOpt(5,
                                OneMax(),
                                maximize=True,
                                min_val=0,
                                max_val=4)

        rand = problem.random()

        assert (len(rand) == 5 and max(rand) >= 0 and min(rand) <= 4)
Exemplo n.º 26
0
    def test_best_neighbor_min():
        """Test best_neighbor method for a minimization problem"""

        problem = OptProb(5, OneMax(), maximize=False)

        pop = np.array([[0, 0, 0, 0, 1], [1, 0, 1, 0, 1], [1, 1, 1, 1, 0],
                        [1, 0, 0, 0, 1], [100, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                        [1, 1, 1, 1, 1], [0, 0, 0, 0, -50]])

        problem.neighbors = pop
        x = problem.best_neighbor()

        assert np.array_equal(x, np.array([0, 0, 0, 0, -50]))
Exemplo n.º 27
0
    def test_eval_mate_probs():
        """Test eval_mate_probs method"""

        problem = OptProb(5, OneMax(), maximize=True)
        pop = np.array([[0, 0, 0, 0, 1], [1, 0, 1, 0, 1], [1, 1, 1, 1, 0],
                        [1, 0, 0, 0, 1], [0, 0, 0, 0, 0], [1, 1, 1, 1, 1]])

        problem.set_population(pop)
        problem.eval_mate_probs()

        probs = np.array([0.06667, 0.2, 0.26667, 0.13333, 0, 0.33333])

        assert np.allclose(problem.get_mate_probs(), probs, atol=0.00001)
Exemplo n.º 28
0
    def test_find_neighbors_max2():
        """Test find_neighbors method when max_val is equal to 2"""

        problem = DiscreteOpt(5, OneMax(), maximize=True, max_val=2)

        x = np.array([0, 1, 0, 1, 0])
        problem.set_state(x)
        problem.find_neighbors()

        neigh = np.array([[1, 1, 0, 1, 0], [0, 0, 0, 1, 0], [0, 1, 1, 1, 0],
                          [0, 1, 0, 0, 0], [0, 1, 0, 1, 1]])

        assert np.array_equal(np.array(problem.neighbors), neigh)
Exemplo n.º 29
0
    def test_best_child_min():
        """Test best_child method for a minimization problem"""

        problem = OptProb(5, OneMax(), maximize=False)

        pop = np.array([[0, 0, 0, 0, 1], [1, 0, 1, 0, 1], [1, 1, 1, 1, 0],
                        [1, 0, 0, 0, 1], [100, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                        [1, 1, 1, 1, 1], [0, 0, 0, 0, -50]])

        problem.set_population(pop)
        x = problem.best_child()

        assert np.array_equal(x, np.array([0, 0, 0, 0, -50]))
Exemplo n.º 30
0
    def test_simulated_annealing_max_iters():
        """Test simulated_annealing function with max_iters less than
        infinite"""

        problem = DiscreteOpt(5, OneMax(), maximize=True)
        x = np.array([0, 0, 0, 0, 0])

        best_state, best_fitness, _ = simulated_annealing(problem,
                                                          max_attempts=1,
                                                          max_iters=1,
                                                          init_state=x)

        assert best_fitness == 1