public override object Clone(CloneInfo info) { object res = Utils.Eval(info.StackFrame, Expression); var list = res as IEnumerable; var splice = new Splice(list); return(splice); }
private static int CalculerProximite(Splice mot, Splice dico, int max = Int32.MaxValue) { if (max < 0) { return(max); } if (dico.Length == 0) { return(Math.Min(max, mot.Length * 2)); } if (mot.Length == 0) { return(Math.Min(max, dico.Length * 2)); } if (mot[0] == dico[0]) { return(CalculerProximite(mot.Substring(1), dico.Substring(1))); } int best = max; var remplacement = 3 + CalculerProximite(mot.Substring(1), dico.Substring(1), best - 3); if (remplacement < best) { best = remplacement; } var suppression1 = 2 + CalculerProximite(mot.Substring(1), dico, best - 2); if (suppression1 < best) { best = suppression1; } var suppression2 = 2 + CalculerProximite(mot, dico.Substring(1), best - 2); if (suppression2 < best) { best = suppression2; } if (dico.Length >= 2 && mot.Length >= 2) { if (dico[0] == mot[1] && dico[1] == mot[0]) { var inversion = 3 + CalculerProximite(mot.Substring(2), dico.Substring(2), best - 3); if (inversion < best) { best = inversion; } } } return(best); }
/// <summary> /// Demonstrate the crossover splice operator. Two offspring will be created by swapping three /// segments of the parents (two cut points). Some genes may repeat. /// </summary> public static void Splice() { Console.WriteLine("Crossover Splice"); // Create a random number generator IGenerateRandom rnd = new MersenneTwisterGenerateRandom(); // Create a new population. IPopulation pop = new BasicPopulation(); pop.GenomeFactory = new IntegerArrayGenomeFactory(10); // Create a trainer with a very simple score function. We do not care // about the calculation of the score, as they will never be calculated. IEvolutionaryAlgorithm train = new BasicEA(pop, new NullScore()); // Create a splice operator, length = 5. Use it 1.0 (100%) of the time. var opp = new Splice(5); train.AddOperation(1.0, opp); // Create two parents, the genes are set to 1,2,3,4,5,7,8,9,10 // and 10,9,8,7,6,5,4,3,2,1. var parents = new IntegerArrayGenome[2]; parents[0] = (IntegerArrayGenome)pop.GenomeFactory.Factor(); parents[1] = (IntegerArrayGenome)pop.GenomeFactory.Factor(); for (int i = 1; i <= 10; i++) { parents[0].Data[i - 1] = i; parents[1].Data[i - 1] = 11 - i; } // Create an array to hold the offspring. var offspring = new IntegerArrayGenome[2]; // Perform the operation opp.PerformOperation(rnd, parents, 0, offspring, 0); // Display the results Console.WriteLine("Parent 1: " + string.Join(",", parents[0].Data)); Console.WriteLine("Parent 2: " + string.Join(",", parents[1].Data)); Console.WriteLine("Offspring 1: " + string.Join(",", offspring[0].Data)); Console.WriteLine("Offspring 2: " + string.Join(",", offspring[1].Data)); }
public void TestGlue() { List <double> BListBBuf = new List <double>(); List <double> BListBFile = new List <double>(); List <double> Testi = new List <double>(); List <double> Testin = new List <double>(); BListBBuf.Add(1.1148); BListBFile.Add(1.1146); Splice Test = new Splice(); Testi = Test.glue(BListBFile, BListBBuf, 1, 1); Testin.Add(1.1146); Testin.Add(1.1148); Assert.AreEqual(Testin[0], Testi[0]); Assert.AreEqual(Testin[1], Testi[1]); BListBBuf.Add(1.1142); BListBFile.Add(1.1141); Testi = Test.glue(BListBFile, BListBBuf, 2, 2); List <double> Testin1 = new List <double>(); Testin1.Add(1.1146); Testin1.Add(1.1141); Testin1.Add(1.1148); Testin1.Add(1.1142); Assert.AreEqual(Testin1[0], Testi[0]); Assert.AreEqual(Testin1[2], Testi[2]); Assert.AreEqual(Testin1[3], Testi[3]); Assert.AreEqual(4, Testi.Count); Testi = Test.glue(BListBBuf, BListBFile); Assert.AreEqual(3, Testi.Count); Assert.AreEqual(Testin1[0], Testi[0]); Assert.AreEqual(Testin1[3], Testi[2]); }
//多组合并 public void Merge(Splice group) { _str.Append(group._str); }