source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/PermutationEncoding.cs @ 11753

Last change on this file since 11753 was 11753, checked in by mkommend, 5 years ago

#2174: First working version of refactored programmable problem.

File size: 9.9 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
98    public PermutationEncoding() : this("Permutation", 10, PermutationTypes.Absolute) { }
99    public PermutationEncoding(int length) : this("Permuration", length, PermutationTypes.Absolute) { }
100    public PermutationEncoding(string name, int length, PermutationTypes type)
101      : base(name) {
102      lengthParameter = new FixedValueParameter<IntValue>(Name + ".Length", new IntValue(length));
103      permutationTypeParameter = new FixedValueParameter<PermutationType>(Name + ".Type", new PermutationType(type));
104      Parameters.Add(lengthParameter);
105      Parameters.Add(permutationTypeParameter);
106
107      SolutionCreator = new RandomPermutationCreator();
108      RegisterParameterEvents();
109      DiscoverOperators();
110    }
111
112    private void OnLengthParameterChanged() {
113      RegisterLengthParameterEvents();
114      ConfigureOperators(Operators);
115    }
116
117    private void OnPermutationTypeParameterChanged() {
118      RegisterPermutationTypeParameterEvents();
119      ConfigureOperators(Operators);
120    }
121
122    private void RegisterParameterEvents() {
123      RegisterLengthParameterEvents();
124      RegisterPermutationTypeParameterEvents();
125    }
126    private void RegisterLengthParameterEvents() {
127      LengthParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
128    }
129    private void RegisterPermutationTypeParameterEvents() {
130      PermutationTypeParameter.Value.ValueChanged += (o, s) => ConfigureOperators(Operators);
131    }
132
133    #region Operator Discovery
134    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
135    static PermutationEncoding() {
136      encodingSpecificOperatorTypes = new List<Type>() {
137          typeof (IPermutationOperator),
138          typeof (IPermutationCreator),
139          typeof (IPermutationCrossover),
140          typeof (IPermutationManipulator),
141          typeof (IPermutationMultiNeighborhoodShakingOperator),
142          typeof (IPermutationMoveOperator),
143          typeof (IPermutationInversionMoveOperator),
144          typeof (IPermutationScrambleMoveOperator),
145          typeof (IPermutationSwap2MoveOperator),                   
146          typeof (IPermutationTranslocationMoveOperator)
147      };
148    }
149    private void DiscoverOperators() {
150      var pluginDescription = ApplicationManager.Manager.GetDeclaringPlugin(typeof(IPermutationOperator));
151      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, pluginDescription, true, false, false);
152      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
153      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
154
155      ConfigureOperators(newOperators);
156      foreach (var @operator in newOperators)
157        encodingOperators.Add(@operator);
158    }
159    #endregion
160
161    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
162      ConfigureCreators(operators.OfType<IPermutationCreator>());
163      ConfigureCrossovers(operators.OfType<IPermutationCrossover>());
164      ConfigureManipulators(operators.OfType<IPermutationManipulator>());
165      ConfigureShakingOperators(operators.OfType<IPermutationMultiNeighborhoodShakingOperator>());
166      ConfigureMoveOperators(operators.OfType<IPermutationMoveOperator>());
167      ConfigureInversionMoveOperators(operators.OfType<IPermutationInversionMoveOperator>());
168      ConfigureScrambleMoveOperators(operators.OfType<IPermutationScrambleMoveOperator>());
169      ConfigureSwap2MoveOperators(operators.OfType<IPermutationSwap2MoveOperator>());
170      ConfigureTranslocationMoveOperators(operators.OfType<IPermutationTranslocationMoveOperator>());
171    }
172
173    #region specific operator wiring
174    private void ConfigureCreators(IEnumerable<IPermutationCreator> creators) {
175      foreach (var creator in creators) {
176        creator.LengthParameter.ActualName = LengthParameter.Name;
177        creator.PermutationParameter.ActualName = Name;
178        creator.PermutationTypeParameter.Value.Value = Type;
179      }
180    }
181    private void ConfigureCrossovers(IEnumerable<IPermutationCrossover> crossovers) {
182      foreach (var crossover in crossovers) {
183        crossover.ChildParameter.ActualName = Name;
184        crossover.ParentsParameter.ActualName = Name;
185      }
186    }
187    private void ConfigureManipulators(IEnumerable<IPermutationManipulator> manipulators) {
188      foreach (var manipulator in manipulators) {
189        manipulator.PermutationParameter.ActualName = Name;
190      }
191    }
192    private void ConfigureShakingOperators(IEnumerable<IPermutationMultiNeighborhoodShakingOperator> shakingOperators) {
193      foreach (var shakingOperator in shakingOperators) {
194        shakingOperator.PermutationParameter.ActualName = Name;
195      }
196    }
197    private void ConfigureMoveOperators(IEnumerable<IPermutationMoveOperator> moveOperators) {
198      foreach (var moveOperator in moveOperators) {
199        moveOperator.PermutationParameter.ActualName = Name;
200      }
201    }
202    private void ConfigureInversionMoveOperators(IEnumerable<IPermutationInversionMoveOperator> inversionMoveOperators) {
203      foreach (var inversionMoveOperator in inversionMoveOperators) {
204        inversionMoveOperator.InversionMoveParameter.ActualName = Name + ".InversionMove";
205      }
206    }
207    private void ConfigureScrambleMoveOperators(IEnumerable<IPermutationScrambleMoveOperator> scrambleMoveOperators) {
208      foreach (var scrambleMoveOperator in scrambleMoveOperators) {
209        scrambleMoveOperator.ScrambleMoveParameter.ActualName = Name + ".ScambleMove";
210      }
211    }
212    private void ConfigureSwap2MoveOperators(IEnumerable<IPermutationSwap2MoveOperator> swap2MoveOperators) {
213      foreach (var swap2MoveOperator in swap2MoveOperators) {
214        swap2MoveOperator.Swap2MoveParameter.ActualName = Name + ".Swap2Move";
215      }
216    }
217    private void ConfigureTranslocationMoveOperators(IEnumerable<IPermutationTranslocationMoveOperator> translocationMoveOperators) {
218      foreach (var translocationMoveOperator in translocationMoveOperators) {
219        translocationMoveOperator.TranslocationMoveParameter.ActualName = Name + ".TranslocationMove";
220      }
221    }
222
223    #endregion
224  }
225}
Note: See TracBrowser for help on using the repository browser.