Free cookie consent management tool by TermsFeed Policy Generator

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