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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
30 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
31 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
32 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
33 | using HeuristicLab.Optimization;
|
---|
34 | using HeuristicLab.Parameters;
|
---|
35 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
36 | using HeuristicLab.PluginInfrastructure;
|
---|
37 |
|
---|
38 | namespace HeuristicLab.Problems.Programmable {
|
---|
39 | [Item("Programmable Problem (multi-objective)", "Represents a multi-objective problem that can be programmed.")]
|
---|
40 | [Creatable("Problems")]
|
---|
41 | [StorableClass]
|
---|
42 | public class MultiObjectiveProgrammableProblem : MultiObjectiveHeuristicOptimizationProblem<IMultiObjectiveProgrammableProblemEvaluator, ISolutionCreator>, IParameterizedNamedItem, IStorableContent {
|
---|
43 | public string Filename { get; set; }
|
---|
44 |
|
---|
45 | public static new Image StaticItemImage {
|
---|
46 | get { return Common.Resources.VSImageLibrary.Script; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public new ParameterCollection Parameters {
|
---|
50 | get { return base.Parameters; }
|
---|
51 | }
|
---|
52 | IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
|
---|
53 | get { return Parameters; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public IValueParameter<IMultiObjectiveProblemDefinitionHost> ProblemDefinitionParameter {
|
---|
57 | get { return (IValueParameter<IMultiObjectiveProblemDefinitionHost>)Parameters["ProblemDefinition"]; }
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected IValueParameter<Configuration> ConfigurationParameter {
|
---|
61 | get { return (IValueParameter<Configuration>)Parameters["Configuration"]; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public IMultiObjectiveProblemDefinitionHost ProblemDefinition {
|
---|
65 | get { return ProblemDefinitionParameter.Value; }
|
---|
66 | set { ProblemDefinitionParameter.Value = value; }
|
---|
67 | }
|
---|
68 |
|
---|
69 | [Storable]
|
---|
70 | protected List<IParameter> DynamicConfigurationParameters;
|
---|
71 |
|
---|
72 | [StorableConstructor]
|
---|
73 | protected MultiObjectiveProgrammableProblem(bool deserializing) : base(deserializing) { }
|
---|
74 | protected MultiObjectiveProgrammableProblem(MultiObjectiveProgrammableProblem original, Cloner cloner)
|
---|
75 | : base(original, cloner) {
|
---|
76 | DynamicConfigurationParameters = original.DynamicConfigurationParameters.Select(cloner.Clone).ToList();
|
---|
77 | RegisterEventHandlers();
|
---|
78 | }
|
---|
79 | public MultiObjectiveProgrammableProblem()
|
---|
80 | : base(new MultiObjectiveEvaluator(), new ParameterVectorCreater()) {
|
---|
81 | Parameters.Add(new ValueParameter<IMultiObjectiveProblemDefinitionHost>("ProblemDefinition", "Defines the problem.", new MultiObjectiveProblemDefinitionScript() { Name = Name }));
|
---|
82 | Parameters.Add(new ValueParameter<Configuration>("Configuration", "Describes which parameters exist, what they're called, what type they are and their bounds if any."));
|
---|
83 |
|
---|
84 | DynamicConfigurationParameters = new List<IParameter>();
|
---|
85 |
|
---|
86 | Operators.Add(new MultiObjectiveAnalyzer());
|
---|
87 | Operators.Add(Evaluator);
|
---|
88 | Operators.Add(SolutionCreator);
|
---|
89 |
|
---|
90 | RegisterEventHandlers();
|
---|
91 | }
|
---|
92 |
|
---|
93 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
94 | return new MultiObjectiveProgrammableProblem(this, cloner);
|
---|
95 | }
|
---|
96 |
|
---|
97 | [StorableHook(HookType.AfterDeserialization)]
|
---|
98 | // ReSharper disable UnusedMember.Local
|
---|
99 | private void AfterDeserialization() {
|
---|
100 | RegisterEventHandlers();
|
---|
101 | }
|
---|
102 | // ReSharper restore UnusedMember.Local
|
---|
103 |
|
---|
104 | private void RegisterEventHandlers() {
|
---|
105 | ProblemDefinitionParameter.ValueChanged += ProblemDefinitionParameterOnValueChanged;
|
---|
106 | RegisterHostInstanceChanges();
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void ProblemDefinitionParameterOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
110 | RegisterHostInstanceChanges();
|
---|
111 | Parameterize();
|
---|
112 | }
|
---|
113 |
|
---|
114 | private void RegisterHostInstanceChanges() {
|
---|
115 | ProblemDefinitionParameter.Value.InstanceChanged += ProblemDefinitionHostOnInstanceChanged;
|
---|
116 | ProblemDefinitionParameter.Value.NameChanged += ProblemDefinitionHostOnNameChanged;
|
---|
117 | }
|
---|
118 |
|
---|
119 | private void ProblemDefinitionHostOnNameChanged(object sender, EventArgs eventArgs) {
|
---|
120 | if (sender != ProblemDefinitionParameter.Value) return;
|
---|
121 | Name = ProblemDefinitionParameter.Value.Name;
|
---|
122 | }
|
---|
123 |
|
---|
124 | protected override void OnNameChanged() {
|
---|
125 | base.OnNameChanged();
|
---|
126 | ProblemDefinitionParameter.Value.Name = Name;
|
---|
127 | }
|
---|
128 |
|
---|
129 | protected override void OnEvaluatorChanged() {
|
---|
130 | base.OnEvaluatorChanged();
|
---|
131 | Parameterize();
|
---|
132 | }
|
---|
133 |
|
---|
134 | protected virtual void ProblemDefinitionHostOnInstanceChanged(object sender, EventArgs eventArgs) {
|
---|
135 | Parameterize();
|
---|
136 | }
|
---|
137 |
|
---|
138 | protected virtual void Parameterize() {
|
---|
139 | var instance = ProblemDefinitionParameter.Value.Instance;
|
---|
140 | if (instance == null) return;
|
---|
141 |
|
---|
142 | Configuration configuration;
|
---|
143 | try {
|
---|
144 | configuration = instance.GetConfiguration();
|
---|
145 | } catch { return; }
|
---|
146 | ConfigurationParameter.Value = configuration;
|
---|
147 | Maximization = new BoolArray(instance.Maximization);
|
---|
148 |
|
---|
149 | Evaluator.ConfigurationParameter.ActualName = ConfigurationParameter.Name;
|
---|
150 | Evaluator.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
|
---|
151 | foreach (var evalOp in Operators.OfType<IMultiObjectiveProgrammableProblemEvaluator>()) {
|
---|
152 | evalOp.ConfigurationParameter.ActualName = ConfigurationParameter.Name;
|
---|
153 | evalOp.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
|
---|
154 | }
|
---|
155 | foreach (var analyzeOp in Operators.OfType<IMultiObjectiveProgrammableProblemAnalyzer>()) {
|
---|
156 | analyzeOp.ConfigurationParameter.ActualName = ConfigurationParameter.Name;
|
---|
157 | analyzeOp.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
|
---|
158 | analyzeOp.QualitiesParameter.ActualName = Evaluator.QualitiesParameter.ActualName;
|
---|
159 | }
|
---|
160 |
|
---|
161 | var solutionCreators = UpdateDynamicConfigurationParameters(configuration);
|
---|
162 |
|
---|
163 | if (solutionCreators.Count == 1) {
|
---|
164 | Operators.RemoveAll(x => x is ParameterVectorCrossover
|
---|
165 | || x is ParameterVectorManipulator);
|
---|
166 | UpdateSingleVectorEncodingOperators(solutionCreators, configuration);
|
---|
167 | } else {
|
---|
168 | UpdateMultiVectorEncodingOperators(solutionCreators, configuration);
|
---|
169 | }
|
---|
170 | UpdateMoveOperators();
|
---|
171 | }
|
---|
172 |
|
---|
173 | protected virtual List<ISolutionCreator> UpdateDynamicConfigurationParameters(Configuration configuration) {
|
---|
174 | foreach (var param in DynamicConfigurationParameters)
|
---|
175 | if (Parameters.Contains(param)) Parameters.Remove(param);
|
---|
176 | DynamicConfigurationParameters.Clear();
|
---|
177 |
|
---|
178 | var solutionCreators = new List<ISolutionCreator>();
|
---|
179 | foreach (var param in configuration.Parameters) {
|
---|
180 | #region Configure BinaryVector Creator
|
---|
181 | var binConfig = param.Value as BinaryParameterConfiguration;
|
---|
182 | if (binConfig != null) {
|
---|
183 | var p = new ValueParameter<IntValue>(param.Key + "Length", binConfig.Length);
|
---|
184 | DynamicConfigurationParameters.Add(p);
|
---|
185 |
|
---|
186 | var creator = new RandomBinaryVectorCreator();
|
---|
187 | creator.BinaryVectorParameter.ActualName = param.Key;
|
---|
188 | creator.LengthParameter.ActualName = p.Name;
|
---|
189 | solutionCreators.Add(creator);
|
---|
190 | }
|
---|
191 | #endregion
|
---|
192 | #region Configure IntegerVector Creator
|
---|
193 | var intConfig = param.Value as IntegerParameterConfiguration;
|
---|
194 | if (intConfig != null) {
|
---|
195 | var l = new ValueParameter<IntValue>(param.Key + "Length", intConfig.Length);
|
---|
196 | var b = new ValueParameter<IntMatrix>(param.Key + "Bounds", intConfig.Bounds);
|
---|
197 | DynamicConfigurationParameters.Add(l);
|
---|
198 | DynamicConfigurationParameters.Add(b);
|
---|
199 |
|
---|
200 | var creator = new UniformRandomIntegerVectorCreator();
|
---|
201 | creator.IntegerVectorParameter.ActualName = param.Key;
|
---|
202 | creator.LengthParameter.ActualName = l.Name;
|
---|
203 | creator.BoundsParameter.ActualName = b.Name;
|
---|
204 | solutionCreators.Add(creator);
|
---|
205 | }
|
---|
206 | #endregion
|
---|
207 | #region Configure RealVector Creator
|
---|
208 | var realConfig = param.Value as RealParameterConfiguration;
|
---|
209 | if (realConfig != null) {
|
---|
210 | var l = new ValueParameter<IntValue>(param.Key + "Length", realConfig.Length);
|
---|
211 | var b = new ValueParameter<DoubleMatrix>(param.Key + "Bounds", realConfig.Bounds);
|
---|
212 | DynamicConfigurationParameters.Add(l);
|
---|
213 | DynamicConfigurationParameters.Add(b);
|
---|
214 |
|
---|
215 | var creator = new UniformRandomRealVectorCreator();
|
---|
216 | creator.RealVectorParameter.ActualName = param.Key;
|
---|
217 | creator.LengthParameter.ActualName = l.Name;
|
---|
218 | creator.BoundsParameter.ActualName = b.Name;
|
---|
219 | solutionCreators.Add(creator);
|
---|
220 | }
|
---|
221 | #endregion
|
---|
222 | #region Configure Permutation Creator
|
---|
223 | var permConfig = param.Value as PermutationParameterConfiguration;
|
---|
224 | if (permConfig != null) {
|
---|
225 | var l = new ValueParameter<IntValue>(param.Key + "Length", permConfig.Length);
|
---|
226 | DynamicConfigurationParameters.Add(l);
|
---|
227 |
|
---|
228 | var creator = new RandomPermutationCreator();
|
---|
229 | creator.PermutationParameter.ActualName = param.Key;
|
---|
230 | creator.LengthParameter.ActualName = l.Name;
|
---|
231 | creator.PermutationTypeParameter.Value = permConfig.Type;
|
---|
232 | solutionCreators.Add(creator);
|
---|
233 | }
|
---|
234 | #endregion
|
---|
235 | }
|
---|
236 |
|
---|
237 | foreach (var param in DynamicConfigurationParameters) {
|
---|
238 | param.Hidden = true;
|
---|
239 | Parameters.Add(param);
|
---|
240 | }
|
---|
241 | return solutionCreators;
|
---|
242 | }
|
---|
243 |
|
---|
244 | protected virtual void UpdateSingleVectorEncodingOperators(List<ISolutionCreator> solutionCreators, Configuration configuration) {
|
---|
245 | var newCreator = solutionCreators.Single();
|
---|
246 | #region Configure Operators for BinaryVectorEncoding
|
---|
247 | var binCreator = newCreator as IBinaryVectorCreator;
|
---|
248 | if (binCreator != null) {
|
---|
249 | var paramName = binCreator.BinaryVectorParameter.ActualName;
|
---|
250 | // do not replace a binary vector creator that was manually set
|
---|
251 | if (!(SolutionCreator is IBinaryVectorCreator) || ((IBinaryVectorCreator)SolutionCreator).BinaryVectorParameter.ActualName != paramName) {
|
---|
252 | Operators.Remove(SolutionCreator);
|
---|
253 | SolutionCreator = newCreator;
|
---|
254 | Operators.Add(SolutionCreator);
|
---|
255 | }
|
---|
256 |
|
---|
257 | #region Wire BinaryVector Crossovers
|
---|
258 | Operators.RemoveAll(x => x is IBinaryVectorCrossover && ((IBinaryVectorCrossover)x).ChildParameter.ActualName != paramName);
|
---|
259 | var crossovers = ApplicationManager.Manager.GetInstances<IBinaryVectorCrossover>().ToList();
|
---|
260 | foreach (var xo in crossovers) {
|
---|
261 | xo.ChildParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;
|
---|
262 | xo.ChildParameter.Hidden = true;
|
---|
263 | xo.ParentsParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;
|
---|
264 | xo.ParentsParameter.Hidden = true;
|
---|
265 | }
|
---|
266 | Operators.AddRange(crossovers.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
267 | #endregion
|
---|
268 | #region Wire BinaryVector Manipulators
|
---|
269 | Operators.RemoveAll(x => x is IBinaryVectorManipulator && ((IBinaryVectorManipulator)x).BinaryVectorParameter.ActualName != paramName);
|
---|
270 | var manipulators = ApplicationManager.Manager.GetInstances<IBinaryVectorManipulator>().ToList();
|
---|
271 | foreach (var m in manipulators) {
|
---|
272 | m.BinaryVectorParameter.ActualName = binCreator.BinaryVectorParameter.ActualName;
|
---|
273 | m.BinaryVectorParameter.Hidden = true;
|
---|
274 | }
|
---|
275 | Operators.AddRange(manipulators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
276 | #endregion
|
---|
277 | #region Wire BinaryVector ShakingOperators
|
---|
278 | Operators.RemoveAll(x => x is IBinaryVectorMultiNeighborhoodShakingOperator && ((IBinaryVectorManipulator)x).BinaryVectorParameter.ActualName != paramName);
|
---|
279 | var shakingOperators = ApplicationManager.Manager.GetInstances<IBinaryVectorMultiNeighborhoodShakingOperator>().ToList();
|
---|
280 | foreach (var so in shakingOperators) {
|
---|
281 | so.BinaryVectorParameter.ActualName = paramName;
|
---|
282 | so.BinaryVectorParameter.Hidden = true;
|
---|
283 | }
|
---|
284 | Operators.AddRange(shakingOperators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
285 | #endregion
|
---|
286 | } else {
|
---|
287 | Operators.RemoveAll(x => x is IBinaryVectorCrossover
|
---|
288 | || x is IBinaryVectorManipulator
|
---|
289 | || x is IBinaryVectorMultiNeighborhoodShakingOperator);
|
---|
290 | }
|
---|
291 | #endregion
|
---|
292 | #region Configure Operators for IntegerVectorEncoding
|
---|
293 | var intCreator = newCreator as IIntegerVectorCreator;
|
---|
294 | if (intCreator != null) {
|
---|
295 | var paramName = intCreator.IntegerVectorParameter.ActualName;
|
---|
296 | // do not replace an integer vector creator that was manually set
|
---|
297 | if (!(SolutionCreator is IIntegerVectorCreator)
|
---|
298 | || ((IIntegerVectorCreator)SolutionCreator).IntegerVectorParameter.ActualName != intCreator.IntegerVectorParameter.ActualName) {
|
---|
299 | Operators.Remove(SolutionCreator);
|
---|
300 | SolutionCreator = newCreator;
|
---|
301 | Operators.Add(SolutionCreator);
|
---|
302 | }
|
---|
303 |
|
---|
304 | #region Wire IntegerVector Crossovers
|
---|
305 | Operators.RemoveAll(x => x is IIntegerVectorCrossover && ((IIntegerVectorCrossover)x).ChildParameter.ActualName != paramName);
|
---|
306 | var crossovers = ApplicationManager.Manager.GetInstances<IIntegerVectorCrossover>().ToList();
|
---|
307 | foreach (var xo in crossovers) {
|
---|
308 | xo.ChildParameter.ActualName = intCreator.IntegerVectorParameter.ActualName;
|
---|
309 | xo.ChildParameter.Hidden = true;
|
---|
310 | xo.ParentsParameter.ActualName = intCreator.IntegerVectorParameter.ActualName;
|
---|
311 | xo.ParentsParameter.Hidden = true;
|
---|
312 | var bx = xo as IBoundedIntegerVectorOperator;
|
---|
313 | if (bx != null) {
|
---|
314 | bx.BoundsParameter.ActualName = intCreator.BoundsParameter.ActualName;
|
---|
315 | bx.BoundsParameter.Hidden = true;
|
---|
316 | }
|
---|
317 | }
|
---|
318 | Operators.AddRange(crossovers.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
319 | #endregion
|
---|
320 | #region Wire IntegerVector Manipulators
|
---|
321 | Operators.RemoveAll(x => x is IIntegerVectorManipulator && ((IIntegerVectorManipulator)x).IntegerVectorParameter.ActualName != paramName);
|
---|
322 | var manipulators = ApplicationManager.Manager.GetInstances<IIntegerVectorManipulator>().ToList();
|
---|
323 | foreach (var m in manipulators) {
|
---|
324 | m.IntegerVectorParameter.ActualName = intCreator.IntegerVectorParameter.ActualName;
|
---|
325 | m.IntegerVectorParameter.Hidden = true;
|
---|
326 | var sm = m as ISelfAdaptiveManipulator;
|
---|
327 | if (sm != null) {
|
---|
328 | var p = sm.StrategyParameterParameter as ILookupParameter;
|
---|
329 | if (p != null) {
|
---|
330 | p.ActualName = paramName + "Strategy";
|
---|
331 | p.Hidden = true;
|
---|
332 | }
|
---|
333 | }
|
---|
334 | var bm = m as IBoundedIntegerVectorOperator;
|
---|
335 | if (bm != null) {
|
---|
336 | bm.BoundsParameter.ActualName = intCreator.BoundsParameter.ActualName;
|
---|
337 | bm.BoundsParameter.Hidden = true;
|
---|
338 | }
|
---|
339 | }
|
---|
340 | Operators.AddRange(manipulators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
341 | #region Wire IntegerVector StrategyParameters for SelfAdaptiveManipulators
|
---|
342 | Operators.RemoveAll(x => x is IStrategyParameterCreator && !(x is IIntegerVectorStdDevStrategyParameterCreator) || x is IIntegerVectorStdDevStrategyParameterCreator && ((IIntegerVectorStdDevStrategyParameterCreator)x).StrategyParameterParameter.ActualName != paramName + "Strategy");
|
---|
343 | Operators.RemoveAll(x => x is IStrategyParameterManipulator && !(x is IIntegerVectorStdDevStrategyParameterManipulator) || x is IIntegerVectorStdDevStrategyParameterManipulator && ((IIntegerVectorStdDevStrategyParameterManipulator)x).StrategyParameterParameter.ActualName != paramName + "Strategy");
|
---|
344 | Operators.RemoveAll(x => x is IStrategyParameterCrossover && !(x is IIntegerVectorStdDevStrategyParameterCrossover) || x is IIntegerVectorStdDevStrategyParameterCrossover && ((IIntegerVectorStdDevStrategyParameterCrossover)x).StrategyParameterParameter.ActualName != paramName + "Strategy");
|
---|
345 | var strategyOperators = ApplicationManager.Manager.GetInstances<IIntegerVectorStdDevStrategyParameterOperator>().ToList();
|
---|
346 | if (!(configuration.Parameters.First().Value is IntegerParameterConfiguration)) throw new InvalidOperationException("Single-encoded problem with integervector creator that is not an integer-coded problem.");
|
---|
347 | var problemSize = ((IntegerParameterConfiguration)configuration.Parameters.First().Value).Length.Value;
|
---|
348 | var b = ((IntegerParameterConfiguration)configuration.Parameters.First().Value).Bounds;
|
---|
349 | var bounds = new DoubleMatrix(b.Rows, b.Columns);
|
---|
350 | for (var i = 0; i < bounds.Rows; i++) {
|
---|
351 | bounds[i, 1] = (int)Math.Ceiling(0.33 * (b[i, 1] - b[i, 0]));
|
---|
352 | bounds[i, 0] = 0;
|
---|
353 | if (bounds.Columns > 2) bounds[i, 2] = b[i, 2];
|
---|
354 | }
|
---|
355 | foreach (var s in strategyOperators) {
|
---|
356 | var c = s as IIntegerVectorStdDevStrategyParameterCreator;
|
---|
357 | if (c != null) {
|
---|
358 | c.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();
|
---|
359 | c.LengthParameter.ActualName = intCreator.LengthParameter.ActualName;
|
---|
360 | c.StrategyParameterParameter.ActualName = paramName + "Strategy";
|
---|
361 | }
|
---|
362 | var m = s as IIntegerVectorStdDevStrategyParameterManipulator;
|
---|
363 | if (m != null) {
|
---|
364 | m.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();
|
---|
365 | m.StrategyParameterParameter.ActualName = paramName + "Strategy";
|
---|
366 | }
|
---|
367 | var mm = s as Encodings.IntegerVectorEncoding.StdDevStrategyVectorManipulator;
|
---|
368 | if (mm != null) {
|
---|
369 | mm.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * problemSize));
|
---|
370 | mm.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(problemSize)));
|
---|
371 | }
|
---|
372 | var x = s as IIntegerVectorStdDevStrategyParameterCrossover;
|
---|
373 | if (x != null) {
|
---|
374 | x.ParentsParameter.ActualName = paramName + "Strategy";
|
---|
375 | x.StrategyParameterParameter.ActualName = paramName + "Strategy";
|
---|
376 | }
|
---|
377 | }
|
---|
378 | Operators.AddRange(strategyOperators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
379 | #endregion
|
---|
380 | #endregion
|
---|
381 | #region Wire IntegerVector ShakingOperators
|
---|
382 | Operators.RemoveAll(x => x is IIntegerVectorMultiNeighborhoodShakingOperator && ((IIntegerVectorMultiNeighborhoodShakingOperator)x).IntegerVectorParameter.ActualName != paramName);
|
---|
383 | var shakingOperators = ApplicationManager.Manager.GetInstances<IIntegerVectorMultiNeighborhoodShakingOperator>().ToList();
|
---|
384 | foreach (var so in shakingOperators) {
|
---|
385 | so.IntegerVectorParameter.ActualName = paramName;
|
---|
386 | so.IntegerVectorParameter.Hidden = true;
|
---|
387 | }
|
---|
388 | Operators.AddRange(shakingOperators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
389 | #endregion
|
---|
390 | } else {
|
---|
391 | Operators.RemoveAll(x => x is IIntegerVectorCrossover
|
---|
392 | || x is IIntegerVectorManipulator
|
---|
393 | || x is IIntegerVectorStdDevStrategyParameterOperator
|
---|
394 | || x is IIntegerVectorMultiNeighborhoodShakingOperator);
|
---|
395 | }
|
---|
396 | #endregion
|
---|
397 | #region Configure Operators for RealVectorEncoding
|
---|
398 | var realCreator = newCreator as IRealVectorCreator;
|
---|
399 | if (realCreator != null) {
|
---|
400 | var paramName = realCreator.RealVectorParameter.ActualName;
|
---|
401 | // do not replace a real vector creator that was manually set
|
---|
402 | if (!(SolutionCreator is IRealVectorCreator)
|
---|
403 | || ((IRealVectorCreator)SolutionCreator).RealVectorParameter.ActualName != realCreator.RealVectorParameter.ActualName) {
|
---|
404 | Operators.Remove(SolutionCreator);
|
---|
405 | SolutionCreator = newCreator;
|
---|
406 | Operators.Add(SolutionCreator);
|
---|
407 | }
|
---|
408 |
|
---|
409 | #region Wire RealVector Crossovers
|
---|
410 | Operators.RemoveAll(x => x is IRealVectorCrossover && ((IRealVectorCrossover)x).ChildParameter.ActualName != paramName);
|
---|
411 | var crossovers = ApplicationManager.Manager.GetInstances<IRealVectorCrossover>().ToList();
|
---|
412 | foreach (var xo in crossovers) {
|
---|
413 | xo.ChildParameter.ActualName = paramName;
|
---|
414 | xo.ChildParameter.Hidden = true;
|
---|
415 | xo.ParentsParameter.ActualName = paramName;
|
---|
416 | xo.ParentsParameter.Hidden = true;
|
---|
417 | xo.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;
|
---|
418 | xo.BoundsParameter.Hidden = true;
|
---|
419 | }
|
---|
420 | Operators.AddRange(crossovers.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
421 | #endregion
|
---|
422 | #region Wire RealVector Manipulators
|
---|
423 | Operators.RemoveAll(x => x is IRealVectorManipulator && ((IRealVectorManipulator)x).RealVectorParameter.ActualName != paramName);
|
---|
424 | var manipulators = ApplicationManager.Manager.GetInstances<IRealVectorManipulator>().ToList();
|
---|
425 | foreach (var m in manipulators) {
|
---|
426 | m.RealVectorParameter.ActualName = paramName;
|
---|
427 | m.RealVectorParameter.Hidden = true;
|
---|
428 | m.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;
|
---|
429 | m.BoundsParameter.Hidden = true;
|
---|
430 | var sm = m as ISelfAdaptiveManipulator;
|
---|
431 | if (sm != null) {
|
---|
432 | var p = sm.StrategyParameterParameter as ILookupParameter;
|
---|
433 | if (p != null) {
|
---|
434 | p.ActualName = paramName + "Strategy";
|
---|
435 | p.Hidden = true;
|
---|
436 | }
|
---|
437 | }
|
---|
438 | }
|
---|
439 | Operators.AddRange(manipulators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
440 | #region Wire RealVector Strategy Parameters for SelfAdaptiveManipulators
|
---|
441 |
|
---|
442 | Operators.RemoveAll(x => x is IStrategyParameterCreator && !(x is IRealVectorStdDevStrategyParameterCreator) || x is IRealVectorStdDevStrategyParameterCreator && ((IRealVectorStdDevStrategyParameterCreator)x).StrategyParameterParameter.ActualName != paramName + "Strategy");
|
---|
443 | Operators.RemoveAll(x => x is IStrategyParameterManipulator && !(x is IRealVectorStdDevStrategyParameterManipulator) || x is IRealVectorStdDevStrategyParameterManipulator && ((IRealVectorStdDevStrategyParameterManipulator)x).StrategyParameterParameter.ActualName != paramName + "Strategy");
|
---|
444 | Operators.RemoveAll(x => x is IStrategyParameterCrossover && !(x is IRealVectorStdDevStrategyParameterCrossover) || x is IRealVectorStdDevStrategyParameterCrossover && ((IRealVectorStdDevStrategyParameterCrossover)x).StrategyParameterParameter.ActualName != paramName + "Strategy");
|
---|
445 | var strategyOperators = ApplicationManager.Manager.GetInstances<IRealVectorStdDevStrategyParameterOperator>().ToList();
|
---|
446 | if (!(configuration.Parameters.First().Value is RealParameterConfiguration)) throw new InvalidOperationException("Single-encoded problem with realvector creator that is not a real-coded problem.");
|
---|
447 | var problemSize = ((RealParameterConfiguration)configuration.Parameters.First().Value).Length.Value;
|
---|
448 | var bounds = (DoubleMatrix)((RealParameterConfiguration)configuration.Parameters.First().Value).Bounds.Clone();
|
---|
449 | for (var i = 0; i < bounds.Rows; i++) {
|
---|
450 | bounds[i, 1] = 0.1 * (bounds[i, 1] - bounds[i, 0]);
|
---|
451 | bounds[i, 0] = 0;
|
---|
452 | }
|
---|
453 | foreach (var s in strategyOperators) {
|
---|
454 | var c = s as IRealVectorStdDevStrategyParameterCreator;
|
---|
455 | if (c != null) {
|
---|
456 | c.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();
|
---|
457 | c.LengthParameter.ActualName = realCreator.LengthParameter.ActualName;
|
---|
458 | c.StrategyParameterParameter.ActualName = paramName + "Strategy";
|
---|
459 | }
|
---|
460 | var m = s as IRealVectorStdDevStrategyParameterManipulator;
|
---|
461 | if (m != null) {
|
---|
462 | m.BoundsParameter.Value = (DoubleMatrix)bounds.Clone();
|
---|
463 | m.StrategyParameterParameter.ActualName = paramName + "Strategy";
|
---|
464 | }
|
---|
465 | var mm = s as Encodings.RealVectorEncoding.StdDevStrategyVectorManipulator;
|
---|
466 | if (mm != null) {
|
---|
467 | mm.GeneralLearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * problemSize));
|
---|
468 | mm.LearningRateParameter.Value = new DoubleValue(1.0 / Math.Sqrt(2 * Math.Sqrt(problemSize)));
|
---|
469 | }
|
---|
470 | var x = s as IRealVectorStdDevStrategyParameterCrossover;
|
---|
471 | if (x != null) {
|
---|
472 | x.ParentsParameter.ActualName = paramName + "Strategy";
|
---|
473 | x.StrategyParameterParameter.ActualName = paramName + "Strategy";
|
---|
474 | }
|
---|
475 | }
|
---|
476 | Operators.AddRange(strategyOperators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
477 | #endregion
|
---|
478 | #endregion
|
---|
479 | #region Wire RealVector ParticleCreators
|
---|
480 | Operators.RemoveAll(x => x is IRealVectorParticleCreator && ((IRealVectorParticleCreator)x).RealVectorParameter.ActualName != paramName);
|
---|
481 | var particleCreators = ApplicationManager.Manager.GetInstances<IRealVectorParticleCreator>().ToList();
|
---|
482 | foreach (var pc in particleCreators) {
|
---|
483 | pc.RealVectorParameter.ActualName = paramName;
|
---|
484 | pc.RealVectorParameter.Hidden = true;
|
---|
485 | pc.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;
|
---|
486 | pc.BoundsParameter.Hidden = true;
|
---|
487 | pc.ProblemSizeParameter.ActualName = realCreator.LengthParameter.ActualName;
|
---|
488 | pc.ProblemSizeParameter.Hidden = true;
|
---|
489 | }
|
---|
490 | Operators.AddRange(particleCreators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
491 | #endregion
|
---|
492 | #region Wire RealVector ParticleUpdaters
|
---|
493 | Operators.RemoveAll(x => x is IRealVectorParticleUpdater && ((IRealVectorParticleUpdater)x).RealVectorParameter.ActualName != paramName);
|
---|
494 | var particleUpdaters = ApplicationManager.Manager.GetInstances<IRealVectorParticleUpdater>().ToList();
|
---|
495 | foreach (var pu in particleUpdaters) {
|
---|
496 | pu.RealVectorParameter.ActualName = paramName;
|
---|
497 | pu.RealVectorParameter.Hidden = true;
|
---|
498 | pu.BoundsParameter.ActualName = realCreator.BoundsParameter.ActualName;
|
---|
499 | pu.BoundsParameter.Hidden = true;
|
---|
500 | }
|
---|
501 | Operators.AddRange(particleUpdaters.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
502 | #endregion
|
---|
503 | #region Wire RealVector SwarmUpdaters
|
---|
504 | Operators.RemoveAll(x => x is IRealVectorSwarmUpdater && ((IRealVectorSwarmUpdater)x).RealVectorParameter.ActualName != paramName);
|
---|
505 | var swarmUpdaters = ApplicationManager.Manager.GetInstances<IRealVectorSwarmUpdater>().ToList();
|
---|
506 | foreach (var su in swarmUpdaters) {
|
---|
507 | su.RealVectorParameter.ActualName = paramName;
|
---|
508 | su.RealVectorParameter.Hidden = true;
|
---|
509 | su.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
510 | su.MaximizationParameter.Hidden = true;
|
---|
511 | }
|
---|
512 | Operators.AddRange(swarmUpdaters.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
513 | #endregion
|
---|
514 | #region Wire RealVector ShakingOperators
|
---|
515 | Operators.RemoveAll(x => x is IRealVectorMultiNeighborhoodShakingOperator && ((IRealVectorMultiNeighborhoodShakingOperator)x).RealVectorParameter.ActualName != paramName);
|
---|
516 | var shakingOperators = ApplicationManager.Manager.GetInstances<IRealVectorMultiNeighborhoodShakingOperator>().ToList();
|
---|
517 | foreach (var so in shakingOperators) {
|
---|
518 | so.RealVectorParameter.ActualName = paramName;
|
---|
519 | so.RealVectorParameter.Hidden = true;
|
---|
520 | }
|
---|
521 | Operators.AddRange(shakingOperators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
522 | #endregion
|
---|
523 | } else {
|
---|
524 | Operators.RemoveAll(x => x is IRealVectorCrossover
|
---|
525 | || x is IRealVectorManipulator
|
---|
526 | || x is IRealVectorStdDevStrategyParameterOperator
|
---|
527 | || x is IRealVectorParticleCreator
|
---|
528 | || x is IRealVectorParticleUpdater
|
---|
529 | || x is IRealVectorSwarmUpdater
|
---|
530 | || x is IRealVectorMultiNeighborhoodShakingOperator);
|
---|
531 | }
|
---|
532 | #endregion
|
---|
533 | #region Configure Operators for PermutationEncoding
|
---|
534 | var permCreator = newCreator as IPermutationCreator;
|
---|
535 | if (permCreator != null) {
|
---|
536 | var paramName = permCreator.PermutationParameter.ActualName;
|
---|
537 | // do not replace a permutation creator that was manually set
|
---|
538 | if (!(SolutionCreator is IPermutationCreator)
|
---|
539 | || ((IPermutationCreator)SolutionCreator).PermutationParameter.ActualName != permCreator.PermutationParameter.ActualName) {
|
---|
540 | Operators.Remove(SolutionCreator);
|
---|
541 | SolutionCreator = newCreator;
|
---|
542 | Operators.Add(SolutionCreator);
|
---|
543 | }
|
---|
544 |
|
---|
545 | #region Wire Permutation Crossovers
|
---|
546 | Operators.RemoveAll(x => x is IPermutationCrossover && ((IPermutationCrossover)x).ChildParameter.ActualName != paramName);
|
---|
547 | var crossovers = ApplicationManager.Manager.GetInstances<IPermutationCrossover>().ToList();
|
---|
548 | foreach (var xo in crossovers) {
|
---|
549 | xo.ChildParameter.ActualName = permCreator.PermutationParameter.ActualName;
|
---|
550 | xo.ChildParameter.Hidden = true;
|
---|
551 | xo.ParentsParameter.ActualName = permCreator.PermutationParameter.ActualName;
|
---|
552 | xo.ParentsParameter.Hidden = true;
|
---|
553 | }
|
---|
554 | Operators.AddRange(crossovers.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
555 | #endregion
|
---|
556 | #region Wire Permutation Manipulators
|
---|
557 | Operators.RemoveAll(x => x is IPermutationManipulator && ((IPermutationManipulator)x).PermutationParameter.ActualName != paramName);
|
---|
558 | var manipulators = ApplicationManager.Manager.GetInstances<IPermutationManipulator>().ToList();
|
---|
559 | foreach (var m in manipulators) {
|
---|
560 | m.PermutationParameter.ActualName = permCreator.PermutationParameter.ActualName;
|
---|
561 | m.PermutationParameter.Hidden = true;
|
---|
562 | }
|
---|
563 | Operators.AddRange(manipulators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
564 | #endregion
|
---|
565 | #region Wire ShakingOperators
|
---|
566 | Operators.RemoveAll(x => x is IPermutationMultiNeighborhoodShakingOperator && ((IPermutationMultiNeighborhoodShakingOperator)x).PermutationParameter.ActualName != paramName);
|
---|
567 | var shakingOperators = ApplicationManager.Manager.GetInstances<IPermutationMultiNeighborhoodShakingOperator>().ToList();
|
---|
568 | foreach (var op in shakingOperators) {
|
---|
569 | op.PermutationParameter.ActualName = paramName;
|
---|
570 | op.PermutationParameter.Hidden = true;
|
---|
571 | }
|
---|
572 | Operators.AddRange(shakingOperators.Where(x => Operators.All(y => x.GetType() != y.GetType())));
|
---|
573 | #endregion
|
---|
574 | } else {
|
---|
575 | Operators.RemoveAll(x => x is IPermutationCrossover
|
---|
576 | || x is IPermutationManipulator
|
---|
577 | || x is IPermutationMultiNeighborhoodShakingOperator);
|
---|
578 | }
|
---|
579 | #endregion
|
---|
580 | }
|
---|
581 |
|
---|
582 | protected virtual void UpdateMultiVectorEncodingOperators(List<ISolutionCreator> solutionCreators, Configuration configuration) {
|
---|
583 | var oldCreator = SolutionCreator as ParameterVectorCreater;
|
---|
584 | var newCreator = new ParameterVectorCreater();
|
---|
585 |
|
---|
586 | #region Configure BinaryVector Creator
|
---|
587 | var newBinParams = new HashSet<string>(solutionCreators.OfType<IBinaryVectorCreator>().Select(x => x.BinaryVectorParameter.ActualName));
|
---|
588 | if (oldCreator != null) {
|
---|
589 | // we want to reuse the old creator
|
---|
590 | var oldParams = new HashSet<string>(oldCreator.Operators.OfType<IBinaryVectorCreator>()
|
---|
591 | .Select(x => x.BinaryVectorParameter.ActualName));
|
---|
592 | foreach (var toAdd in newBinParams.Except(oldParams)) {
|
---|
593 | var paramName = toAdd;
|
---|
594 | oldCreator.Operators.Add(
|
---|
595 | solutionCreators.OfType<IBinaryVectorCreator>().Single(x => x.BinaryVectorParameter.ActualName == paramName));
|
---|
596 | }
|
---|
597 | foreach (var toRemove in oldParams.Except(newBinParams)) {
|
---|
598 | var paramName = toRemove;
|
---|
599 | var op =
|
---|
600 | oldCreator.Operators.OfType<IBinaryVectorCreator>()
|
---|
601 | .SingleOrDefault(x => x.BinaryVectorParameter.ActualName == paramName);
|
---|
602 | if (op != null) oldCreator.Operators.Remove(op);
|
---|
603 | }
|
---|
604 | } else {
|
---|
605 | // we will use the new creator
|
---|
606 | foreach (var binCreator in solutionCreators.OfType<IBinaryVectorCreator>()) {
|
---|
607 | newCreator.Operators.Add(binCreator);
|
---|
608 | }
|
---|
609 | }
|
---|
610 | #endregion
|
---|
611 | #region Configure IntegerVector Creator
|
---|
612 | var newIntParams = new HashSet<string>(solutionCreators.OfType<IIntegerVectorCreator>().Select(x => x.IntegerVectorParameter.ActualName));
|
---|
613 | if (oldCreator != null) {
|
---|
614 | // we want to reuse the old creator
|
---|
615 | var oldParams = new HashSet<string>(oldCreator
|
---|
616 | .Operators.OfType<IIntegerVectorCreator>()
|
---|
617 | .Select(x => x.IntegerVectorParameter.ActualName));
|
---|
618 | foreach (var toAdd in newIntParams.Except(oldParams)) {
|
---|
619 | var paramName = toAdd;
|
---|
620 | oldCreator.Operators.Add(
|
---|
621 | solutionCreators.OfType<IIntegerVectorCreator>().Single(x => x.IntegerVectorParameter.ActualName == paramName));
|
---|
622 | }
|
---|
623 | foreach (var toRemove in oldParams.Except(newIntParams)) {
|
---|
624 | var paramName = toRemove;
|
---|
625 | var op =
|
---|
626 | oldCreator.Operators.OfType<IIntegerVectorCreator>()
|
---|
627 | .SingleOrDefault(x => x.IntegerVectorParameter.ActualName == paramName);
|
---|
628 | if (op != null) oldCreator.Operators.Remove(op);
|
---|
629 | }
|
---|
630 | } else {
|
---|
631 | // we will use the new creator
|
---|
632 | foreach (var intCreator in solutionCreators.OfType<IIntegerVectorCreator>()) {
|
---|
633 | newCreator.Operators.Add(intCreator);
|
---|
634 | }
|
---|
635 | }
|
---|
636 | #endregion
|
---|
637 | #region Configure RealVector Creator
|
---|
638 | var newRealParams = new HashSet<string>(solutionCreators.OfType<IRealVectorCreator>().Select(x => x.RealVectorParameter.ActualName));
|
---|
639 | if (oldCreator != null) {
|
---|
640 | // we want to reuse the old creator
|
---|
641 | var oldParams = new HashSet<string>(oldCreator
|
---|
642 | .Operators.OfType<IRealVectorCreator>()
|
---|
643 | .Select(x => x.RealVectorParameter.ActualName));
|
---|
644 | foreach (var toAdd in newRealParams.Except(oldParams)) {
|
---|
645 | var paramName = toAdd;
|
---|
646 | oldCreator.Operators.Add(
|
---|
647 | solutionCreators.OfType<IRealVectorCreator>().Single(x => x.RealVectorParameter.ActualName == paramName));
|
---|
648 | }
|
---|
649 | foreach (var toRemove in oldParams.Except(newRealParams)) {
|
---|
650 | var paramName = toRemove;
|
---|
651 | var op =
|
---|
652 | oldCreator.Operators.OfType<IRealVectorCreator>()
|
---|
653 | .SingleOrDefault(x => x.RealVectorParameter.ActualName == paramName);
|
---|
654 | if (op != null) oldCreator.Operators.Remove(op);
|
---|
655 | }
|
---|
656 | } else {
|
---|
657 | // we will use the new creator
|
---|
658 | foreach (var realCreator in solutionCreators.OfType<IRealVectorCreator>()) {
|
---|
659 | newCreator.Operators.Add(realCreator);
|
---|
660 | }
|
---|
661 | }
|
---|
662 | #endregion
|
---|
663 | #region Configure Permutation Creator
|
---|
664 | var newPermParams = new HashSet<string>(solutionCreators.OfType<IPermutationCreator>().Select(x => x.PermutationParameter.ActualName));
|
---|
665 | if (oldCreator != null) {
|
---|
666 | // we want to reuse the old creator
|
---|
667 | var oldParams = new HashSet<string>(oldCreator
|
---|
668 | .Operators.OfType<IPermutationCreator>()
|
---|
669 | .Select(x => x.PermutationParameter.ActualName));
|
---|
670 | foreach (var toAdd in newPermParams.Except(oldParams)) {
|
---|
671 | var paramName = toAdd;
|
---|
672 | oldCreator.Operators.Add(
|
---|
673 | solutionCreators.OfType<IPermutationCreator>().Single(x => x.PermutationParameter.ActualName == paramName));
|
---|
674 | }
|
---|
675 | foreach (var toRemove in oldParams.Except(newPermParams)) {
|
---|
676 | var paramName = toRemove;
|
---|
677 | var op =
|
---|
678 | oldCreator.Operators.OfType<IPermutationCreator>()
|
---|
679 | .SingleOrDefault(x => x.PermutationParameter.ActualName == paramName);
|
---|
680 | if (op != null) oldCreator.Operators.Remove(op);
|
---|
681 | }
|
---|
682 | // we also have to sync the permutation type (in case this changes, as it is a value parameter)
|
---|
683 | foreach (var intersect in newPermParams.Intersect(oldParams)) {
|
---|
684 | var paramName = intersect;
|
---|
685 | var oldPermCreator = oldCreator.Operators.OfType<IPermutationCreator>()
|
---|
686 | .Single(x => x.PermutationParameter.ActualName == paramName);
|
---|
687 | var newPermCreator = solutionCreators.OfType<IPermutationCreator>()
|
---|
688 | .Single(x => x.PermutationParameter.ActualName == paramName);
|
---|
689 | oldPermCreator.PermutationTypeParameter.Value = newPermCreator.PermutationTypeParameter.Value;
|
---|
690 | }
|
---|
691 | } else {
|
---|
692 | // we will use the new creator
|
---|
693 | foreach (var permCreator in solutionCreators.OfType<IPermutationCreator>()) {
|
---|
694 | newCreator.Operators.Add(permCreator);
|
---|
695 | }
|
---|
696 | }
|
---|
697 | #endregion
|
---|
698 |
|
---|
699 | if (oldCreator == null) SolutionCreator = newCreator;
|
---|
700 |
|
---|
701 | // crossover and manipulator for multi-vector encoding
|
---|
702 | // the condition checks if a multi-vector encoding is to be updated (there already exists ParameterVectorCrossover and ParameterVectorManipulator)
|
---|
703 | if (Operators.OfType<ParameterVectorCrossover>().Any() && Operators.OfType<ParameterVectorManipulator>().Any()) {
|
---|
704 | #region Update existing multi-vector encoding
|
---|
705 | #region Update ParameterVector Crossover ...
|
---|
706 | foreach (var oldXo in Operators.OfType<ParameterVectorCrossover>()) {
|
---|
707 | #region ... for binary parameters
|
---|
708 | var oldBinParams = new HashSet<string>(oldXo.Operators.OfType<IBinaryVectorCrossover>()
|
---|
709 | .Select(x => x.ChildParameter.ActualName));
|
---|
710 | foreach (var toAdd in newBinParams.Except(oldBinParams))
|
---|
711 | oldXo.Operators.Add(GetDefaultBinaryCrossover(toAdd, configuration));
|
---|
712 | foreach (var toRemove in oldBinParams.Except(newBinParams)) {
|
---|
713 | var op =
|
---|
714 | oldXo.Operators.OfType<IBinaryVectorCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);
|
---|
715 | if (op != null) oldXo.Operators.Remove(op);
|
---|
716 | }
|
---|
717 | #endregion
|
---|
718 | #region ... for integer parameters
|
---|
719 | var oldIntParams = new HashSet<string>(oldXo.Operators.OfType<IIntegerVectorCrossover>()
|
---|
720 | .Select(x => x.ChildParameter.ActualName));
|
---|
721 | foreach (var toAdd in newIntParams.Except(oldIntParams))
|
---|
722 | oldXo.Operators.Add(GetDefaultIntegerCrossover(toAdd, configuration));
|
---|
723 | foreach (var toRemove in oldIntParams.Except(newIntParams)) {
|
---|
724 | var op =
|
---|
725 | oldXo.Operators.OfType<IIntegerVectorCrossover>()
|
---|
726 | .SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);
|
---|
727 | if (op != null) oldXo.Operators.Remove(op);
|
---|
728 | }
|
---|
729 | #endregion
|
---|
730 | #region ... for real parameters
|
---|
731 | var oldRealParams = new HashSet<string>(oldXo.Operators.OfType<IRealVectorCrossover>()
|
---|
732 | .Select(x => x.ChildParameter.ActualName));
|
---|
733 | foreach (var toAdd in newRealParams.Except(oldRealParams))
|
---|
734 | oldXo.Operators.Add(GetDefaultRealCrossover(toAdd, configuration));
|
---|
735 | foreach (var toRemove in oldRealParams.Except(newRealParams)) {
|
---|
736 | var op =
|
---|
737 | oldXo.Operators.OfType<IRealVectorCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);
|
---|
738 | if (op != null) oldXo.Operators.Remove(op);
|
---|
739 | }
|
---|
740 | #endregion
|
---|
741 | #region ... for permutation parameters
|
---|
742 | var oldPermParams = new HashSet<string>(oldXo.Operators.OfType<IPermutationCrossover>()
|
---|
743 | .Select(x => x.ChildParameter.ActualName));
|
---|
744 | foreach (var toAdd in newPermParams.Except(oldPermParams))
|
---|
745 | oldXo.Operators.Add(GetDefaultPermutationCrossover(toAdd, configuration));
|
---|
746 | foreach (var toRemove in oldPermParams.Except(newPermParams)) {
|
---|
747 | var op =
|
---|
748 | oldXo.Operators.OfType<IPermutationCrossover>().SingleOrDefault(x => x.ChildParameter.ActualName == toRemove);
|
---|
749 | if (op != null) oldXo.Operators.Remove(op);
|
---|
750 | }
|
---|
751 | #endregion
|
---|
752 | }
|
---|
753 | #endregion
|
---|
754 | #region Update ParameterVector Manipulator ...
|
---|
755 | foreach (var oldM in Operators.OfType<ParameterVectorManipulator>()) {
|
---|
756 | #region ... for binary parameters
|
---|
757 | var oldBinParams = new HashSet<string>(oldM.Operators.OfType<IBinaryVectorManipulator>()
|
---|
758 | .Select(x => x.BinaryVectorParameter.ActualName));
|
---|
759 | foreach (var toAdd in newBinParams.Except(oldBinParams))
|
---|
760 | oldM.Operators.Add(GetDefaultBinaryManipulator(toAdd, configuration));
|
---|
761 | foreach (var toRemove in oldBinParams.Except(newBinParams)) {
|
---|
762 | var op =
|
---|
763 | oldM.Operators.OfType<IBinaryVectorManipulator>()
|
---|
764 | .SingleOrDefault(x => x.BinaryVectorParameter.ActualName == toRemove);
|
---|
765 | if (op != null) oldM.Operators.Remove(op);
|
---|
766 | }
|
---|
767 | #endregion
|
---|
768 | #region ... for integer parameters
|
---|
769 | var oldIntParams = new HashSet<string>(oldM.Operators.OfType<IIntegerVectorManipulator>()
|
---|
770 | .Select(x => x.IntegerVectorParameter.ActualName));
|
---|
771 | foreach (var toAdd in newIntParams.Except(oldIntParams))
|
---|
772 | oldM.Operators.Add(GetDefaultIntegerManipulator(toAdd, configuration));
|
---|
773 | foreach (var toRemove in oldIntParams.Except(newIntParams)) {
|
---|
774 | var op =
|
---|
775 | oldM.Operators.OfType<IIntegerVectorManipulator>()
|
---|
776 | .SingleOrDefault(x => x.IntegerVectorParameter.ActualName == toRemove);
|
---|
777 | if (op != null) oldM.Operators.Remove(op);
|
---|
778 | }
|
---|
779 | #endregion
|
---|
780 | #region ... for real parameters
|
---|
781 | var oldRealParams = new HashSet<string>(oldM.Operators.OfType<IRealVectorManipulator>()
|
---|
782 | .Select(x => x.RealVectorParameter.ActualName));
|
---|
783 | foreach (var toAdd in newRealParams.Except(oldRealParams))
|
---|
784 | oldM.Operators.Add(GetDefaultRealManipulator(toAdd, configuration));
|
---|
785 | foreach (var toRemove in oldRealParams.Except(newRealParams)) {
|
---|
786 | var op =
|
---|
787 | oldM.Operators.OfType<IRealVectorManipulator>()
|
---|
788 | .SingleOrDefault(x => x.RealVectorParameter.ActualName == toRemove);
|
---|
789 | if (op != null) oldM.Operators.Remove(op);
|
---|
790 | }
|
---|
791 | #endregion
|
---|
792 | #region ... for permutation parameters
|
---|
793 | var oldPermParams = new HashSet<string>(oldM.Operators.OfType<IPermutationManipulator>()
|
---|
794 | .Select(x => x.PermutationParameter.ActualName));
|
---|
795 | foreach (var toAdd in newPermParams.Except(oldPermParams))
|
---|
796 | oldM.Operators.Add(GetDefaultPermutationManipulator(toAdd, configuration));
|
---|
797 | foreach (var toRemove in oldPermParams.Except(newPermParams)) {
|
---|
798 | var op =
|
---|
799 | oldM.Operators.OfType<IPermutationManipulator>()
|
---|
800 | .SingleOrDefault(x => x.PermutationParameter.ActualName == toRemove);
|
---|
801 | if (op != null) oldM.Operators.Remove(op);
|
---|
802 | }
|
---|
803 | #endregion
|
---|
804 | }
|
---|
805 | #endregion
|
---|
806 | #endregion
|
---|
807 | } else {
|
---|
808 | #region Handle transition from single-vector to multi-vector encoding
|
---|
809 | Operators.RemoveAll(x => x is ICrossover);
|
---|
810 | Operators.RemoveAll(x => x is IManipulator);
|
---|
811 | Operators.RemoveAll(x => x is IStrategyParameterCreator || x is IStrategyParameterManipulator || x is IStrategyParameterCrossover);
|
---|
812 | Operators.RemoveAll(x => x is IParticleCreator);
|
---|
813 | Operators.RemoveAll(x => x is IParticleUpdater);
|
---|
814 | Operators.RemoveAll(x => x is ISwarmUpdater);
|
---|
815 | Operators.RemoveAll(x => x is IMultiNeighborhoodShakingOperator);
|
---|
816 |
|
---|
817 | var crossover = new ParameterVectorCrossover();
|
---|
818 | var manipulator = new ParameterVectorManipulator();
|
---|
819 | foreach (var param in configuration.Parameters) {
|
---|
820 | if (param.Value is BinaryParameterConfiguration) {
|
---|
821 | crossover.Operators.Add(GetDefaultBinaryCrossover(param.Key, configuration));
|
---|
822 | manipulator.Operators.Add(GetDefaultBinaryManipulator(param.Key, configuration));
|
---|
823 | continue;
|
---|
824 | }
|
---|
825 | var intConfig = param.Value as IntegerParameterConfiguration;
|
---|
826 | if (intConfig != null) {
|
---|
827 | crossover.Operators.Add(GetDefaultIntegerCrossover(param.Key, configuration));
|
---|
828 | manipulator.Operators.Add(GetDefaultIntegerManipulator(param.Key, configuration));
|
---|
829 | continue;
|
---|
830 | }
|
---|
831 | var realConfig = param.Value as RealParameterConfiguration;
|
---|
832 | if (realConfig != null) {
|
---|
833 | crossover.Operators.Add(GetDefaultRealCrossover(param.Key, configuration));
|
---|
834 | manipulator.Operators.Add(GetDefaultRealManipulator(param.Key, configuration));
|
---|
835 | continue;
|
---|
836 | }
|
---|
837 | var permConfig = param.Value as PermutationParameterConfiguration;
|
---|
838 | if (permConfig != null) {
|
---|
839 | crossover.Operators.Add(GetDefaultPermutationCrossover(param.Key, configuration));
|
---|
840 | manipulator.Operators.Add(GetDefaultPermutationManipulator(param.Key, configuration));
|
---|
841 | continue;
|
---|
842 | }
|
---|
843 | throw new InvalidOperationException("Unknown type for parameter " + param.Key);
|
---|
844 | }
|
---|
845 | Operators.Add(crossover);
|
---|
846 | Operators.Add(manipulator);
|
---|
847 | #endregion
|
---|
848 | }
|
---|
849 | }
|
---|
850 |
|
---|
851 | protected virtual void UpdateMoveOperators() {
|
---|
852 | Operators.RemoveAll(x => x is IParameterVectorMoveOperator);
|
---|
853 | var generator = new ParameterVectorMoveGenerator();
|
---|
854 | generator.ConfigurationParameter.ActualName = ConfigurationParameter.Name;
|
---|
855 | generator.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
|
---|
856 |
|
---|
857 | var evaluator = new ParameterVectorMoveEvaluator();
|
---|
858 | evaluator.ConfigurationParameter.ActualName = ConfigurationParameter.Name;
|
---|
859 | evaluator.QualityParameter.ActualName = Evaluator.QualitiesParameter.ActualName;
|
---|
860 | evaluator.ProblemDefinitionParameter.ActualName = ProblemDefinitionParameter.Name;
|
---|
861 |
|
---|
862 | var maker = new ParameterVectorMoveMaker();
|
---|
863 | maker.ConfigurationParameter.ActualName = ConfigurationParameter.Name;
|
---|
864 | maker.MoveQualityParameter.ActualName = evaluator.MoveQualityParameter.ActualName;
|
---|
865 | maker.QualityParameter.ActualName = Evaluator.QualitiesParameter.ActualName;
|
---|
866 |
|
---|
867 | Operators.AddRange(new IItem[] { generator, evaluator, maker });
|
---|
868 | }
|
---|
869 |
|
---|
870 | #region GetDefaultOperators for Crossovers and Manipulators
|
---|
871 | // ReSharper disable RedundantNameQualifier
|
---|
872 | protected virtual IBinaryVectorCrossover GetDefaultBinaryCrossover(string paramName, Configuration config) {
|
---|
873 | var binConfig = (BinaryParameterConfiguration)config.Parameters[paramName];
|
---|
874 | IBinaryVectorCrossover binXo;
|
---|
875 | if (binConfig.Length.Value > 3) binXo = new Encodings.BinaryVectorEncoding.SinglePointCrossover();
|
---|
876 | else binXo = new Encodings.BinaryVectorEncoding.UniformCrossover();
|
---|
877 | binXo.ChildParameter.ActualName = paramName;
|
---|
878 | binXo.ParentsParameter.ActualName = paramName;
|
---|
879 | return binXo;
|
---|
880 | }
|
---|
881 |
|
---|
882 | protected virtual IBinaryVectorManipulator GetDefaultBinaryManipulator(string paramName, Configuration config) {
|
---|
883 | var binM = new Encodings.BinaryVectorEncoding.SomePositionsBitflipManipulator();
|
---|
884 | binM.BinaryVectorParameter.ActualName = paramName;
|
---|
885 | binM.MutationProbabilityParameter.Value = new DoubleValue(0.1);
|
---|
886 | return binM;
|
---|
887 | }
|
---|
888 |
|
---|
889 | protected virtual IIntegerVectorCrossover GetDefaultIntegerCrossover(string paramName, Configuration config) {
|
---|
890 | var intXo = new Encodings.IntegerVectorEncoding.RoundedBlendAlphaCrossover();
|
---|
891 | intXo.ChildParameter.ActualName = paramName;
|
---|
892 | intXo.ParentsParameter.ActualName = paramName;
|
---|
893 | intXo.BoundsParameter.ActualName = paramName + "Bounds";
|
---|
894 | return intXo;
|
---|
895 | }
|
---|
896 |
|
---|
897 | protected virtual IIntegerVectorManipulator GetDefaultIntegerManipulator(string paramName, Configuration configuration) {
|
---|
898 | var intM = new Encodings.IntegerVectorEncoding.UniformSomePositionsManipulator();
|
---|
899 | intM.IntegerVectorParameter.ActualName = paramName;
|
---|
900 | intM.BoundsParameter.ActualName = paramName + "Bounds";
|
---|
901 | intM.ProbabilityParameter.Value = new DoubleValue(0.1);
|
---|
902 | return intM;
|
---|
903 | }
|
---|
904 |
|
---|
905 | protected virtual IRealVectorCrossover GetDefaultRealCrossover(string paramName, Configuration configuration) {
|
---|
906 | var realXo = new Encodings.RealVectorEncoding.BlendAlphaCrossover();
|
---|
907 | realXo.ChildParameter.ActualName = paramName;
|
---|
908 | realXo.ParentsParameter.ActualName = paramName;
|
---|
909 | realXo.BoundsParameter.ActualName = paramName + "Bounds";
|
---|
910 | return realXo;
|
---|
911 | }
|
---|
912 |
|
---|
913 | protected virtual IRealVectorManipulator GetDefaultRealManipulator(string paramName, Configuration configuration) {
|
---|
914 | var realM = new Encodings.RealVectorEncoding.BreederGeneticAlgorithmManipulator();
|
---|
915 | realM.RealVectorParameter.ActualName = paramName;
|
---|
916 | realM.BoundsParameter.ActualName = paramName + "Bounds";
|
---|
917 | return realM;
|
---|
918 | }
|
---|
919 |
|
---|
920 | protected virtual IPermutationCrossover GetDefaultPermutationCrossover(string paramName, Configuration configuration) {
|
---|
921 | var permXo = new Encodings.PermutationEncoding.PartiallyMatchedCrossover();
|
---|
922 | permXo.ChildParameter.ActualName = paramName;
|
---|
923 | permXo.ParentsParameter.ActualName = paramName;
|
---|
924 | return permXo;
|
---|
925 | }
|
---|
926 |
|
---|
927 | protected virtual IPermutationManipulator GetDefaultPermutationManipulator(string paramName, Configuration configuration) {
|
---|
928 | var permM = new Encodings.PermutationEncoding.Swap2Manipulator();
|
---|
929 | permM.PermutationParameter.ActualName = paramName;
|
---|
930 | return permM;
|
---|
931 | }
|
---|
932 | // ReSharper restore RedundantNameQualifier
|
---|
933 | #endregion
|
---|
934 | }
|
---|
935 | }
|
---|