private void LogData(NsgaLog log, List <List <Individual> > fronts, int iteration) { log.FrontsNumberLog.Add(fronts.Count); log.FirstFrontSizeLog.Add(fronts.First().Count); var individuals = fronts.SelectMany(f => f).ToList(); var crowdingDistances = individuals.Select(ind => ind.CrowdingDistance).Where(dist => !double.IsInfinity(dist)).ToList(); if (crowdingDistances.Any()) { log.CrowdingDistanceVar.Add(crowdingDistances.Variance()); log.CrowdingDistanceAvg.Add(crowdingDistances.Average()); } else { log.CrowdingDistanceVar.Add(0); log.CrowdingDistanceAvg.Add(0); } var feasibleRatio = (double)individuals.Where(i => i.IsFeasible).ToList().Count / individuals.Count; log.FeasibleSolutions.Add(feasibleRatio); var evaluiationTypes = individuals.First().Evaluations.Select(e => e.Type); foreach (var evaluiationType in evaluiationTypes) { log.ObjectiveLogs.Add(GetFrontObjectiveLog(individuals, evaluiationType, iteration)); } }
public NsgaResult Solve() { var log = new NsgaLog(); var watch = Stopwatch.StartNew(); var individuals = InitializeIndividuals(); _evaluator.Evaluate(individuals); var fronts = _sorter.Sort(individuals).ToList(); for (var iteration = 0; iteration < _config.MaxIterations; iteration++) { AssignCrowdingDistance(fronts); //Create Offspring individuals.AddRange(CreateOffspring(individuals)); //sort fronts = _sorter.Sort(individuals); AssignCrowdingDistance(fronts); //select next Gen fronts = SelectNextGeneration(fronts); individuals = fronts.SelectMany(f => f).ToList(); LogData(log, fronts, iteration); Debug.WriteLine($"iteration {iteration}"); if (individuals.Count != _config.PopulationSize) { throw new ApplicationException($"Population size is {individuals.Count} instead of {_config.PopulationSize}"); } } _evaluator.Evaluate(individuals); fronts = _sorter.Sort(individuals).ToList(); watch.Stop(); log.SolvingTime = (int)watch.ElapsedMilliseconds; return(new NsgaResult { Log = log, Fronts = fronts }); }