1. Add origin_node and origin_conn.
2. Change the behavior of crossover and mutation. Now, TensorNEAT will use all fix_attrs(include historical marker if it has one) as identifier for gene in crossover and distance calculation.
3. Other slightly change.
4. Add two related examples: xor_origin and hopper_origin
5. Add related test file.
This commit is contained in:
wls2002
2024-12-18 16:20:34 +08:00
parent e9a8110af5
commit ee1a2a8271
18 changed files with 667 additions and 204 deletions

View File

@@ -0,0 +1,49 @@
from tensorneat.pipeline import Pipeline
from tensorneat.algorithm.neat import NEAT
from tensorneat.genome import DefaultGenome, OriginNode, OriginConn
from tensorneat.problem.rl import BraxEnv
from tensorneat.common import ACT, AGG
"""
Solving Hopper with OriginGene
See https://github.com/EMI-Group/tensorneat/issues/11
"""
if __name__ == "__main__":
pipeline = Pipeline(
algorithm=NEAT(
pop_size=1000,
species_size=20,
survival_threshold=0.1,
compatibility_threshold=1.0,
genome=DefaultGenome(
num_inputs=11,
num_outputs=3,
init_hidden_layers=(),
# origin node gene, which use the same crossover behavior to the origin NEAT paper
node_gene=OriginNode(
activation_options=ACT.tanh,
aggregation_options=AGG.sum,
response_lower_bound = 1,
response_upper_bound= 1, # fix response to 1
),
# use origin connection, which using historical marker
conn_gene=OriginConn(),
output_transform=ACT.tanh,
),
),
problem=BraxEnv(
env_name="hopper",
max_step=1000,
),
seed=42,
generation_limit=100,
fitness_target=5000,
)
# initialize state
state = pipeline.setup()
# print(state)
# run until terminate
state, best = pipeline.auto_run(state)

View File

@@ -30,7 +30,8 @@ pipeline.show(state, best)
# visualize the best individual
network = algorithm.genome.network_dict(state, *best)
algorithm.genome.visualize(network, save_path="./imgs/xor_network.svg")
print(algorithm.genome.repr(state, *best))
# algorithm.genome.visualize(network, save_path="./imgs/xor_network.svg")
# transform the best individual to latex formula
from tensorneat.common.sympy_tools import to_latex_code, to_python_code

View File

@@ -0,0 +1,55 @@
from tensorneat.pipeline import Pipeline
from tensorneat import algorithm, genome, problem
from tensorneat.genome import OriginNode, OriginConn
from tensorneat.common import ACT
"""
Solving XOR-3d problem with OriginGene
See https://github.com/EMI-Group/tensorneat/issues/11
"""
algorithm = algorithm.NEAT(
pop_size=10000,
species_size=20,
survival_threshold=0.01,
genome=genome.DefaultGenome(
node_gene=OriginNode(),
conn_gene=OriginConn(),
num_inputs=3,
num_outputs=1,
max_nodes=7,
output_transform=ACT.sigmoid,
),
)
problem = problem.XOR3d()
pipeline = Pipeline(
algorithm,
problem,
generation_limit=200,
fitness_target=-1e-6,
seed=42,
)
state = pipeline.setup()
# run until terminate
state, best = pipeline.auto_run(state)
# show result
pipeline.show(state, best)
# visualize the best individual
network = algorithm.genome.network_dict(state, *best)
print(algorithm.genome.repr(state, *best))
# algorithm.genome.visualize(network, save_path="./imgs/xor_network.svg")
# transform the best individual to latex formula
from tensorneat.common.sympy_tools import to_latex_code, to_python_code
sympy_res = algorithm.genome.sympy_func(
state, network, sympy_output_transform=ACT.obtain_sympy(ACT.sigmoid)
)
latex_code = to_latex_code(*sympy_res)
print(latex_code)
# transform the best individual to python code
python_code = to_python_code(*sympy_res)
print(python_code)