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 | SolutionCreator = new UniformRandomIntegerVectorCreator();
|
---|
81 | DiscoverOperators();
|
---|
82 | RegisterEventHandlers();
|
---|
83 | }
|
---|
84 | public IntegerVectorEncoding(string name, int length, IList<int> min, IList<int> max, IList<int> step = null)
|
---|
85 | : base(name, length) {
|
---|
86 | if (min.Count == 0) throw new ArgumentException("Bounds must be given for the integer parameters.");
|
---|
87 | if (min.Count != max.Count) throw new ArgumentException("min must be of the same length as max", "min");
|
---|
88 | if (step != null && min.Count != step.Count) throw new ArgumentException("step must be of the same length as min or null", "step");
|
---|
89 | if (min.Zip(max, (mi, ma) => mi >= ma).Any(x => x)) throw new ArgumentException("min must be less than max in each dimension", "min");
|
---|
90 |
|
---|
91 | var bounds = new IntMatrix(min.Count, step != null ? 3 : 2);
|
---|
92 | for (int i = 0; i < min.Count; i++) {
|
---|
93 | bounds[i, 0] = min[i];
|
---|
94 | bounds[i, 1] = max[i];
|
---|
95 | if (step != null) bounds[i, 2] = step[i];
|
---|
96 | }
|
---|
97 |
|
---|
98 | BoundsParameter = new ValueParameter<IntMatrix>(Name + ".Bounds", bounds);
|
---|
99 | Parameters.Add(BoundsParameter);
|
---|
100 |
|
---|
101 | SolutionCreator = new UniformRandomIntegerVectorCreator();
|
---|
102 | DiscoverOperators();
|
---|
103 | RegisterEventHandlers();
|
---|
104 | }
|
---|
105 |
|
---|
106 | private void RegisterEventHandlers() {
|
---|
107 | IntMatrixParameterChangeHandler.Create(BoundsParameter, () => {
|
---|
108 | ConfigureOperators(Operators);
|
---|
109 | OnBoundsChanged();
|
---|
110 | });
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | #region Operator Discovery
|
---|
115 | private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
|
---|
116 | static IntegerVectorEncoding() {
|
---|
117 | encodingSpecificOperatorTypes = new List<Type>() {
|
---|
118 | typeof (IIntegerVectorOperator),
|
---|
119 | typeof (IIntegerVectorCreator),
|
---|
120 | typeof (IIntegerVectorCrossover),
|
---|
121 | typeof (IIntegerVectorManipulator),
|
---|
122 | typeof (IIntegerVectorStdDevStrategyParameterOperator),
|
---|
123 | typeof (IIntegerVectorMultiNeighborhoodShakingOperator),
|
---|
124 | };
|
---|
125 | }
|
---|
126 | private void DiscoverOperators() {
|
---|
127 | var assembly = typeof(IIntegerVectorOperator).Assembly;
|
---|
128 | var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
|
---|
129 | var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
|
---|
130 | var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
|
---|
131 |
|
---|
132 | ConfigureOperators(newOperators);
|
---|
133 | foreach (var @operator in newOperators)
|
---|
134 | AddOperator(@operator);
|
---|
135 | }
|
---|
136 | #endregion
|
---|
137 |
|
---|
138 | public override void ConfigureOperators(IEnumerable<IItem> operators) {
|
---|
139 | base.ConfigureOperators(operators);
|
---|
140 | ConfigureBoundedOperators(operators.OfType<IBoundedIntegerVectorOperator>());
|
---|
141 | ConfigureCreators(operators.OfType<IIntegerVectorCreator>());
|
---|
142 | ConfigureCrossovers(operators.OfType<IIntegerVectorCrossover>());
|
---|
143 | ConfigureManipulators(operators.OfType<IIntegerVectorManipulator>());
|
---|
144 | ConfigureShakingOperators(operators.OfType<IIntegerVectorMultiNeighborhoodShakingOperator>());
|
---|
145 | ConfigureStrategyVectorOperator(operators.OfType<IIntegerVectorStdDevStrategyParameterOperator>());
|
---|
146 | }
|
---|
147 |
|
---|
148 | #region Specific Operator Wiring
|
---|
149 | private void ConfigureBoundedOperators(IEnumerable<IBoundedIntegerVectorOperator> boundedOperators) {
|
---|
150 | foreach (var boundedOperator in boundedOperators) {
|
---|
151 | boundedOperator.BoundsParameter.ActualName = BoundsParameter.Name;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | private void ConfigureCreators(IEnumerable<IIntegerVectorCreator> creators) {
|
---|
156 | foreach (var creator in creators) {
|
---|
157 | creator.IntegerVectorParameter.ActualName = Name;
|
---|
158 | creator.BoundsParameter.ActualName = BoundsParameter.Name;
|
---|
159 | creator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | private void ConfigureCrossovers(IEnumerable<IIntegerVectorCrossover> crossovers) {
|
---|
164 | foreach (var crossover in crossovers) {
|
---|
165 | crossover.ChildParameter.ActualName = Name;
|
---|
166 | crossover.ParentsParameter.ActualName = Name;
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | private void ConfigureManipulators(IEnumerable<IIntegerVectorManipulator> manipulators) {
|
---|
171 | foreach (var manipulator in manipulators) {
|
---|
172 | manipulator.IntegerVectorParameter.ActualName = Name;
|
---|
173 | manipulator.IntegerVectorParameter.Hidden = true;
|
---|
174 | var sm = manipulator as ISelfAdaptiveManipulator;
|
---|
175 | if (sm != null) {
|
---|
176 | var p = sm.StrategyParameterParameter as ILookupParameter;
|
---|
177 | if (p != null) {
|
---|
178 | p.ActualName = Name + "Strategy";
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | private void ConfigureShakingOperators(IEnumerable<IIntegerVectorMultiNeighborhoodShakingOperator> shakingOperators) {
|
---|
185 | foreach (var shakingOperator in shakingOperators) {
|
---|
186 | shakingOperator.IntegerVectorParameter.ActualName = Name;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | private void ConfigureStrategyVectorOperator(IEnumerable<IIntegerVectorStdDevStrategyParameterOperator> strategyVectorOperators) {
|
---|
191 | var bounds = new DoubleMatrix(Bounds.Rows, Bounds.Columns);
|
---|
192 | for (var i = 0; i < bounds.Rows; i++) {
|
---|
193 | bounds[i, 1] = (int)Math.Ceiling(0.33 * (Bounds[i, 1] - Bounds[i, 0]));
|
---|
194 | bounds[i, 0] = 0;
|
---|
195 | if (bounds.Columns > 2) bounds[i, 2] = Bounds[i, 2];
|
---|
196 | }
|
---|
197 | foreach (var s in strategyVectorOperators) {
|
---|
198 | var c = s as IIntegerVectorStdDevStrategyParameterCreator;
|
---|
199 | if (c != null) {
|
---|
200 | c.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();
|
---|
201 | c.LengthParameter.ActualName = Name;
|
---|
202 | c.StrategyParameterParameter.ActualName = Name + "Strategy";
|
---|
203 | }
|
---|
204 | var m = s as IIntegerVectorStdDevStrategyParameterManipulator;
|
---|
205 | if (m != null) {
|
---|
206 | m.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();
|
---|
207 | m.StrategyParameterParameter.ActualName = Name + "Strategy";
|
---|
208 | }
|
---|
209 | var mm = s as StdDevStrategyVectorManipulator;
|
---|
210 | if (mm != null) {
|
---|
211 | mm.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Length));
|
---|
212 | mm.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(Length)));
|
---|
213 | }
|
---|
214 | var x = s as IIntegerVectorStdDevStrategyParameterCrossover;
|
---|
215 | if (x != null) {
|
---|
216 | x.ParentsParameter.ActualName = Name + "Strategy";
|
---|
217 | x.StrategyParameterParameter.ActualName = Name + "Strategy";
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 | #endregion
|
---|
222 |
|
---|
223 | protected override void OnLengthChanged() {
|
---|
224 | ConfigureOperators(Operators);
|
---|
225 | base.OnLengthChanged();
|
---|
226 | }
|
---|
227 |
|
---|
228 | public event EventHandler BoundsChanged;
|
---|
229 | private void OnBoundsChanged() {
|
---|
230 | BoundsChanged?.Invoke(this, EventArgs.Empty);
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|