[3150] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3150] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
[9980] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[8720] | 25 | using HeuristicLab.Analysis;
|
---|
[3150] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
[12102] | 31 | using HeuristicLab.Optimization.Operators;
|
---|
[3150] | 32 | using HeuristicLab.Parameters;
|
---|
| 33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[9980] | 34 | using HeuristicLab.Problems.Instances;
|
---|
[3150] | 35 |
|
---|
[3170] | 36 | namespace HeuristicLab.Problems.TestFunctions {
|
---|
[12504] | 37 | [Item("Test Function (single-objective)", "Test function with real valued inputs and a single objective.")]
|
---|
[3150] | 38 | [StorableClass]
|
---|
[12504] | 39 | [Creatable(CreatableAttribute.Categories.Problems, Priority = 90)]
|
---|
[13361] | 40 | public sealed class SingleObjectiveTestFunctionProblem :
|
---|
[13403] | 41 | SingleObjectiveProblem<RealVectorEncoding, RealVector>,
|
---|
| 42 | IProblemInstanceConsumer<SOTFData> {
|
---|
[4419] | 43 |
|
---|
[13403] | 44 | public override bool Maximization {
|
---|
| 45 | get { return Parameters.ContainsKey("TestFunction") && TestFunction.Maximization; }
|
---|
| 46 | }
|
---|
[3336] | 47 |
|
---|
[3150] | 48 | #region Parameter Properties
|
---|
[13403] | 49 | private IFixedValueParameter<IntValue> ProblemSizeParameter {
|
---|
| 50 | get { return (IFixedValueParameter<IntValue>)Parameters["ProblemSize"]; }
|
---|
[3150] | 51 | }
|
---|
[13403] | 52 | private IValueParameter<DoubleMatrix> BoundsParameter {
|
---|
| 53 | get { return (IValueParameter<DoubleMatrix>)Parameters["Bounds"]; }
|
---|
[3150] | 54 | }
|
---|
[3781] | 55 | public OptionalValueParameter<RealVector> BestKnownSolutionParameter {
|
---|
| 56 | get { return (OptionalValueParameter<RealVector>)Parameters["BestKnownSolution"]; }
|
---|
| 57 | }
|
---|
[13403] | 58 | public IValueParameter<ISingleObjectiveTestFunction> TestFunctionParameter {
|
---|
| 59 | get { return (IValueParameter<ISingleObjectiveTestFunction>)Parameters["TestFunction"]; }
|
---|
| 60 | }
|
---|
[3150] | 61 | #endregion
|
---|
| 62 |
|
---|
| 63 | #region Properties
|
---|
[13403] | 64 | public int ProblemSize {
|
---|
| 65 | get { return ProblemSizeParameter.Value.Value; }
|
---|
| 66 | set { ProblemSizeParameter.Value.Value = value; }
|
---|
| 67 | }
|
---|
[3150] | 68 | public DoubleMatrix Bounds {
|
---|
| 69 | get { return BoundsParameter.Value; }
|
---|
| 70 | set { BoundsParameter.Value = value; }
|
---|
| 71 | }
|
---|
[13403] | 72 | public ISingleObjectiveTestFunction TestFunction {
|
---|
| 73 | get { return TestFunctionParameter.Value; }
|
---|
| 74 | set { TestFunctionParameter.Value = value; }
|
---|
[3150] | 75 | }
|
---|
[13403] | 76 |
|
---|
[3789] | 77 | private BestSingleObjectiveTestFunctionSolutionAnalyzer BestSingleObjectiveTestFunctionSolutionAnalyzer {
|
---|
[6938] | 78 | get { return Operators.OfType<BestSingleObjectiveTestFunctionSolutionAnalyzer>().FirstOrDefault(); }
|
---|
[3647] | 79 | }
|
---|
[3150] | 80 | #endregion
|
---|
| 81 |
|
---|
| 82 | [StorableConstructor]
|
---|
[4118] | 83 | private SingleObjectiveTestFunctionProblem(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 84 | private SingleObjectiveTestFunctionProblem(SingleObjectiveTestFunctionProblem original, Cloner cloner)
|
---|
| 85 | : base(original, cloner) {
|
---|
[7351] | 86 | RegisterEventHandlers();
|
---|
[4722] | 87 | }
|
---|
[3170] | 88 | public SingleObjectiveTestFunctionProblem()
|
---|
[13403] | 89 | : base(new RealVectorEncoding("Point")) {
|
---|
| 90 | Parameters.Add(new FixedValueParameter<IntValue>("ProblemSize", "The dimensionality of the problem instance (number of variables in the function).", new IntValue(2)));
|
---|
| 91 | Parameters.Add(new ValueParameter<DoubleMatrix>("Bounds", "The bounds of the solution given as either one line for all variables or a line for each variable. The first column specifies lower bound, the second upper bound.", new DoubleMatrix(new double[,] { { -100, 100 } })));
|
---|
[3781] | 92 | Parameters.Add(new OptionalValueParameter<RealVector>("BestKnownSolution", "The best known solution for this test function instance."));
|
---|
[13403] | 93 | Parameters.Add(new ValueParameter<ISingleObjectiveTestFunction>("TestFunction", "The function that is to be optimized.", new Ackley()));
|
---|
[3150] | 94 |
|
---|
[13403] | 95 | Encoding.LengthParameter = ProblemSizeParameter;
|
---|
| 96 | Encoding.BoundsParameter = BoundsParameter;
|
---|
| 97 | BestKnownQuality = TestFunction.BestKnownQuality;
|
---|
[6939] | 98 |
|
---|
[4098] | 99 | InitializeOperators();
|
---|
[7351] | 100 | RegisterEventHandlers();
|
---|
[3150] | 101 | }
|
---|
| 102 |
|
---|
| 103 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 104 | return new SingleObjectiveTestFunctionProblem(this, cloner);
|
---|
[3150] | 105 | }
|
---|
| 106 |
|
---|
[13403] | 107 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 108 | private void AfterDeserialization() {
|
---|
| 109 | RegisterEventHandlers();
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | private void RegisterEventHandlers() {
|
---|
| 113 | Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
| 114 | TestFunctionParameter.ValueChanged += TestFunctionParameterOnValueChanged;
|
---|
| 115 | ProblemSizeParameter.Value.ValueChanged += ProblemSizeOnValueChanged;
|
---|
| 116 | BoundsParameter.ValueChanged += BoundsParameterOnValueChanged;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | public override double Evaluate(RealVector individual, IRandom random) {
|
---|
| 120 | return TestFunction.Evaluate(individual);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[3150] | 123 | #region Events
|
---|
[13403] | 124 | protected override void OnEncodingChanged() {
|
---|
| 125 | base.OnEncodingChanged();
|
---|
| 126 | Parameterize();
|
---|
[3150] | 127 | }
|
---|
[6938] | 128 | protected override void OnEvaluatorChanged() {
|
---|
| 129 | base.OnEvaluatorChanged();
|
---|
[13403] | 130 | Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
| 131 | Parameterize();
|
---|
[3150] | 132 | }
|
---|
| 133 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[13403] | 134 | Parameterize();
|
---|
[3150] | 135 | }
|
---|
[13403] | 136 | private void TestFunctionParameterOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
| 137 | var problemSizeChange = ProblemSize < TestFunction.MinimumProblemSize
|
---|
| 138 | || ProblemSize > TestFunction.MaximumProblemSize;
|
---|
| 139 | if (problemSizeChange) {
|
---|
| 140 | ProblemSize = Math.Max(TestFunction.MinimumProblemSize, Math.Min(ProblemSize, TestFunction.MaximumProblemSize));
|
---|
[3187] | 141 | }
|
---|
[13403] | 142 | BestKnownQuality = TestFunction.BestKnownQuality;
|
---|
| 143 | Bounds = (DoubleMatrix)TestFunction.Bounds.Clone();
|
---|
| 144 | var bestSolution = TestFunction.GetBestKnownSolution(ProblemSize);
|
---|
| 145 | BestKnownSolutionParameter.Value = bestSolution;
|
---|
| 146 |
|
---|
| 147 | OnReset();
|
---|
[3182] | 148 | }
|
---|
[13403] | 149 | private void ProblemSizeOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
| 150 | if (ProblemSize < TestFunction.MinimumProblemSize
|
---|
| 151 | || ProblemSize > TestFunction.MaximumProblemSize)
|
---|
| 152 | ProblemSize = Math.Min(TestFunction.MaximumProblemSize, Math.Max(TestFunction.MinimumProblemSize, ProblemSize));
|
---|
[3318] | 153 | }
|
---|
[13403] | 154 | private void BoundsParameterOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
| 155 | Parameterize();
|
---|
[3318] | 156 | }
|
---|
[3150] | 157 | #endregion
|
---|
| 158 |
|
---|
| 159 | #region Helpers
|
---|
| 160 | private void InitializeOperators() {
|
---|
[8334] | 161 | Operators.Add(new SingleObjectiveTestFunctionImprovementOperator());
|
---|
| 162 | Operators.Add(new SingleObjectiveTestFunctionPathRelinker());
|
---|
| 163 | Operators.Add(new SingleObjectiveTestFunctionSimilarityCalculator());
|
---|
[12126] | 164 | Operators.Add(new QualitySimilarityCalculator());
|
---|
[12102] | 165 | Operators.Add(new NoSimilarityCalculator());
|
---|
[13403] | 166 | Operators.Add(new AdditiveMoveEvaluator());
|
---|
[8334] | 167 |
|
---|
[6938] | 168 | Operators.Add(new BestSingleObjectiveTestFunctionSolutionAnalyzer());
|
---|
[12069] | 169 | Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
|
---|
[13403] | 170 | Parameterize();
|
---|
[3150] | 171 | }
|
---|
[13403] | 172 |
|
---|
| 173 | private void Parameterize() {
|
---|
| 174 | var operators = new List<IItem>();
|
---|
| 175 | if (BestSingleObjectiveTestFunctionSolutionAnalyzer != null) {
|
---|
| 176 | operators.Add(BestSingleObjectiveTestFunctionSolutionAnalyzer);
|
---|
| 177 | BestSingleObjectiveTestFunctionSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
| 178 | BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
| 179 | BestSingleObjectiveTestFunctionSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;
|
---|
| 180 | BestSingleObjectiveTestFunctionSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 181 | BestSingleObjectiveTestFunctionSolutionAnalyzer.TestFunctionParameter.ActualName = TestFunctionParameter.Name;
|
---|
[3150] | 182 | }
|
---|
[9258] | 183 | foreach (var op in Operators.OfType<ISingleObjectiveTestFunctionAdditiveMoveEvaluator>()) {
|
---|
[13403] | 184 | operators.Add(op);
|
---|
[3187] | 185 | op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
[6051] | 186 | op.QualityParameter.Hidden = true;
|
---|
[13403] | 187 | foreach (var movOp in Encoding.Operators.OfType<IRealVectorAdditiveMoveQualityOperator>())
|
---|
| 188 | movOp.MoveQualityParameter.ActualName = op.MoveQualityParameter.ActualName;
|
---|
[3187] | 189 | }
|
---|
[9258] | 190 | foreach (var op in Operators.OfType<IRealVectorSwarmUpdater>()) {
|
---|
[5951] | 191 | op.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
[6051] | 192 | op.MaximizationParameter.Hidden = true;
|
---|
[5561] | 193 | }
|
---|
[9258] | 194 | foreach (var op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
|
---|
[13403] | 195 | operators.Add(op);
|
---|
| 196 | op.SolutionParameter.ActualName = Encoding.Name;
|
---|
[8334] | 197 | op.SolutionParameter.Hidden = true;
|
---|
| 198 | }
|
---|
[13403] | 199 | foreach (var op in Operators.OfType<ITestFunctionSolutionSimilarityCalculator>()) {
|
---|
| 200 | operators.Add(op);
|
---|
| 201 | op.SolutionVariableName = Encoding.Name;
|
---|
[8334] | 202 | op.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
[13403] | 203 | op.Bounds = Bounds;
|
---|
[8334] | 204 | }
|
---|
[13403] | 205 |
|
---|
| 206 | if (operators.Count > 0) Encoding.ConfigureOperators(operators);
|
---|
[3150] | 207 | }
|
---|
| 208 | #endregion
|
---|
[9980] | 209 |
|
---|
| 210 | public void Load(SOTFData data) {
|
---|
| 211 | Name = data.Name;
|
---|
| 212 | Description = data.Description;
|
---|
[13403] | 213 | TestFunction = data.TestFunction;
|
---|
[9980] | 214 | }
|
---|
[3150] | 215 | }
|
---|
| 216 | }
|
---|