Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func reproduce(new_agent : Agent, agent1 : Agent, agent2 : Agent):
- randomize()
- var average_fitness = (agent1.fitness + agent2.fitness)/2
- var drastic_mutations : bool = false
- var drastic_percentage_mutations : bool = false
- var mutation_amount : float = 15.0
- var mutation_percentage_amount : float = randf_range(1.0, 2.0)
- if agent1.failed or agent2.failed:
- drastic_mutations = true
- drastic_percentage_mutations = true
- if agent1.worse_than_previous_generation or agent2.worse_than_previous_generation:
- drastic_mutations = true
- drastic_percentage_mutations = true
- if drastic_mutations:
- mutation_amount = randf_range(0.0, 99999.99)
- mutation_percentage_amount = randf_range(1.0, 5.0)
- var x = Node2D.new()
- x.name = "Neurons"
- for i in agent1.neurons.size():
- var n = Neuron.new()
- n.name = "Neuron %d" % [i]
- if randi_range(0, 10) > 5:
- n.weights = agent1.neurons[i].weights
- else:
- n.weights = agent2.neurons[i].weights
- for weight in n.weights:
- if randi_range(0, 100) > 30:
- weight *= randf_range(-mutation_percentage_amount, mutation_percentage_amount)
- if randi_range(0, 10) > 5:
- n.bias = agent1.neurons[i].bias + randf_range(-mutation_amount, mutation_amount)
- else:
- n.bias = agent2.neurons[i].bias + randf_range(-mutation_amount, mutation_amount)
- if randi_range(0, 10) > 5:
- n.threshold = agent1.neurons[i].threshold + randf_range(-mutation_amount, mutation_amount)
- else:
- n.threshold = agent2.neurons[i].threshold + randf_range(-mutation_amount, mutation_amount)
- x.add_child(n)
- new_agent.neurons.append(n)
- new_agent.add_child(x)
- var y = Node2D.new()
- y.name = "Output Neurons"
- for i in agent1.output_neurons.size():
- var n = Neuron.new()
- n.name = "Neuron %d" % [i]
- if randi_range(0, 10) > 5:
- n.weights = agent1.output_neurons[i].weights
- else:
- n.weights = agent2.output_neurons[i].weights
- for weight in n.weights:
- if randi_range(0, 100) > 80:
- weight += randf_range(-mutation_amount, mutation_amount)
- if randi_range(0, 10) > 5:
- n.bias = agent1.output_neurons[i].bias + randf_range(-mutation_amount, mutation_amount)
- else:
- n.bias = agent2.output_neurons[i].bias + randf_range(-mutation_amount, mutation_amount)
- if randi_range(0, 10) > 5:
- n.threshold = agent1.output_neurons[i].threshold + randf_range(-mutation_amount, mutation_amount)
- else:
- n.threshold = agent2.output_neurons[i].threshold + randf_range(-mutation_amount, mutation_amount)
- y.add_child(n)
- new_agent.output_neurons.append(n)
- new_agent.add_child(y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement