The whole NEAT algorithm is written into functional programming.
This commit is contained in:
@@ -1,26 +0,0 @@
|
||||
import jax
|
||||
from jax import numpy as jnp
|
||||
|
||||
from evox import algorithms, problems, pipelines
|
||||
from evox.monitors import StdSOMonitor
|
||||
|
||||
monitor = StdSOMonitor()
|
||||
|
||||
pso = algorithms.PSO(
|
||||
lb=jnp.full(shape=(2,), fill_value=-32),
|
||||
ub=jnp.full(shape=(2,), fill_value=32),
|
||||
pop_size=100,
|
||||
)
|
||||
|
||||
ackley = problems.classic.Ackley()
|
||||
|
||||
pipeline = pipelines.StdPipeline(pso, ackley, fitness_transform=monitor.record_fit)
|
||||
|
||||
key = jax.random.PRNGKey(42)
|
||||
state = pipeline.init(key)
|
||||
|
||||
# run the pipeline for 100 steps
|
||||
for i in range(100):
|
||||
state = pipeline.step(state)
|
||||
|
||||
print(monitor.get_min_fitness())
|
||||
@@ -1,28 +0,0 @@
|
||||
import numpy as np
|
||||
|
||||
from configs import Configer
|
||||
from jit_pipeline import Pipeline
|
||||
|
||||
xor_inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32)
|
||||
xor_outputs = np.array([[0], [1], [1], [0]], dtype=np.float32)
|
||||
|
||||
|
||||
def evaluate(forward_func):
|
||||
"""
|
||||
:param forward_func: (4: batch, 2: input size) -> (pop_size, 4: batch, 1: output size)
|
||||
:return:
|
||||
"""
|
||||
outs = forward_func(xor_inputs)
|
||||
fitnesses = 4 - np.sum((outs - xor_outputs) ** 2, axis=(1, 2))
|
||||
return np.array(fitnesses) # returns a list
|
||||
|
||||
|
||||
def main():
|
||||
config = Configer.load_config("xor.ini")
|
||||
pipeline = Pipeline(config, seed=6)
|
||||
nodes, cons = pipeline.auto_run(evaluate)
|
||||
print(nodes, cons)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -1,7 +1,8 @@
|
||||
import numpy as np
|
||||
|
||||
from configs import Configer
|
||||
from pipeline import Pipeline
|
||||
from algorithms.neat.pipeline import Pipeline
|
||||
|
||||
|
||||
xor_inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32)
|
||||
xor_outputs = np.array([[0], [1], [1], [0]], dtype=np.float32)
|
||||
@@ -21,7 +22,8 @@ def main():
|
||||
config = Configer.load_config("xor.ini")
|
||||
pipeline = Pipeline(config, seed=6)
|
||||
nodes, cons = pipeline.auto_run(evaluate)
|
||||
print(nodes, cons)
|
||||
print(nodes)
|
||||
print(cons)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user