#region License Information
/* HeuristicLab
* Copyright (C) 2002-2019 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.Linq;
//using HeuristicLab.Analysis;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.RealVectorEncoding;
using HeuristicLab.Optimization;
//using HeuristicLab.Optimization.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.PluginInfrastructure;
//using HeuristicLab.Problems.Instances;
namespace HeuristicLab.Problems.MovingPeaksBenchmark {
[Item("Moving Peaks Benchmark", "Moving peaks benchmark problem for dynamic optimization.")]
[StorableClass]
[Creatable(CreatableAttribute.Categories.Problems, Priority = 97)]
public sealed class MovingPeaksBenchmarkProblem : SingleObjectiveHeuristicOptimizationProblem, IStorableContent {
public string Filename { get; set; }
//[Storable]
//private StdDevStrategyVectorCreator strategyVectorCreator;
//[Storable]
//private StdDevStrategyVectorCrossover strategyVectorCrossover;
//[Storable]
//private StdDevStrategyVectorManipulator strategyVectorManipulator;
#region Parameter Properties
public ValueParameter BoundsParameter {
get { return (ValueParameter)Parameters["Bounds"]; }
}
public ValueParameter ProblemSizeParameter {
get { return (ValueParameter)Parameters["ProblemSize"]; }
}
public ValueParameter PeaksParameter {
get { return (ValueParameter)Parameters["Peaks"]; }
}
public ValueParameter InitialPeakLocationsParameter {
get { return (ValueParameter)Parameters["InitialPeakLocations"]; }
}
public ValueParameter InitialPeakWidthsParameter {
get { return (ValueParameter)Parameters["InitialPeakWidths"]; }
}
public ValueParameter InitialPeakHeightsParameter {
get { return (ValueParameter)Parameters["InitialPeakHeights"]; }
}
public FixedValueParameter MovingPeaksRandomSeedParameter {
get { return (FixedValueParameter)Parameters["MovingPeaksRandomSeed"]; }
}
public FixedValueParameter MinPeakWidthParameter {
get { return (FixedValueParameter)Parameters["MinPeakWidth"]; }
}
public FixedValueParameter MaxPeakWidthParameter {
get { return (FixedValueParameter)Parameters["MaxPeakWidth"]; }
}
public FixedValueParameter MinPeakHeightParameter {
get { return (FixedValueParameter)Parameters["MinPeakHeight"]; }
}
public FixedValueParameter MaxPeakHeightParameter {
get { return (FixedValueParameter)Parameters["MaxPeakHeight"]; }
}
public FixedValueParameter PeakMovementIntervalParameter {
get { return (FixedValueParameter)Parameters["PeakMovementInterval"]; }
}
public FixedValueParameter PeakMovementStrengthParameter {
get { return (FixedValueParameter)Parameters["PeakMovementStrength"]; }
}
public OptionalValueParameter BestKnownSolutionParameter {
get { return (OptionalValueParameter)Parameters["BestKnownSolution"]; }
}
#endregion
#region Properties
public DoubleMatrix Bounds {
get { return BoundsParameter.Value; }
set { BoundsParameter.Value = value; }
}
public IntValue ProblemSize {
get { return ProblemSizeParameter.Value; }
set { ProblemSizeParameter.Value = value; }
}
public IntValue Peaks {
get { return PeaksParameter.Value; }
set { PeaksParameter.Value = value; }
}
public DoubleMatrix InitialPeakLocations {
get { return InitialPeakLocationsParameter.Value; }
set { InitialPeakLocationsParameter.Value = value; }
}
public DoubleArray InitialPeakWidths {
get { return InitialPeakWidthsParameter.Value; }
set { InitialPeakWidthsParameter.Value = value; }
}
public DoubleArray InitialPeakHeights {
get { return InitialPeakHeightsParameter.Value; }
set { InitialPeakHeightsParameter.Value = value; }
}
public IntValue MovingPeaksRandomSeed {
get { return MovingPeaksRandomSeedParameter.Value; }
}
public DoubleValue MinPeakWidth {
get { return MinPeakWidthParameter.Value; }
}
public DoubleValue MaxPeakWidth {
get { return MaxPeakWidthParameter.Value; }
}
public DoubleValue MinPeakHeight {
get { return MinPeakHeightParameter.Value; }
}
public DoubleValue MaxPeakHeight {
get { return MaxPeakHeightParameter.Value; }
}
public IntValue PeakMovementInterval {
get { return PeakMovementIntervalParameter.Value; }
}
public DoubleValue PeakMovementStrength {
get { return PeakMovementStrengthParameter.Value; }
}
private BestMovingPeaksBenchmarkSolutionAnalyzer BestMovingPeaksBenchmarkSolutionAnalyzer {
get { return Operators.OfType().FirstOrDefault(); }
}
#endregion
[StorableConstructor]
private MovingPeaksBenchmarkProblem(bool deserializing) : base(deserializing) { }
private MovingPeaksBenchmarkProblem(MovingPeaksBenchmarkProblem original, Cloner cloner)
: base(original, cloner) {
//strategyVectorCreator = cloner.Clone(original.strategyVectorCreator);
//strategyVectorCrossover = cloner.Clone(original.strategyVectorCrossover);
//strategyVectorManipulator = cloner.Clone(original.strategyVectorManipulator);
RegisterEventHandlers();
}
public MovingPeaksBenchmarkProblem()
: base(new MovingPeaksBenchmarkProblemEvaluator(), new UniformRandomRealVectorCreator()) {
var defaultBounds = new double[,] {
{ 0.0, 100.0 }
};
var defaultPeaks = new double[,] {
{ 08.0, 64.0, 67.0, 55.0, 04.0 },
{ 50.0, 13.0, 76.0, 15.0, 07.0 },
{ 09.0, 19.0, 27.0, 67.0, 24.0 },
{ 66.0, 87.0, 65.0, 19.0, 43.0 },
{ 76.0, 32.0, 43.0, 54.0, 65.0 },
{ 25.0, 51.0, 17.0, 25.0, 16.0 },
{ 19.0, 93.0, 06.0, 35.0, 15.0 },
{ 88.0, 44.0, 37.0, 77.0, 74.0 },
{ 37.0, 74.0, 55.0, 09.0, 53.0 },
{ 42.0, 22.0, 93.0, 84.0, 35.0 }
};
var defaultWidths = new double[] { -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0 };
var defaultHeights = new double[] { 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0, 50.0 };
var defaultBestKnown = new double[] { 08.0, 64.0, 67.0, 55.0, 04.0 };
Parameters.Add(new ValueParameter("Bounds", "The lower and upper bounds in each dimension.", new DoubleMatrix(defaultBounds)));
Parameters.Add(new ValueParameter("ProblemSize", "The dimension of the problem.", new IntValue(5)));
Parameters.Add(new ValueParameter("Peaks", "The number of peaks.", new IntValue(10)));
Parameters.Add(new ValueParameter("InitialPeakLocations", "Initial coordinates of each peaks.", new DoubleMatrix(defaultPeaks)));
Parameters.Add(new ValueParameter("InitialPeakWidths", "Initial width of each peak.", new DoubleArray(defaultWidths)));
Parameters.Add(new ValueParameter("InitialPeakHeights", "Initial height of each peak.", new DoubleArray(defaultHeights)));
Parameters.Add(new FixedValueParameter("MovingPeaksRandomSeed", "The random seed for initializing the PRNG for changing the peaks.", new IntValue(666)));
Parameters.Add(new FixedValueParameter("MinPeakWidth", "The minimum width of each peak.", new DoubleValue(1.0)));
Parameters.Add(new FixedValueParameter("MaxPeakWidth", "The maximum width of each peak.", new DoubleValue(12.0)));
Parameters.Add(new FixedValueParameter("MinPeakHeight", "The minimum height of each peak.", new DoubleValue(30.0)));
Parameters.Add(new FixedValueParameter("MaxPeakHeight", "The maximum height of each peak.", new DoubleValue(70.0)));
Parameters.Add(new FixedValueParameter("PeakMovementInterval", "The interval in evaluated solutions in which peaks are moved.", new IntValue(5000)));
Parameters.Add(new FixedValueParameter("PeakMovementStrength", "The length of the random vector used for changing peak locations.", new DoubleValue(1.5)));
Parameters.Add(new OptionalValueParameter("BestKnownSolution", "The location of the highest peak.", new RealVector(defaultBestKnown)));
Maximization.Value = true;
BestKnownQuality = new DoubleValue(defaultHeights.Max());
//strategyVectorCreator = new StdDevStrategyVectorCreator();
//strategyVectorCreator.LengthParameter.ActualName = ProblemSizeParameter.Name;
//strategyVectorCrossover = new StdDevStrategyVectorCrossover();
//strategyVectorManipulator = new StdDevStrategyVectorManipulator();
//strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(0.5);
//strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.5);
SolutionCreator.RealVectorParameter.ActualName = "Point";
ParameterizeSolutionCreator();
ParameterizeEvaluator();
InitializeOperators();
RegisterEventHandlers();
UpdateStrategyVectorBounds();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new MovingPeaksBenchmarkProblem(this, cloner);
}
#region Events
protected override void OnSolutionCreatorChanged() {
base.OnSolutionCreatorChanged();
ParameterizeSolutionCreator();
ParameterizeAnalyzers();
SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
SolutionCreator_RealVectorParameter_ActualNameChanged(null, EventArgs.Empty);
}
protected override void OnEvaluatorChanged() {
base.OnEvaluatorChanged();
ParameterizeEvaluator();
UpdateMoveEvaluators();
ParameterizeAnalyzers();
Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
Evaluator_QualityParameter_ActualNameChanged(null, EventArgs.Empty);
OnReset();
}
private void ProblemSizeParameter_ValueChanged(object sender, EventArgs e) {
ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
ProblemSize_ValueChanged(null, EventArgs.Empty);
}
private void ProblemSize_ValueChanged(object sender, EventArgs e) {
if (ProblemSize.Value < 1) ProblemSize.Value = 1;
ParameterizeSolutionCreator();
ParameterizeEvaluator();
//strategyVectorManipulator.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * ProblemSize.Value));
//strategyVectorManipulator.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(ProblemSize.Value)));
OnReset();
}
private void SolutionCreator_RealVectorParameter_ActualNameChanged(object sender, EventArgs e) {
ParameterizeEvaluator();
ParameterizeOperators();
ParameterizeAnalyzers();
}
private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
ParameterizeOperators();
}
private void BoundsParameter_ValueChanged(object sender, EventArgs e) {
Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
Bounds_ToStringChanged(null, EventArgs.Empty);
}
private void Bounds_ToStringChanged(object sender, EventArgs e) {
if (Bounds.Columns != 2 || Bounds.Rows < 1)
Bounds = new DoubleMatrix(1, 2);
ParameterizeOperators();
UpdateStrategyVectorBounds();
}
private void Bounds_ItemChanged(object sender, EventArgs e) {
if (e.Value2 == 0 && Bounds[e.Value, 1] <= Bounds[e.Value, 0])
Bounds[e.Value, 1] = Bounds[e.Value, 0] + 0.1;
if (e.Value2 == 1 && Bounds[e.Value, 0] >= Bounds[e.Value, 1])
Bounds[e.Value, 0] = Bounds[e.Value, 1] - 0.1;
ParameterizeOperators();
UpdateStrategyVectorBounds();
}
private void MoveGenerator_AdditiveMoveParameter_ActualNameChanged(object sender, EventArgs e) {
string name = ((ILookupParameter)sender).ActualName;
foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType()) {
op.AdditiveMoveParameter.ActualName = name;
}
}
//private void SphereEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
// SphereEvaluator eval = (Evaluator as SphereEvaluator);
// if (eval != null) {
// foreach (ISphereMoveEvaluator op in Operators.OfType()) {
// op.C = eval.C;
// op.Alpha = eval.Alpha;
// }
// }
//}
//private void RastriginEvaluator_Parameter_ValueChanged(object sender, EventArgs e) {
// RastriginEvaluator eval = (Evaluator as RastriginEvaluator);
// if (eval != null) {
// foreach (IRastriginMoveEvaluator op in Operators.OfType()) {
// op.A = eval.A;
// }
// }
//}
//private void strategyVectorCreator_BoundsParameter_ValueChanged(object sender, EventArgs e) {
// strategyVectorManipulator.BoundsParameter.Value = (DoubleMatrix)strategyVectorCreator.BoundsParameter.Value.Clone();
//}
//private void strategyVectorCreator_StrategyParameterParameter_ActualNameChanged(object sender, EventArgs e) {
// string name = strategyVectorCreator.StrategyParameterParameter.ActualName;
// strategyVectorCrossover.ParentsParameter.ActualName = name;
// strategyVectorCrossover.StrategyParameterParameter.ActualName = name;
// strategyVectorManipulator.StrategyParameterParameter.ActualName = name;
//}
#endregion
#region Helpers
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterEventHandlers();
}
private void RegisterEventHandlers() {
ProblemSizeParameter.ValueChanged += new EventHandler(ProblemSizeParameter_ValueChanged);
ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged);
BoundsParameter.ValueChanged += new EventHandler(BoundsParameter_ValueChanged);
Bounds.ToStringChanged += new EventHandler(Bounds_ToStringChanged);
Bounds.ItemChanged += new EventHandler>(Bounds_ItemChanged);
SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged);
Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
//strategyVectorCreator.BoundsParameter.ValueChanged += new EventHandler(strategyVectorCreator_BoundsParameter_ValueChanged);
//strategyVectorCreator.StrategyParameterParameter.ActualNameChanged += new EventHandler(strategyVectorCreator_StrategyParameterParameter_ActualNameChanged);
}
private void ParameterizeAnalyzers() {
if (BestMovingPeaksBenchmarkSolutionAnalyzer != null) {
BestMovingPeaksBenchmarkSolutionAnalyzer.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
BestMovingPeaksBenchmarkSolutionAnalyzer.ResultsParameter.ActualName = "Results";
BestMovingPeaksBenchmarkSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
BestMovingPeaksBenchmarkSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
BestMovingPeaksBenchmarkSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
BestMovingPeaksBenchmarkSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
}
}
private void InitializeOperators() {
//Operators.Add(new SingleObjectiveTestFunctionImprovementOperator());
//Operators.Add(new SingleObjectiveTestFunctionPathRelinker());
//Operators.Add(new SingleObjectiveTestFunctionSimilarityCalculator());
//Operators.Add(new HammingSimilarityCalculator());
//Operators.Add(new EuclideanSimilarityCalculator());
//Operators.Add(new QualitySimilarityCalculator());
Operators.Add(new BestMovingPeaksBenchmarkSolutionAnalyzer());
//Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType()));
ParameterizeAnalyzers();
Operators.AddRange(ApplicationManager.Manager.GetInstances().Cast());
//Operators.Add(strategyVectorCreator);
//Operators.Add(strategyVectorCrossover);
//Operators.Add(strategyVectorManipulator);
UpdateMoveEvaluators();
ParameterizeOperators();
InitializeMoveGenerators();
}
private void InitializeMoveGenerators() {
foreach (IAdditiveRealVectorMoveOperator op in Operators.OfType()) {
if (op is IMoveGenerator) {
op.AdditiveMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_AdditiveMoveParameter_ActualNameChanged);
}
}
}
private void UpdateMoveEvaluators() {
//foreach (ISingleObjectiveTestFunctionMoveEvaluator op in Operators.OfType().ToList())
// Operators.Remove(op);
//foreach (ISingleObjectiveTestFunctionMoveEvaluator op in ApplicationManager.Manager.GetInstances())
// if (op.EvaluatorType == Evaluator.GetType()) {
// Operators.Add(op);
// #region Synchronize evaluator specific parameters with the parameters of the corresponding move evaluators
// if (op is ISphereMoveEvaluator) {
// SphereEvaluator e = (Evaluator as SphereEvaluator);
// e.AlphaParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
// e.CParameter.ValueChanged += new EventHandler(SphereEvaluator_Parameter_ValueChanged);
// ISphereMoveEvaluator em = (op as ISphereMoveEvaluator);
// em.C = e.C;
// em.Alpha = e.Alpha;
// } else if (op is IRastriginMoveEvaluator) {
// RastriginEvaluator e = (Evaluator as RastriginEvaluator);
// e.AParameter.ValueChanged += new EventHandler(RastriginEvaluator_Parameter_ValueChanged);
// IRastriginMoveEvaluator em = (op as IRastriginMoveEvaluator);
// em.A = e.A;
// }
// #endregion
// }
ParameterizeOperators();
OnOperatorsChanged();
}
private void ParameterizeSolutionCreator() {
SolutionCreator.LengthParameter.Value = new IntValue(ProblemSize.Value);
SolutionCreator.LengthParameter.Hidden = true;
SolutionCreator.BoundsParameter.ActualName = BoundsParameter.Name;
SolutionCreator.BoundsParameter.Hidden = true;
}
private void ParameterizeEvaluator() {
Evaluator.PointParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
Evaluator.PointParameter.Hidden = true;
//try {
// BestKnownSolutionParameter.Value = Evaluator.GetBestKnownSolution(ProblemSize.Value);
//}
//catch (ArgumentException e) {
// ErrorHandling.ShowErrorDialog(e);
// ProblemSize.Value = Evaluator.MinimumProblemSize;
//}
}
private void ParameterizeOperators() {
foreach (var op in Operators.OfType()) {
op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
op.ParentsParameter.Hidden = true;
op.ChildParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
op.ChildParameter.Hidden = true;
op.BoundsParameter.ActualName = BoundsParameter.Name;
op.BoundsParameter.Hidden = true;
}
foreach (var op in Operators.OfType()) {
op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
op.RealVectorParameter.Hidden = true;
op.BoundsParameter.ActualName = BoundsParameter.Name;
op.BoundsParameter.Hidden = true;
}
foreach (var op in Operators.OfType()) {
op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
op.RealVectorParameter.Hidden = true;
}
foreach (var op in Operators.OfType()) {
op.BoundsParameter.ActualName = BoundsParameter.Name;
op.BoundsParameter.Hidden = true;
}
//foreach (var op in Operators.OfType()) {
// op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
// op.QualityParameter.Hidden = true;
// op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
// op.RealVectorParameter.Hidden = true;
//}
foreach (var op in Operators.OfType()) {
op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
op.RealVectorParameter.Hidden = true;
op.BoundsParameter.ActualName = BoundsParameter.Name;
op.BoundsParameter.Hidden = true;
}
foreach (var op in Operators.OfType()) {
op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
op.RealVectorParameter.Hidden = true;
op.BoundsParameter.ActualName = BoundsParameter.Name;
op.BoundsParameter.Hidden = true;
}
foreach (var op in Operators.OfType()) {
op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
op.RealVectorParameter.Hidden = true;
op.MaximizationParameter.ActualName = MaximizationParameter.Name;
op.MaximizationParameter.Hidden = true;
}
foreach (var op in Operators.OfType()) {
op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
op.RealVectorParameter.Hidden = true;
}
foreach (var op in Operators.OfType()) {
op.SolutionParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
op.SolutionParameter.Hidden = true;
}
foreach (var op in Operators.OfType()) {
op.ParentsParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
op.ParentsParameter.Hidden = true;
}
foreach (var op in Operators.OfType()) {
op.SolutionVariableName = SolutionCreator.RealVectorParameter.ActualName;
op.QualityVariableName = Evaluator.QualityParameter.ActualName;
//var calc = op as SingleObjectiveTestFunctionSimilarityCalculator;
//if (calc != null) calc.Bounds = Bounds;
}
}
private void UpdateStrategyVectorBounds() {
//var strategyBounds = (DoubleMatrix)Bounds.Clone();
//for (int i = 0; i < strategyBounds.Rows; i++) {
// if (strategyBounds[i, 0] < 0) strategyBounds[i, 0] = 0;
// strategyBounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
//}
//strategyVectorCreator.BoundsParameter.Value = strategyBounds;
}
#endregion
}
}