Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/BinaryEncoding.cs @ 11598

Last change on this file since 11598 was 11598, checked in by mkommend, 9 years ago

#2174: Added first version of refactored individuals.

File size: 6.8 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.BinaryVectorEncoding;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Problems.Programmable {
34  [Item("BinaryEncoding", "Describes a binary vector encoding.")]
35  [StorableClass]
36  public sealed class BinaryEncoding : Encoding<IBinaryVectorCreator> {
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 BinaryEncoding(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 BinaryEncoding(this, cloner); }
68    private BinaryEncoding(BinaryEncoding original, Cloner cloner)
69      : base(original, cloner) {
70      lengthParameter = cloner.Clone(original.lengthParameter);
71      RegisterParameterEvents();
72    }
73    public BinaryEncoding(string name, int length)
74      : base(name) {
75      lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
76      Parameters.Add(lengthParameter);
77
78      SolutionCreator = new RandomBinaryVectorCreator();
79      RegisterParameterEvents();
80      DiscoverOperators();
81    }
82
83    private void OnLengthParameterChanged() {
84      RegisterLengthParameterEvents();
85      ConfigureOperators(Operators);
86    }
87    private void RegisterParameterEvents() {
88      RegisterLengthParameterEvents();
89    }
90    private void RegisterLengthParameterEvents() {
91      LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
92      LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
93    }
94
95    #region Operator Discovery
96    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
97    static BinaryEncoding() {
98      encodingSpecificOperatorTypes = new List<Type>() {
99        typeof (IBinaryVectorOperator),
100        typeof (IBinaryVectorCreator),
101        typeof (IBinaryVectorCrossover),
102        typeof (IBinaryVectorManipulator),
103        typeof (IBinaryVectorMoveOperator),
104        typeof (IBinaryVectorMultiNeighborhoodShakingOperator),
105      };
106    }
107    private void DiscoverOperators() {
108      var pluginDescription = ApplicationManager.Manager.GetDeclaringPlugin(typeof(IBinaryVectorOperator));
109      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, pluginDescription, true, false, false);
110      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
111      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
112
113      ConfigureOperators(newOperators);
114      foreach (var @operator in newOperators)
115        encodingOperators.Add(@operator);
116    }
117    #endregion
118
119    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
120      ConfigureCreators(operators.OfType<IBinaryVectorCreator>());
121      ConfigureCrossovers(operators.OfType<IBinaryVectorCrossover>());
122      ConfigureManipulators(operators.OfType<IBinaryVectorManipulator>());
123      ConfigureMoveOperators(operators.OfType<IBinaryVectorMoveOperator>());
124      ConfigureBitFlipMoveOperators(operators.OfType<IOneBitflipMoveOperator>());
125      ConfigureShakingOperators(operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>());
126    }
127
128    #region Specific Operator Wiring
129    private void ConfigureCreators(IEnumerable<IBinaryVectorCreator> creators) {
130      foreach (var creator in creators) {
131        creator.BinaryVectorParameter.ActualName = Name;
132        creator.LengthParameter.ActualName = LengthParameter.Name;
133      }
134    }
135    private void ConfigureCrossovers(IEnumerable<IBinaryVectorCrossover> crossovers) {
136      foreach (var crossover in crossovers) {
137        crossover.ParentsParameter.ActualName = Name;
138        crossover.ChildParameter.ActualName = Name;
139      }
140    }
141    private void ConfigureManipulators(IEnumerable<IBinaryVectorManipulator> manipulators) {
142      foreach (var manipulator in manipulators) {
143        manipulator.BinaryVectorParameter.ActualName = Name;
144      }
145    }
146    private void ConfigureMoveOperators(IEnumerable<IBinaryVectorMoveOperator> moveOperators) {
147      foreach (var moveOperator in moveOperators) {
148        moveOperator.BinaryVectorParameter.ActualName = Name;
149      }
150    }
151    private void ConfigureBitFlipMoveOperators(IEnumerable<IOneBitflipMoveOperator> oneBitflipMoveOperators) {
152      foreach (var oneBitFlipMoveOperator in oneBitflipMoveOperators) {
153        oneBitFlipMoveOperator.OneBitflipMoveParameter.ActualName = Name + "_OneBitFlipMove";
154      }
155    }
156    private void ConfigureShakingOperators(IEnumerable<IBinaryVectorMultiNeighborhoodShakingOperator> shakingOperators) {
157      foreach (var shakingOperator in shakingOperators) {
158        shakingOperator.BinaryVectorParameter.ActualName = Name;
159      }
160    }
161    #endregion
162  }
163}
Note: See TracBrowser for help on using the repository browser.