Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorEncoding.cs @ 13403

Last change on this file since 13403 was 13403, checked in by abeham, 8 years ago

#2521:

  • Adapted single-objective test function problem to new problem infrastructure
  • Added additional interfaces to RealVectorEncoding
  • Fixed IParticleUpdater interface (must implement IStochasticOperator if it contains a Random parameter)
File size: 14.7 KB
RevLine 
[11484]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11484]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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
[11543]28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
[11484]30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[11546]31using HeuristicLab.PluginInfrastructure;
[11484]32
[11949]33namespace HeuristicLab.Encodings.RealVectorEncoding {
[11885]34  [Item("RealVectorEncoding", "Describes a real vector encoding.")]
[11484]35  [StorableClass]
[13361]36  public sealed class RealVectorEncoding : Encoding<RealVector> {
[11550]37    #region Encoding Parameters
[11598]38    [Storable]
[11543]39    private IFixedValueParameter<IntValue> lengthParameter;
40    public IFixedValueParameter<IntValue> LengthParameter {
41      get { return lengthParameter; }
[11484]42      set {
[11546]43        if (value == null) throw new ArgumentNullException("Length parameter must not be null.");
[11588]44        if (value.Value == null) throw new ArgumentNullException("Length parameter value must not be null.");
[11543]45        if (lengthParameter == value) return;
[11588]46
47        if (lengthParameter != null) Parameters.Remove(lengthParameter);
[11543]48        lengthParameter = value;
[11588]49        Parameters.Add(lengthParameter);
[11543]50        OnLengthParameterChanged();
[11484]51      }
52    }
[11598]53    [Storable]
[11543]54    private IValueParameter<DoubleMatrix> boundsParameter;
55    public IValueParameter<DoubleMatrix> BoundsParameter {
56      get { return boundsParameter; }
[11484]57      set {
[11543]58        if (value == null) throw new ArgumentNullException("Bounds parameter must not be null.");
59        if (boundsParameter == value) return;
[11588]60
61        if (boundsParameter != null) Parameters.Remove(boundsParameter);
[11543]62        boundsParameter = value;
[11588]63        Parameters.Add(boundsParameter);
[11543]64        OnBoundsParameterChanged();
[11484]65      }
66    }
[11550]67    #endregion
[11543]68
69    public int Length {
70      get { return LengthParameter.Value.Value; }
71      set { LengthParameter.Value.Value = value; }
72    }
73    public DoubleMatrix Bounds {
74      get { return BoundsParameter.Value; }
75      set { BoundsParameter.Value = value; }
76    }
77
[11484]78    [StorableConstructor]
[11885]79    private RealVectorEncoding(bool deserializing) : base(deserializing) { }
[11546]80    [StorableHook(HookType.AfterDeserialization)]
81    private void AfterDeserialization() {
82      RegisterParameterEvents();
[11550]83      DiscoverOperators();
[11546]84    }
85
[11885]86    public override IDeepCloneable Clone(Cloner cloner) { return new RealVectorEncoding(this, cloner); }
87    private RealVectorEncoding(RealVectorEncoding original, Cloner cloner)
[11484]88      : base(original, cloner) {
[11543]89      lengthParameter = cloner.Clone(original.lengthParameter);
90      boundsParameter = cloner.Clone(original.boundsParameter);
[11546]91      RegisterParameterEvents();
[11484]92    }
[11546]93
[11885]94    public RealVectorEncoding() : this("RealVector", 10) { }
[11892]95    public RealVectorEncoding(string name) : this(name, 10) { }
[11885]96    public RealVectorEncoding(int length) : this("RealVector", length) { }
[13361]97    public RealVectorEncoding(string name, int length, double min = -1000, double max = 1000)
[11484]98      : base(name) {
99      if (min >= max) throw new ArgumentException("min must be less than max", "min");
[11543]100
101      var bounds = new DoubleMatrix(1, 2);
[11484]102      bounds[0, 0] = min;
103      bounds[0, 1] = max;
[11543]104
[11593]105      lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
106      boundsParameter = new ValueParameter<DoubleMatrix>(Name + ".Bounds", bounds);
107      Parameters.Add(lengthParameter);
108      Parameters.Add(boundsParameter);
[11587]109
110      SolutionCreator = new UniformRandomRealVectorCreator();
[11546]111      RegisterParameterEvents();
[11550]112      DiscoverOperators();
[11484]113    }
[11546]114
[11885]115    public RealVectorEncoding(string name, int length, IList<double> min, IList<double> max)
[11484]116      : base(name) {
117      if (min.Count == 0) throw new ArgumentException("Bounds must be given for the real parameters.");
118      if (min.Count != max.Count) throw new ArgumentException("min must be of the same length as max", "min");
119      if (min.Zip(max, (mi, ma) => mi >= ma).Any(x => x)) throw new ArgumentException("min must be less than max in each dimension", "min");
[11543]120
121      var bounds = new DoubleMatrix(min.Count, 2);
[11484]122      for (int i = 0; i < min.Count; i++) {
123        bounds[i, 0] = min[i];
124        bounds[i, 1] = max[i];
125      }
[12837]126      lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
127      boundsParameter = new ValueParameter<DoubleMatrix>(Name + ".Bounds", bounds);
128      Parameters.Add(lengthParameter);
129      Parameters.Add(boundsParameter);
[11587]130
131      SolutionCreator = new UniformRandomRealVectorCreator();
[11546]132      RegisterParameterEvents();
[11550]133      DiscoverOperators();
[11484]134    }
135
[11543]136    private void OnLengthParameterChanged() {
[11546]137      RegisterLengthParameterEvents();
[11550]138      ConfigureOperators(Operators);
[11543]139    }
140    private void OnBoundsParameterChanged() {
[11546]141      RegisterBoundsParameterEvents();
[11550]142      ConfigureOperators(Operators);
[11543]143    }
[11559]144
[11550]145    private void RegisterParameterEvents() {
[11546]146      RegisterLengthParameterEvents();
147      RegisterBoundsParameterEvents();
148    }
149    private void RegisterLengthParameterEvents() {
[11550]150      LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
151      LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
[11546]152    }
153    private void RegisterBoundsParameterEvents() {
[11550]154      BoundsParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
155      boundsParameter.Value.ToStringChanged += (o, s) => ConfigureOperators(Operators);
[11546]156    }
[11543]157
[11550]158    #region Operator Discovery
159    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
[11885]160    static RealVectorEncoding() {
[11550]161      encodingSpecificOperatorTypes = new List<Type>() {
[11561]162          typeof (IRealVectorOperator),
163          typeof (IRealVectorCreator),
164          typeof (IRealVectorCrossover),
165          typeof (IRealVectorManipulator),
166          typeof (IRealVectorStdDevStrategyParameterOperator),
167          typeof (IRealVectorSwarmUpdater),
168          typeof (IRealVectorParticleCreator),
169          typeof (IRealVectorParticleUpdater),
170          typeof (IRealVectorMultiNeighborhoodShakingOperator),
171          typeof (IRealVectorBoundsChecker),
172          typeof (IRealVectorMoveOperator),
[13403]173          typeof (IRealVectorMoveGenerator),
174          typeof (IRealVectorSolutionOperator),
175          typeof (IRealVectorSolutionsOperator),
176          typeof (IRealVectorBoundedOperator)
[11561]177      };
[11546]178    }
[11550]179    private void DiscoverOperators() {
[11773]180      var assembly = typeof(IRealVectorOperator).Assembly;
181      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
[11550]182      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
[11587]183      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
[11543]184
[11550]185      ConfigureOperators(newOperators);
[11587]186      foreach (var @operator in newOperators)
[11952]187        AddOperator(@operator);
[11559]188
189      foreach (var strategyVectorCreator in Operators.OfType<IRealVectorStdDevStrategyParameterCreator>())
190        strategyVectorCreator.BoundsParameter.ValueChanged += strategyVectorCreator_BoundsParameter_ValueChanged;
[11550]191    }
192    #endregion
193
[11559]194
195    private void strategyVectorCreator_BoundsParameter_ValueChanged(object sender, EventArgs e) {
196      var boundsParameter = (IValueLookupParameter<DoubleMatrix>)sender;
197      if (boundsParameter.Value == null) return;
198      foreach (var strategyVectorManipulator in Operators.OfType<IRealVectorStdDevStrategyParameterManipulator>())
199        strategyVectorManipulator.BoundsParameter.Value = (DoubleMatrix)boundsParameter.Value.Clone();
200    }
201
[13396]202    public override void ConfigureOperators(IEnumerable<IItem> operators) {
[11546]203      ConfigureCreators(operators.OfType<IRealVectorCreator>());
204      ConfigureCrossovers(operators.OfType<IRealVectorCrossover>());
205      ConfigureManipulators(operators.OfType<IRealVectorManipulator>());
206      ConfigureStdDevStrategyParameterOperators(operators.OfType<IRealVectorStdDevStrategyParameterOperator>());
207      ConfigureSwarmUpdaters(operators.OfType<IRealVectorSwarmUpdater>());
208      ConfigureParticleCreators(operators.OfType<IRealVectorParticleCreator>());
209      ConfigureParticleUpdaters(operators.OfType<IRealVectorParticleUpdater>());
210      ConfigureShakingOperators(operators.OfType<IRealVectorMultiNeighborhoodShakingOperator>());
[11550]211      ConfigureBoundsCheckers(operators.OfType<IRealVectorBoundsChecker>());
[11559]212      ConfigureMoveGenerators(operators.OfType<IRealVectorMoveGenerator>());
[11550]213      ConfigureMoveOperators(operators.OfType<IRealVectorMoveOperator>());
[13403]214      ConfigureAdditiveMoveOperator(operators.OfType<IRealVectorAdditiveMoveOperator>());
215      ConfigureRealVectorSolutionOperators(operators.OfType<IRealVectorSolutionOperator>());
216      ConfigureRealVectorSolutionsOperators(operators.OfType<IRealVectorSolutionsOperator>());
217      ConfigureRealVectorBoundedOperators(operators.OfType<IRealVectorBoundedOperator>());
[11546]218    }
219
[11550]220    #region Specific Operator Wiring
[11546]221    private void ConfigureCreators(IEnumerable<IRealVectorCreator> creators) {
222      foreach (var creator in creators) {
223        creator.LengthParameter.ActualName = LengthParameter.Name;
224      }
225    }
226    private void ConfigureCrossovers(IEnumerable<IRealVectorCrossover> crossovers) {
227      foreach (var crossover in crossovers) {
228        crossover.ChildParameter.ActualName = Name;
229        crossover.ParentsParameter.ActualName = Name;
230      }
231    }
232    private void ConfigureManipulators(IEnumerable<IRealVectorManipulator> manipulators) {
233      foreach (var manipulator in manipulators) {
234        var sm = manipulator as ISelfAdaptiveManipulator;
235        if (sm != null) {
236          var p = sm.StrategyParameterParameter as ILookupParameter;
237          if (p != null) {
[11593]238            p.ActualName = Name + ".Strategy";
[11546]239          }
240        }
241      }
242    }
243    private void ConfigureStdDevStrategyParameterOperators(IEnumerable<IRealVectorStdDevStrategyParameterOperator> strategyOperators) {
[11559]244      var bounds = new DoubleMatrix(Bounds.Rows, Bounds.Columns);
245      for (var i = 0; i < Bounds.Rows; i++) {
[11546]246        bounds[i, 0] = 0;
[11559]247        bounds[i, 1] = 0.1 * (Bounds[i, 1] - Bounds[i, 0]);
[11546]248      }
249      foreach (var s in strategyOperators) {
250        var c = s as IRealVectorStdDevStrategyParameterCreator;
251        if (c != null) {
252          c.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();
253          c.LengthParameter.ActualName = LengthParameter.Name;
[11593]254          c.StrategyParameterParameter.ActualName = Name + ".Strategy";
[11546]255        }
256        var m = s as IRealVectorStdDevStrategyParameterManipulator;
257        if (m != null) {
258          m.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();
[11593]259          m.StrategyParameterParameter.ActualName = Name + ".Strategy";
[11546]260        }
261        var mm = s as StdDevStrategyVectorManipulator;
262        if (mm != null) {
263          mm.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Length));
264          mm.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(Length)));
265        }
266        var x = s as IRealVectorStdDevStrategyParameterCrossover;
267        if (x != null) {
[11593]268          x.ParentsParameter.ActualName = Name + ".Strategy";
269          x.StrategyParameterParameter.ActualName = Name + ".Strategy";
[11546]270        }
271      }
272    }
273    private void ConfigureSwarmUpdaters(IEnumerable<IRealVectorSwarmUpdater> swarmUpdaters) {
[13403]274      // swarm updaters don't have additional parameters besides the solution parameter
[11546]275    }
276    private void ConfigureParticleCreators(IEnumerable<IRealVectorParticleCreator> particleCreators) {
277      foreach (var particleCreator in particleCreators) {
278        particleCreator.ProblemSizeParameter.ActualName = LengthParameter.Name;
279      }
280    }
281    private void ConfigureParticleUpdaters(IEnumerable<IRealVectorParticleUpdater> particleUpdaters) {
[13403]282      // particle updaters don't have additional parameters besides solution and bounds parameter
[11546]283    }
284    private void ConfigureShakingOperators(IEnumerable<IRealVectorMultiNeighborhoodShakingOperator> shakingOperators) {
[13403]285      // shaking operators don't have additional parameters besides solution and bounds parameter
[11546]286    }
[11550]287    private void ConfigureBoundsCheckers(IEnumerable<IRealVectorBoundsChecker> boundsCheckers) {
288      foreach (var boundsChecker in boundsCheckers) {
289        boundsChecker.RealVectorParameter.ActualName = Name;
290        boundsChecker.BoundsParameter.ActualName = BoundsParameter.Name;
291      }
292    }
293    private void ConfigureMoveOperators(IEnumerable<IRealVectorMoveOperator> moveOperators) {
[13403]294      // move operators don't have additional parameters besides the solution parameter
[11550]295    }
296    private void ConfigureMoveGenerators(IEnumerable<IRealVectorMoveGenerator> moveGenerators) {
[13403]297      // move generators don't have additional parameters besides solution and bounds parameter
[11550]298    }
[13403]299    private void ConfigureAdditiveMoveOperator(IEnumerable<IRealVectorAdditiveMoveOperator> additiveMoveOperators) {
[11559]300      foreach (var additiveMoveOperator in additiveMoveOperators) {
[11593]301        additiveMoveOperator.AdditiveMoveParameter.ActualName = Name + ".AdditiveMove";
[11559]302      }
303    }
[13403]304    private void ConfigureRealVectorSolutionOperators(IEnumerable<IRealVectorSolutionOperator> solutionOperators) {
305      foreach (var solutionOperator in solutionOperators)
306        solutionOperator.RealVectorParameter.ActualName = Name;
307    }
308    private void ConfigureRealVectorSolutionsOperators(IEnumerable<IRealVectorSolutionsOperator> solutionsOperators) {
309      foreach (var solutionsOperator in solutionsOperators)
310        solutionsOperator.RealVectorsParameter.ActualName = Name;
311    }
312    private void ConfigureRealVectorBoundedOperators(IEnumerable<IRealVectorBoundedOperator> boundedOperators) {
313      foreach (var boundedOperator in boundedOperators) {
314        boundedOperator.BoundsParameter.ActualName = BoundsParameter.Name;
315      }
316    }
[11550]317    #endregion
[11484]318  }
[11546]319}
Note: See TracBrowser for help on using the repository browser.