public static IInd CreateIndividual() { string val = ""; for (int i = 0; i < 20; i++) { val += Rand.Next(0, 2); } StringInd res = new StringInd(val); return res; }
public static IInd Mutation(IInd inpStringInd, double rate) { Char[] inpBits = inpStringInd.ToCharArray(); for (int i = 0; i < inpBits.Length; i++) { var r = Rand.NextDouble(); if (r <= rate) { switch (inpBits[i]) { case '0': inpBits[i] = '1'; break; case '1': inpBits[i] = '0'; break; } } } IInd res = new StringInd(new string(inpBits)); return res; }
public static Tuple<IInd, IInd> Crossover(Tuple<IInd, IInd> inp) { Char[] p1Bits = inp.Item1.ToCharArray(); Char[] p2Bits = inp.Item2.ToCharArray(); Char[] c1Bits = new Char[p1Bits.Length]; Char[] c2Bits = new Char[p1Bits.Length]; var splitpoint = Rand.Next(1, p1Bits.Length-1); p1Bits.Take(splitpoint).ToArray().CopyTo(c1Bits, 0); p2Bits.Skip(splitpoint).ToArray().CopyTo(c1Bits, splitpoint); p2Bits.Take(splitpoint).ToArray().CopyTo(c2Bits, 0); p1Bits.Skip(splitpoint).ToArray().CopyTo(c2Bits, splitpoint); IInd child1 = new StringInd(new string(c1Bits)); IInd child2 = new StringInd(new string(c2Bits)); return new Tuple<IInd, IInd>(child1, child2); }