Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/IntegerEncoding.cs @ 11587

Last change on this file since 11587 was 11587, checked in by mkommend, 9 years ago

#2174: Implemented multi-encoding operators and adapated wiring of operators in the programmable problems.

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