def mutate(self, config): wmr = config.weight_mutate_rate wms = config.weight_mutate_stdev wrr = config.weight_replace_rate emr = config.enabled_mutate_rate wim = config.weight_init_mean wis = config.weight_init_stdev wit = config.weight_init_type wmin = config.weight_min_value wmax = config.weight_max_value if config.single_structural_mutation: success = False mutation_probs = np.array([wmr, wrr, emr]) while True: mutation_probs = mutation_probs / np.sum(mutation_probs) mut_idx = np.random.choice(range(3), 1, p=mutation_probs)[0] if mut_idx == 0: self.weight = clamp(self.weight + random_normal(0.0, wms), wmin, wmax) success = True elif mut_idx == 1: if wit == 'normal': self.weight = clamp(random_normal(wim, wis), wmin, wmax) elif wit == 'uniform': self.weight = random_uniform(wim, wis, wmin, wmax) else: raise InvalidConfigurationError() success = True else: self.enabled = True if self.enabled is False else False success = True if success is True: break mutation_probs[mut_idx] = 0.0 if np.sum(mutation_probs) == 0.0: break else: if random.random() < wmr: self.weight = clamp(self.weight + random_normal(0.0, wms), wmin, wmax) if random.random() < wrr: if wit == 'normal': self.weight = clamp(random_normal(wim, wis), wmin, wmax) elif wit == 'uniform': self.weight = random_uniform(wim, wis, wmin, wmax) else: raise InvalidConfigurationError() if random.random() < emr: self.enabled = True if self.enabled is False else False
def initialize(self, _id, source_id, target_id, weight, enabled, config=None): self.id = _id self.source_id = source_id self.target_id = target_id self.weight = weight self.enabled = enabled if config is not None: wit = config.weight_init_type wim = config.weight_init_mean wis = config.weight_init_stdev wmin = config.weight_min_value wmax = config.weight_max_value if wit == 'normal': self.weight = clamp(random_normal(wim, wis), wmin, wmax) elif wit == 'uniform': self.weight = random_uniform(wim, wis, wmin, wmax) else: raise InvalidConfigurationError() self.enabled = config.enabled_default
def initialize(self, _id, _type, bias, res, act, agg, config=None): self.id = _id self.type = _type self.bias = bias self.res = res self.act = act self.agg = agg if config is not None: bim = config.bias_init_mean bis = config.bias_init_stdev bit = config.bias_init_type bmin = config.bias_min_value bmax = config.bias_max_value rim = config.response_init_mean ris = config.response_init_stdev rit = config.response_init_type rmin = config.response_min_value rmax = config.response_max_value if self.type == 'input': self.bias = 0.0 self.res = 1.0 self.agg = 'sum' self.act = 'identity' else: if bit == 'normal': self.bias = clamp(random_normal(bim, bis), bmin, bmax) elif bit == 'uniform': self.bias = random_uniform(bim, bis, bmin, bmax) else: raise InvalidConfigurationError() if rit == 'normal': self.res = clamp(random_normal(rim, ris), rmin, rmax) elif rit == 'uniform': self.res = random_uniform(rim, ris, rmin, rmax) else: raise InvalidConfigurationError() self.agg = config.aggregation_default self.act = config.activation_default_output if self.type == 'output' else config.activation_default
def parse(self, section, config_parser): if self.type == int: return config_parser.getint(section, self.name) if self.type == bool: return config_parser.getboolean(section, self.name) if self.type == float: return config_parser.getfloat(section, self.name) if self.type == list: v = config_parser.get(section, self.name) return v.split(" ") if self.type == str: return config_parser.get(section, self.name) raise InvalidConfigurationError( "UNEXPECTED CONFIGURATION TYPE [{}]".format(repr(self.type)))
def from_json(self, config_json): result = json.loads(config_json) for k, v in result.items(): if k == 'neat': setattr(self, k, DefaultNEATConfig().from_json(v)) elif k == 'node': setattr(self, k, DefaultNodeConfig().from_json(v)) elif k == 'connection': setattr(self, k, DefaultConnectionConfig().from_json(v)) elif k == 'genome': setattr(self, k, DefaultGenomeConfig().from_json(v)) elif k == 'species': setattr(self, k, DefaultSpeciesConfig().from_json(v)) elif k == 'reproduction': setattr(self, k, DefaultReproductionConfig().from_json(v)) else: raise InvalidConfigurationError() return self
def initialize(self, node_config, connection_config): for _ in range(self.config.num_inputs): n_id = self._next_node_id() self._create_node(n_id, 'input', config=node_config) self.node_ids['input'].add(n_id) self.node_ids['all'].add(n_id) for _ in range(self.config.num_hidden): n_id = self._next_node_id() self._create_node(n_id, 'hidden', config=node_config) self.node_ids['hidden'].add(n_id) self.node_ids['all'].add(n_id) for _ in range(self.config.num_outputs): n_id = self._next_node_id() self._create_node(n_id, 'output', config=node_config) self.node_ids['output'].add(n_id) self.node_ids['all'].add(n_id) self._compute_probable_connectors() if self.config.initial_connection == 'fs_neat_no_hidden': source_id = random.choice(list(self.node_ids['input'])) for target_id in self.node_ids['output']: self._create_connection(source_id, target_id, config=connection_config) elif self.config.initial_connection == 'fs_neat_hidden': source_id = random.choice(list(self.node_ids['input'])) for target_id in set().union(self.node_ids['hidden'], self.node_ids['output']): self._create_connection(source_id, target_id, config=connection_config) elif self.config.initial_connection == 'full_no_direct': for source_id, target_id in self._compute_full_connectors(False): self._create_connection(source_id, target_id, config=connection_config) elif self.config.initial_connection == 'full_direct': for source_id, target_id in self._compute_full_connectors(True): self._create_connection(source_id, target_id, config=connection_config) elif self.config.initial_connection == 'partial_no_direct': connectors = self._compute_full_connectors(False) random.shuffle(connectors) connections_to_add = int( round(len(connectors) * self.config.partial_connection_rate)) for source_id, target_id in connectors[:connections_to_add]: self._create_connection(source_id, target_id, config=connection_config) elif self.config.initial_connection == 'partial_direct': connectors = self._compute_full_connectors(True) random.shuffle(connectors) connections_to_add = int( round(len(connectors) * self.config.partial_connection_rate)) for source_id, target_id in connectors[:connections_to_add]: self._create_connection(source_id, target_id, config=connection_config) elif self.config.initial_connection == 'unconnected': pass else: raise InvalidConfigurationError( 'Unexpected configuration value [{}]'.format( self.config.initial_connection))
def mutate(self, config): assert (self.type != 'input') bmr = config.bias_mutate_rate bms = config.bias_mutate_stdev brr = config.bias_replace_rate bim = config.bias_init_mean bis = config.bias_init_stdev bit = config.bias_init_type bmin = config.bias_min_value bmax = config.bias_max_value rmr = config.response_mutate_rate rms = config.response_mutate_stdev rrr = config.response_replace_rate rim = config.response_init_mean ris = config.response_init_stdev rit = config.response_init_type rmin = config.response_min_value rmax = config.response_max_value act_mr = config.activation_mutate_rate act_opt = config.activation_options agg_mr = config.aggregation_mutate_rate agg_opt = config.aggregation_options if config.single_structural_mutation: success = False mutation_probs = np.array([bmr, brr, rmr, rrr, act_mr, agg_mr]) while True: mutation_probs = mutation_probs / np.sum(mutation_probs) mut_idx = np.random.choice(range(6), 1, p=mutation_probs)[0] if mut_idx == 0: self.bias = clamp(self.bias + random_normal(0.0, bms), bmin, bmax) success = True elif mut_idx == 1: if bit == 'normal': self.bias = clamp(random_normal(bim, bis), bmin, bmax) elif bit == 'uniform': self.bias = random_uniform(bim, bis, bmin, bmax) else: raise InvalidConfigurationError() success = True elif mut_idx == 2: self.res = clamp(self.res + random_normal(0.0, rms), rmin, rmax) success = True elif mut_idx == 3: if rit == 'normal': self.res = clamp(random_normal(rim, ris), rmin, rmax) elif rit == 'uniform': self.res = random_uniform(rim, ris, rmin, rmax) else: raise InvalidConfigurationError() success = True elif mut_idx == 4: nb_activations = len(act_opt) if self.type != 'input' and self.type != 'output' and nb_activations > 1: choices = list(range(nb_activations)) choices.remove(act_opt.index(self.act)) choice_idx = random.choice(choices) self.act = act_opt[choice_idx] success = True else: nb_aggregations = len(agg_opt) if nb_aggregations > 1: choices = list(range(nb_aggregations)) choices.remove(agg_opt.index(self.agg)) choice_idx = random.choice(choices) self.agg = agg_opt[choice_idx] success = True if success is True: break mutation_probs[mut_idx] = 0.0 if np.sum(mutation_probs) == 0.0: break else: if random.random() < bmr: self.bias = clamp(self.bias + random_normal(0.0, bms), bmin, bmax) if random.random() < brr: if bit == 'normal': self.bias = clamp(random_normal(bim, bis), bmin, bmax) elif bit == 'uniform': self.bias = random_uniform(bim, bis, bmin, bmax) else: raise InvalidConfigurationError() if random.random() < rmr: self.res = clamp(self.res + random_normal(0.0, rms), rmin, rmax) if random.random() < rrr: if rit == 'normal': self.res = clamp(random_normal(rim, ris), rmin, rmax) elif rit == 'uniform': self.res = random_uniform(rim, ris, rmin, rmax) else: raise InvalidConfigurationError() if random.random() < act_mr: nb_activations = len(act_opt) if self.type != 'input' and self.type != 'output' and nb_activations > 1: choices = list(range(nb_activations)) choices.remove(act_opt.index(self.act)) choice_idx = random.choice(choices) self.act = act_opt[choice_idx] if random.random() < agg_mr: nb_aggregations = len(agg_opt) if nb_aggregations > 1: choices = list(range(nb_aggregations)) choices.remove(agg_opt.index(self.agg)) choice_idx = random.choice(choices) self.agg = agg_opt[choice_idx]