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