/// <summary> /// Helper mehtod: Selects a pair of nurons at random from diffrent /// levels of the network. The nurons are always listed in order. It /// returns false if it was ubale to generate a valid pair. /// </summary> /// <param name="rng">A random number generator</param> /// <param name="n1">The lower level nuron</param> /// <param name="n2">The upper level nuron</param> /// <returns>True if a vaild pair was generated</returns> private bool GetRandomPair(VRandom rng, out Nuron n1, out Nuron n2) { //generates two random nurons n1 = GetRandomNuron(rng); n2 = GetRandomNuron(rng); int count = 0; Nuron n3 = null; //keeps searching while the nurons are on the same level while (n1.Level == n2.Level && count < MAX_TRY) { n3 = GetRandomNuron(rng); n1 = n2; n2 = n3; count++; } //swaps the nurons if they are out of order if (n1.Level > n2.Level) { n3 = n1; n1 = n2; n2 = n3; } //indicates if we found a valid pair return(n1.Level != n2.Level); }
public void Mutate1(VRandom rng, double rate) { if (rng.RandBool(P_Insert * rate)) { //selects a random character in the ASCII range int c = rng.RandInt(32, 127); mystr = mystr + (char)c; } else { //converts the string to a char array char[] temp = mystr.ToCharArray(); //increments or decrements a random character int index = rng.RandInt(0, temp.Length); bool add = rng.RandBool(); int c = (int)temp[index] + (add ? 1 : -1); //makes shure the resulting character is valid if (c < 32) { c = 32; } if (c > 126) { c = 126; } temp[index] = (char)c; //sets the new mutated string mystr = new String(temp); } }
public void Crossover(VRandom rng, GenString genome) { int len1 = this.mystr.Length; int len2 = genome.mystr.Length; int min = Math.Min(len1, len2); int cp = rng.RandInt(-min, min); string s1, s2; if (cp < 0) { //corrects for the negative index cp = min + cp; s1 = genome.mystr.Substring(0, cp); s2 = this.mystr.Substring(cp, len1 - cp); } else { s1 = this.mystr.Substring(0, cp); s2 = genome.mystr.Substring(cp, len2 - cp); } //concatinates the two substrings mystr = s1 + s2; }
//GG public override VInt3 RandomPointOnSurface() { // Find a random point inside the triangle // This generates uniformly distributed trilinear coordinates // See http://mathworld.wolfram.com/TrianglePointPicking.html /*float r1; * float r2; * * do { * r1 = Random.value; * r2 = Random.value; * } while (r1+r2 > 1);*/ //GG int r1; int r2; do { r1 = VRandom.Random(0, 1000); r2 = VRandom.Random(0, 1000); } while (r1 + r2 > 1000); var holder = GetNavmeshHolder(GraphIndex); // Pick the point corresponding to the trilinear coordinate //GG //return ((Vector3)(holder.GetVertex(v1)-holder.GetVertex(v0)))*r1 + ((Vector3)(holder.GetVertex(v2)-holder.GetVertex(v0)))*r2 + (Vector3)holder.GetVertex(v0); return(((holder.GetVertex(v1) - holder.GetVertex(v0))) * r1 / 1000 + ((holder.GetVertex(v2) - holder.GetVertex(v0))) * r2 / 1000 + holder.GetVertex(v0)); }
private Axon GetRandomAxon(VRandom rng) { //uses the RNG to select a random axon var list = axons.ListItems(); return(rng.RandElement(list)); }
/// <summary> /// Clones the current nural net with some random mutation of its /// genotpye. The rate of mutaiton determins how many of the network /// connections are preturbed. For exampe, a mutation rate of 0.5 /// indicates that half the weights will be perturbed. /// </summary> /// <param name="rng">Random number generator</param> /// <param name="rate">Rate of mutation</param> /// <returns>A mutated network</returns> public NetworkCPP Mutate(VRandom rng, double rate) { //clones a child and then mutates it var child = new NetworkCPP(rng, this); child.MutateSelf(rate); return(child); }
/// <summary> /// Creates a new nural net with more genes than its parent. This is /// diffrent from regular mutaiton, as the genotype becomes bigger, /// increasing the search space and opening new opertunites for /// diversification and improvment. /// </summary> /// <param name="rng">Random number generator</param> /// <returns>An organism with an expanded genotype</returns> public NetworkCPP Expand(VRandom rng) { //clones a child and then expands it var child = new NetworkCPP(rng, this); child.ExpandSelf(); return(child); }
//NOTE: Add an OnEvaluate event that trigers whenever the fitness funciton //is called. That way we display the progress between generations. public EvolMonogen(int popsize, double rate, Fitness <T> fitness) { //uses the Mersin Twister for RNG this.rng = new RandMT(); this.pop = new T[popsize]; this.rate = rate; this.fit = fitness; }
//GG //public override Vector3 RandomPointOnSurface () { public override VInt3 RandomPointOnSurface() { GridGraph gg = GridNode.GetGridGraph(GraphIndex); //GG //var graphSpacePosition = gg.transform.InverseTransform((Vector3)position); var graphSpacePosition = gg.transform.InverseTransform(position); //GG //return gg.transform.Transform(graphSpacePosition + new Vector3(Random.value - 0.5f, 0, Random.value - 0.5f)); return(gg.transform.Transform(graphSpacePosition + new VInt3(VRandom.Random(1, 1000) - 500, 0, VRandom.Random(1, 1000) - 500))); }
void Reset() { // Create a new random 64 bit value (62 bit actually because we skip negative numbers, but that's still enough by a huge margin) /*var rnd1 = (ulong)Random.Range(0, int.MaxValue); * var rnd2 = ((ulong)Random.Range(0, int.MaxValue) << 32);*/ //GG var rnd1 = (ulong)VRandom.Random(0, int.MaxValue); var rnd2 = ((ulong)VRandom.Random(0, int.MaxValue) << 32); uniqueID = rnd1 | rnd2; usedIDs[uniqueID] = this; }
public void Randomize(VRandom rng) { char[] temp = new char[mystr.Length]; for (int i = 0; i < temp.Length; i++) { //selects a random character in the ASCII range int c = rng.RandInt(32, 127); temp[i] = (char)c; } //sets the new random string mystr = new String(temp); }
public void Randomize(VRandom rng) { //used to list all the axons var ittr = axons.ListItems(); //sets the weight of each axon to a random value foreach (Axon ax in ittr) { if (!ax.Enabled) { continue; } ax.Weight = rng.RandGauss() * SD_NEW; } }
//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; } } }
//This is the (Old) One. It assumes a rate between 0.0 and 1.0 public void MutateAlt(VRandom rng, double rate) { //clamps the rate to be between zero and one rate = VMath.Clamp(rate); //expands the size of the network at random //if (rng.RandBool(P_EXPAND)) Expand(rng); if (rng.RandBool(rate)) { Expand(rng); } //used in itterating the structure var ittr1 = nurons.ListItems(); var ittr2 = axons.ListItems(); //foreach (Nuron n in ittr1) //{ // //skips over input nurons // if (n.IsInput) continue; // //mutates nodes based on the augmented mutation rate // if (!rng.RandBool(P_NODE * rate)) continue; // //updates the activation funciton // n.Func = GetRandomActivation(rng); //} foreach (Axon ax in ittr2) { if (rng.RandBool(P_TOGGEL * rate)) { //toggeles the enabled state ax.Enabled = !ax.Enabled; if (ax.Enabled) { ax.Weight = 0.0; } } if (ax.Enabled) { //permutes the weight by a small amount double delta = rng.RandGauss() * SD_NEW; ax.Weight = ax.Weight + (delta * rate); } } }
public void Mutate(VRandom rng, double rate) { if (rng.RandBool(P_Insert * rate)) { int index = rng.RandInt(1, mystr.Length); int len = mystr.Length; //splits the string at the instertion point string s1 = mystr.Substring(0, index); string s2 = mystr.Substring(index, len - index); //selects a random character in the ASCII range int c = rng.RandInt(32, 127); mystr = s1 + (char)c + s2; } //converts the string to a char array char[] temp = mystr.ToCharArray(); if (rng.RandBool(P_Swap * rate)) { //select two random indicies int i = rng.RandInt(0, temp.Length); int j = rng.RandInt(0, temp.Length); //swaps the characters at the two incicies char c = temp[i]; temp[i] = temp[j]; temp[j] = c; } for (int i = 0; i < temp.Length; i++) { //skips over characters with the inverse rate if (!rng.RandBool(rate)) { continue; } //selects a random character in the ASCII range int c = rng.RandInt(32, 127); temp[i] = (char)c; } //sets the new mutated string mystr = new String(temp); }
/// <summary> /// Clones a given network by make a deep copy of the internal /// structor, so that the clone may be manipulated without effecting /// the original. It also replaces the internal random number generator /// with a potentialy diffrent generator. /// </summary> /// <param name="rng">Random number generator</param> /// <param name="other">Network to clone</param> private NetworkCPP(VRandom rng, NetworkCPP other) { //copies the RNG by refrence this.rng = rng; //creates a new table with the same size int size = other.nurons.Buckets; nurons = new TableOpen <Int32, NuronOld>(size); //makes a deep copy of the former network's structor foreach (var pair in other.nurons) { NuronOld copy = new NuronOld(this, pair.Item); nurons.Add(pair.Key, copy); } }
/// <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 Mutate2(VRandom rng, double rate) { if (rng.RandBool(P_Insert * rate)) { //selects a random character in the ASCII range int c = rng.RandInt(32, 127); mystr = mystr + (char)c; } else { //converts the string to a char array char[] temp = mystr.ToCharArray(); for (int i = 0; i < temp.Length; i++) { //alters each character with a chance of 'rate' if (!rng.RandBool(rate)) { continue; } //increments or decrements the character bool add = rng.RandBool(); int c = (int)temp[i] + (add ? 1 : -1); //makes shure the resulting character is valid if (c < 32) { c = 32; } if (c > 126) { c = 126; } temp[i] = (char)c; } //sets the new mutated string mystr = new String(temp); } }
private int GetRandomIndex(VRandom rng) { int index = -1; int count = 0; while (index < 0 && count < MAX_TRY) { //chooses a ranom positive interger index = rng.RandInt(MAX_ID); //invalidates the index if we have a collision if (nurons.HasKey(index)) { index = -1; } count++; } return(index); }
private int species_target; // = 30; //10 public EvolSpecies(int popsize, double rate, Fitness <T> fitf) { this.rng = new RandMT(); this.fitf = fitf; this.rate = rate; this.pop = new Organism <T> [popsize]; this.species = new List <Species <T> >(popsize / 2); int death = (int)(popsize * DeathRate) + 1; this.deadpool = new Stack <Organism <T> >(death); this.babies = new HeepArray <Double, Organism <T> >(death, true); generation = 0; champ = 0; //species_target = (int)Math.Sqrt(popsize) + 1; species_target = 20; //20 }
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(); }
internal Organism <T> GetRandMember(VRandom rng) { //int index = rng.RandInt(members.Count); //int id = members[index]; //return pop.GetMember(id); //creates a vector to store the probablity distribution Vector pd = new Vector(members.Count); //uses the fitness of each member as the weight for (int i = 0; i < pd.Length; i++) { var mem = pop.GetMember(members[i]); pd[i] = mem.Fitness; } //returns a random member based on fitness int index = rng.SampleDesc(pd); int id = members[index]; return(pop.GetMember(id)); }
public void Mutate4(VRandom rng, double rate) { if (rng.RandBool(P_Insert * rate)) { //selects a random character in the ASCII range int c = rng.RandInt(32, 127); mystr = mystr + (char)c; } else { //converts the string to a char array char[] temp = mystr.ToCharArray(); //increments or decrements a random character int index = rng.RandInt(0, temp.Length); int c = rng.RandInt(32, 127); temp[index] = (char)c; //sets the new mutated string mystr = new String(temp); } }
private ActFunc GetRandomActivation(VRandom rng) { //generates a random nuber to select the fuciton int test = rng.RandInt(6); switch (test) { case 0: return(ActFunc.Identity); case 1: return(ActFunc.Sine); case 2: return(ActFunc.Cosine); case 3: return(ActFunc.Gaussian); case 4: return(ActFunc.Sigmoid); case 5: return(ActFunc.Sinc); } //we should never reach this point throw new NotImplementedException(); }
public void Mutate(VRandom rng, double rate) { throw new NotImplementedException(); }
public NetworkComp SpawnRandom(VRandom rng) { throw new NotImplementedException(); }
public void Randomize(VRandom rng) { throw new NotImplementedException(); }
public void Crossover(VRandom rng, CPPN genome) { network.Crossover(rng, genome.network); }
public void Mutate(VRandom rng, double rate) { network.Mutate(rng, rate); }
//public CPPN SpawnRandom(VRandom rng) //{ // //throw new NotImplementedException(); // var next = network.SpawnRandom(rng); // return new CPPN(next); //} public void Randomize(VRandom rng) { network.Randomize(rng); }