コード例 #1
0
ファイル: test_opt_probs.py プロジェクト: xadahiya/mlrose
    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)
コード例 #2
0
ファイル: CompareProblems.py プロジェクト: wtomjack3/CS-7641-
    def knapsack(self, max_weight_percent, stateCount):
        state, weights, values = self.generate_test_state(stateCount, "KS")
        initial = Knapsack(weights, values, max_weight_percent)
        problem = DiscreteOpt(length=stateCount,
                              fitness_fn=initial,
                              maximize=True)

        problem.set_state(state)
        return problem
コード例 #3
0
ファイル: CompareProblems.py プロジェクト: wtomjack3/CS-7641-
    def four_peaks(self, threshold, stateCount):
        initial = FourPeaks(t_pct=threshold)
        state = self.generate_test_state(stateCount, "FP")
        problem = DiscreteOpt(length=stateCount,
                              fitness_fn=initial,
                              maximize=True)
        problem.set_state(state)

        return problem
コード例 #4
0
ファイル: CompareProblems.py プロジェクト: wtomjack3/CS-7641-
 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
コード例 #5
0
ファイル: test_opt_probs.py プロジェクト: xadahiya/mlrose
    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)
コード例 #6
0
ファイル: test_opt_probs.py プロジェクト: xadahiya/mlrose
    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)
コード例 #7
0
ファイル: test_opt_probs.py プロジェクト: xadahiya/mlrose
    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)
コード例 #8
0
ファイル: test_opt_probs.py プロジェクト: shomik/mlrose
    def test_reproduce_mut1_max_gt2():
        """Test reproduce method when mutation_prob is 1 and max_val is
        greater than 2"""

        problem = DiscreteOpt(5, OneMax(), maximize=True, max_val=3)
        problem._crossover = OnePointCrossOver(problem)

        father = np.array([0, 0, 0, 0, 0])
        mother = np.array([2, 2, 2, 2, 2])

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

        assert (len(child) == 5 and sum(child) > 0 and sum(child) < 10)
コード例 #9
0
ファイル: test_opt_probs.py プロジェクト: xadahiya/mlrose
    def test_random_neighbor_max_gt2():
        """Test random_neighbor method when max_val is greater than 2"""

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

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

        neigh = problem.random_neighbor()
        abs_diff = np.abs(x - neigh)
        abs_diff[abs_diff > 0] = 1

        sum_diff = np.sum(abs_diff)

        assert (len(neigh) == 5 and sum_diff == 1)
コード例 #10
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)
コード例 #11
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)
コード例 #12
0
 def __init__(self, length=8, verbose=False):
     self.problem = 'flipflop{l}'.format(l=length)
     self.verbose = verbose
     self.name = 'Flip Flop'
     fitness_fn = FlipFlop()
     self.problem_fit = DiscreteOpt(length=length,
                                    fitness_fn=fitness_fn,
                                    maximize=True)
コード例 #13
0
 def __init__(self, length=10, t_pct=0.1, verbose=False):
     self.problem = 'fourpeaks{l}'.format(l=length)
     self.verbose = verbose
     self.name = 'Four Peaks'
     fitness_fn = FourPeaks(t_pct=t_pct)
     self.problem_fit = DiscreteOpt(length=length,
                                    fitness_fn=fitness_fn,
                                    maximize=True,
                                    max_val=2)
コード例 #14
0
ファイル: test_algorithms.py プロジェクト: tapos/mlrose
    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)
コード例 #15
0
 def __init__(self, length=8, verbose=False):
     self.problem = 'queens{l}'.format(l=length)
     self.verbose = verbose
     self.name = 'N Queens'
     fitness_fn = Queens()
     self.problem_fit = DiscreteOpt(length=length,
                                    fitness_fn=fitness_fn,
                                    maximize=True,
                                    max_val=length)
コード例 #16
0
ファイル: test_algorithms.py プロジェクト: tapos/mlrose
    def test_hill_climb_discrete_min():
        """Test hill_climb function for a discrete minimization problem"""

        problem = DiscreteOpt(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)
コード例 #17
0
ファイル: test_algorithms.py プロジェクト: tapos/mlrose
    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)
コード例 #18
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)
コード例 #19
0
ファイル: test_opt_probs.py プロジェクト: xadahiya/mlrose
    def test_eval_node_probs():
        """Test eval_node_probs method"""

        problem = DiscreteOpt(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.keep_sample = pop
        problem.eval_node_probs()

        parent = np.array([2, 0, 1, 0])
        probs = np.array([[[0.33333, 0.66667], [0.33333, 0.66667]],
                          [[1.0, 0.0], [0.33333, 0.66667]],
                          [[1.0, 0.0], [0.25, 0.75]], [[1.0, 0.0], [0.0, 1.0]],
                          [[0.5, 0.5], [0.25, 0.75]]])

        assert (np.allclose(problem.node_probs, probs, atol=0.00001)
                and np.array_equal(problem.parent_nodes, parent))
コード例 #20
0
ファイル: test_algorithms.py プロジェクト: vermachint/mlrose
    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)
コード例 #21
0
ファイル: test_algorithms.py プロジェクト: vermachint/mlrose
    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
コード例 #22
0
ファイル: test_algorithms.py プロジェクト: vermachint/mlrose
    def test_simulated_annealing_discrete_min():
        """Test simulated_annealing function for a discrete minimization
        problem"""

        problem = DiscreteOpt(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)
コード例 #23
0
ファイル: test_algorithms.py プロジェクト: tapos/mlrose
    def test_random_hill_climb_discrete_max():
        """Test random_hill_climb function for a discrete maximization
        problem"""

        problem = DiscreteOpt(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)
コード例 #24
0
ファイル: test_algorithms.py プロジェクト: tapos/mlrose
    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
コード例 #25
0
ファイル: test_algorithms.py プロジェクト: vermachint/mlrose
    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
コード例 #26
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)
コード例 #27
0
ファイル: test_algorithms.py プロジェクト: tapos/mlrose
    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
コード例 #28
0
ファイル: test_algorithms.py プロジェクト: vermachint/mlrose
    def test_random_hill_climb_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)
        x = np.array([0, 0, 0, 0, 0])

        max_attempts = 10

        best_state, best_fitness, all_fitness = random_hill_climb(
            problem,
            max_attempts=max_attempts,
            restarts=0,
            init_state=x,
            curve=True)
        assert len(all_fitness) != max_attempts
コード例 #29
0
ファイル: test_opt_probs.py プロジェクト: xadahiya/mlrose
    def test_find_top_pct_min():
        """Test find_top_pct method for a minimization problem"""

        problem = DiscreteOpt(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)
        problem.find_top_pct(keep_pct=0.25)

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

        assert np.array_equal(problem.get_keep_sample(), x)
コード例 #30
0
ファイル: test_opt_probs.py プロジェクト: xadahiya/mlrose
    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)