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); } }
/// <summary> /// Obtains the prototype of a randomly slected species. By evaluating /// diffrent species, and not just the best preforming organism, we can /// tell if the popluation is remaning homogonous or diversifying. /// </summary> /// <param name="container">A container to store the genome</param> public void GetRandProto(T container) { int index = rng.RandInt(species.Count); T proto = species[index].Prototype.Genome; container.Overwrite(proto); }
/// <summary> /// Helper method: Selects a single nuron from the network at random. /// </summary> /// <returns>A randomly selected nuron</returns> private NuronOld RandNuron() { //generates a random nuron in O(n) int index = rng.RandInt(nurons.Count); return(nurons.ElementAt(index).Item); }
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; }
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); }
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); } }
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 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 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(); }