Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2174: Fixed addition of parameters in the encodings and set default values for encoding operators in MultiEncodingOperator.

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