Esempio n. 1
0
    def test_act_all_incorrect(self):
        mux = make_discrete_mux_env(num_address_bits=1, shuffle_dataset=False)
        incorrect_actions_iter = iter([1, 1, 0, 0, 1, 0, 1, 0])

        while not mux.is_terminal():
            response = mux.act(next(incorrect_actions_iter))
            assert not response.was_correct_action
Esempio n. 2
0
 def _setup_short_epoch(self):
     num_address_bits = 1
     total_bits = calc_total_bits(num_address_bits)
     num_data_points = 2**total_bits
     mux = make_discrete_mux_env(num_address_bits=num_address_bits,
                                 shuffle_dataset=False)
     return mux, num_data_points
Esempio n. 3
0
 def test_obs_space_integrity(self):
     num_address_bits = 1
     mux = make_discrete_mux_env(num_address_bits=1)
     total_feature_dims = calc_total_bits(num_address_bits)
     assert len(mux.obs_space) == total_feature_dims
     for dim in mux.obs_space:
         assert dim.lower == 0
         assert dim.upper == 1
Esempio n. 4
0
 def test_observe_order_no_shuffle(self):
     mux = make_discrete_mux_env(num_address_bits=1, shuffle_dataset=False)
     expected_obs_seq_iter = \
         iter([[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1],
               [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]])
     while not mux.is_terminal():
         obs = mux.observe()
         assert np.array_equal(obs, next(expected_obs_seq_iter))
         mux.act(self._DUMMY_ACTION)
Esempio n. 5
0
    def test_reset_with_two_epochs_no_shuffle(self):
        mux = make_discrete_mux_env(num_address_bits=1, shuffle_dataset=False)

        first_epoch_obs_seq = []
        first_epoch_reward_seq = []
        while not mux.is_terminal():
            first_epoch_obs_seq.append(mux.observe())
            response = mux.act(self._DUMMY_ACTION)
            first_epoch_reward_seq.append(response.reward)

        mux.reset()
        second_epoch_obs_seq = []
        second_epoch_reward_seq = []
        while not mux.is_terminal():
            second_epoch_obs_seq.append(mux.observe())
            response = mux.act(self._DUMMY_ACTION)
            second_epoch_reward_seq.append(response.reward)

        assert np.array_equal(first_epoch_obs_seq, second_epoch_obs_seq)
        assert np.array_equal(first_epoch_reward_seq, second_epoch_reward_seq)
    def test_sample_training_run(self):
        # 6-mux
        env = make_discrete_mux_env(num_address_bits=2,
                                    shuffle_dataset=True,
                                    reward_correct=1000,
                                    reward_incorrect=0)

        rule_repr = DiscreteRuleRepr()

        alg_hyperparams = {
            "N": 400,
            "beta": 0.2,
            "alpha": 0.1,
            "epsilon_nought": 0.01,
            "nu": 5,
            "gamma": 0.71,
            "theta_ga": 12,
            "chi": 0.8,
            "mu": 0.04,
            "theta_del": 20,
            "delta": 0.1,
            "theta_sub": 20,
            "p_wildcard": 0.33,
            "prediction_I": 1e-3,
            "epsilon_I": 1e-3,
            "fitness_I": 1e-3,
            "p_explore": 0.5,
            "theta_mna": len(env.action_set),
            "do_ga_subsumption": True,
            "do_as_subsumption": True,
            "seed": 0
        }

        alg = make_canonical_xcs(env, rule_repr, alg_hyperparams)

        monitor_items = [
            MonitorItem("num_micros",
                        lambda experiment: experiment.population.num_micros),
            MonitorItem("num_macros",
                        lambda experiment: experiment.population.num_macros),
            MonitorItem(
                "performance", lambda experiment: experiment.calc_performance(
                    strat="accuracy")),
            MonitorItem(
                "mean_error", lambda experiment: calc_summary_stat(
                    experiment.population, "mean", "error")),
            MonitorItem(
                "max_fitness", lambda experiment: calc_summary_stat(
                    experiment.population, "max", "fitness")),
            MonitorItem(
                "deletion", lambda experiment: experiment.population.
                operations_record["deletion"]),
            MonitorItem(
                "covering", lambda experiment: experiment.population.
                operations_record["covering"]),
            MonitorItem(
                "as_subsumption", lambda experiment: experiment.population.
                operations_record["as_subsumption"]),
            MonitorItem(
                "ga_subsumption", lambda experiment: experiment.population.
                operations_record["ga_subsumption"]),
            MonitorItem(
                "discovery", lambda experiment: experiment.population.
                operations_record["discovery"]),
            MonitorItem(
                "absorption", lambda experiment: experiment.population.
                operations_record["absorption"])
        ]

        experiment = Experiment(env,
                                alg,
                                num_training_epochs=10,
                                monitor_items=monitor_items)
        experiment.run()
        print(experiment._monitor.query())
Esempio n. 7
0
 def test_action_set_integrity(self):
     mux = make_discrete_mux_env()
     assert mux.action_set == {0, 1}
Esempio n. 8
0
 def test_labels_dtype(self):
     mux = make_discrete_mux_env()
     for elem in mux.labels.flatten():
         assert isinstance(elem, np.integer)
Esempio n. 9
0
 def test_labels_values(self):
     mux = make_discrete_mux_env(num_address_bits=1)
     expected_labels = [0, 0, 1, 1, 0, 1, 0, 1]
     assert np.array_equal(mux.labels, expected_labels)
Esempio n. 10
0
 def test_data_values(self):
     mux = make_discrete_mux_env(num_address_bits=1)
     expected_data = [[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0],
                      [1, 0, 1], [1, 1, 0], [1, 1, 1]]
     assert np.array_equal(mux.data, expected_data)
Esempio n. 11
0
 def test_bad_num_address_bits_negative(self):
     with pytest.raises(InvalidSpecError):
         make_discrete_mux_env(num_address_bits=-1)
Esempio n. 12
0
 def test_step_type(self):
     mux = make_discrete_mux_env()
     assert mux.step_type == EnvironmentStepTypes.single_step