public void Crossover(VRandom rng, NetworkComp genome) { //determins the rate of crossover double rate = rng.RandDouble(0.25, 0.75); //calculates the maximum number of nurons and axons //this is nessary incase the genomes have diffrent numbers int nmax = Math.Min(this.NuronCount, genome.NuronCount); int amax = Math.Min(this.AxonCount, genome.AxonCount); for (int i = 0; i < nmax; i++) { if (rng.NextDouble() > rate) { continue; } NuronComp copy = genome.nurons[i]; nurons[i].Funciton = copy.Funciton; } for (int i = 0; i < amax; i++) { if (rng.NextDouble() > rate) { continue; } //NOTE: should we copy the entier axon or just the weight? AxonComp copy = genome.axons[i]; axons[i].Weight = copy.Weight; axons[i].Enabled = copy.Enabled; axons[i].Input = copy.Input; axons[i].Output = copy.Output; } throw new NotImplementedException(); }
//In this method we assume a standard mutation rate of 1.0; public void Mutate(VRandom rng, double rate) { //makes shure the rate is positive rate = Math.Abs(rate); double selector = rng.NextDouble(); if (selector < P_EXPAND) { //expands the network Expand(rng); } else if (selector < P_TOGGEL) { Axon ax = GetRandomAxon(rng); if (ax.Enabled) { //outright disables the nuron ax.Enabled = false; } else { //resets the neuron to a large weight ax.Weight = rng.RandGauss() * SD_NEW; ax.Enabled = true; } } else { Axon ax = GetRandomAxon(rng); if (ax.Enabled) { //permutes the weight by a small amount double delta = rng.RandGauss() * SD_SHIFT; ax.Weight = ax.Weight + delta; } else { //resets the nuron to a small weight double delta = rng.RandGauss() * SD_SHIFT; ax.Weight = delta; ax.Enabled = true; } } }
/// <summary> /// Combines the genes of the curent nural net with the genes of /// another network to create a brand new offspring. The idea is /// that the child network will possess trates from both its /// parents, similar to sexual reproduction. /// </summary> /// <param name="rng">Random number generator</param> /// <param name="mate">Mate of the curent network</param> /// <returns>The child of both networks</returns> public NetworkCPP Combine(VRandom rng, NetworkCPP mate) { //makes a clone of the dominate parent NetworkCPP child = new NetworkCPP(rng, this); //determins weather or not to do liniar crossover bool liniar = rng.RandBool(P_Linear); double a = rng.NextDouble(); //lists all the axons in the mate var axons = mate.ListAxons(); foreach (Axon ax in axons) { //obtains the matching child axon Axon axc = child.FindMatch(ax); if (axc == null) { continue; } if (liniar) { //chooses a value between the two weights double weight = axc.Weight * (1.0 - a); axc.Weight = weight + (ax.Weight * a); } else { //determins the new weight based on crossover bool cross = rng.RandBool(); if (cross) { axc.Weight = ax.Weight; } } //has a chance of enabling if either are disabled bool en = ax.Enabled && axc.Enabled; axc.Enabled = en || rng.RandBool(0.25); } return(child); }
public void Crossover(VRandom rng, NetworkAuto genome) { //throw new NotImplementedException(); //determins weather or not to do liniar crossover bool liniar = rng.RandBool(P_Linear); double a = rng.NextDouble(); //lists all the axons and nurons in the mate var ittr1 = genome.nurons.ListItems(); var ittr2 = genome.axons.ListItems(); foreach (Nuron n in ittr1) { //obtains the matching child nuron Nuron nc = nurons.GetValue(n.Index); if (nc == null) { //adds the missing nuron nc = new Nuron(this, n); nurons.Add(nc.Index, nc); } //else //{ // //determins the activation based on crossover // bool cross = rng.RandBool(); // if (cross) nc.Func = n.Func; //} } foreach (Axon ax in ittr2) { //obtains the matching child axon Axon axc = axons.GetValue(ax.Index); if (axc == null) { //adds the missing axon, but disabled axc = new Axon(ax); axc.Enabled = false; axons.Add(axc.Index, axc); } else { if (liniar) { //chooses a value between the two weights double weight = axc.Weight * (1.0 - a); axc.Weight = weight + (ax.Weight * a); } else { //determins the new weight based on crossover bool cross = rng.RandBool(); if (cross) { axc.Weight = ax.Weight; } } //if the axon is present in both networks, it has a //strong chance of becoming enabled bool en = ax.Enabled && axc.Enabled; axc.Enabled = en || rng.RandBool(0.25); } } }