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