finish all refactoring
This commit is contained in:
@@ -1,38 +1,36 @@
|
||||
import jax.numpy as jnp
|
||||
|
||||
from config import *
|
||||
from pipeline import Pipeline
|
||||
from algorithm import NEAT
|
||||
from algorithm.neat.gene import NormalGene, NormalGeneConfig
|
||||
from problem.rl_env import BraxEnv, BraxConfig
|
||||
|
||||
|
||||
def example_conf():
|
||||
return Config(
|
||||
basic=BasicConfig(
|
||||
seed=42,
|
||||
fitness_target=10000,
|
||||
pop_size=100
|
||||
),
|
||||
neat=NeatConfig(
|
||||
inputs=27,
|
||||
outputs=8,
|
||||
),
|
||||
gene=NormalGeneConfig(
|
||||
activation_default=Act.tanh,
|
||||
activation_options=(Act.tanh,),
|
||||
),
|
||||
problem=BraxConfig(
|
||||
env_name="ant"
|
||||
)
|
||||
)
|
||||
from algorithm.neat import *
|
||||
|
||||
from problem.rl_env import BraxEnv
|
||||
from utils import Act
|
||||
|
||||
if __name__ == '__main__':
|
||||
conf = example_conf()
|
||||
pipeline = Pipeline(
|
||||
algorithm=NEAT(
|
||||
species=DefaultSpecies(
|
||||
genome=DefaultGenome(
|
||||
num_inputs=27,
|
||||
num_outputs=8,
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
node_gene=DefaultNodeGene(
|
||||
activation_options=(Act.tanh,),
|
||||
activation_default=Act.tanh,
|
||||
)
|
||||
),
|
||||
pop_size=1000,
|
||||
species_size=10,
|
||||
),
|
||||
),
|
||||
problem=BraxEnv(
|
||||
env_name='ant',
|
||||
),
|
||||
generation_limit=10000,
|
||||
fitness_target=5000
|
||||
)
|
||||
|
||||
algorithm = NEAT(conf, NormalGene)
|
||||
pipeline = Pipeline(conf, algorithm, BraxEnv)
|
||||
# initialize state
|
||||
state = pipeline.setup()
|
||||
pipeline.pre_compile(state)
|
||||
state, best = pipeline.auto_run(state)
|
||||
# print(state)
|
||||
# run until terminate
|
||||
state, best = pipeline.auto_run(state)
|
||||
@@ -1,42 +1,36 @@
|
||||
import jax.numpy as jnp
|
||||
|
||||
from config import *
|
||||
from pipeline import Pipeline
|
||||
from algorithm import NEAT
|
||||
from algorithm.neat.gene import NormalGene, NormalGeneConfig
|
||||
from problem.rl_env import BraxEnv, BraxConfig
|
||||
|
||||
|
||||
# ['ant', 'halfcheetah', 'hopper', 'humanoid', 'humanoidstandup', 'inverted_pendulum', 'inverted_double_pendulum', 'pusher', 'reacher', 'walker2d']
|
||||
|
||||
|
||||
def example_conf():
|
||||
return Config(
|
||||
basic=BasicConfig(
|
||||
seed=42,
|
||||
fitness_target=10000,
|
||||
generation_limit=10,
|
||||
pop_size=100
|
||||
),
|
||||
neat=NeatConfig(
|
||||
inputs=17,
|
||||
outputs=6,
|
||||
),
|
||||
gene=NormalGeneConfig(
|
||||
activation_default=Act.tanh,
|
||||
activation_options=(Act.tanh,),
|
||||
),
|
||||
problem=BraxConfig(
|
||||
env_name="halfcheetah"
|
||||
)
|
||||
)
|
||||
from algorithm.neat import *
|
||||
|
||||
from problem.rl_env import BraxEnv
|
||||
from utils import Act
|
||||
|
||||
if __name__ == '__main__':
|
||||
conf = example_conf()
|
||||
algorithm = NEAT(conf, NormalGene)
|
||||
pipeline = Pipeline(conf, algorithm, BraxEnv)
|
||||
pipeline = Pipeline(
|
||||
algorithm=NEAT(
|
||||
species=DefaultSpecies(
|
||||
genome=DefaultGenome(
|
||||
num_inputs=17,
|
||||
num_outputs=6,
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
node_gene=DefaultNodeGene(
|
||||
activation_options=(Act.tanh,),
|
||||
activation_default=Act.tanh,
|
||||
)
|
||||
),
|
||||
pop_size=1000,
|
||||
species_size=10,
|
||||
),
|
||||
),
|
||||
problem=BraxEnv(
|
||||
env_name='halhcheetah',
|
||||
),
|
||||
generation_limit=10000,
|
||||
fitness_target=5000
|
||||
)
|
||||
|
||||
# initialize state
|
||||
state = pipeline.setup()
|
||||
pipeline.pre_compile(state)
|
||||
state, best = pipeline.auto_run(state)
|
||||
pipeline.show(state, best, save_path="half_cheetah.gif", )
|
||||
# print(state)
|
||||
# run until terminate
|
||||
state, best = pipeline.auto_run(state)
|
||||
@@ -1,38 +1,36 @@
|
||||
import jax.numpy as jnp
|
||||
|
||||
from config import *
|
||||
from pipeline import Pipeline
|
||||
from algorithm import NEAT
|
||||
from algorithm.neat.gene import NormalGene, NormalGeneConfig
|
||||
from problem.rl_env import BraxEnv, BraxConfig
|
||||
|
||||
|
||||
def example_conf():
|
||||
return Config(
|
||||
basic=BasicConfig(
|
||||
seed=42,
|
||||
fitness_target=10000,
|
||||
pop_size=1000
|
||||
),
|
||||
neat=NeatConfig(
|
||||
inputs=11,
|
||||
outputs=2,
|
||||
),
|
||||
gene=NormalGeneConfig(
|
||||
activation_default=Act.tanh,
|
||||
activation_options=(Act.tanh,),
|
||||
),
|
||||
problem=BraxConfig(
|
||||
env_name="reacher"
|
||||
)
|
||||
)
|
||||
from algorithm.neat import *
|
||||
|
||||
from problem.rl_env import BraxEnv
|
||||
from utils import Act
|
||||
|
||||
if __name__ == '__main__':
|
||||
conf = example_conf()
|
||||
pipeline = Pipeline(
|
||||
algorithm=NEAT(
|
||||
species=DefaultSpecies(
|
||||
genome=DefaultGenome(
|
||||
num_inputs=11,
|
||||
num_outputs=2,
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
node_gene=DefaultNodeGene(
|
||||
activation_options=(Act.tanh,),
|
||||
activation_default=Act.tanh,
|
||||
)
|
||||
),
|
||||
pop_size=100,
|
||||
species_size=10,
|
||||
),
|
||||
),
|
||||
problem=BraxEnv(
|
||||
env_name='reacher',
|
||||
),
|
||||
generation_limit=10000,
|
||||
fitness_target=5000
|
||||
)
|
||||
|
||||
algorithm = NEAT(conf, NormalGene)
|
||||
pipeline = Pipeline(conf, algorithm, BraxEnv)
|
||||
# initialize state
|
||||
state = pipeline.setup()
|
||||
pipeline.pre_compile(state)
|
||||
state, best = pipeline.auto_run(state)
|
||||
# print(state)
|
||||
# run until terminate
|
||||
state, best = pipeline.auto_run(state)
|
||||
@@ -1,73 +0,0 @@
|
||||
import imageio
|
||||
import jax
|
||||
|
||||
import brax
|
||||
from brax import envs
|
||||
from brax.io import image
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
import time
|
||||
from tqdm import tqdm
|
||||
import numpy as np
|
||||
|
||||
|
||||
def inference_func(key, *args):
|
||||
return jax.random.normal(key, shape=(env.action_size,))
|
||||
|
||||
|
||||
env_name = "ant"
|
||||
backend = "generalized"
|
||||
|
||||
env = envs.create(env_name=env_name, backend=backend)
|
||||
|
||||
jit_env_reset = jax.jit(env.reset)
|
||||
jit_env_step = jax.jit(env.step)
|
||||
jit_inference_fn = jax.jit(inference_func)
|
||||
|
||||
rng = jax.random.PRNGKey(seed=1)
|
||||
ori_state = jit_env_reset(rng=rng)
|
||||
state = ori_state
|
||||
|
||||
render_history = []
|
||||
|
||||
for i in range(100):
|
||||
act_rng, rng = jax.random.split(rng)
|
||||
|
||||
tic = time.time()
|
||||
act = jit_inference_fn(act_rng, state.obs)
|
||||
state = jit_env_step(state, act)
|
||||
print("step time: ", time.time() - tic)
|
||||
|
||||
render_history.append(state.pipeline_state)
|
||||
|
||||
# img = image.render_array(sys=env.sys, state=pipeline_state, width=512, height=512)
|
||||
# print("render time: ", time.time() - tic)
|
||||
|
||||
# plt.imsave("../images/ant_{}.png".format(i), img)
|
||||
|
||||
reward = state.reward
|
||||
done = state.done
|
||||
print(i, reward)
|
||||
|
||||
render_history = jax.device_get(render_history)
|
||||
# print(render_history)
|
||||
|
||||
imgs = [image.render_array(sys=env.sys, state=s, width=512, height=512) for s in tqdm(render_history)]
|
||||
|
||||
|
||||
# for i, s in enumerate(tqdm(render_history)):
|
||||
# img = image.render_array(sys=env.sys, state=s, width=512, height=512)
|
||||
# print(img.shape)
|
||||
# # print(type(img))
|
||||
# plt.imsave("../images/ant_{}.png".format(i), img)
|
||||
|
||||
|
||||
def create_gif(image_list, gif_name, duration):
|
||||
with imageio.get_writer(gif_name, mode='I', duration=duration) as writer:
|
||||
for image in image_list:
|
||||
# 确保图像的数据类型正确
|
||||
formatted_image = np.array(image, dtype=np.uint8)
|
||||
writer.append_data(formatted_image)
|
||||
|
||||
|
||||
create_gif(imgs, "../images/ant.gif", 0.1)
|
||||
@@ -1,54 +0,0 @@
|
||||
import brax
|
||||
from brax import envs
|
||||
from brax.envs.wrappers import gym as gym_wrapper
|
||||
from brax.io import image
|
||||
import jax
|
||||
import jax.numpy as jnp
|
||||
import matplotlib.pyplot as plt
|
||||
import traceback
|
||||
|
||||
# print(f"Using Brax {brax.__version__}, Jax {jax.__version__}")
|
||||
# print("From GymWrapper, env.reset()")
|
||||
# try:
|
||||
# env = envs.create("inverted_pendulum",
|
||||
# batch_size=1,
|
||||
# episode_length=150,
|
||||
# backend='generalized')
|
||||
# env = gym_wrapper.GymWrapper(env)
|
||||
# env.reset()
|
||||
# img = env.render(mode='rgb_array')
|
||||
# plt.imshow(img)
|
||||
# except Exception:
|
||||
# traceback.print_exc()
|
||||
#
|
||||
# print("From GymWrapper, env.reset() and action")
|
||||
# try:
|
||||
# env = envs.create("inverted_pendulum",
|
||||
# batch_size=1,
|
||||
# episode_length=150,
|
||||
# backend='generalized')
|
||||
# env = gym_wrapper.GymWrapper(env)
|
||||
# env.reset()
|
||||
# action = jnp.zeros(env.action_space.shape)
|
||||
# env.step(action)
|
||||
# img = env.render(mode='rgb_array')
|
||||
# plt.imshow(img)
|
||||
# except Exception:
|
||||
# traceback.print_exc()
|
||||
|
||||
print("From brax env")
|
||||
try:
|
||||
env = envs.create("inverted_pendulum",
|
||||
batch_size=1,
|
||||
episode_length=150,
|
||||
backend='generalized')
|
||||
key = jax.random.PRNGKey(0)
|
||||
initial_env_state = env.reset(key)
|
||||
base_state = initial_env_state.pipeline_state
|
||||
pipeline_state = env.pipeline_init(base_state.q.ravel(), base_state.qd.ravel())
|
||||
img = image.render_array(sys=env.sys, state=pipeline_state, width=256, height=256)
|
||||
print(f"pixel values: [{img.min()}, {img.max()}]")
|
||||
plt.imshow(img)
|
||||
plt.show()
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
@@ -1,32 +1,31 @@
|
||||
from config import *
|
||||
from pipeline import Pipeline
|
||||
from algorithm import NEAT
|
||||
from algorithm.neat.gene import NormalGene, NormalGeneConfig
|
||||
from problem.func_fit import XOR, FuncFitConfig
|
||||
from algorithm.neat import *
|
||||
|
||||
from problem.func_fit import XOR3d
|
||||
|
||||
if __name__ == '__main__':
|
||||
# running config
|
||||
config = Config(
|
||||
basic=BasicConfig(
|
||||
seed=42,
|
||||
fitness_target=-1e-2,
|
||||
pop_size=10000
|
||||
pipeline = Pipeline(
|
||||
algorithm=NEAT(
|
||||
species=DefaultSpecies(
|
||||
genome=DefaultGenome(
|
||||
num_inputs=3,
|
||||
num_outputs=1,
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
),
|
||||
pop_size=10000,
|
||||
species_size=10,
|
||||
compatibility_threshold=3.5,
|
||||
),
|
||||
),
|
||||
neat=NeatConfig(
|
||||
inputs=2,
|
||||
outputs=1
|
||||
),
|
||||
gene=NormalGeneConfig(),
|
||||
problem=FuncFitConfig(
|
||||
error_method='rmse'
|
||||
)
|
||||
problem=XOR3d(),
|
||||
generation_limit=10000,
|
||||
fitness_target=-1e-8
|
||||
)
|
||||
# define algorithm: NEAT with NormalGene
|
||||
algorithm = NEAT(config, NormalGene)
|
||||
# full pipeline
|
||||
pipeline = Pipeline(config, algorithm, XOR)
|
||||
|
||||
# initialize state
|
||||
state = pipeline.setup()
|
||||
# print(state)
|
||||
# run until terminate
|
||||
state, best = pipeline.auto_run(state)
|
||||
# show result
|
||||
|
||||
51
examples/func_fit/xor3d_hyperneat.py
Normal file
51
examples/func_fit/xor3d_hyperneat.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from pipeline import Pipeline
|
||||
from algorithm.neat import *
|
||||
from algorithm.hyperneat import *
|
||||
from utils import Act
|
||||
|
||||
from problem.func_fit import XOR3d
|
||||
|
||||
if __name__ == '__main__':
|
||||
pipeline = Pipeline(
|
||||
algorithm=HyperNEAT(
|
||||
substrate=FullSubstrate(
|
||||
input_coors=[(-1, -1), (0.333, -1), (-0.333, -1), (1, -1)],
|
||||
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=[(0, 1), ],
|
||||
),
|
||||
neat=NEAT(
|
||||
species=DefaultSpecies(
|
||||
genome=DefaultGenome(
|
||||
num_inputs=4, # [-1, -1, -1, 0]
|
||||
num_outputs=1,
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
node_gene=DefaultNodeGene(
|
||||
activation_default=Act.tanh,
|
||||
activation_options=(Act.tanh,),
|
||||
),
|
||||
),
|
||||
pop_size=10000,
|
||||
species_size=10,
|
||||
compatibility_threshold=3.5,
|
||||
),
|
||||
),
|
||||
activation=Act.sigmoid,
|
||||
activate_time=10,
|
||||
),
|
||||
problem=XOR3d(),
|
||||
generation_limit=300,
|
||||
fitness_target=-1e-6
|
||||
)
|
||||
|
||||
# initialize state
|
||||
state = pipeline.setup()
|
||||
# print(state)
|
||||
# run until terminate
|
||||
state, best = pipeline.auto_run(state)
|
||||
# show result
|
||||
pipeline.show(state, best)
|
||||
@@ -1,41 +0,0 @@
|
||||
from config import *
|
||||
from pipeline import Pipeline
|
||||
from algorithm.neat import NormalGene, NormalGeneConfig
|
||||
from algorithm.hyperneat import HyperNEAT, NormalSubstrate, NormalSubstrateConfig
|
||||
from problem.func_fit import XOR3d, FuncFitConfig
|
||||
from utils import Act
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
config = Config(
|
||||
basic=BasicConfig(
|
||||
seed=42,
|
||||
fitness_target=0,
|
||||
pop_size=1000
|
||||
),
|
||||
neat=NeatConfig(
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
max_species=30,
|
||||
inputs=4,
|
||||
outputs=1
|
||||
),
|
||||
hyperneat=HyperNeatConfig(
|
||||
inputs=3,
|
||||
outputs=1
|
||||
),
|
||||
substrate=NormalSubstrateConfig(
|
||||
input_coors=((-1, -1), (-0.5, -1), (0.5, -1), (1, -1)),
|
||||
),
|
||||
gene=NormalGeneConfig(
|
||||
activation_default=Act.tanh,
|
||||
activation_options=(Act.tanh, ),
|
||||
),
|
||||
problem=FuncFitConfig()
|
||||
)
|
||||
|
||||
algorithm = HyperNEAT(config, NormalGene, NormalSubstrate)
|
||||
pipeline = Pipeline(config, algorithm, XOR3d)
|
||||
state = pipeline.setup()
|
||||
state, best = pipeline.auto_run(state)
|
||||
pipeline.show(state, best)
|
||||
@@ -1,41 +1,41 @@
|
||||
from config import *
|
||||
from pipeline import Pipeline
|
||||
from algorithm import NEAT
|
||||
from algorithm.neat.gene import RecurrentGene, RecurrentGeneConfig
|
||||
from problem.func_fit import XOR3d, FuncFitConfig
|
||||
from algorithm.neat import *
|
||||
|
||||
from problem.func_fit import XOR3d
|
||||
from utils.activation import ACT_ALL
|
||||
from utils.aggregation import AGG_ALL
|
||||
|
||||
if __name__ == '__main__':
|
||||
config = Config(
|
||||
basic=BasicConfig(
|
||||
seed=42,
|
||||
fitness_target=-1e-2,
|
||||
generation_limit=300,
|
||||
pop_size=1000
|
||||
pipeline = Pipeline(
|
||||
seed=0,
|
||||
algorithm=NEAT(
|
||||
species=DefaultSpecies(
|
||||
genome=RecurrentGenome(
|
||||
num_inputs=3,
|
||||
num_outputs=1,
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
activate_time=5,
|
||||
node_gene=DefaultNodeGene(
|
||||
activation_options=ACT_ALL,
|
||||
# aggregation_options=AGG_ALL,
|
||||
activation_replace_rate=0.2
|
||||
),
|
||||
),
|
||||
pop_size=10000,
|
||||
species_size=10,
|
||||
compatibility_threshold=3.5,
|
||||
),
|
||||
),
|
||||
neat=NeatConfig(
|
||||
network_type="recurrent",
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
max_species=30,
|
||||
conn_add=0.5,
|
||||
conn_delete=0.5,
|
||||
node_add=0.4,
|
||||
node_delete=0.4,
|
||||
inputs=3,
|
||||
outputs=1
|
||||
),
|
||||
gene=RecurrentGeneConfig(
|
||||
activate_times=10
|
||||
),
|
||||
problem=FuncFitConfig(
|
||||
error_method='rmse'
|
||||
)
|
||||
problem=XOR3d(),
|
||||
generation_limit=10000,
|
||||
fitness_target=-1e-8
|
||||
)
|
||||
|
||||
algorithm = NEAT(config, RecurrentGene)
|
||||
pipeline = Pipeline(config, algorithm, XOR3d)
|
||||
# initialize state
|
||||
state = pipeline.setup()
|
||||
pipeline.pre_compile(state)
|
||||
# print(state)
|
||||
# run until terminate
|
||||
state, best = pipeline.auto_run(state)
|
||||
# show result
|
||||
pipeline.show(state, best)
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
from config import *
|
||||
from pipeline import Pipeline
|
||||
from algorithm import NEAT
|
||||
from algorithm.neat.gene import NormalGene, NormalGeneConfig
|
||||
from problem.func_fit import XOR, FuncFitConfig
|
||||
|
||||
if __name__ == '__main__':
|
||||
config = Config(
|
||||
basic=BasicConfig(
|
||||
seed=42,
|
||||
fitness_target=-1e-2,
|
||||
pop_size=10000
|
||||
),
|
||||
neat=NeatConfig(
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
max_species=30,
|
||||
conn_add=0.8,
|
||||
conn_delete=0,
|
||||
node_add=0.4,
|
||||
node_delete=0,
|
||||
inputs=2,
|
||||
outputs=1
|
||||
),
|
||||
gene=NormalGeneConfig(),
|
||||
problem=FuncFitConfig(
|
||||
error_method='rmse'
|
||||
)
|
||||
)
|
||||
|
||||
algorithm = NEAT(config, NormalGene)
|
||||
pipeline = Pipeline(config, algorithm, XOR)
|
||||
state = pipeline.setup()
|
||||
pipeline.pre_compile(state)
|
||||
state, best = pipeline.auto_run(state)
|
||||
pipeline.show(state, best)
|
||||
@@ -1,39 +0,0 @@
|
||||
import jax.numpy as jnp
|
||||
|
||||
from config import *
|
||||
from pipeline import Pipeline
|
||||
from algorithm import NEAT
|
||||
from algorithm.neat.gene import NormalGene, NormalGeneConfig
|
||||
from problem.rl_env import GymNaxConfig, GymNaxEnv
|
||||
|
||||
|
||||
def example_conf():
|
||||
return Config(
|
||||
basic=BasicConfig(
|
||||
seed=42,
|
||||
fitness_target=0,
|
||||
pop_size=10000
|
||||
),
|
||||
neat=NeatConfig(
|
||||
inputs=6,
|
||||
outputs=3,
|
||||
),
|
||||
gene=NormalGeneConfig(
|
||||
activation_default=Act.tanh,
|
||||
activation_options=(Act.tanh,),
|
||||
),
|
||||
problem=GymNaxConfig(
|
||||
env_name='Acrobot-v1',
|
||||
output_transform=lambda out: jnp.argmax(out) # the action of acrobot is {0, 1, 2}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
conf = example_conf()
|
||||
|
||||
algorithm = NEAT(conf, NormalGene)
|
||||
pipeline = Pipeline(conf, algorithm, GymNaxEnv)
|
||||
state = pipeline.setup()
|
||||
pipeline.pre_compile(state)
|
||||
state, best = pipeline.auto_run(state)
|
||||
34
examples/gymnax/arcbot.py
Normal file
34
examples/gymnax/arcbot.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import jax.numpy as jnp
|
||||
|
||||
from pipeline import Pipeline
|
||||
from algorithm.neat import *
|
||||
|
||||
from problem.rl_env import GymNaxEnv
|
||||
|
||||
if __name__ == '__main__':
|
||||
pipeline = Pipeline(
|
||||
algorithm=NEAT(
|
||||
species=DefaultSpecies(
|
||||
genome=DefaultGenome(
|
||||
num_inputs=6,
|
||||
num_outputs=3,
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
output_transform=lambda out: jnp.argmax(out) # the action of acrobot is {0, 1, 2}
|
||||
),
|
||||
pop_size=10000,
|
||||
species_size=10,
|
||||
),
|
||||
),
|
||||
problem=GymNaxEnv(
|
||||
env_name='Acrobot-v1',
|
||||
),
|
||||
generation_limit=10000,
|
||||
fitness_target=-62
|
||||
)
|
||||
|
||||
# initialize state
|
||||
state = pipeline.setup()
|
||||
# print(state)
|
||||
# run until terminate
|
||||
state, best = pipeline.auto_run(state)
|
||||
@@ -1,84 +1,34 @@
|
||||
import jax.numpy as jnp
|
||||
|
||||
from config import *
|
||||
from pipeline import Pipeline
|
||||
from algorithm import NEAT
|
||||
from algorithm.neat.gene import NormalGene, NormalGeneConfig
|
||||
from problem.rl_env import GymNaxConfig, GymNaxEnv
|
||||
|
||||
|
||||
def example_conf1():
|
||||
return Config(
|
||||
basic=BasicConfig(
|
||||
seed=42,
|
||||
fitness_target=500,
|
||||
pop_size=10000
|
||||
),
|
||||
neat=NeatConfig(
|
||||
inputs=4,
|
||||
outputs=1,
|
||||
),
|
||||
gene=NormalGeneConfig(
|
||||
activation_default=Act.sigmoid,
|
||||
activation_options=(Act.sigmoid,),
|
||||
),
|
||||
problem=GymNaxConfig(
|
||||
env_name='CartPole-v1',
|
||||
output_transform=lambda out: jnp.where(out[0] > 0.5, 1, 0) # the action of cartpole is {0, 1}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def example_conf2():
|
||||
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,),
|
||||
),
|
||||
problem=GymNaxConfig(
|
||||
env_name='CartPole-v1',
|
||||
output_transform=lambda out: jnp.where(out[0] > 0, 1, 0) # the action of cartpole is {0, 1}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def example_conf3():
|
||||
return Config(
|
||||
basic=BasicConfig(
|
||||
seed=42,
|
||||
fitness_target=501,
|
||||
pop_size=10000
|
||||
),
|
||||
neat=NeatConfig(
|
||||
inputs=4,
|
||||
outputs=2,
|
||||
),
|
||||
gene=NormalGeneConfig(
|
||||
activation_default=Act.tanh,
|
||||
activation_options=(Act.tanh,),
|
||||
),
|
||||
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 problem.rl_env import GymNaxEnv
|
||||
|
||||
if __name__ == '__main__':
|
||||
# all config files above can solve cartpole
|
||||
conf = example_conf3()
|
||||
pipeline = Pipeline(
|
||||
algorithm=NEAT(
|
||||
species=DefaultSpecies(
|
||||
genome=DefaultGenome(
|
||||
num_inputs=4,
|
||||
num_outputs=2,
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
output_transform=lambda out: jnp.argmax(out) # the action of cartpole is {0, 1}
|
||||
),
|
||||
pop_size=10000,
|
||||
species_size=10,
|
||||
),
|
||||
),
|
||||
problem=GymNaxEnv(
|
||||
env_name='CartPole-v1',
|
||||
),
|
||||
generation_limit=10000,
|
||||
fitness_target=500
|
||||
)
|
||||
|
||||
algorithm = NEAT(conf, NormalGene)
|
||||
pipeline = Pipeline(conf, algorithm, GymNaxEnv)
|
||||
# initialize state
|
||||
state = pipeline.setup()
|
||||
pipeline.pre_compile(state)
|
||||
state, best = pipeline.auto_run(state)
|
||||
# print(state)
|
||||
# run until terminate
|
||||
state, best = pipeline.auto_run(state)
|
||||
@@ -1,39 +1,34 @@
|
||||
import jax.numpy as jnp
|
||||
|
||||
from config import *
|
||||
from pipeline import Pipeline
|
||||
from algorithm import NEAT
|
||||
from algorithm.neat.gene import NormalGene, NormalGeneConfig
|
||||
from problem.rl_env import GymNaxConfig, GymNaxEnv
|
||||
|
||||
|
||||
def example_conf():
|
||||
return Config(
|
||||
basic=BasicConfig(
|
||||
seed=42,
|
||||
fitness_target=0,
|
||||
pop_size=10000
|
||||
),
|
||||
neat=NeatConfig(
|
||||
inputs=2,
|
||||
outputs=3,
|
||||
),
|
||||
gene=NormalGeneConfig(
|
||||
activation_default=Act.sigmoid,
|
||||
activation_options=(Act.sigmoid,),
|
||||
),
|
||||
problem=GymNaxConfig(
|
||||
env_name='MountainCar-v0',
|
||||
output_transform=lambda out: jnp.argmax(out) # the action of cartpole is {0, 1, 2}
|
||||
)
|
||||
)
|
||||
from algorithm.neat import *
|
||||
|
||||
from problem.rl_env import GymNaxEnv
|
||||
|
||||
if __name__ == '__main__':
|
||||
conf = example_conf()
|
||||
pipeline = Pipeline(
|
||||
algorithm=NEAT(
|
||||
species=DefaultSpecies(
|
||||
genome=DefaultGenome(
|
||||
num_inputs=2,
|
||||
num_outputs=3,
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
output_transform=lambda out: jnp.argmax(out) # the action of mountain car is {0, 1, 2}
|
||||
),
|
||||
pop_size=10000,
|
||||
species_size=10,
|
||||
),
|
||||
),
|
||||
problem=GymNaxEnv(
|
||||
env_name='MountainCar-v0',
|
||||
),
|
||||
generation_limit=10000,
|
||||
fitness_target=0
|
||||
)
|
||||
|
||||
algorithm = NEAT(conf, NormalGene)
|
||||
pipeline = Pipeline(conf, algorithm, GymNaxEnv)
|
||||
# initialize state
|
||||
state = pipeline.setup()
|
||||
pipeline.pre_compile(state)
|
||||
state, best = pipeline.auto_run(state)
|
||||
# print(state)
|
||||
# run until terminate
|
||||
state, best = pipeline.auto_run(state)
|
||||
@@ -1,38 +1,36 @@
|
||||
import jax.numpy as jnp
|
||||
|
||||
from config import *
|
||||
from pipeline import Pipeline
|
||||
from algorithm import NEAT
|
||||
from algorithm.neat.gene import NormalGene, NormalGeneConfig
|
||||
from problem.rl_env import GymNaxConfig, GymNaxEnv
|
||||
|
||||
|
||||
def example_conf():
|
||||
return Config(
|
||||
basic=BasicConfig(
|
||||
seed=42,
|
||||
fitness_target=100,
|
||||
pop_size=10000
|
||||
),
|
||||
neat=NeatConfig(
|
||||
inputs=2,
|
||||
outputs=1,
|
||||
),
|
||||
gene=NormalGeneConfig(
|
||||
activation_default=Act.tanh,
|
||||
activation_options=(Act.tanh,),
|
||||
),
|
||||
problem=GymNaxConfig(
|
||||
env_name='MountainCarContinuous-v0'
|
||||
)
|
||||
)
|
||||
from algorithm.neat import *
|
||||
|
||||
from problem.rl_env import GymNaxEnv
|
||||
from utils import Act
|
||||
|
||||
if __name__ == '__main__':
|
||||
conf = example_conf()
|
||||
pipeline = Pipeline(
|
||||
algorithm=NEAT(
|
||||
species=DefaultSpecies(
|
||||
genome=DefaultGenome(
|
||||
num_inputs=2,
|
||||
num_outputs=1,
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
node_gene=DefaultNodeGene(
|
||||
activation_options=(Act.tanh, ),
|
||||
activation_default=Act.tanh,
|
||||
)
|
||||
),
|
||||
pop_size=10000,
|
||||
species_size=10,
|
||||
),
|
||||
),
|
||||
problem=GymNaxEnv(
|
||||
env_name='MountainCarContinuous-v0',
|
||||
),
|
||||
generation_limit=10000,
|
||||
fitness_target=500
|
||||
)
|
||||
|
||||
algorithm = NEAT(conf, NormalGene)
|
||||
pipeline = Pipeline(conf, algorithm, GymNaxEnv)
|
||||
# initialize state
|
||||
state = pipeline.setup()
|
||||
pipeline.pre_compile(state)
|
||||
state, best = pipeline.auto_run(state)
|
||||
# print(state)
|
||||
# run until terminate
|
||||
state, best = pipeline.auto_run(state)
|
||||
@@ -1,40 +1,37 @@
|
||||
import jax.numpy as jnp
|
||||
|
||||
from config import *
|
||||
from pipeline import Pipeline
|
||||
from algorithm import NEAT
|
||||
from algorithm.neat.gene import NormalGene, NormalGeneConfig
|
||||
from problem.rl_env import GymNaxConfig, GymNaxEnv
|
||||
|
||||
|
||||
def example_conf():
|
||||
return Config(
|
||||
basic=BasicConfig(
|
||||
seed=42,
|
||||
fitness_target=0,
|
||||
pop_size=10000
|
||||
),
|
||||
neat=NeatConfig(
|
||||
inputs=3,
|
||||
outputs=1,
|
||||
),
|
||||
gene=NormalGeneConfig(
|
||||
activation_default=Act.tanh,
|
||||
activation_options=(Act.tanh,),
|
||||
),
|
||||
problem=GymNaxConfig(
|
||||
env_name='Pendulum-v1',
|
||||
output_transform=lambda out: out * 2 # the action of pendulum is [-2, 2]
|
||||
)
|
||||
)
|
||||
from algorithm.neat import *
|
||||
|
||||
from problem.rl_env import GymNaxEnv
|
||||
from utils import Act
|
||||
|
||||
if __name__ == '__main__':
|
||||
conf = example_conf()
|
||||
pipeline = Pipeline(
|
||||
algorithm=NEAT(
|
||||
species=DefaultSpecies(
|
||||
genome=DefaultGenome(
|
||||
num_inputs=3,
|
||||
num_outputs=1,
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
node_gene=DefaultNodeGene(
|
||||
activation_options=(Act.tanh,),
|
||||
activation_default=Act.tanh,
|
||||
),
|
||||
output_transform=lambda out: out * 2 # the action of pendulum is [-2, 2]
|
||||
),
|
||||
pop_size=10000,
|
||||
species_size=10,
|
||||
),
|
||||
),
|
||||
problem=GymNaxEnv(
|
||||
env_name='Pendulum-v1',
|
||||
),
|
||||
generation_limit=10000,
|
||||
fitness_target=0
|
||||
)
|
||||
|
||||
algorithm = NEAT(conf, NormalGene)
|
||||
pipeline = Pipeline(conf, algorithm, GymNaxEnv)
|
||||
# initialize state
|
||||
state = pipeline.setup()
|
||||
pipeline.pre_compile(state)
|
||||
state, best = pipeline.auto_run(state)
|
||||
|
||||
# print(state)
|
||||
# run until terminate
|
||||
state, best = pipeline.auto_run(state)
|
||||
@@ -1,36 +1,33 @@
|
||||
from config import *
|
||||
import jax.numpy as jnp
|
||||
|
||||
from pipeline import Pipeline
|
||||
from algorithm import NEAT
|
||||
from algorithm.neat.gene import NormalGene, NormalGeneConfig
|
||||
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=8,
|
||||
outputs=2,
|
||||
),
|
||||
gene=NormalGeneConfig(
|
||||
activation_default=Act.sigmoid,
|
||||
activation_options=(Act.sigmoid,),
|
||||
),
|
||||
problem=GymNaxConfig(
|
||||
env_name='Reacher-misc',
|
||||
)
|
||||
)
|
||||
from algorithm.neat import *
|
||||
|
||||
from problem.rl_env import GymNaxEnv
|
||||
|
||||
if __name__ == '__main__':
|
||||
conf = example_conf()
|
||||
pipeline = Pipeline(
|
||||
algorithm=NEAT(
|
||||
species=DefaultSpecies(
|
||||
genome=DefaultGenome(
|
||||
num_inputs=8,
|
||||
num_outputs=2,
|
||||
max_nodes=50,
|
||||
max_conns=100,
|
||||
),
|
||||
pop_size=10000,
|
||||
species_size=10,
|
||||
),
|
||||
),
|
||||
problem=GymNaxEnv(
|
||||
env_name='Reacher-misc',
|
||||
),
|
||||
generation_limit=10000,
|
||||
fitness_target =500
|
||||
)
|
||||
|
||||
algorithm = NEAT(conf, NormalGene)
|
||||
pipeline = Pipeline(conf, algorithm, GymNaxEnv)
|
||||
# initialize state
|
||||
state = pipeline.setup()
|
||||
pipeline.pre_compile(state)
|
||||
state, best = pipeline.auto_run(state)
|
||||
# print(state)
|
||||
# run until terminate
|
||||
state, best = pipeline.auto_run(state)
|
||||
Reference in New Issue
Block a user