#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using HeuristicLab.Algorithms.MemPR.Interfaces;
using HeuristicLab.Analysis;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Random;
namespace HeuristicLab.Algorithms.MemPR {
[Item("MemPR Algorithm", "Base class for MemPR algorithms")]
[StorableClass]
public abstract class MemPRAlgorithm : BasicAlgorithm, INotifyPropertyChanged
where TProblem : class, IItem, ISingleObjectiveHeuristicOptimizationProblem
where TSolution : class, IItem
where TPopulationContext : MemPRPopulationContext, new()
where TSolutionContext : MemPRSolutionContext {
public override Type ProblemType {
get { return typeof(TProblem); }
}
public new TProblem Problem {
get { return (TProblem)base.Problem; }
set { base.Problem = value; }
}
public override bool SupportsPause {
get { return true; }
}
protected string QualityName {
get { return Problem != null && Problem.Evaluator != null ? Problem.Evaluator.QualityParameter.ActualName : null; }
}
public int? MaximumEvaluations {
get {
var val = ((OptionalValueParameter)Parameters["MaximumEvaluations"]).Value;
return val != null ? val.Value : (int?)null;
}
set {
var param = (OptionalValueParameter)Parameters["MaximumEvaluations"];
param.Value = value.HasValue ? new IntValue(value.Value) : null;
}
}
public TimeSpan? MaximumExecutionTime {
get {
var val = ((OptionalValueParameter)Parameters["MaximumExecutionTime"]).Value;
return val != null ? val.Value : (TimeSpan?)null;
}
set {
var param = (OptionalValueParameter)Parameters["MaximumExecutionTime"];
param.Value = value.HasValue ? new TimeSpanValue(value.Value) : null;
}
}
public double? TargetQuality {
get {
var val = ((OptionalValueParameter)Parameters["TargetQuality"]).Value;
return val != null ? val.Value : (double?)null;
}
set {
var param = (OptionalValueParameter)Parameters["TargetQuality"];
param.Value = value.HasValue ? new DoubleValue(value.Value) : null;
}
}
protected FixedValueParameter MaximumPopulationSizeParameter {
get { return ((FixedValueParameter)Parameters["MaximumPopulationSize"]); }
}
public int MaximumPopulationSize {
get { return MaximumPopulationSizeParameter.Value.Value; }
set { MaximumPopulationSizeParameter.Value.Value = value; }
}
public bool SetSeedRandomly {
get { return ((FixedValueParameter)Parameters["SetSeedRandomly"]).Value.Value; }
set { ((FixedValueParameter)Parameters["SetSeedRandomly"]).Value.Value = value; }
}
public int Seed {
get { return ((FixedValueParameter)Parameters["Seed"]).Value.Value; }
set { ((FixedValueParameter)Parameters["Seed"]).Value.Value = value; }
}
public IAnalyzer Analyzer {
get { return ((ValueParameter)Parameters["Analyzer"]).Value; }
set { ((ValueParameter)Parameters["Analyzer"]).Value = value; }
}
public IConstrainedValueParameter> SolutionModelTrainerParameter {
get { return (IConstrainedValueParameter>)Parameters["SolutionModelTrainer"]; }
}
public IConstrainedValueParameter> LocalSearchParameter {
get { return (IConstrainedValueParameter>)Parameters["LocalSearch"]; }
}
[Storable]
private TPopulationContext context;
public TPopulationContext Context {
get { return context; }
protected set {
if (context == value) return;
context = value;
OnPropertyChanged("State");
}
}
[Storable]
private BestAverageWorstQualityAnalyzer qualityAnalyzer;
[Storable]
private QualityPerClockAnalyzer qualityPerClockAnalyzer;
[Storable]
private QualityPerEvaluationsAnalyzer qualityPerEvaluationsAnalyzer;
[StorableConstructor]
protected MemPRAlgorithm(bool deserializing) : base(deserializing) { }
protected MemPRAlgorithm(MemPRAlgorithm original, Cloner cloner) : base(original, cloner) {
context = cloner.Clone(original.context);
qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
qualityPerClockAnalyzer = cloner.Clone(original.qualityPerClockAnalyzer);
qualityPerEvaluationsAnalyzer = cloner.Clone(original.qualityPerEvaluationsAnalyzer);
RegisterEventHandlers();
}
protected MemPRAlgorithm() {
Parameters.Add(new ValueParameter("Analyzer", "The analyzer to apply to the population.", new MultiAnalyzer()));
Parameters.Add(new FixedValueParameter("MaximumPopulationSize", "The maximum size of the population that is evolved.", new IntValue(20)));
Parameters.Add(new OptionalValueParameter("MaximumEvaluations", "The maximum number of solution evaluations."));
Parameters.Add(new OptionalValueParameter("MaximumExecutionTime", "The maximum runtime.", new TimeSpanValue(TimeSpan.FromMinutes(10))));
Parameters.Add(new OptionalValueParameter("TargetQuality", "The target quality at which the algorithm terminates."));
Parameters.Add(new FixedValueParameter("SetSeedRandomly", "Whether each run of the algorithm should be conducted with a new random seed.", new BoolValue(true)));
Parameters.Add(new FixedValueParameter("Seed", "The random number seed that is used in case SetSeedRandomly is false.", new IntValue(0)));
Parameters.Add(new ConstrainedValueParameter>("SolutionModelTrainer", "The object that creates a solution model that can be sampled."));
Parameters.Add(new ConstrainedValueParameter>("LocalSearch", "The local search operator to use."));
qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
qualityPerClockAnalyzer = new QualityPerClockAnalyzer();
qualityPerEvaluationsAnalyzer = new QualityPerEvaluationsAnalyzer();
RegisterEventHandlers();
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterEventHandlers();
}
private void RegisterEventHandlers() {
MaximumPopulationSizeParameter.Value.ValueChanged += MaximumPopulationSizeOnChanged;
}
private void MaximumPopulationSizeOnChanged(object sender, EventArgs eventArgs) {
if (ExecutionState == ExecutionState.Started || ExecutionState == ExecutionState.Paused)
throw new InvalidOperationException("Cannot change maximum population size before algorithm finishes.");
Prepare();
}
protected override void OnProblemChanged() {
base.OnProblemChanged();
qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
qualityAnalyzer.MaximizationParameter.Hidden = true;
qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
qualityAnalyzer.QualityParameter.Depth = 1;
qualityAnalyzer.QualityParameter.Hidden = true;
qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
var multiAnalyzer = Analyzer as MultiAnalyzer;
if (multiAnalyzer != null) {
multiAnalyzer.Operators.Clear();
if (Problem != null) {
foreach (var analyzer in Problem.Operators.OfType()) {
foreach (var param in analyzer.Parameters.OfType())
param.Depth = 1;
multiAnalyzer.Operators.Add(analyzer, analyzer.EnabledByDefault || analyzer is ISimilarityBasedOperator);
}
}
multiAnalyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
multiAnalyzer.Operators.Add(qualityPerClockAnalyzer, true);
multiAnalyzer.Operators.Add(qualityPerEvaluationsAnalyzer, true);
}
}
public override void Prepare() {
base.Prepare();
Results.Clear();
Context = null;
}
protected virtual TPopulationContext CreateContext() {
return new TPopulationContext();
}
public void StartSync() {
using (var w = new AutoResetEvent(false)) {
EventHandler handler = (sender, e) => {
if (ExecutionState == ExecutionState.Paused
|| ExecutionState == ExecutionState.Stopped)
w.Set();
};
ExecutionStateChanged += handler;
try {
Start();
w.WaitOne();
} finally { ExecutionStateChanged -= handler; }
}
}
protected sealed override void Run(CancellationToken token) {
if (Context == null) {
Context = CreateContext();
if (SetSeedRandomly) Seed = new System.Random().Next();
Context.Random.Reset(Seed);
Context.Scope.Variables.Add(new Variable("Results", Results));
Context.Problem = Problem;
}
if (MaximumExecutionTime.HasValue)
CancellationTokenSource.CancelAfter(MaximumExecutionTime.Value);
IExecutionContext context = null;
foreach (var item in Problem.ExecutionContextItems)
context = new Core.ExecutionContext(context, item, Context.Scope);
context = new Core.ExecutionContext(context, this, Context.Scope);
Context.Parent = context;
if (!Context.Initialized) {
// We initialize the population with two local optima
while (Context.PopulationCount < 2) {
var child = Create(token);
Context.LocalSearchEvaluations += HillClimb(child, token);
Context.LocalOptimaLevel += child.Fitness;
Context.AddToPopulation(child);
Context.BestQuality = child.Fitness;
Analyze(CancellationToken.None);
token.ThrowIfCancellationRequested();
if (Terminate()) return;
}
Context.LocalSearchEvaluations /= 2;
Context.LocalOptimaLevel /= 2;
Context.Initialized = true;
}
while (!Terminate()) {
Iterate(token);
Analyze(token);
token.ThrowIfCancellationRequested();
}
}
private void Iterate(CancellationToken token) {
var replaced = false;
ISingleObjectiveSolutionScope offspring = null;
offspring = Breed(token);
if (offspring != null) {
var replNew = Replace(offspring, token);
if (replNew) {
replaced = true;
Context.ByBreeding++;
}
}
offspring = Relink(token);
if (offspring != null) {
if (Replace(offspring, token)) {
replaced = true;
Context.ByRelinking++;
}
}
offspring = Delink(token);
if (offspring != null) {
if (Replace(offspring, token)) {
replaced = true;
Context.ByDelinking++;
}
}
offspring = Sample(token);
if (offspring != null) {
if (Replace(offspring, token)) {
replaced = true;
Context.BySampling++;
}
}
if (!replaced && offspring != null) {
if (Context.HillclimbingSuited(offspring.Fitness)) {
HillClimb(offspring, token, CalculateSubspace(Context.Population.Select(x => x.Solution)));
if (Replace(offspring, token)) {
Context.ByHillclimbing++;
replaced = true;
}
}
}
if (!replaced) {
var before = Context.Population.SampleRandom(Context.Random);
offspring = (ISingleObjectiveSolutionScope)before.Clone();
AdaptiveWalk(offspring, Context.LocalSearchEvaluations * 2, token);
if (!Eq(before, offspring))
Context.AddAdaptivewalkingResult(before, offspring);
if (Replace(offspring, token)) {
Context.ByAdaptivewalking++;
replaced = true;
}
}
Context.Iterations++;
}
protected void Analyze(CancellationToken token) {
IResult res;
if (!Results.TryGetValue("EvaluatedSolutions", out res))
Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
else ((IntValue)res.Value).Value = Context.EvaluatedSolutions;
if (!Results.TryGetValue("Iterations", out res))
Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
else ((IntValue)res.Value).Value = Context.Iterations;
if (!Results.TryGetValue("LocalSearch Evaluations", out res))
Results.Add(new Result("LocalSearch Evaluations", new IntValue(Context.LocalSearchEvaluations)));
else ((IntValue)res.Value).Value = Context.LocalSearchEvaluations;
if (!Results.TryGetValue("ByBreeding", out res))
Results.Add(new Result("ByBreeding", new IntValue(Context.ByBreeding)));
else ((IntValue)res.Value).Value = Context.ByBreeding;
if (!Results.TryGetValue("ByRelinking", out res))
Results.Add(new Result("ByRelinking", new IntValue(Context.ByRelinking)));
else ((IntValue)res.Value).Value = Context.ByRelinking;
if (!Results.TryGetValue("ByDelinking", out res))
Results.Add(new Result("ByDelinking", new IntValue(Context.ByDelinking)));
else ((IntValue)res.Value).Value = Context.ByDelinking;
if (!Results.TryGetValue("BySampling", out res))
Results.Add(new Result("BySampling", new IntValue(Context.BySampling)));
else ((IntValue)res.Value).Value = Context.BySampling;
if (!Results.TryGetValue("ByHillclimbing", out res))
Results.Add(new Result("ByHillclimbing", new IntValue(Context.ByHillclimbing)));
else ((IntValue)res.Value).Value = Context.ByHillclimbing;
if (!Results.TryGetValue("ByAdaptivewalking", out res))
Results.Add(new Result("ByAdaptivewalking", new IntValue(Context.ByAdaptivewalking)));
else ((IntValue)res.Value).Value = Context.ByAdaptivewalking;
var sp = new ScatterPlot("Breeding Correlation", "");
sp.Rows.Add(new ScatterPlotDataRow("Parent1 vs Offspring", "", Context.BreedingStat.Select(x => new Point2D(x.Item1, x.Item4))) { VisualProperties = { PointSize = 6 }});
sp.Rows.Add(new ScatterPlotDataRow("Parent2 vs Offspring", "", Context.BreedingStat.Select(x => new Point2D(x.Item2, x.Item4))) { VisualProperties = { PointSize = 6 } });
sp.Rows.Add(new ScatterPlotDataRow("Parent Distance vs Offspring", "", Context.BreedingStat.Select(x => new Point2D(x.Item3, x.Item4))) { VisualProperties = { PointSize = 6 } });
if (!Results.TryGetValue("BreedingStat", out res)) {
Results.Add(new Result("BreedingStat", sp));
} else res.Value = sp;
sp = new ScatterPlot("Relinking Correlation", "");
sp.Rows.Add(new ScatterPlotDataRow("A vs Relink", "", Context.RelinkingStat.Select(x => new Point2D(x.Item1, x.Item4))) { VisualProperties = { PointSize = 6 } });
sp.Rows.Add(new ScatterPlotDataRow("B vs Relink", "", Context.RelinkingStat.Select(x => new Point2D(x.Item2, x.Item4))) { VisualProperties = { PointSize = 6 } });
sp.Rows.Add(new ScatterPlotDataRow("d(A,B) vs Offspring", "", Context.RelinkingStat.Select(x => new Point2D(x.Item3, x.Item4))) { VisualProperties = { PointSize = 6 } });
if (!Results.TryGetValue("RelinkingStat", out res)) {
Results.Add(new Result("RelinkingStat", sp));
} else res.Value = sp;
sp = new ScatterPlot("Delinking Correlation", "");
sp.Rows.Add(new ScatterPlotDataRow("A vs Delink", "", Context.DelinkingStat.Select(x => new Point2D(x.Item1, x.Item4))) { VisualProperties = { PointSize = 6 } });
sp.Rows.Add(new ScatterPlotDataRow("B vs Delink", "", Context.DelinkingStat.Select(x => new Point2D(x.Item2, x.Item4))) { VisualProperties = { PointSize = 6 } });
sp.Rows.Add(new ScatterPlotDataRow("d(A,B) vs Offspring", "", Context.DelinkingStat.Select(x => new Point2D(x.Item3, x.Item4))) { VisualProperties = { PointSize = 6 } });
if (!Results.TryGetValue("DelinkingStat", out res)) {
Results.Add(new Result("DelinkingStat", sp));
} else res.Value = sp;
sp = new ScatterPlot("Sampling Correlation", "");
sp.Rows.Add(new ScatterPlotDataRow("AvgFitness vs Sample", "", Context.SamplingStat.Select(x => new Point2D(x.Item1, x.Item2))) { VisualProperties = { PointSize = 6 } });
if (!Results.TryGetValue("SampleStat", out res)) {
Results.Add(new Result("SampleStat", sp));
} else res.Value = sp;
sp = new ScatterPlot("Hillclimbing Correlation", "");
sp.Rows.Add(new ScatterPlotDataRow("Start vs Improvement", "", Context.HillclimbingStat.Select(x => new Point2D(x.Item1, x.Item2))) { VisualProperties = { PointSize = 6 } });
if (!Results.TryGetValue("HillclimbingStat", out res)) {
Results.Add(new Result("HillclimbingStat", sp));
} else res.Value = sp;
sp = new ScatterPlot("Adaptivewalking Correlation", "");
sp.Rows.Add(new ScatterPlotDataRow("Start vs Best", "", Context.AdaptivewalkingStat.Select(x => new Point2D(x.Item1, x.Item2))) { VisualProperties = { PointSize = 6 } });
if (!Results.TryGetValue("AdaptivewalkingStat", out res)) {
Results.Add(new Result("AdaptivewalkingStat", sp));
} else res.Value = sp;
Context.RunOperator(Analyzer, Context.Scope, token);
}
protected bool Replace(ISingleObjectiveSolutionScope child, CancellationToken token) {
if (double.IsNaN(child.Fitness)) {
Context.Evaluate(child, token);
Context.IncrementEvaluatedSolutions(1);
}
if (Context.IsBetter(child.Fitness, Context.BestQuality)) {
Context.BestQuality = child.Fitness;
Context.BestSolution = (TSolution)child.Solution.Clone();
}
var popSize = MaximumPopulationSize;
if (Context.Population.All(p => !Eq(p, child))) {
if (Context.PopulationCount < popSize) {
Context.AddToPopulation(child);
return true;// Context.PopulationCount - 1;
}
// The set of replacement candidates consists of all solutions at least as good as the new one
var candidates = Context.Population.Select((p, i) => new { Index = i, Individual = p })
.Where(x => x.Individual.Fitness == child.Fitness
|| Context.IsBetter(child, x.Individual)).ToList();
if (candidates.Count == 0) return false;// -1;
var repCand = -1;
var avgChildDist = 0.0;
var minChildDist = double.MaxValue;
var plateau = new List();
var worstPlateau = -1;
var minAvgPlateauDist = double.MaxValue;
var minPlateauDist = double.MaxValue;
// If there are equally good solutions it is first tried to replace one of those
// The criteria for replacement is that the new solution has better average distance
// to all other solutions at this "plateau"
foreach (var c in candidates.Where(x => x.Individual.Fitness == child.Fitness)) {
var dist = Dist(c.Individual, child);
avgChildDist += dist;
if (dist < minChildDist) minChildDist = dist;
plateau.Add(c.Index);
}
if (plateau.Count > 2) {
avgChildDist /= plateau.Count;
foreach (var p in plateau) {
var avgDist = 0.0;
var minDist = double.MaxValue;
foreach (var q in plateau) {
if (p == q) continue;
var dist = Dist(Context.AtPopulation(p), Context.AtPopulation(q));
avgDist += dist;
if (dist < minDist) minDist = dist;
}
var d = Dist(Context.AtPopulation(p), child);
avgDist += d;
avgDist /= plateau.Count;
if (d < minDist) minDist = d;
if (minDist < minPlateauDist || (minDist == minPlateauDist && avgDist < avgChildDist)) {
minAvgPlateauDist = avgDist;
minPlateauDist = minDist;
worstPlateau = p;
}
}
if (minPlateauDist < minChildDist || (minPlateauDist == minChildDist && minAvgPlateauDist < avgChildDist))
repCand = worstPlateau;
}
if (repCand < 0) {
// If no solution at the same plateau were identified for replacement
// a worse solution with smallest distance is chosen
var minDist = double.MaxValue;
foreach (var c in candidates.Where(x => Context.IsBetter(child, x.Individual))) {
var d = Dist(c.Individual, child);
if (d < minDist) {
minDist = d;
repCand = c.Index;
}
}
}
// If no replacement was identified, this can only mean that there are
// no worse solutions and those on the same plateau are all better
// stretched out than the new one
if (repCand < 0) return false;// -1;
Context.ReplaceAtPopulation(repCand, child);
return true;// repCand;
}
return false;// -1;
}
protected bool Eq(ISingleObjectiveSolutionScope a, ISingleObjectiveSolutionScope b) {
return Eq(a.Solution, b.Solution);
}
protected abstract bool Eq(TSolution a, TSolution b);
protected abstract double Dist(ISingleObjectiveSolutionScope a, ISingleObjectiveSolutionScope b);
protected abstract ISolutionSubspace CalculateSubspace(IEnumerable solutions, bool inverse = false);
#region Create
protected virtual ISingleObjectiveSolutionScope Create(CancellationToken token) {
var child = Context.ToScope(null);
Context.RunOperator(Problem.SolutionCreator, child, token);
return child;
}
#endregion
#region Improve
protected virtual int HillClimb(ISingleObjectiveSolutionScope scope, CancellationToken token, ISolutionSubspace subspace = null) {
if (double.IsNaN(scope.Fitness)) {
Context.Evaluate(scope, token);
Context.IncrementEvaluatedSolutions(1);
}
var before = (ISingleObjectiveSolutionScope)scope.Clone();
var lscontext = Context.CreateSingleSolutionContext(scope);
LocalSearchParameter.Value.Optimize(lscontext);
Context.AddHillclimbingResult(before, scope);
Context.IncrementEvaluatedSolutions(lscontext.EvaluatedSolutions);
return lscontext.EvaluatedSolutions;
}
protected virtual void AdaptiveClimb(ISingleObjectiveSolutionScope scope, int maxEvals, CancellationToken token, ISolutionSubspace subspace = null) {
if (double.IsNaN(scope.Fitness)) {
Context.Evaluate(scope, token);
Context.IncrementEvaluatedSolutions(1);
}
var newScope = (ISingleObjectiveSolutionScope)scope.Clone();
AdaptiveWalk(newScope, maxEvals, token, subspace);
Context.AddAdaptivewalkingResult(scope, newScope);
if (Context.IsBetter(newScope, scope)) {
scope.Adopt(newScope);
}
}
protected abstract void AdaptiveWalk(ISingleObjectiveSolutionScope scope, int maxEvals, CancellationToken token, ISolutionSubspace subspace = null);
#endregion
#region Breed
protected virtual ISingleObjectiveSolutionScope Breed(CancellationToken token) {
var i1 = Context.Random.Next(Context.PopulationCount);
var i2 = Context.Random.Next(Context.PopulationCount);
while (i1 == i2) i2 = Context.Random.Next(Context.PopulationCount);
var p1 = Context.AtPopulation(i1);
var p2 = Context.AtPopulation(i2);
if (double.IsNaN(p1.Fitness)) {
Context.Evaluate(p1, token);
Context.IncrementEvaluatedSolutions(1);
}
if (double.IsNaN(p2.Fitness)) {
Context.Evaluate(p2, token);
Context.IncrementEvaluatedSolutions(1);
}
if (!Context.BreedingSuited(p1, p2, Dist(p1, p2))) return null;
var offspring = Breed(p1, p2, token);
if (double.IsNaN(offspring.Fitness)) {
Context.Evaluate(offspring, token);
Context.IncrementEvaluatedSolutions(1);
}
Context.AddBreedingResult(p1, p2, Dist(p1, p2), offspring);
// new best solutions are improved using hill climbing in full solution space
if (Context.Population.All(p => Context.IsBetter(offspring, p)))
HillClimb(offspring, token);
else if (!Eq(offspring, p1) && !Eq(offspring, p2) && Context.HillclimbingSuited(offspring.Fitness))
HillClimb(offspring, token, CalculateSubspace(new[] { p1.Solution, p2.Solution }, inverse: false));
return offspring;
}
protected abstract ISingleObjectiveSolutionScope Breed(ISingleObjectiveSolutionScope p1, ISingleObjectiveSolutionScope p2, CancellationToken token);
#endregion
#region Relink/Delink
protected virtual ISingleObjectiveSolutionScope Relink(CancellationToken token) {
var i1 = Context.Random.Next(Context.PopulationCount);
var i2 = Context.Random.Next(Context.PopulationCount);
while (i1 == i2) i2 = Context.Random.Next(Context.PopulationCount);
var p1 = Context.AtPopulation(i1);
var p2 = Context.AtPopulation(i2);
if (!Context.RelinkSuited(p1, p2, Dist(p1, p2))) return null;
var link = PerformRelinking(p1, p2, token, delink: false);
// new best solutions are improved using hill climbing in full solution space
if (Context.Population.All(p => Context.IsBetter(link, p)))
HillClimb(link, token);
else if (!Eq(link, p1) && !Eq(link, p2) && Context.HillclimbingSuited(link.Fitness))
HillClimb(link, token, CalculateSubspace(new[] { p1.Solution, p2.Solution }, inverse: true));
return link;
}
protected virtual ISingleObjectiveSolutionScope Delink(CancellationToken token) {
var i1 = Context.Random.Next(Context.PopulationCount);
var i2 = Context.Random.Next(Context.PopulationCount);
while (i1 == i2) i2 = Context.Random.Next(Context.PopulationCount);
var p1 = Context.AtPopulation(i1);
var p2 = Context.AtPopulation(i2);
if (!Context.DelinkSuited(p1, p2, Dist(p1, p2))) return null;
var link = PerformRelinking(p1, p2, token, delink: true);
// new best solutions are improved using hill climbing in full solution space
if (Context.Population.All(p => Context.IsBetter(link, p)))
HillClimb(link, token);
// intentionally not making hill climbing otherwise after delinking in sub-space
return link;
}
protected virtual ISingleObjectiveSolutionScope PerformRelinking(ISingleObjectiveSolutionScope a, ISingleObjectiveSolutionScope b, CancellationToken token, bool delink = false) {
var relink = Link(a, b, token, delink);
if (double.IsNaN(relink.Fitness)) {
Context.Evaluate(relink, token);
Context.IncrementEvaluatedSolutions(1);
}
if (delink) {
Context.AddDelinkingResult(a, b, Dist(a, b), relink);
} else {
Context.AddRelinkingResult(a, b, Dist(a, b), relink);
}
return relink;
}
protected abstract ISingleObjectiveSolutionScope Link(ISingleObjectiveSolutionScope a, ISingleObjectiveSolutionScope b, CancellationToken token, bool delink = false);
#endregion
#region Sample
protected virtual ISingleObjectiveSolutionScope Sample(CancellationToken token) {
if (Context.PopulationCount == MaximumPopulationSize) {
SolutionModelTrainerParameter.Value.TrainModel(Context);
ISingleObjectiveSolutionScope bestSample = null;
var tries = 1;
var avgDist = (from a in Context.Population.Shuffle(Context.Random)
from b in Context.Population.Shuffle(Context.Random)
select Dist(a, b)).Average();
for (; tries < 100; tries++) {
var sample = Context.ToScope(Context.Model.Sample());
Context.Evaluate(sample, token);
if (bestSample == null || Context.IsBetter(sample, bestSample)) {
bestSample = sample;
if (Context.Population.Any(x => !Context.IsBetter(x, bestSample))) break;
}
if (!Context.SamplingSuited(avgDist)) break;
}
Context.IncrementEvaluatedSolutions(tries);
Context.AddSamplingResult(bestSample, avgDist);
return bestSample;
}
return null;
}
#endregion
protected virtual bool Terminate() {
var maximization = ((IValueParameter)Problem.MaximizationParameter).Value.Value;
return MaximumEvaluations.HasValue && Context.EvaluatedSolutions >= MaximumEvaluations.Value
|| MaximumExecutionTime.HasValue && ExecutionTime >= MaximumExecutionTime.Value
|| TargetQuality.HasValue && (maximization && Context.BestQuality >= TargetQuality.Value
|| !maximization && Context.BestQuality <= TargetQuality.Value);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string property) {
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(property));
}
}
}