1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.PluginInfrastructure;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Encodings.BinaryVectorEncoding {
|
---|
34 | [Item("BinaryVectorEncoding", "Describes a binary vector encoding.")]
|
---|
35 | [StorableClass]
|
---|
36 | public sealed class BinaryVectorEncoding : Encoding<BinaryVector> {
|
---|
37 | #region Encoding Parameters
|
---|
38 | [Storable]
|
---|
39 | private IFixedValueParameter<IntValue> lengthParameter;
|
---|
40 | public IFixedValueParameter<IntValue> LengthParameter {
|
---|
41 | get { return lengthParameter; }
|
---|
42 | set {
|
---|
43 | if (value == null) throw new ArgumentNullException("Length parameter must not be null.");
|
---|
44 | if (value.Value == null) throw new ArgumentNullException("Length parameter value must not be null.");
|
---|
45 | if (lengthParameter == value) return;
|
---|
46 |
|
---|
47 | if (lengthParameter != null) Parameters.Remove(lengthParameter);
|
---|
48 | lengthParameter = value;
|
---|
49 | Parameters.Add(lengthParameter);
|
---|
50 | OnLengthParameterChanged();
|
---|
51 | }
|
---|
52 | }
|
---|
53 | #endregion
|
---|
54 |
|
---|
55 | public int Length {
|
---|
56 | get { return LengthParameter.Value.Value; }
|
---|
57 | set { LengthParameter.Value.Value = value; }
|
---|
58 | }
|
---|
59 |
|
---|
60 | [StorableConstructor]
|
---|
61 | private BinaryVectorEncoding(bool deserializing) : base(deserializing) { }
|
---|
62 | [StorableHook(HookType.AfterDeserialization)]
|
---|
63 | private void AfterDeserialization() {
|
---|
64 | RegisterParameterEvents();
|
---|
65 | DiscoverOperators();
|
---|
66 | }
|
---|
67 | public override IDeepCloneable Clone(Cloner cloner) { return new BinaryVectorEncoding(this, cloner); }
|
---|
68 | private BinaryVectorEncoding(BinaryVectorEncoding original, Cloner cloner)
|
---|
69 | : base(original, cloner) {
|
---|
70 | lengthParameter = cloner.Clone(original.lengthParameter);
|
---|
71 | RegisterParameterEvents();
|
---|
72 | }
|
---|
73 |
|
---|
74 | public BinaryVectorEncoding() : this("BinaryVector", 10) { }
|
---|
75 | public BinaryVectorEncoding(string name) : this(name, 10) { }
|
---|
76 | public BinaryVectorEncoding(int length) : this("BinaryVector", length) { }
|
---|
77 | public BinaryVectorEncoding(string name, int length)
|
---|
78 | : base(name) {
|
---|
79 | lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
|
---|
80 | Parameters.Add(lengthParameter);
|
---|
81 |
|
---|
82 | SolutionCreator = new RandomBinaryVectorCreator();
|
---|
83 | RegisterParameterEvents();
|
---|
84 | DiscoverOperators();
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void OnLengthParameterChanged() {
|
---|
88 | RegisterLengthParameterEvents();
|
---|
89 | ConfigureOperators(Operators);
|
---|
90 | }
|
---|
91 | private void RegisterParameterEvents() {
|
---|
92 | RegisterLengthParameterEvents();
|
---|
93 | }
|
---|
94 | private void RegisterLengthParameterEvents() {
|
---|
95 | LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
96 | LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
|
---|
97 | }
|
---|
98 |
|
---|
99 | #region Operator Discovery
|
---|
100 | private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
|
---|
101 | static BinaryVectorEncoding() {
|
---|
102 | encodingSpecificOperatorTypes = new List<Type>() {
|
---|
103 | typeof (IBinaryVectorOperator),
|
---|
104 | typeof (IBinaryVectorCreator),
|
---|
105 | typeof (IBinaryVectorCrossover),
|
---|
106 | typeof (IBinaryVectorManipulator),
|
---|
107 | typeof (IBinaryVectorMoveOperator),
|
---|
108 | typeof (IBinaryVectorMultiNeighborhoodShakingOperator),
|
---|
109 | typeof (IBinaryVectorSolutionOperator),
|
---|
110 | typeof (IBinaryVectorSolutionsOperator)
|
---|
111 | };
|
---|
112 | }
|
---|
113 | private void DiscoverOperators() {
|
---|
114 | var assembly = typeof(IBinaryVectorOperator).Assembly;
|
---|
115 | var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
|
---|
116 | var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
|
---|
117 | var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
|
---|
118 |
|
---|
119 | ConfigureOperators(newOperators);
|
---|
120 | foreach (var @operator in newOperators)
|
---|
121 | AddOperator(@operator);
|
---|
122 | }
|
---|
123 | #endregion
|
---|
124 |
|
---|
125 | public override void ConfigureOperators(IEnumerable<IItem> operators) {
|
---|
126 | ConfigureCreators(operators.OfType<IBinaryVectorCreator>());
|
---|
127 | ConfigureCrossovers(operators.OfType<IBinaryVectorCrossover>());
|
---|
128 | ConfigureManipulators(operators.OfType<IBinaryVectorManipulator>());
|
---|
129 | ConfigureMoveOperators(operators.OfType<IBinaryVectorMoveOperator>());
|
---|
130 | ConfigureBitFlipMoveOperators(operators.OfType<IOneBitflipMoveOperator>());
|
---|
131 | ConfigureShakingOperators(operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>());
|
---|
132 | ConfigureSolutionOperators(operators.OfType<IBinaryVectorSolutionOperator>());
|
---|
133 | ConfigureSolutionsOperators(operators.OfType<IBinaryVectorSolutionsOperator>());
|
---|
134 | }
|
---|
135 |
|
---|
136 | #region Specific Operator Wiring
|
---|
137 | private void ConfigureCreators(IEnumerable<IBinaryVectorCreator> creators) {
|
---|
138 | foreach (var creator in creators) {
|
---|
139 | creator.LengthParameter.ActualName = LengthParameter.Name;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | private void ConfigureCrossovers(IEnumerable<IBinaryVectorCrossover> crossovers) {
|
---|
143 | foreach (var crossover in crossovers) {
|
---|
144 | crossover.ParentsParameter.ActualName = Name;
|
---|
145 | crossover.ChildParameter.ActualName = Name;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | private void ConfigureManipulators(IEnumerable<IBinaryVectorManipulator> manipulators) {
|
---|
149 | // binary vector manipulators don't contain additional parameters besides the solution parameter
|
---|
150 | }
|
---|
151 | private void ConfigureMoveOperators(IEnumerable<IBinaryVectorMoveOperator> moveOperators) {
|
---|
152 | // binary vector move operators don't contain additional parameters besides the solution parameter
|
---|
153 | }
|
---|
154 | private void ConfigureBitFlipMoveOperators(IEnumerable<IOneBitflipMoveOperator> oneBitflipMoveOperators) {
|
---|
155 | foreach (var oneBitFlipMoveOperator in oneBitflipMoveOperators) {
|
---|
156 | oneBitFlipMoveOperator.OneBitflipMoveParameter.ActualName = Name + "_OneBitFlipMove";
|
---|
157 | }
|
---|
158 | }
|
---|
159 | private void ConfigureShakingOperators(IEnumerable<IBinaryVectorMultiNeighborhoodShakingOperator> shakingOperators) {
|
---|
160 | // binary vector shaking operators don't contain additional parameters besides the solution parameter
|
---|
161 | }
|
---|
162 | private void ConfigureSolutionOperators(IEnumerable<IBinaryVectorSolutionOperator> solutionOperators) {
|
---|
163 | foreach (var solutionOperator in solutionOperators)
|
---|
164 | solutionOperator.BinaryVectorParameter.ActualName = Name;
|
---|
165 | }
|
---|
166 | private void ConfigureSolutionsOperators(IEnumerable<IBinaryVectorSolutionsOperator> solutionsOperators) {
|
---|
167 | foreach (var solutionsOperator in solutionsOperators)
|
---|
168 | solutionsOperator.BinaryVectorsParameter.ActualName = Name;
|
---|
169 | }
|
---|
170 | #endregion
|
---|
171 | }
|
---|
172 | }
|
---|