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