static void Main(string[] args) { Stopwatch watch = Stopwatch.StartNew(); Population p = new Population(); int g = 1; while (true) { Console.WriteLine("Genaration:{0} Distance:{1}", g, p.BestIndivid.Deviation); Console.WriteLine(ProgramText.GetProgramCode(p.BestIndivid.Gene)); p.LiveCycle(); g++; // Check if the solution is found if (p.BestIndivid.Deviation == 0) { break; } } watch.Stop(); Console.WriteLine("Genaration:{0} Distance:{1}", g, p.BestIndivid.Deviation); Console.WriteLine("Elapsed time {0} seconds", watch.ElapsedMilliseconds / 1000); Console.WriteLine("The following code was built:"); Console.WriteLine(ProgramText.GetProgramCode(p.BestIndivid.Gene)); Console.ReadLine(); }
// Creates individual with random gene, that is compiled successfully //public static Individ CreateIndivid() //{ // Individ ind = new Individ(); // CompilerResults results = null; // // Loop until compiled // while (true) // { // // Get code sample with randomly built function // sProgramCodeFinal = ProgramText.GetProgramCode(ind.Gene); // // Try to compile // results = provider.CompileAssemblyFromSource(parameters, sProgramCodeFinal); // if (results.Errors.HasErrors) // { // ind = new Individ(); // continue; // } // break; // } // // Get access to the compiled function // Assembly assembly = results.CompiledAssembly; // Type program = assembly.GetType("Program.WorkingClass"); // MethodInfo main = program.GetMethod("F"); // int[] f_results = new int[TrainingDataBack.a_array.Length]; // // Calculate function values with training data // for (int itry = 0; itry < TrainingDataBack.a_array.Length; itry++) // { // InvokeParams[0] = TrainingDataBack.a_array[itry]; // InvokeParams[1] = TrainingDataBack.b_array[itry]; // f_results[itry] = Convert.ToInt32(main.Invoke(null, InvokeParams)); // } // // Calculate deviation with the training data at the place // ind.CalculateDeviation(f_results); // return ind; //} // // Creates individual with two parent's genes and possible mutation public static Individ CreateIndivid(Individ i1 = null, Individ i2 = null, bool mutate = true) { Individ ind = new Individ(i1, i2, mutate); CompilerResults results = null; // Loop until compiled while (true) { // Get code sample with randomly built function sProgramCodeFinal = ProgramText.GetProgramCode(ind.Gene); // Try to compile results = provider.CompileAssemblyFromSource(parameters, sProgramCodeFinal); if (results.Errors.HasErrors) { ind = new Individ(i1, i2, true); continue; } break; } // Get access to the compiled function Assembly assembly = results.CompiledAssembly; Type program = assembly.GetType("Program.WorkingClass"); MethodInfo main = program.GetMethod("F"); int[] f_results = new int[TrainingDataBack.a_array.Length]; // Calculate function values with training data for (int itry = 0; itry < TrainingDataBack.a_array.Length; itry++) { InvokeParams[0] = TrainingDataBack.a_array[itry]; InvokeParams[1] = TrainingDataBack.b_array[itry]; f_results[itry] = Convert.ToInt32(main.Invoke(null, InvokeParams)); } ind.CalculateDeviation(f_results); return(ind); }