remove useless codes
This commit is contained in:
@@ -1,84 +0,0 @@
|
||||
from typing import Callable, List
|
||||
from functools import partial
|
||||
|
||||
import numpy as np
|
||||
|
||||
from utils import Configer
|
||||
from algorithms.neat.genome.numpy import analysis, distance
|
||||
from algorithms.neat.genome.numpy import create_initialize_function, create_mutate_function
|
||||
|
||||
|
||||
def real_distance(nodes1, connections1, nodes2, connections2, input_idx, output_idx):
|
||||
nodes1, connections1 = analysis(nodes1, connections1, input_idx, output_idx)
|
||||
nodes2, connections2 = analysis(nodes2, connections2, input_idx, output_idx)
|
||||
compatibility_coe = 0.5
|
||||
disjoint_coe = 1.
|
||||
node_distance = 0.0
|
||||
if nodes1 or nodes2: # otherwise, both are empty
|
||||
disjoint_nodes = 0
|
||||
for k2 in nodes2:
|
||||
if k2 not in nodes1:
|
||||
disjoint_nodes += 1
|
||||
|
||||
for k1, n1 in nodes1.items():
|
||||
n2 = nodes2.get(k1)
|
||||
if n2 is None:
|
||||
disjoint_nodes += 1
|
||||
else:
|
||||
if n1[0] is None:
|
||||
continue
|
||||
d = abs(n1[0] - n2[0]) + abs(n1[1] - n2[1])
|
||||
d += 1 if n1[2] != n2[2] else 0
|
||||
d += 1 if n1[3] != n2[3] else 0
|
||||
node_distance += d
|
||||
|
||||
max_nodes = max(len(nodes1), len(nodes2))
|
||||
node_distance = (compatibility_coe * node_distance + disjoint_coe * disjoint_nodes) / max_nodes
|
||||
|
||||
connection_distance = 0.0
|
||||
if connections1 or connections2:
|
||||
disjoint_connections = 0
|
||||
for k2 in connections2:
|
||||
if k2 not in connections1:
|
||||
disjoint_connections += 1
|
||||
|
||||
for k1, c1 in connections1.items():
|
||||
c2 = connections2.get(k1)
|
||||
if c2 is None:
|
||||
disjoint_connections += 1
|
||||
else:
|
||||
# Homologous genes compute their own distance value.
|
||||
d = abs(c1[0] - c2[0])
|
||||
d += 1 if c1[1] != c2[1] else 0
|
||||
connection_distance += d
|
||||
max_conn = max(len(connections1), len(connections2))
|
||||
connection_distance = (compatibility_coe * connection_distance + disjoint_coe * disjoint_connections) / max_conn
|
||||
|
||||
return node_distance + connection_distance
|
||||
|
||||
|
||||
def main():
|
||||
config = Configer.load_config()
|
||||
keys_idx = config.basic.num_inputs + config.basic.num_outputs
|
||||
pop_size = config.neat.population.pop_size
|
||||
init_func = create_initialize_function(config)
|
||||
pop_nodes, pop_connections, input_idx, output_idx = init_func()
|
||||
|
||||
mutate_func = create_mutate_function(config, input_idx, output_idx, batch=True)
|
||||
|
||||
while True:
|
||||
pop_nodes, pop_connections = mutate_func(pop_nodes, pop_connections, list(range(keys_idx, keys_idx + pop_size)))
|
||||
keys_idx += pop_size
|
||||
for i in range(pop_size):
|
||||
for j in range(pop_size):
|
||||
nodes1, connections1 = pop_nodes[i], pop_connections[i]
|
||||
nodes2, connections2 = pop_nodes[j], pop_connections[j]
|
||||
numpy_d = distance(nodes1, connections1, nodes2, connections2)
|
||||
real_d = real_distance(nodes1, connections1, nodes2, connections2, input_idx, output_idx)
|
||||
assert np.isclose(numpy_d, real_d), f'{numpy_d} != {real_d}'
|
||||
print(numpy_d, real_d)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
np.random.seed(0)
|
||||
main()
|
||||
@@ -1,24 +0,0 @@
|
||||
import numpy as np
|
||||
from jax import numpy as jnp
|
||||
|
||||
from algorithms.neat.genome.genome import analysis
|
||||
from algorithms.neat.genome import create_forward_function
|
||||
|
||||
|
||||
error_nodes = np.load('error_nodes.npy')
|
||||
error_connections = np.load('error_connections.npy')
|
||||
|
||||
node_dict, connection_dict = analysis(error_nodes, error_connections, np.array([0, 1]), np.array([2, ]))
|
||||
print(node_dict, connection_dict, sep='\n')
|
||||
|
||||
N = error_nodes.shape[0]
|
||||
|
||||
xor_inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
|
||||
|
||||
func = create_forward_function(error_nodes, error_connections, N, jnp.array([0, 1]), jnp.array([2, ]),
|
||||
batch=True, debug=True)
|
||||
out = func(np.array([1, 0]))
|
||||
|
||||
print(error_nodes)
|
||||
print(error_connections)
|
||||
print(out)
|
||||
@@ -1,11 +0,0 @@
|
||||
import numpy as np
|
||||
from algorithms.neat.genome import distance
|
||||
|
||||
r_nodes = np.load('too_large_distance_r_nodes.npy')
|
||||
r_connections = np.load('too_large_distance_r_connections.npy')
|
||||
nodes = np.load('too_large_distance_nodes.npy')
|
||||
connections = np.load('too_large_distance_connections.npy')
|
||||
|
||||
d1 = distance(r_nodes, r_connections, nodes, connections)
|
||||
d2 = distance(nodes, connections, r_nodes, r_connections)
|
||||
print(d1, d2)
|
||||
@@ -1,71 +0,0 @@
|
||||
import time
|
||||
|
||||
import jax.random
|
||||
|
||||
from utils import Configer
|
||||
from algorithms.neat.genome.genome import *
|
||||
|
||||
from algorithms.neat.species import SpeciesController
|
||||
from algorithms.neat.genome.forward import create_forward_function
|
||||
from algorithms.neat.genome.mutate import create_mutate_function
|
||||
|
||||
if __name__ == '__main__':
|
||||
N = 10
|
||||
pop_nodes, pop_connections, input_idx, output_idx = initialize_genomes(10000, N, 2, 1,
|
||||
default_act=9, default_agg=0)
|
||||
inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
|
||||
# forward = create_forward_function(pop_nodes, pop_connections, 5, input_idx, output_idx, batch=True)
|
||||
nodes, connections = pop_nodes[0], pop_connections[0]
|
||||
|
||||
forward = create_forward_function(pop_nodes, pop_connections, N, input_idx, output_idx, batch=True)
|
||||
out = forward(inputs)
|
||||
print(out.shape)
|
||||
print(out)
|
||||
|
||||
config = Configer.load_config()
|
||||
s_c = SpeciesController(config.neat)
|
||||
s_c.speciate(pop_nodes, pop_connections, 0)
|
||||
s_c.speciate(pop_nodes, pop_connections, 0)
|
||||
print(s_c.genome_to_species)
|
||||
|
||||
start = time.time()
|
||||
for i in range(100):
|
||||
print(i)
|
||||
s_c.speciate(pop_nodes, pop_connections, i)
|
||||
print(time.time() - start)
|
||||
|
||||
seed = jax.random.PRNGKey(42)
|
||||
mutate_func = create_mutate_function(config, input_idx, output_idx, batch=False)
|
||||
print(nodes, connections, sep='\n')
|
||||
print(*mutate_func(seed, nodes, connections, 100), sep='\n')
|
||||
|
||||
randseeds = jax.random.split(seed, 10000)
|
||||
new_node_keys = jax.random.randint(randseeds[0], minval=0, maxval=10000, shape=(10000,))
|
||||
batch_mutate_func = create_mutate_function(config, input_idx, output_idx, batch=True)
|
||||
pop_nodes, pop_connections = batch_mutate_func(randseeds, pop_nodes, pop_connections, new_node_keys)
|
||||
print(pop_nodes, pop_connections, sep='\n')
|
||||
|
||||
start = time.time()
|
||||
for i in range(100):
|
||||
print(i)
|
||||
pop_nodes, pop_connections = batch_mutate_func(randseeds, pop_nodes, pop_connections, new_node_keys)
|
||||
print(time.time() - start)
|
||||
|
||||
print(nodes, connections, sep='\n')
|
||||
nodes, connections = add_node(6, nodes, connections)
|
||||
nodes, connections = add_node(7, nodes, connections)
|
||||
print(nodes, connections, sep='\n')
|
||||
|
||||
nodes, connections = add_connection(6, 7, nodes, connections)
|
||||
nodes, connections = add_connection(0, 7, nodes, connections)
|
||||
nodes, connections = add_connection(1, 7, nodes, connections)
|
||||
print(nodes, connections, sep='\n')
|
||||
|
||||
nodes, connections = delete_connection(6, 7, nodes, connections)
|
||||
print(nodes, connections, sep='\n')
|
||||
|
||||
nodes, connections = delete_node(6, nodes, connections)
|
||||
print(nodes, connections, sep='\n')
|
||||
|
||||
nodes, connections = delete_node(7, nodes, connections)
|
||||
print(nodes, connections, sep='\n')
|
||||
@@ -18,7 +18,6 @@ def evaluate(forward_func: Callable) -> List[float]:
|
||||
"""
|
||||
outs = forward_func(xor_inputs)
|
||||
fitnesses = 4 - np.sum((outs - xor_outputs) ** 2, axis=(1, 2))
|
||||
# print(fitnesses)
|
||||
return fitnesses.tolist() # returns a list
|
||||
|
||||
|
||||
@@ -29,13 +28,6 @@ def main():
|
||||
pipeline = Pipeline(config, seed=11323)
|
||||
pipeline.auto_run(evaluate)
|
||||
|
||||
# for _ in range(100):
|
||||
# s = time.time()
|
||||
# forward_func = pipeline.ask(batch=True)
|
||||
# fitnesses = evaluate(forward_func)
|
||||
# pipeline.tell(fitnesses)
|
||||
# print(time.time() - s)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user