Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Introduce class to make parameter change handling easier, fix bug in SingleObjectiveProblem regarding MaximizationChanged

File size: 7.0 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.ComponentModel;
25using System.Linq;
26using HEAL.Attic;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.PluginInfrastructure;
33
34namespace 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 });
70     
71      SolutionCreator = new RandomBinaryVectorCreator();
72      DiscoverOperators();
73
74      RegisterEventHandlers();
75    }
76
77    private void RegisterEventHandlers() {
78      IntValueParameterChangeHandler.Create(LengthParameter, () => OnPropertyChanged(nameof(Length)));
79    }
80
81    #region Operator Discovery
82    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
83
84    static BinaryVectorEncoding() {
85      encodingSpecificOperatorTypes = new List<Type>() {
86        typeof (IBinaryVectorOperator),
87        typeof (IBinaryVectorCreator),
88        typeof (IBinaryVectorCrossover),
89        typeof (IBinaryVectorManipulator),
90        typeof (IBinaryVectorMoveOperator),
91        typeof (IBinaryVectorMultiNeighborhoodShakingOperator),
92        typeof (IBinaryVectorSolutionOperator),
93        typeof (IBinaryVectorSolutionsOperator)
94      };
95    }
96    private void DiscoverOperators() {
97      var assembly = typeof(IBinaryVectorOperator).Assembly;
98      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
99      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
100      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
101
102      ConfigureOperators(newOperators);
103      foreach (var @operator in newOperators)
104        AddOperator(@operator);
105    }
106    #endregion
107
108    public override void ConfigureOperators(IEnumerable<IItem> operators) {
109      ConfigureCreators(operators.OfType<IBinaryVectorCreator>());
110      ConfigureCrossovers(operators.OfType<IBinaryVectorCrossover>());
111      ConfigureManipulators(operators.OfType<IBinaryVectorManipulator>());
112      ConfigureMoveOperators(operators.OfType<IBinaryVectorMoveOperator>());
113      ConfigureBitFlipMoveOperators(operators.OfType<IOneBitflipMoveOperator>());
114      ConfigureShakingOperators(operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>());
115      ConfigureSolutionOperators(operators.OfType<IBinaryVectorSolutionOperator>());
116      ConfigureSolutionsOperators(operators.OfType<IBinaryVectorSolutionsOperator>());
117    }
118
119    #region Specific Operator Wiring
120    private void ConfigureCreators(IEnumerable<IBinaryVectorCreator> creators) {
121      foreach (var creator in creators) {
122        creator.LengthParameter.ActualName = LengthParameter.Name;
123      }
124    }
125    private void ConfigureCrossovers(IEnumerable<IBinaryVectorCrossover> crossovers) {
126      foreach (var crossover in crossovers) {
127        crossover.ParentsParameter.ActualName = Name;
128        crossover.ChildParameter.ActualName = Name;
129      }
130    }
131    private void ConfigureManipulators(IEnumerable<IBinaryVectorManipulator> manipulators) {
132      // binary vector manipulators don't contain additional parameters besides the solution parameter
133    }
134    private void ConfigureMoveOperators(IEnumerable<IBinaryVectorMoveOperator> moveOperators) {
135      // binary vector move operators don't contain additional parameters besides the solution parameter
136    }
137    private void ConfigureBitFlipMoveOperators(IEnumerable<IOneBitflipMoveOperator> oneBitflipMoveOperators) {
138      foreach (var oneBitFlipMoveOperator in oneBitflipMoveOperators) {
139        oneBitFlipMoveOperator.OneBitflipMoveParameter.ActualName = Name + "_OneBitFlipMove";
140      }
141    }
142    private void ConfigureShakingOperators(IEnumerable<IBinaryVectorMultiNeighborhoodShakingOperator> shakingOperators) {
143      // binary vector shaking operators don't contain additional parameters besides the solution parameter
144    }
145    private void ConfigureSolutionOperators(IEnumerable<IBinaryVectorSolutionOperator> solutionOperators) {
146      foreach (var solutionOperator in solutionOperators)
147        solutionOperator.BinaryVectorParameter.ActualName = Name;
148    }
149    private void ConfigureSolutionsOperators(IEnumerable<IBinaryVectorSolutionsOperator> solutionsOperators) {
150      foreach (var solutionsOperator in solutionsOperators)
151        solutionsOperator.BinaryVectorsParameter.ActualName = Name;
152    }
153    #endregion
154
155    public event PropertyChangedEventHandler PropertyChanged;
156    private void OnPropertyChanged(string property) {
157      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.