change a lot a lot a lot!!!!!!!

This commit is contained in:
wls2002
2023-07-24 02:16:02 +08:00
parent 48f90c7eef
commit ac295c1921
49 changed files with 1138 additions and 1460 deletions

View File

@@ -1 +1,2 @@
from .config import Configer
from .config import *

View File

@@ -1,70 +1,103 @@
import os
import warnings
import configparser
import numpy as np
from dataclasses import dataclass
from typing import Union
class Configer:
@dataclass(frozen=True)
class BasicConfig:
seed: int = 42
fitness_target: float = 1
generation_limit: int = 1000
num_inputs: int = 2
num_outputs: int = 1
pop_size: int = 100
@classmethod
def __load_default_config(cls):
par_dir = os.path.dirname(os.path.abspath(__file__))
default_config_path = os.path.join(par_dir, "default_config.ini")
return cls.__load_config(default_config_path)
def __post_init__(self):
assert self.num_inputs > 0, "the inputs number of the problem must be greater than 0"
assert self.num_outputs > 0, "the outputs number of the problem must be greater than 0"
assert self.pop_size > 0, "the population size must be greater than 0"
@classmethod
def __load_config(cls, config_path):
c = configparser.ConfigParser()
c.read(config_path)
config = {}
for section in c.sections():
for key, value in c.items(section):
config[key] = eval(value)
@dataclass(frozen=True)
class NeatConfig:
network_type: str = "feedforward"
activate_times: Union[int, None] = None # None means the network is feedforward
maximum_nodes: int = 100
maximum_conns: int = 50
maximum_species: int = 10
return config
# genome config
compatibility_disjoint: float = 1
compatibility_weight: float = 0.5
conn_add: float = 0.4
conn_delete: float = 0.4
node_add: float = 0.2
node_delete: float = 0.2
@classmethod
def __check_redundant_config(cls, default_config, config):
for key in config:
if key not in default_config:
warnings.warn(f"Redundant config: {key} in config!")
# species config
compatibility_threshold: float = 3.0
species_elitism: int = 2
max_stagnation: int = 15
genome_elitism: int = 2
survival_threshold: float = 0.2
min_species_size: int = 1
spawn_number_change_rate: float = 0.5
@classmethod
def __complete_config(cls, default_config, config):
for key in default_config:
if key not in config:
config[key] = default_config[key]
@classmethod
def load_config(cls, config_path=None):
default_config = cls.__load_default_config()
if config_path is None:
config = {}
elif not os.path.exists(config_path):
warnings.warn(f"config file {config_path} not exist!")
config = {}
def __post_init__(self):
assert self.network_type in ["feedforward", "recurrent"], "the network type must be feedforward or recurrent"
if self.network_type == "feedforward":
assert self.activate_times is None, "the activate times of feedforward network must be None"
else:
config = cls.__load_config(config_path)
assert isinstance(self.activate_times, int), "the activate times of recurrent network must be int"
assert self.activate_times > 0, "the activate times of recurrent network must be greater than 0"
cls.__check_redundant_config(default_config, config)
cls.__complete_config(default_config, config)
assert self.maximum_nodes > 0, "the maximum nodes must be greater than 0"
assert self.maximum_conns > 0, "the maximum connections must be greater than 0"
assert self.maximum_species > 0, "the maximum species must be greater than 0"
cls.refactor_activation(config)
cls.refactor_aggregation(config)
assert self.compatibility_disjoint > 0, "the compatibility disjoint must be greater than 0"
assert self.compatibility_weight > 0, "the compatibility weight must be greater than 0"
assert self.conn_add > 0, "the connection add probability must be greater than 0"
assert self.conn_delete > 0, "the connection delete probability must be greater than 0"
assert self.node_add > 0, "the node add probability must be greater than 0"
assert self.node_delete > 0, "the node delete probability must be greater than 0"
config['input_idx'] = np.arange(config['num_inputs'])
config['output_idx'] = np.arange(config['num_inputs'], config['num_inputs'] + config['num_outputs'])
assert self.compatibility_threshold > 0, "the compatibility threshold must be greater than 0"
assert self.species_elitism > 0, "the species elitism must be greater than 0"
assert self.max_stagnation > 0, "the max stagnation must be greater than 0"
assert self.genome_elitism > 0, "the genome elitism must be greater than 0"
assert self.survival_threshold > 0, "the survival threshold must be greater than 0"
assert self.min_species_size > 0, "the min species size must be greater than 0"
assert self.spawn_number_change_rate > 0, "the spawn number change rate must be greater than 0"
return config
@classmethod
def refactor_activation(cls, config):
config['activation_default'] = 0
config['activation_options'] = np.arange(len(config['activation_option_names']))
@dataclass(frozen=True)
class HyperNeatConfig:
below_threshold: float = 0.2
max_weight: float = 3
activation: str = "sigmoid"
aggregation: str = "sum"
activate_times: int = 5
@classmethod
def refactor_aggregation(cls, config):
config['aggregation_default'] = 0
config['aggregation_options'] = np.arange(len(config['aggregation_option_names']))
def __post_init__(self):
assert self.below_threshold > 0, "the below threshold must be greater than 0"
assert self.max_weight > 0, "the max weight must be greater than 0"
assert self.activate_times > 0, "the activate times must be greater than 0"
@dataclass(frozen=True)
class GeneConfig:
pass
@dataclass(frozen=True)
class SubstrateConfig:
pass
@dataclass(frozen=True)
class Config:
basic: BasicConfig = BasicConfig()
neat: NeatConfig = NeatConfig()
hyper_neat: HyperNeatConfig = HyperNeatConfig()
gene: GeneConfig = GeneConfig()
substrate: SubstrateConfig = SubstrateConfig()

View File

@@ -1,8 +1,6 @@
[basic]
random_seed = 0
generation_limit = 1000
[problem]
fitness_threshold = 3.9999
num_inputs = 2
num_outputs = 1
@@ -14,6 +12,13 @@ maximum_nodes = 50
maximum_conns = 50
maximum_species = 10
compatibility_disjoint = 1.0
compatibility_weight = 0.5
conn_add_prob = 0.4
conn_delete_prob = 0
node_add_prob = 0.2
node_delete_prob = 0
[hyperneat]
below_threshold = 0.2
max_weight = 3
@@ -26,17 +31,6 @@ input_coors = [[-1, 1], [0, 1], [1, 1]]
hidden_coors = [[-1, 0], [0, 0], [1, 0]]
output_coors = [[0, -1]]
[population]
pop_size = 10
[genome]
compatibility_disjoint = 1.0
compatibility_weight = 0.5
conn_add_prob = 0.4
conn_delete_prob = 0
node_add_prob = 0.2
node_delete_prob = 0
[species]
compatibility_threshold = 3.0
species_elitism = 2