update HyperNEAT;
All example can currently run!
This commit is contained in:
@@ -1,53 +1,74 @@
|
||||
import jax.numpy as jnp
|
||||
import jax
|
||||
|
||||
from config import *
|
||||
from pipeline import Pipeline
|
||||
from algorithm import NEAT
|
||||
from algorithm.neat.gene import NormalGene, NormalGeneConfig
|
||||
from algorithm.hyperneat import HyperNEAT, NormalSubstrateConfig, NormalSubstrate
|
||||
from problem.rl_env import GymNaxConfig, GymNaxEnv
|
||||
|
||||
|
||||
def example_conf():
|
||||
return Config(
|
||||
basic=BasicConfig(seed=42, fitness_target=500, pop_size=10000),
|
||||
neat=NeatConfig(
|
||||
inputs=4,
|
||||
outputs=1,
|
||||
),
|
||||
gene=NormalGeneConfig(
|
||||
activation_default=Act.tanh,
|
||||
activation_options=(Act.tanh,),
|
||||
),
|
||||
hyperneat=HyperNeatConfig(activation=Act.sigmoid, inputs=4, outputs=2),
|
||||
substrate=NormalSubstrateConfig(
|
||||
input_coors=((-1, -1), (-0.5, -1), (0, -1), (0.5, -1), (1, -1)),
|
||||
hidden_coors=(
|
||||
# (-1, -0.5), (-0.5, -0.5), (0, -0.5), (0.5, -0.5),
|
||||
(1, 0),
|
||||
(-1, 0),
|
||||
(-0.5, 0),
|
||||
(0, 0),
|
||||
(0.5, 0),
|
||||
(1, 0),
|
||||
# (1, 0.5), (-1, 0.5), (-0.5, 0.5), (0, 0.5), (0.5, 0.5), (1, 0.5),
|
||||
),
|
||||
output_coors=((-1, 1), (1, 1)),
|
||||
),
|
||||
problem=GymNaxConfig(
|
||||
env_name="CartPole-v1",
|
||||
output_transform=lambda out: jnp.argmax(
|
||||
out
|
||||
), # the action of cartpole is {0, 1}
|
||||
),
|
||||
)
|
||||
from algorithm.neat import *
|
||||
from algorithm.hyperneat import *
|
||||
from utils import Act
|
||||
|
||||
from problem.rl_env import GymNaxEnv
|
||||
|
||||
if __name__ == "__main__":
|
||||
conf = example_conf()
|
||||
pipeline = Pipeline(
|
||||
algorithm=HyperNEAT(
|
||||
substrate=FullSubstrate(
|
||||
input_coors=[
|
||||
(-1, -1),
|
||||
(-0.5, -1),
|
||||
(0, -1),
|
||||
(0.5, -1),
|
||||
(1, -1),
|
||||
], # 4(problem inputs) + 1(bias)
|
||||
hidden_coors=[
|
||||
(-1, -0.5),
|
||||
(0.333, -0.5),
|
||||
(-0.333, -0.5),
|
||||
(1, -0.5),
|
||||
(-1, 0),
|
||||
(0.333, 0),
|
||||
(-0.333, 0),
|
||||
(1, 0),
|
||||
(-1, 0.5),
|
||||
(0.333, 0.5),
|
||||
(-0.333, 0.5),
|
||||
(1, 0.5),
|
||||
],
|
||||
output_coors=[
|
||||
(-1, 1),
|
||||
(1, 1), # one output
|
||||
],
|
||||
),
|
||||
neat=NEAT(
|
||||
species=DefaultSpecies(
|
||||
genome=DefaultGenome(
|
||||
num_inputs=4, # [*coor1, *coor2]
|
||||
num_outputs=1, # the weight of connection between two coor1 and coor2
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
node_gene=DefaultNodeGene(
|
||||
activation_default=Act.tanh,
|
||||
activation_options=(Act.tanh,),
|
||||
),
|
||||
output_transform=Act.tanh, # the activation function for output node in NEAT
|
||||
),
|
||||
pop_size=10000,
|
||||
species_size=10,
|
||||
compatibility_threshold=3.5,
|
||||
survival_threshold=0.03,
|
||||
),
|
||||
),
|
||||
activation=Act.tanh, # the activation function for output node in HyperNEAT
|
||||
activate_time=10,
|
||||
output_transform=jax.numpy.argmax, # action of cartpole is in {0, 1}
|
||||
),
|
||||
problem=GymNaxEnv(
|
||||
env_name="CartPole-v1",
|
||||
),
|
||||
generation_limit=300,
|
||||
fitness_target=500,
|
||||
)
|
||||
|
||||
algorithm = HyperNEAT(conf, NormalGene, NormalSubstrate)
|
||||
pipeline = Pipeline(conf, algorithm, GymNaxEnv)
|
||||
# initialize state
|
||||
state = pipeline.setup()
|
||||
pipeline.pre_compile(state)
|
||||
# print(state)
|
||||
# run until terminate
|
||||
state, best = pipeline.auto_run(state)
|
||||
|
||||
Reference in New Issue
Block a user