Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorEncoding.cs @ 17614

Last change on this file since 17614 was 17614, checked in by abeham, 4 years ago

#2521: work in progress (removed solution creator parameter from encoding), OrienteeringProblem and test functions are broken

File size: 5.9 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Optimization;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Encodings.BinaryVectorEncoding {
32  [Item("BinaryVectorEncoding", "Describes a binary vector encoding.")]
33  [StorableType("889C5E1A-3FBF-4AB3-AB2E-199A781503B5")]
34  public sealed class BinaryVectorEncoding : VectorEncoding<BinaryVector> {
35
36    [StorableConstructor]
37    private BinaryVectorEncoding(StorableConstructorFlag _) : base(_) { }
38    [StorableHook(HookType.AfterDeserialization)]
39    private void AfterDeserialization() {
40      DiscoverOperators();
41    }
42    public override IDeepCloneable Clone(Cloner cloner) { return new BinaryVectorEncoding(this, cloner); }
43    private BinaryVectorEncoding(BinaryVectorEncoding original, Cloner cloner) : base(original, cloner) { }
44    public BinaryVectorEncoding() : this("BinaryVector", 10) { }
45    public BinaryVectorEncoding(string name) : this(name, 10) { }
46    public BinaryVectorEncoding(int length) : this("BinaryVector", length) { }
47    public BinaryVectorEncoding(string name, int length)
48      : base(name, length) {
49      DiscoverOperators();
50    }
51
52    #region Operator Discovery
53    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
54
55    static BinaryVectorEncoding() {
56      encodingSpecificOperatorTypes = new List<Type>() {
57        typeof (IBinaryVectorOperator),
58        typeof (IBinaryVectorCreator),
59        typeof (IBinaryVectorCrossover),
60        typeof (IBinaryVectorManipulator),
61        typeof (IBinaryVectorMoveOperator),
62        typeof (IBinaryVectorMultiNeighborhoodShakingOperator),
63        typeof (IBinaryVectorSolutionOperator),
64        typeof (IBinaryVectorSolutionsOperator)
65      };
66    }
67    private void DiscoverOperators() {
68      var assembly = typeof(IBinaryVectorOperator).Assembly;
69      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
70      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
71      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
72
73      ConfigureOperators(newOperators);
74      foreach (var @operator in newOperators)
75        AddOperator(@operator);
76    }
77    #endregion
78
79    public override void ConfigureOperators(IEnumerable<IItem> operators) {
80      base.ConfigureOperators(operators);
81      ConfigureCreators(operators.OfType<IBinaryVectorCreator>());
82      ConfigureCrossovers(operators.OfType<IBinaryVectorCrossover>());
83      ConfigureManipulators(operators.OfType<IBinaryVectorManipulator>());
84      ConfigureMoveOperators(operators.OfType<IBinaryVectorMoveOperator>());
85      ConfigureBitFlipMoveOperators(operators.OfType<IOneBitflipMoveOperator>());
86      ConfigureShakingOperators(operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>());
87      ConfigureSolutionOperators(operators.OfType<IBinaryVectorSolutionOperator>());
88      ConfigureSolutionsOperators(operators.OfType<IBinaryVectorSolutionsOperator>());
89    }
90
91    #region Specific Operator Wiring
92    private void ConfigureCreators(IEnumerable<IBinaryVectorCreator> creators) {
93      foreach (var creator in creators) {
94        creator.LengthParameter.ActualName = LengthParameter.Name;
95      }
96    }
97    private void ConfigureCrossovers(IEnumerable<IBinaryVectorCrossover> crossovers) {
98      foreach (var crossover in crossovers) {
99        crossover.ParentsParameter.ActualName = Name;
100        crossover.ChildParameter.ActualName = Name;
101      }
102    }
103    private void ConfigureManipulators(IEnumerable<IBinaryVectorManipulator> manipulators) {
104      // binary vector manipulators don't contain additional parameters besides the solution parameter
105    }
106    private void ConfigureMoveOperators(IEnumerable<IBinaryVectorMoveOperator> moveOperators) {
107      // binary vector move operators don't contain additional parameters besides the solution parameter
108    }
109    private void ConfigureBitFlipMoveOperators(IEnumerable<IOneBitflipMoveOperator> oneBitflipMoveOperators) {
110      foreach (var oneBitFlipMoveOperator in oneBitflipMoveOperators) {
111        oneBitFlipMoveOperator.OneBitflipMoveParameter.ActualName = Name + "_OneBitFlipMove";
112      }
113    }
114    private void ConfigureShakingOperators(IEnumerable<IBinaryVectorMultiNeighborhoodShakingOperator> shakingOperators) {
115      // binary vector shaking operators don't contain additional parameters besides the solution parameter
116    }
117    private void ConfigureSolutionOperators(IEnumerable<IBinaryVectorSolutionOperator> solutionOperators) {
118      foreach (var solutionOperator in solutionOperators)
119        solutionOperator.BinaryVectorParameter.ActualName = Name;
120    }
121    private void ConfigureSolutionsOperators(IEnumerable<IBinaryVectorSolutionsOperator> solutionsOperators) {
122      foreach (var solutionsOperator in solutionsOperators)
123        solutionsOperator.BinaryVectorsParameter.ActualName = Name;
124    }
125    #endregion
126  }
127}
Note: See TracBrowser for help on using the repository browser.