Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/PermutationEncoding.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: 9.6 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.PermutationEncoding;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Problems.Programmable {
34  [Item("PermutationEncoding", "Describes a permutation encoding.")]
35  [StorableClass]
36  public sealed class PermutationEncoding : Encoding<IPermutationCreator> {
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
54    [Storable]
55    private IFixedValueParameter<PermutationType> permutationTypeParameter;
56    public IFixedValueParameter<PermutationType> PermutationTypeParameter {
57      get { return permutationTypeParameter; }
58      set {
59        if (value == null) throw new ArgumentNullException("Permutation type parameter must not be null.");
60        if (value.Value == null) throw new ArgumentNullException("Permutation type parameter value must not be null.");
61        if (permutationTypeParameter == value) return;
62
63        if (permutationTypeParameter != null) Parameters.Remove(permutationTypeParameter);
64        permutationTypeParameter = value;
65        Parameters.Add(permutationTypeParameter);
66        OnPermutationTypeParameterChanged();
67      }
68    }
69    #endregion
70
71    public int Length {
72      get { return LengthParameter.Value.Value; }
73      set { LengthParameter.Value.Value = value; }
74    }
75
76    public PermutationTypes Type {
77      get { return PermutationTypeParameter.Value.Value; }
78      set { PermutationTypeParameter.Value.Value = value; }
79    }
80
81    [StorableConstructor]
82    private PermutationEncoding(bool deserializing) : base(deserializing) { }
83    [StorableHook(HookType.AfterDeserialization)]
84    private void AfterDeserialization() {
85      RegisterParameterEvents();
86      DiscoverOperators();
87    }
88
89    public override IDeepCloneable Clone(Cloner cloner) { return new PermutationEncoding(this, cloner); }
90    private PermutationEncoding(PermutationEncoding original, Cloner cloner)
91      : base(original, cloner) {
92      lengthParameter = cloner.Clone(original.lengthParameter);
93      permutationTypeParameter = cloner.Clone(original.permutationTypeParameter);
94      RegisterParameterEvents();
95    }
96
97    public PermutationEncoding(string name, int length, PermutationTypes type)
98      : base(name) {
99      lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
100      permutationTypeParameter = new FixedValueParameter<PermutationType>(Name + ".Type", new PermutationType(type));
101      Parameters.Add(lengthParameter);
102      Parameters.Add(permutationTypeParameter);
103
104      SolutionCreator = new RandomPermutationCreator();
105      RegisterParameterEvents();
106      DiscoverOperators();
107    }
108
109    private void OnLengthParameterChanged() {
110      RegisterLengthParameterEvents();
111      ConfigureOperators(Operators);
112    }
113
114    private void OnPermutationTypeParameterChanged() {
115      RegisterPermutationTypeParameterEvents();
116      ConfigureOperators(Operators);
117    }
118
119    private void RegisterParameterEvents() {
120      RegisterLengthParameterEvents();
121      RegisterPermutationTypeParameterEvents();
122    }
123    private void RegisterLengthParameterEvents() {
124      LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
125    }
126    private void RegisterPermutationTypeParameterEvents() {
127      PermutationTypeParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
128    }
129
130    #region Operator Discovery
131    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
132    static PermutationEncoding() {
133      encodingSpecificOperatorTypes = new List<Type>() {
134          typeof (IPermutationOperator),
135          typeof (IPermutationCreator),
136          typeof (IPermutationCrossover),
137          typeof (IPermutationManipulator),
138          typeof (IPermutationMultiNeighborhoodShakingOperator),
139          typeof (IPermutationMoveOperator),
140          typeof (IPermutationInversionMoveOperator),
141          typeof (IPermutationScrambleMoveOperator),
142          typeof (IPermutationSwap2MoveOperator),                   
143          typeof (IPermutationTranslocationMoveOperator)
144      };
145    }
146    private void DiscoverOperators() {
147      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, true, false, false);
148      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
149      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
150
151      ConfigureOperators(newOperators);
152      foreach (var @operator in newOperators)
153        encodingOperators.Add(@operator);
154    }
155    #endregion
156
157    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
158      ConfigureCreators(Operators.OfType<IPermutationCreator>());
159      ConfigureCrossovers(Operators.OfType<IPermutationCrossover>());
160      ConfigureManipulators(Operators.OfType<IPermutationManipulator>());
161      ConfigureShakingOperators(Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>());
162      ConfigureMoveOperators(Operators.OfType<IPermutationMoveOperator>());
163      ConfigureInversionMoveOperators(Operators.OfType<IPermutationInversionMoveOperator>());
164      ConfigureScrambleMoveOperators(Operators.OfType<IPermutationScrambleMoveOperator>());
165      ConfigureSwap2MoveOperators(Operators.OfType<IPermutationSwap2MoveOperator>());
166      ConfigureTranslocationMoveOperators(Operators.OfType<IPermutationTranslocationMoveOperator>());
167    }
168
169    #region specific operator wiring
170    private void ConfigureCreators(IEnumerable<IPermutationCreator> creators) {
171      foreach (var creator in creators) {
172        creator.LengthParameter.ActualName = LengthParameter.Name;
173        creator.PermutationParameter.ActualName = Name;
174        creator.PermutationTypeParameter.Value.Value = Type;
175      }
176    }
177    private void ConfigureCrossovers(IEnumerable<IPermutationCrossover> crossovers) {
178      foreach (var crossover in crossovers) {
179        crossover.ChildParameter.ActualName = Name;
180        crossover.ParentsParameter.ActualName = Name;
181      }
182    }
183    private void ConfigureManipulators(IEnumerable<IPermutationManipulator> manipulators) {
184      foreach (var manipulator in manipulators) {
185        manipulator.PermutationParameter.ActualName = Name;
186      }
187    }
188    private void ConfigureShakingOperators(IEnumerable<IPermutationMultiNeighborhoodShakingOperator> shakingOperators) {
189      foreach (var shakingOperator in shakingOperators) {
190        shakingOperator.PermutationParameter.ActualName = Name;
191      }
192    }
193    private void ConfigureMoveOperators(IEnumerable<IPermutationMoveOperator> moveOperators) {
194      foreach (var moveOperator in moveOperators) {
195        moveOperator.PermutationParameter.ActualName = Name;
196      }
197    }
198    private void ConfigureInversionMoveOperators(IEnumerable<IPermutationInversionMoveOperator> inversionMoveOperators) {
199      foreach (var inversionMoveOperator in inversionMoveOperators) {
200        inversionMoveOperator.InversionMoveParameter.ActualName = Name + ".InversionMove";
201      }
202    }
203    private void ConfigureScrambleMoveOperators(IEnumerable<IPermutationScrambleMoveOperator> scrambleMoveOperators) {
204      foreach (var scrambleMoveOperator in scrambleMoveOperators) {
205        scrambleMoveOperator.ScrambleMoveParameter.ActualName = Name + ".ScambleMove";
206      }
207    }
208    private void ConfigureSwap2MoveOperators(IEnumerable<IPermutationSwap2MoveOperator> swap2MoveOperators) {
209      foreach (var swap2MoveOperator in swap2MoveOperators) {
210        swap2MoveOperator.Swap2MoveParameter.ActualName = Name + ".Swap2Move";
211      }
212    }
213    private void ConfigureTranslocationMoveOperators(IEnumerable<IPermutationTranslocationMoveOperator> translocationMoveOperators) {
214      foreach (var translocationMoveOperator in translocationMoveOperators) {
215        translocationMoveOperator.TranslocationMoveParameter.ActualName = Name + ".TranslocationMove";
216      }
217    }
218
219    #endregion
220  }
221}
Note: See TracBrowser for help on using the repository browser.