Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2174: Adapted access modifiers of IntegerVectorEncoding.

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