Advertisement
psychworld

Reproduction Function

Jun 2nd, 2025
1,806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func reproduce(new_agent : Agent, agent1 : Agent, agent2 : Agent):
  2.     randomize()
  3.     var average_fitness = (agent1.fitness + agent2.fitness)/2
  4.     var drastic_mutations : bool = false
  5.     var drastic_percentage_mutations : bool = false
  6.     var mutation_amount : float = 15.0
  7.     var mutation_percentage_amount : float = randf_range(1.0, 2.0)
  8.     if agent1.failed or agent2.failed:
  9.         drastic_mutations = true
  10.         drastic_percentage_mutations = true
  11.     if agent1.worse_than_previous_generation or agent2.worse_than_previous_generation:
  12.         drastic_mutations = true
  13.         drastic_percentage_mutations = true
  14.    
  15.    
  16.     if drastic_mutations:
  17.         mutation_amount = randf_range(0.0, 99999.99)
  18.         mutation_percentage_amount = randf_range(1.0, 5.0)
  19.     var x = Node2D.new()
  20.     x.name = "Neurons"
  21.     for i in agent1.neurons.size():
  22.         var n = Neuron.new()
  23.         n.name = "Neuron %d" % [i]
  24.         if randi_range(0, 10) > 5:
  25.             n.weights = agent1.neurons[i].weights
  26.         else:
  27.             n.weights = agent2.neurons[i].weights
  28.         for weight in n.weights:
  29.             if randi_range(0, 100) > 30:
  30.                 weight *= randf_range(-mutation_percentage_amount, mutation_percentage_amount)
  31.         if randi_range(0, 10) > 5:
  32.             n.bias = agent1.neurons[i].bias + randf_range(-mutation_amount, mutation_amount)
  33.         else:
  34.             n.bias = agent2.neurons[i].bias + randf_range(-mutation_amount, mutation_amount)
  35.         if randi_range(0, 10) > 5:
  36.             n.threshold = agent1.neurons[i].threshold + randf_range(-mutation_amount, mutation_amount)
  37.         else:
  38.             n.threshold = agent2.neurons[i].threshold + randf_range(-mutation_amount, mutation_amount)
  39.         x.add_child(n)
  40.         new_agent.neurons.append(n)
  41.     new_agent.add_child(x)
  42.        
  43.     var y = Node2D.new()
  44.     y.name = "Output Neurons"
  45.     for i in agent1.output_neurons.size():
  46.         var n = Neuron.new()
  47.         n.name = "Neuron %d" % [i]
  48.         if randi_range(0, 10) > 5:
  49.             n.weights = agent1.output_neurons[i].weights
  50.         else:
  51.             n.weights = agent2.output_neurons[i].weights
  52.         for weight in n.weights:
  53.             if randi_range(0, 100) > 80:
  54.                 weight += randf_range(-mutation_amount, mutation_amount)
  55.         if randi_range(0, 10) > 5:
  56.             n.bias = agent1.output_neurons[i].bias + randf_range(-mutation_amount, mutation_amount)
  57.         else:
  58.             n.bias = agent2.output_neurons[i].bias + randf_range(-mutation_amount, mutation_amount)
  59.         if randi_range(0, 10) > 5:
  60.             n.threshold = agent1.output_neurons[i].threshold + randf_range(-mutation_amount, mutation_amount)
  61.         else:
  62.             n.threshold = agent2.output_neurons[i].threshold + randf_range(-mutation_amount, mutation_amount)
  63.         y.add_child(n)
  64.         new_agent.output_neurons.append(n)
  65.     new_agent.add_child(y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement