Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11593 was 11593, checked in by mkommend, 10 years ago

#2174: Fixed addition of parameters in the encodings and set default values for encoding operators in MultiEncodingOperator.

File size: 6.7 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    private IFixedValueParameter<IntValue> lengthParameter;
39    public IFixedValueParameter<IntValue> LengthParameter {
40      get { return lengthParameter; }
41      set {
42        if (value == null) throw new ArgumentNullException("Length parameter must not be null.");
43        if (value.Value == null) throw new ArgumentNullException("Length parameter value must not be null.");
44        if (lengthParameter == value) return;
45
46        if (lengthParameter != null) Parameters.Remove(lengthParameter);
47        lengthParameter = value;
48        Parameters.Add(lengthParameter);
49        OnLengthParameterChanged();
50      }
51    }
52    #endregion
53
54    public int Length {
55      get { return LengthParameter.Value.Value; }
56      set { LengthParameter.Value.Value = value; }
57    }
58
59    [StorableConstructor]
60    private BinaryEncoding(bool deserializing) : base(deserializing) { }
61    [StorableHook(HookType.AfterDeserialization)]
62    private void AfterDeserialization() {
63      RegisterParameterEvents();
64      DiscoverOperators();
65    }
66    public override IDeepCloneable Clone(Cloner cloner) { return new BinaryEncoding(this, cloner); }
67    private BinaryEncoding(BinaryEncoding original, Cloner cloner)
68      : base(original, cloner) {
69      lengthParameter = cloner.Clone(original.lengthParameter);
70      RegisterParameterEvents();
71    }
72    public BinaryEncoding(string name, int length)
73      : base(name) {
74      lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
75      Parameters.Add(lengthParameter);
76
77      SolutionCreator = new RandomBinaryVectorCreator();
78      RegisterParameterEvents();
79      DiscoverOperators();
80    }
81
82    private void OnLengthParameterChanged() {
83      RegisterLengthParameterEvents();
84      ConfigureOperators(Operators);
85    }
86    private void RegisterParameterEvents() {
87      RegisterLengthParameterEvents();
88    }
89    private void RegisterLengthParameterEvents() {
90      LengthParameter.ValueChanged += (o, s) => ConfigureOperators(Operators);
91      LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
92    }
93
94    #region Operator Discovery
95    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
96    static BinaryEncoding() {
97      encodingSpecificOperatorTypes = new List<Type>() {
98        typeof (IBinaryVectorOperator),
99        typeof (IBinaryVectorCreator),
100        typeof (IBinaryVectorCrossover),
101        typeof (IBinaryVectorManipulator),
102        typeof (IBinaryVectorMoveOperator),
103        typeof (IBinaryVectorMultiNeighborhoodShakingOperator),
104      };
105    }
106    private void DiscoverOperators() {
107      var pluginDescription = ApplicationManager.Manager.GetDeclaringPlugin(typeof(IBinaryVectorOperator));
108      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, pluginDescription, true, false, false);
109      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
110      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
111
112      ConfigureOperators(newOperators);
113      foreach (var @operator in newOperators)
114        encodingOperators.Add(@operator);
115    }
116    #endregion
117
118    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
119      ConfigureCreators(operators.OfType<IBinaryVectorCreator>());
120      ConfigureCrossovers(operators.OfType<IBinaryVectorCrossover>());
121      ConfigureManipulators(operators.OfType<IBinaryVectorManipulator>());
122      ConfigureMoveOperators(operators.OfType<IBinaryVectorMoveOperator>());
123      ConfigureBitFlipMoveOperators(operators.OfType<IOneBitflipMoveOperator>());
124      ConfigureShakingOperators(operators.OfType<IBinaryVectorMultiNeighborhoodShakingOperator>());
125    }
126
127    #region Specific Operator Wiring
128    private void ConfigureCreators(IEnumerable<IBinaryVectorCreator> creators) {
129      foreach (var creator in creators) {
130        creator.BinaryVectorParameter.ActualName = Name;
131        creator.LengthParameter.ActualName = LengthParameter.Name;
132      }
133    }
134    private void ConfigureCrossovers(IEnumerable<IBinaryVectorCrossover> crossovers) {
135      foreach (var crossover in crossovers) {
136        crossover.ParentsParameter.ActualName = Name;
137        crossover.ChildParameter.ActualName = Name;
138      }
139    }
140    private void ConfigureManipulators(IEnumerable<IBinaryVectorManipulator> manipulators) {
141      foreach (var manipulator in manipulators) {
142        manipulator.BinaryVectorParameter.ActualName = Name;
143      }
144    }
145    private void ConfigureMoveOperators(IEnumerable<IBinaryVectorMoveOperator> moveOperators) {
146      foreach (var moveOperator in moveOperators) {
147        moveOperator.BinaryVectorParameter.ActualName = Name;
148      }
149    }
150    private void ConfigureBitFlipMoveOperators(IEnumerable<IOneBitflipMoveOperator> oneBitflipMoveOperators) {
151      foreach (var oneBitFlipMoveOperator in oneBitflipMoveOperators) {
152        oneBitFlipMoveOperator.OneBitflipMoveParameter.ActualName = Name + "_OneBitFlipMove";
153      }
154    }
155    private void ConfigureShakingOperators(IEnumerable<IBinaryVectorMultiNeighborhoodShakingOperator> shakingOperators) {
156      foreach (var shakingOperator in shakingOperators) {
157        shakingOperator.BinaryVectorParameter.ActualName = Name;
158      }
159    }
160    #endregion
161  }
162}
Note: See TracBrowser for help on using the repository browser.