void Mutate() { int n = Random.Range(0, 6); switch (n) { case 0: //Mutation: Add a Gene and its connections AddGene(); break; //case 0: // //Mutation: Remove Gene and its connections // break; case 1: //Mutation: Add simple Connection Gene i, o; float w = Random.Range(-1f, 1f); //Select a gene from input genes i = genes[Random.Range(0, numberInput)]; //Select the next gene (output or middle one) //(The last gene created is excluded since it make no sense to be connected to itself) o = genes[Random.Range(numberInput, counterGenes)]; ConnectionGene c = population.GetConnection(i, o, w); connections.Add(c.GetID(), c); break; case 2: //RemoveConnection(connections[Random.Range(0,connections.Count)]); break; case 3: //Mutation: Change weigths for (int j = 0; j < connections.Count / 2; j++) { w = connections[j].GetWeight(); connections[j].SetWeight(w + mutationFactor * Random.Range(-1, -1)); } break; case 4: //Mutation: toogle enabled //connections[Random.Range(0, connections.Count)].ChangeEnabled(); break; case 5: //Mutation: Change activation function int func = Random.Range(0, 1); genes[Random.Range(0, genes.Count)].SetActivationFunction(func); break; default: break; } }
/// <summary> /// Add a specific connection between from Gene and to Gene with a given weight /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <param name="weight"></param> public void AddConnection(Gene from, Gene to, float weight) { ConnectionGene c = population.GetConnection(from, to, weight); //if (!from.HasConnection(c)) { from.AddConnection(c); to.AddInput(); connections.Add(c.GetID(), c); } }
public void RemoveConnection(ConnectionGene connection) { connections.Remove(connection.GetID()); connection = null;//not best solution, can bring to memory leaks }