Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorEncoding.cs @ 13883

Last change on this file since 13883 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

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