|
import jax
|
|
import jax.numpy as jnp
|
|
from jax import random
|
|
from evojax.task.slimevolley import SlimeVolley
|
|
from typing import List, Tuple, Dict
|
|
import numpy as np
|
|
import time
|
|
|
|
class NodeGene:
|
|
def __init__(self, id: int, node_type: str, activation: str = 'tanh'):
|
|
self.id = id
|
|
self.type = node_type
|
|
self.activation = activation
|
|
|
|
|
|
timestamp = int(time.time() * 1000)
|
|
key = random.PRNGKey(hash((id, timestamp)) % (2**32))
|
|
self.bias = float(random.normal(key, shape=()) * 0.1)
|
|
|
|
class ConnectionGene:
|
|
def __init__(self, source: int, target: int, weight: float = None, enabled: bool = True):
|
|
self.source = source
|
|
self.target = target
|
|
|
|
|
|
timestamp = int(time.time() * 1000)
|
|
key = random.PRNGKey(hash((source, target, timestamp)) % (2**32))
|
|
|
|
if weight is None:
|
|
key, subkey = random.split(key)
|
|
weight = float(random.normal(subkey, shape=()) * 0.1)
|
|
self.weight = weight
|
|
self.enabled = enabled
|
|
self.innovation = hash((source, target))
|
|
|
|
class Genome:
|
|
def __init__(self, n_inputs: int, n_outputs: int):
|
|
|
|
self.node_genes = {i: NodeGene(i, 'input') for i in range(n_inputs)}
|
|
|
|
|
|
n_outputs = 3
|
|
for i in range(n_outputs):
|
|
self.node_genes[n_inputs + i] = NodeGene(n_inputs + i, 'output')
|
|
|
|
self.connection_genes: List[ConnectionGene] = []
|
|
|
|
|
|
timestamp = int(time.time() * 1000)
|
|
master_key = random.PRNGKey(hash((n_inputs, n_outputs, timestamp)) % (2**32))
|
|
|
|
|
|
for i in range(n_inputs):
|
|
for j in range(n_outputs):
|
|
master_key, key = random.split(master_key)
|
|
if random.uniform(key, shape=()) < 0.7:
|
|
master_key, key = random.split(master_key)
|
|
weight = float(random.normal(key, shape=()) * 0.5)
|
|
self.connection_genes.append(
|
|
ConnectionGene(i, n_inputs + j, weight=weight)
|
|
)
|
|
|
|
|
|
master_key, key = random.split(master_key)
|
|
n_hidden = int(random.randint(key, (), 1, 4))
|
|
hidden_start = n_inputs + n_outputs
|
|
|
|
for i in range(n_hidden):
|
|
node_id = hidden_start + i
|
|
self.node_genes[node_id] = NodeGene(node_id, 'hidden')
|
|
|
|
|
|
for j in range(n_inputs):
|
|
master_key, key = random.split(master_key)
|
|
if random.uniform(key, shape=()) < 0.5:
|
|
master_key, key = random.split(master_key)
|
|
weight = float(random.normal(key, shape=()) * 0.5)
|
|
self.connection_genes.append(
|
|
ConnectionGene(j, node_id, weight=weight)
|
|
)
|
|
|
|
|
|
for j in range(n_outputs):
|
|
master_key, key = random.split(master_key)
|
|
if random.uniform(key, shape=()) < 0.5:
|
|
master_key, key = random.split(master_key)
|
|
weight = float(random.normal(key, shape=()) * 0.5)
|
|
self.connection_genes.append(
|
|
ConnectionGene(node_id, n_inputs + j, weight=weight)
|
|
)
|
|
|
|
def mutate(self, config: Dict):
|
|
key = random.PRNGKey(0)
|
|
|
|
|
|
for conn in self.connection_genes:
|
|
key, subkey = random.split(key)
|
|
if random.uniform(subkey, shape=()) < config['weight_mutation_rate']:
|
|
key, subkey = random.split(key)
|
|
|
|
if random.uniform(subkey, shape=()) < 0.1:
|
|
key, subkey = random.split(key)
|
|
conn.weight = float(random.normal(subkey, shape=()) * 0.5)
|
|
else:
|
|
|
|
key, subkey = random.split(key)
|
|
conn.weight += float(random.normal(subkey) * config['weight_mutation_power'])
|
|
|
|
|
|
for node in self.node_genes.values():
|
|
key, subkey = random.split(key)
|
|
if random.uniform(subkey, shape=()) < 0.1:
|
|
key, subkey = random.split(key)
|
|
node.bias += float(random.normal(subkey) * 0.1)
|
|
|
|
|
|
key, subkey = random.split(key)
|
|
if random.uniform(subkey, shape=()) < config['add_node_rate']:
|
|
if self.connection_genes:
|
|
|
|
conn = np.random.choice(self.connection_genes)
|
|
new_id = max(self.node_genes.keys()) + 1
|
|
|
|
|
|
self.node_genes[new_id] = NodeGene(new_id, 'hidden')
|
|
|
|
|
|
key, subkey = random.split(key)
|
|
weight1 = float(random.normal(subkey, shape=()) * 0.5)
|
|
key, subkey = random.split(key)
|
|
weight2 = float(random.normal(subkey, shape=()) * 0.5)
|
|
|
|
self.connection_genes.append(
|
|
ConnectionGene(conn.source, new_id, weight=weight1)
|
|
)
|
|
self.connection_genes.append(
|
|
ConnectionGene(new_id, conn.target, weight=weight2)
|
|
)
|
|
|
|
|
|
conn.enabled = False
|
|
|
|
|
|
key, subkey = random.split(key)
|
|
if random.uniform(subkey, shape=()) < config['add_connection_rate']:
|
|
|
|
nodes = list(self.node_genes.keys())
|
|
for _ in range(10):
|
|
source = np.random.choice(nodes)
|
|
target = np.random.choice(nodes)
|
|
|
|
|
|
if source < target:
|
|
|
|
if not any(c.source == source and c.target == target
|
|
for c in self.connection_genes):
|
|
key, subkey = random.split(key)
|
|
weight = float(random.normal(subkey, shape=()) * 0.5)
|
|
self.connection_genes.append(
|
|
ConnectionGene(source, target, weight=weight)
|
|
)
|
|
break
|
|
|
|
class Network:
|
|
def __init__(self, genome: Genome):
|
|
self.genome = genome
|
|
|
|
self.input_nodes = sorted([n for n in genome.node_genes.values() if n.type == 'input'], key=lambda x: x.id)
|
|
self.hidden_nodes = sorted([n for n in genome.node_genes.values() if n.type == 'hidden'], key=lambda x: x.id)
|
|
self.output_nodes = sorted([n for n in genome.node_genes.values() if n.type == 'output'], key=lambda x: x.id)
|
|
|
|
|
|
assert len(self.output_nodes) == 3, f"Expected 3 output nodes, got {len(self.output_nodes)}"
|
|
|
|
def forward(self, x: jnp.ndarray) -> jnp.ndarray:
|
|
|
|
if len(x.shape) == 1:
|
|
x = jnp.expand_dims(x, 0)
|
|
|
|
batch_size = x.shape[0]
|
|
|
|
|
|
values = {}
|
|
for node in self.genome.node_genes.values():
|
|
values[node.id] = jnp.zeros((batch_size,))
|
|
values[node.id] = values[node.id] + node.bias
|
|
|
|
|
|
for i, node in enumerate(self.input_nodes):
|
|
values[node.id] = x[:, i]
|
|
|
|
|
|
for node in self.hidden_nodes + self.output_nodes:
|
|
|
|
total = jnp.zeros((batch_size,))
|
|
total = total + node.bias
|
|
|
|
for conn in self.genome.connection_genes:
|
|
if conn.enabled and conn.target == node.id:
|
|
total = total + values[conn.source] * conn.weight
|
|
|
|
|
|
values[node.id] = jnp.tanh(total)
|
|
|
|
|
|
outputs = []
|
|
for node in self.output_nodes:
|
|
outputs.append(values[node.id])
|
|
|
|
|
|
return jnp.stack(outputs, axis=-1)
|
|
|
|
def evaluate_network(network: Network, env: SlimeVolley, n_episodes: int = 10) -> float:
|
|
total_reward = 0.0
|
|
|
|
|
|
timestamp = int(time.time() * 1000)
|
|
network_id = id(network)
|
|
master_key = random.PRNGKey(hash((network_id, timestamp)) % (2**32))
|
|
|
|
for episode in range(n_episodes):
|
|
|
|
master_key, reset_key = random.split(master_key)
|
|
state = env.reset(reset_key[None, :])
|
|
done = False
|
|
episode_reward = 0.0
|
|
steps = 0
|
|
|
|
while not done and steps < 1000:
|
|
|
|
obs = state.obs[None, :] / 10.0
|
|
|
|
|
|
raw_action = network.forward(obs)
|
|
|
|
|
|
thresholds = jnp.array([0.3, 0.3, 0.4])
|
|
binary_action = (raw_action > thresholds).astype(jnp.float32)
|
|
|
|
|
|
both_active = jnp.logical_and(binary_action[:, 0] > 0, binary_action[:, 1] > 0)
|
|
prefer_left = raw_action[:, 0] > raw_action[:, 1]
|
|
|
|
|
|
binary_action = binary_action.at[:, 0].set(
|
|
jnp.where(both_active, prefer_left.astype(jnp.float32), binary_action[:, 0])
|
|
)
|
|
binary_action = binary_action.at[:, 1].set(
|
|
jnp.where(both_active, (~prefer_left).astype(jnp.float32), binary_action[:, 1])
|
|
)
|
|
|
|
|
|
master_key, step_key = random.split(master_key)
|
|
next_state, reward, done = env.step(state, binary_action)
|
|
|
|
|
|
if isinstance(reward, jnp.ndarray):
|
|
reward = float(jnp.reshape(reward, (-1,))[0])
|
|
if isinstance(done, jnp.ndarray):
|
|
done = bool(jnp.reshape(done, (-1,))[0])
|
|
|
|
|
|
any_movement = jnp.any(binary_action[:, :2] > 0)
|
|
movement_reward = 0.1 if bool(any_movement) else 0.0
|
|
|
|
|
|
ball_height = float(jnp.reshape(next_state.obs[1], (-1,))[0]) if hasattr(next_state.obs, '__getitem__') else 0.0
|
|
height_reward = 0.1 if ball_height > 0.5 else 0.0
|
|
|
|
|
|
ball_x = float(jnp.reshape(next_state.obs[4], (-1,))[0])
|
|
ball_vx = float(jnp.reshape(next_state.obs[6], (-1,))[0])
|
|
position_reward = 0.2 if ball_x > 0 else 0.0
|
|
velocity_reward = 0.1 if ball_vx > 0 else 0.0
|
|
|
|
|
|
step_reward = reward * 2.0
|
|
bonus_reward = movement_reward + height_reward + position_reward + velocity_reward
|
|
total_step_reward = step_reward + bonus_reward * 0.5
|
|
|
|
episode_reward += total_step_reward
|
|
state = next_state
|
|
steps += 1
|
|
|
|
|
|
if done and reward > 0:
|
|
episode_reward += 10.0
|
|
|
|
total_reward += episode_reward
|
|
|
|
return total_reward / n_episodes
|
|
|
|
def main():
|
|
|
|
env = SlimeVolley()
|
|
|
|
|
|
config = {
|
|
'population_size': 50,
|
|
'weight_mutation_rate': 0.8,
|
|
'weight_mutation_power': 0.3,
|
|
'add_node_rate': 0.3,
|
|
'add_connection_rate': 0.5,
|
|
}
|
|
|
|
|
|
population = [
|
|
Network(Genome(n_inputs=12, n_outputs=3))
|
|
for _ in range(config['population_size'])
|
|
]
|
|
|
|
best_fitness = float('-inf')
|
|
generations_without_improvement = 0
|
|
|
|
|
|
for generation in range(500):
|
|
print(f"\nGeneration {generation}")
|
|
print("-" * 20)
|
|
|
|
|
|
fitnesses = []
|
|
for i, net in enumerate(population):
|
|
fitness = evaluate_network(net, env)
|
|
fitnesses.append(fitness)
|
|
print(f"Network {i}: Fitness = {fitness:.2f}")
|
|
|
|
if fitness > best_fitness:
|
|
best_fitness = fitness
|
|
generations_without_improvement = 0
|
|
print(f"New best fitness: {best_fitness:.2f}")
|
|
|
|
|
|
generations_without_improvement += 1
|
|
if generations_without_improvement > 20:
|
|
print("No improvement for 20 generations, increasing mutation rates")
|
|
config['weight_mutation_rate'] = min(1.0, config['weight_mutation_rate'] * 1.2)
|
|
config['weight_mutation_power'] = min(0.5, config['weight_mutation_power'] * 1.2)
|
|
generations_without_improvement = 0
|
|
|
|
|
|
avg_fitness = sum(fitnesses) / len(fitnesses)
|
|
print(f"\nBest fitness: {best_fitness:.2f}")
|
|
print(f"Average fitness: {avg_fitness:.2f}")
|
|
|
|
|
|
new_population = []
|
|
sorted_indices = np.argsort(fitnesses)[::-1]
|
|
|
|
|
|
n_elite = 5
|
|
new_population.extend([population[i] for i in sorted_indices[:n_elite]])
|
|
print(f"Keeping top {n_elite} networks")
|
|
|
|
|
|
while len(new_population) < config['population_size']:
|
|
|
|
tournament_size = 5
|
|
tournament = np.random.choice(sorted_indices[:20], tournament_size, replace=False)
|
|
parent_idx = tournament[np.argmax([fitnesses[i] for i in tournament])]
|
|
parent = population[parent_idx]
|
|
|
|
|
|
child_genome = Genome(12, 3)
|
|
child_genome.node_genes = parent.genome.node_genes.copy()
|
|
child_genome.connection_genes = parent.genome.connection_genes.copy()
|
|
|
|
|
|
child_genome.mutate(config)
|
|
|
|
|
|
new_population.append(Network(child_genome))
|
|
|
|
population = new_population
|
|
print(f"Created {len(population)} networks for next generation")
|
|
|
|
if __name__ == '__main__':
|
|
main() |