def test_aggressive_mutation(self, _cond, _effect, cfg): # given condition = Condition(_cond, cfg) effect = Effect(_effect, cfg) cfg.encoder = RealValueEncoder(16) # more precise encoder cfg.mutation_noise = 0.5 # strong noise mutation range mu = 1.0 # mutate every attribute cl = Classifier(condition=deepcopy(condition), effect=deepcopy(effect), cfg=cfg) # when mutate(cl, mu) # then range_min, range_max = cfg.encoder.range for idx, (c, e) in enumerate(zip(cl.condition, cl.effect)): # assert that we have new locus if condition[idx] != cfg.classifier_wildcard: assert condition[idx] != c if effect[idx] != cfg.classifier_wildcard: assert effect[idx] != e # assert if condition values are in ranges assert c.lower_bound >= range_min assert c.upper_bound <= range_max # assert if effect values are in ranges assert e.lower_bound >= range_min assert e.upper_bound <= range_max
def test_encode_should_return_integer(self, _p): # given encoder = RealValueEncoder(2) # when encoded = encoder.encode(_p) # then assert type(encoded) is int
def test_should_decode_values(self): # given bits = 4 encoder = RealValueEncoder(bits) # then assert 0.0 == encoder.decode(0) assert abs(0.5 - encoder.decode(8)) < 0.05 assert 1.0 == encoder.decode(15)
def test_should_encode_values(self): # given bits = 4 # 2^bits discrete states encoder = RealValueEncoder(bits) # then assert 0 == encoder.encode(0.0) assert 7 == encoder.encode(0.5) assert 15 == encoder.encode(1.0)
def test_should_return_min_max_range(self, _bits, _min_range, _max_range): # given encoder = RealValueEncoder(_bits) # when min_val, max_val = encoder.range # then assert min_val == _min_range assert max_val == _max_range
def test_should_encode_and_decode_approximately(self): # given encoder = RealValueEncoder(8) epsilon = 0.01 observation = random.random() # when encoded = encoder.encode(observation) decoded = encoder.decode(encoded) # then assert abs(observation - decoded) < epsilon
def test_should_encode_with_noise_added(self): # given encoder = RealValueEncoder(16) noise_max = 0.1 for _ in range(100): # when val = random.random() encoded = encoder.encode(val) encoded_with_noise = encoder.encode(val, noise_max) # then assert encoded_with_noise <= encoder.range[1] assert encoded_with_noise >= encoded
def test_should_deny_illegal_values_when_decoding(self): # given encoder = RealValueEncoder(2) # when with pytest.raises(ValueError) as e1: encoder.decode(-1) with pytest.raises(ValueError) as e2: encoder.decode(5) # then assert e1 is not None assert e2 is not None
def test_should_deny_encoding_illegal_values(self): # given encoder = RealValueEncoder(2) # when with pytest.raises(ValueError) as e1: encoder.encode(-0.2) with pytest.raises(ValueError) as e2: encoder.encode(1.1) # then assert e1 is not None assert e2 is not None
def test_should_find_similar(self): # given cfg = Configuration(3, 2, encoder=RealValueEncoder(2)) cl1 = Classifier( condition=Condition([UBR(0, 0), UBR(0, 3), UBR(0, 3)], cfg=cfg), action=0, effect=Effect([UBR(0, 3), UBR(0, 3), UBR(0, 3)], cfg=cfg), cfg=cfg ) cl2 = Classifier( condition=Condition([UBR(0, 0), UBR(0, 3), UBR(0, 3)], cfg=cfg), action=0, effect=Effect([UBR(0, 3), UBR(0, 3), UBR(0, 3)], cfg=cfg), cfg=cfg ) # then assert cl1 == cl2
def __init__(self, classifier_length: int, number_of_possible_actions: int, encoder_bits: int, beta=0.05, theta_i=0.1, theta_r=0.9, u_max=100000, theta_exp=20,) -> None: self.oktypes = (UBR,) self.encoder = RealValueEncoder(encoder_bits) self.classifier_length = classifier_length self.number_of_possible_actions = number_of_possible_actions self.classifier_wildcard = UBR(*self.encoder.range) self.beta = beta self.theta_i = theta_i self.theta_r = theta_r self.u_max = u_max self.theta_exp = theta_exp
def cfg(self): return Configuration(classifier_length=2, number_of_possible_actions=2, encoder=RealValueEncoder(4))
metrics = { 'regions': count_averaged_regions(population) } # Add basic population metrics metrics.update(population_metrics(population, environment)) return metrics if __name__ == '__main__': # Load desired environment chckb = gym.make('checkerboard-2D-3div-v0') # Create agent encoder = RealValueEncoder(resolution_bits=4) cfg = Configuration(chckb.observation_space.shape[0], chckb.action_space.n, encoder=encoder, user_metrics_collector_fcn=_checkerboard_metrics, epsilon=0.5, do_ga=True, theta_r=0.9, theta_i=0.2, theta_ga=100, chi=0.5, mu=0.15) agent = RACS(cfg) population, metrics = agent.explore_exploit(chckb, 100)
def test_should_encode(self, _bits, _val, _encoded): assert RealValueEncoder(_bits).encode(_val) == _encoded
def test_should_clip_values_outside_range(self, _bits, _val, _encoded): assert RealValueEncoder(_bits).encode(_val) == _encoded