[11484] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16565] | 3 | * Copyright (C) 2002-2019 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[11561] | 28 | using HeuristicLab.Optimization;
|
---|
[11588] | 29 | using HeuristicLab.Parameters;
|
---|
[16565] | 30 | using HEAL.Attic;
|
---|
[11561] | 31 | using HeuristicLab.PluginInfrastructure;
|
---|
[11484] | 32 |
|
---|
[11949] | 33 | namespace HeuristicLab.Encodings.IntegerVectorEncoding {
|
---|
[11885] | 34 | [Item("IntegerVectorEncoding", "Describes an integer vector encoding.")]
|
---|
[16565] | 35 | [StorableType("15D6E55E-C39F-4784-8350-14A0FD47CF0E")]
|
---|
[11885] | 36 | public sealed class IntegerVectorEncoding : Encoding<IIntegerVectorCreator> {
|
---|
[11561] | 37 | #region Encoding Parameters
|
---|
[11598] | 38 | [Storable]
|
---|
[11561] | 39 | private IFixedValueParameter<IntValue> lengthParameter;
|
---|
| 40 | public IFixedValueParameter<IntValue> LengthParameter {
|
---|
| 41 | get { return lengthParameter; }
|
---|
[11484] | 42 | set {
|
---|
[11561] | 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.");
|
---|
[11561] | 45 | if (lengthParameter == value) return;
|
---|
[11588] | 46 |
|
---|
| 47 | if (lengthParameter != null) Parameters.Remove(lengthParameter);
|
---|
[11561] | 48 | lengthParameter = value;
|
---|
[11588] | 49 | Parameters.Add(lengthParameter);
|
---|
[11561] | 50 | OnLengthParameterChanged();
|
---|
[11484] | 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[11598] | 54 | [Storable]
|
---|
[11561] | 55 | private IValueParameter<IntMatrix> boundsParameter;
|
---|
| 56 | public IValueParameter<IntMatrix> BoundsParameter {
|
---|
| 57 | get { return boundsParameter; }
|
---|
[11484] | 58 | set {
|
---|
[11561] | 59 | if (value == null) throw new ArgumentNullException("Bounds parameter must not be null.");
|
---|
| 60 | if (boundsParameter == value) return;
|
---|
[11588] | 61 |
|
---|
| 62 | if (boundsParameter != null) Parameters.Remove(boundsParameter);
|
---|
[11561] | 63 | boundsParameter = value;
|
---|
[11588] | 64 | Parameters.Add(boundsParameter);
|
---|
[11561] | 65 | OnBoundsParameterChanged();
|
---|
[11484] | 66 | }
|
---|
| 67 | }
|
---|
[11561] | 68 | #endregion
|
---|
| 69 |
|
---|
| 70 | public int Length {
|
---|
| 71 | get { return LengthParameter.Value.Value; }
|
---|
| 72 | set { LengthParameter.Value.Value = value; }
|
---|
| 73 | }
|
---|
| 74 | public IntMatrix Bounds {
|
---|
| 75 | get { return BoundsParameter.Value; }
|
---|
| 76 | set { BoundsParameter.Value = value; }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[11484] | 79 | [StorableConstructor]
|
---|
[16565] | 80 | private IntegerVectorEncoding(StorableConstructorFlag _) : base(_) { }
|
---|
[11561] | 81 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 82 | private void AfterDeserialization() {
|
---|
| 83 | RegisterParameterEvents();
|
---|
| 84 | DiscoverOperators();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[11885] | 87 | private IntegerVectorEncoding(IntegerVectorEncoding original, Cloner cloner)
|
---|
[11484] | 88 | : base(original, cloner) {
|
---|
[11561] | 89 | lengthParameter = cloner.Clone(original.lengthParameter);
|
---|
| 90 | boundsParameter = cloner.Clone(original.boundsParameter);
|
---|
| 91 | RegisterParameterEvents();
|
---|
[11484] | 92 | }
|
---|
[11885] | 93 | public override IDeepCloneable Clone(Cloner cloner) { return new IntegerVectorEncoding(this, cloner); }
|
---|
[11561] | 94 |
|
---|
[11739] | 95 |
|
---|
[11885] | 96 | public IntegerVectorEncoding() : this("IntegerVector", 10) { }
|
---|
[11892] | 97 | public IntegerVectorEncoding(string name) : this(name, 10) { }
|
---|
[11885] | 98 | public IntegerVectorEncoding(int length) : this("integerVector", length) { }
|
---|
| 99 | public IntegerVectorEncoding(string name, int length, int min = int.MinValue, int max = int.MaxValue, int? step = null)
|
---|
[11484] | 100 | : base(name) {
|
---|
| 101 | if (min >= max) throw new ArgumentException("min must be less than max", "min");
|
---|
| 102 | if (step.HasValue && step.Value <= 0) throw new ArgumentException("step must be greater than zero or null", "step");
|
---|
[11587] | 103 |
|
---|
[11588] | 104 | var bounds = new IntMatrix(1, step.HasValue ? 3 : 2);
|
---|
| 105 | bounds[0, 0] = min;
|
---|
| 106 | bounds[0, 1] = max;
|
---|
| 107 | if (step.HasValue) bounds[0, 2] = step.Value;
|
---|
[11587] | 108 |
|
---|
[11593] | 109 | lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
|
---|
| 110 | boundsParameter = new ValueParameter<IntMatrix>(Name + ".Bounds", bounds);
|
---|
| 111 | Parameters.Add(lengthParameter);
|
---|
| 112 | Parameters.Add(boundsParameter);
|
---|
[11588] | 113 |
|
---|
[11587] | 114 | SolutionCreator = new UniformRandomIntegerVectorCreator();
|
---|
[11561] | 115 | RegisterParameterEvents();
|
---|
| 116 | DiscoverOperators();
|
---|
[11484] | 117 | }
|
---|
[11885] | 118 | public IntegerVectorEncoding(string name, int length, IList<int> min, IList<int> max, IList<int> step = null)
|
---|
[11484] | 119 | : base(name) {
|
---|
| 120 | if (min.Count == 0) throw new ArgumentException("Bounds must be given for the integer parameters.");
|
---|
| 121 | if (min.Count != max.Count) throw new ArgumentException("min must be of the same length as max", "min");
|
---|
| 122 | if (step != null && min.Count != step.Count) throw new ArgumentException("step must be of the same length as min or null", "step");
|
---|
| 123 | if (min.Zip(max, (mi, ma) => mi >= ma).Any(x => x)) throw new ArgumentException("min must be less than max in each dimension", "min");
|
---|
[11587] | 124 |
|
---|
[11588] | 125 | var bounds = new IntMatrix(min.Count, step != null ? 3 : 2);
|
---|
[11484] | 126 | for (int i = 0; i < min.Count; i++) {
|
---|
[11588] | 127 | bounds[i, 0] = min[i];
|
---|
| 128 | bounds[i, 1] = max[i];
|
---|
| 129 | if (step != null) bounds[i, 2] = step[i];
|
---|
[11484] | 130 | }
|
---|
[11587] | 131 |
|
---|
[12837] | 132 | lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
|
---|
| 133 | boundsParameter = new ValueParameter<IntMatrix>(Name + ".Bounds", bounds);
|
---|
| 134 | Parameters.Add(lengthParameter);
|
---|
| 135 | Parameters.Add(boundsParameter);
|
---|
[11588] | 136 |
|
---|
[11587] | 137 | SolutionCreator = new UniformRandomIntegerVectorCreator();
|
---|
[11561] | 138 | RegisterParameterEvents();
|
---|
| 139 | DiscoverOperators();
|
---|
[11484] | 140 | }
|
---|
| 141 |
|
---|
[11561] | 142 | private void OnLengthParameterChanged() {
|
---|
| 143 | RegisterLengthParameterEvents();
|
---|
| 144 | ConfigureOperators(Operators);
|
---|
[11484] | 145 | }
|
---|
[11561] | 146 | private void OnBoundsParameterChanged() {
|
---|
| 147 | RegisterBoundsParameterEvents();
|
---|
| 148 | ConfigureOperators(Operators);
|
---|
| 149 | }
|
---|
[11484] | 150 |
|
---|
[11561] | 151 | private void RegisterParameterEvents() {
|
---|
| 152 | RegisterLengthParameterEvents();
|
---|
| 153 | RegisterBoundsParameterEvents();
|
---|
| 154 | }
|
---|
| 155 | private void RegisterLengthParameterEvents() {
|
---|
| 156 | LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 157 | LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 158 | }
|
---|
| 159 | private void RegisterBoundsParameterEvents() {
|
---|
| 160 | BoundsParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 161 | boundsParameter.Value.ToStringChanged += (o, s) => ConfigureOperators(Operators);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | #region Operator Discovery
|
---|
| 166 | private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
|
---|
[11885] | 167 | static IntegerVectorEncoding() {
|
---|
[11561] | 168 | encodingSpecificOperatorTypes = new List<Type>() {
|
---|
| 169 | typeof (IIntegerVectorOperator),
|
---|
| 170 | typeof (IIntegerVectorCreator),
|
---|
| 171 | typeof (IIntegerVectorCrossover),
|
---|
| 172 | typeof (IIntegerVectorManipulator),
|
---|
| 173 | typeof (IIntegerVectorStdDevStrategyParameterOperator),
|
---|
| 174 | typeof (IIntegerVectorMultiNeighborhoodShakingOperator),
|
---|
| 175 | };
|
---|
| 176 | }
|
---|
| 177 | private void DiscoverOperators() {
|
---|
[11773] | 178 | var assembly = typeof(IIntegerVectorOperator).Assembly;
|
---|
| 179 | var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
|
---|
[11561] | 180 | var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
|
---|
[11587] | 181 | var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
|
---|
[11561] | 182 |
|
---|
| 183 | ConfigureOperators(newOperators);
|
---|
[11587] | 184 | foreach (var @operator in newOperators)
|
---|
[11952] | 185 | AddOperator(@operator);
|
---|
[11561] | 186 | }
|
---|
| 187 | #endregion
|
---|
| 188 |
|
---|
| 189 | public override void ConfigureOperators(IEnumerable<IOperator> operators) {
|
---|
[11746] | 190 | ConfigureBoundedOperators(operators.OfType<IBoundedIntegerVectorOperator>());
|
---|
| 191 | ConfigureCreators(operators.OfType<IIntegerVectorCreator>());
|
---|
| 192 | ConfigureCrossovers(operators.OfType<IIntegerVectorCrossover>());
|
---|
| 193 | ConfigureManipulators(operators.OfType<IIntegerVectorManipulator>());
|
---|
| 194 | ConfigureShakingOperators(operators.OfType<IIntegerVectorMultiNeighborhoodShakingOperator>());
|
---|
| 195 | ConfigureStrategyVectorOperator(operators.OfType<IIntegerVectorStdDevStrategyParameterOperator>());
|
---|
[11561] | 196 | }
|
---|
| 197 |
|
---|
| 198 | #region Specific Operator Wiring
|
---|
| 199 | private void ConfigureBoundedOperators(IEnumerable<IBoundedIntegerVectorOperator> boundedOperators) {
|
---|
| 200 | foreach (var boundedOperator in boundedOperators) {
|
---|
| 201 | boundedOperator.BoundsParameter.ActualName = BoundsParameter.Name;
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | private void ConfigureCreators(IEnumerable<IIntegerVectorCreator> creators) {
|
---|
| 206 | foreach (var creator in creators) {
|
---|
| 207 | creator.IntegerVectorParameter.ActualName = Name;
|
---|
| 208 | creator.BoundsParameter.ActualName = BoundsParameter.Name;
|
---|
| 209 | creator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | private void ConfigureCrossovers(IEnumerable<IIntegerVectorCrossover> crossovers) {
|
---|
| 214 | foreach (var crossover in crossovers) {
|
---|
| 215 | crossover.ChildParameter.ActualName = Name;
|
---|
| 216 | crossover.ParentsParameter.ActualName = Name;
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | private void ConfigureManipulators(IEnumerable<IIntegerVectorManipulator> manipulators) {
|
---|
| 221 | foreach (var manipulator in manipulators) {
|
---|
| 222 | manipulator.IntegerVectorParameter.ActualName = Name;
|
---|
| 223 | manipulator.IntegerVectorParameter.Hidden = true;
|
---|
| 224 | var sm = manipulator as ISelfAdaptiveManipulator;
|
---|
| 225 | if (sm != null) {
|
---|
| 226 | var p = sm.StrategyParameterParameter as ILookupParameter;
|
---|
| 227 | if (p != null) {
|
---|
| 228 | p.ActualName = Name + "Strategy";
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | private void ConfigureShakingOperators(IEnumerable<IIntegerVectorMultiNeighborhoodShakingOperator> shakingOperators) {
|
---|
| 235 | foreach (var shakingOperator in shakingOperators) {
|
---|
| 236 | shakingOperator.IntegerVectorParameter.ActualName = Name;
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | private void ConfigureStrategyVectorOperator(IEnumerable<IIntegerVectorStdDevStrategyParameterOperator> strategyVectorOperators) {
|
---|
| 241 | var bounds = new DoubleMatrix(Bounds.Rows, Bounds.Columns);
|
---|
| 242 | for (var i = 0; i < bounds.Rows; i++) {
|
---|
| 243 | bounds[i, 1] = (int)Math.Ceiling(0.33 * (Bounds[i, 1] - Bounds[i, 0]));
|
---|
| 244 | bounds[i, 0] = 0;
|
---|
| 245 | if (bounds.Columns > 2) bounds[i, 2] = Bounds[i, 2];
|
---|
| 246 | }
|
---|
| 247 | foreach (var s in strategyVectorOperators) {
|
---|
| 248 | var c = s as IIntegerVectorStdDevStrategyParameterCreator;
|
---|
| 249 | if (c != null) {
|
---|
| 250 | c.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();
|
---|
| 251 | c.LengthParameter.ActualName = Name;
|
---|
| 252 | c.StrategyParameterParameter.ActualName = Name + "Strategy";
|
---|
| 253 | }
|
---|
| 254 | var m = s as IIntegerVectorStdDevStrategyParameterManipulator;
|
---|
| 255 | if (m != null) {
|
---|
| 256 | m.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();
|
---|
| 257 | m.StrategyParameterParameter.ActualName = Name + "Strategy";
|
---|
| 258 | }
|
---|
| 259 | var mm = s as StdDevStrategyVectorManipulator;
|
---|
| 260 | if (mm != null) {
|
---|
| 261 | mm.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Length));
|
---|
| 262 | mm.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(Length)));
|
---|
| 263 | }
|
---|
| 264 | var x = s as IIntegerVectorStdDevStrategyParameterCrossover;
|
---|
| 265 | if (x != null) {
|
---|
| 266 | x.ParentsParameter.ActualName = Name + "Strategy";
|
---|
| 267 | x.StrategyParameterParameter.ActualName = Name + "Strategy";
|
---|
| 268 | }
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 | #endregion
|
---|
[11484] | 272 | }
|
---|
[11949] | 273 |
|
---|
| 274 | public static class IndividualExtensionMethods {
|
---|
| 275 | public static IntegerVector IntegerVector(this Individual individual) {
|
---|
| 276 | var encoding = individual.GetEncoding<IntegerVectorEncoding>();
|
---|
| 277 | return individual.IntegerVector(encoding.Name);
|
---|
| 278 | }
|
---|
| 279 | public static IntegerVector IntegerVector(this Individual individual, string name) {
|
---|
| 280 | return (IntegerVector)individual[name];
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
[11484] | 283 | }
|
---|