private Evolution initEvolution(ProgressReport pReport) { var e = new Evolution(); e.Iteration = pReport.Iteration; e.MaxFitness = pReport.IterationStatistics.MaximumFitness; e.AvgFitness = pReport.IterationStatistics.AverageFitness; e.Status = pReport.IterationStatus; // return(e); }
//default constructor public Factory() { m_progresReport = new ProgressReport() { BestIteration = -1, BestSolution = null, Iteration = -1, IterationStatus = IterationStatus.Initialize, IterationStatistics = new IterationStatistics() { AverageFitness = -1, MaximumFitness = -1 } }; }
/// <summary> /// Constructor /// </summary> /// <param name="param"></param> /// <param name="funSet"></param> /// <param name="terSet"></param> public Factory(Parameters param, Function[] funSet, int[] terSet, CancellationToken token) { //create population m_Population = new Population(); m_Population.Token = token; m_History = new List <Evolution>(); //prepare progress report for the first time. m_progresReport = new ProgressReport(); m_progresReport.Iteration = 0; m_progresReport.IterationStatus = IterationStatus.Initialize; m_progresReport.IterationStatistics = new IterationStatistics(); m_progresReport.Message = "GPFactory successfully created algorithm."; m_progresReport.BestSolution = new Chromosome(); m_progresReport.IterationStatistics.IterationSeconds = 0; //initialize main GP components parameters, function and terminals initMainComponents(param, funSet, terSet); }
/// <summary> /// Calculate best chromosome in the current population (generation) /// </summary> /// <param name="chromosomes"></param> /// <param name="pr"></param> private void calculatePopulation(List <IChromosome> chromosomes, ProgressReport pr) { // calculate basic stat of the population float fitnessMax = float.MinValue; float fitnessSum = 0; if (chromosomes != null && chromosomes.Count > 0) { ProgresReport.BestSolution = setBestSolution(chromosomes[0]); } else { return; } int counter = 0; for (int i = 0; i < chromosomes.Count; i++) { if (float.IsNaN(chromosomes[i].Fitness)) { continue; } counter++; float fitness = chromosomes[i].Fitness; // sum calculation fitnessSum += fitness; // cal the best chromosome if (fitness > fitnessMax) { fitnessMax = fitness; pr.BestSolution = setBestSolution(chromosomes[i]); } } float fitnessAvg = (float)Math.Round(fitnessSum / counter, 4); pr.IterationStatistics.AverageFitness = fitnessAvg; pr.IterationStatistics.MaximumFitness = fitnessMax; }
/// <summary> /// perform one iteration evolution for the GP /// </summary> /// <param name="pr"></param> private void performIteration(ProgressReport pr, CancellationToken ct) { //mating preparation offspring population m_Population.PrepareForMating(); Stopwatch sw = Stopwatch.StartNew(); string str = $"It={pr.Iteration}; "; //1. operaton m_Population.Crossover(m_Parameters, m_FunctionSet, m_TerminalSet); str += $"Crossover ={sw.Elapsed.TotalSeconds.ToString("F3")} sec| "; sw.Restart(); //2. operation m_Population.Mutate(m_Parameters, m_FunctionSet, m_TerminalSet); str += $"Mutation ={sw.Elapsed.TotalSeconds.ToString("F3")} sec| "; sw.Restart(); //3. operation m_Population.Reproduction(m_Parameters, m_FunctionSet, m_TerminalSet); str += $"Reproduction ={sw.Elapsed.TotalSeconds.ToString("F3")} sec| "; sw.Restart(); //4. operation m_Population.EvaluatePopulation(m_Parameters, pr.Iteration); str += $"FitnessCalc ={sw.Elapsed.TotalSeconds.ToString("F3")} sec| "; sw.Restart(); // m_Population.CreateNewGeneration(m_Parameters, m_FunctionSet, m_TerminalSet); str += $"NewGen ={sw.Elapsed.TotalSeconds.ToString("F3")} sec| "; sw.Restart(); // calculatePopulation(m_Population.m_Chromosomes, pr); str += $"Calculation ={sw.Elapsed.TotalSeconds.ToString("F3")} sec| "; pr.Message = str; }
public void GPFactoryFromString(string strFactory) { if (string.IsNullOrEmpty(strFactory)) { return; } try { Evolution[] hist = null; JsonSerializerSettings sett = new JsonSerializerSettings(); sett.NullValueHandling = NullValueHandling.Ignore; var obj = JsonConvert.DeserializeObject(strFactory, sett); var param = ((dynamic)obj)["m_Parameters"] as Newtonsoft.Json.Linq.JObject; var ter = ((dynamic)obj)["m_TerminalSet"] as Newtonsoft.Json.Linq.JArray; var fun = ((dynamic)obj)["m_FunctionSet"] as Newtonsoft.Json.Linq.JArray; var prog = ((dynamic)obj)["m_progresReport"] as Newtonsoft.Json.Linq.JObject; var tc = ((dynamic)obj)["m_TerminationCriteria"] as Newtonsoft.Json.Linq.JObject; var hi = ((dynamic)obj)["history"] as Newtonsoft.Json.Linq.JArray; var pop = ((dynamic)obj)["popStr"] as Newtonsoft.Json.Linq.JValue; if (param != null) { m_Parameters = param.ToObject <Parameters>(); } if (ter != null) { m_TerminalSet = ter.ToObject <int[]>(); } if (fun != null) { m_FunctionSet = fun.ToObject <Function[]>(); } if (prog != null) { m_progresReport = prog.ToObject <ProgressReport>(); } if (tc != null) { m_TerminationCriteria = tc.ToObject <TerminationCriteria>(); } if (hi != null) { hist = hi.ToObject <Evolution[]>(); } //create history if (hist != null) { m_History = new List <Evolution>(hist); } m_Population = new Population(); if (pop != null) { var popStr = pop.ToObject <string>(); m_Population.PopulationFromString(popStr); } } catch (Exception) { throw; } }