source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Encodings/PackingSequence/Encoding.cs @ 14064

Last change on this file since 14064 was 14064, checked in by gkronber, 5 years ago

#1966: first steps for refactoring to use new Encoding framework

File size: 2.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8using HeuristicLab.Optimization;
9using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
10using HeuristicLab.PluginInfrastructure;
11using HeuristicLab.Problems.BinPacking;
12
13namespace HeuristicLab.Encodings.PackingEncoding.PackingSequence {
14  [StorableClass]
15  internal class Encoding : HeuristicLab.Optimization.Encoding<PackingSolutionCreator> {
16    [StorableConstructor]
17    private Encoding(bool deserializing) : base(deserializing) { }
18    public Encoding() : base("PackingSequence") {
19      SolutionCreator = new PackingSequenceRandomCreator();
20      RegisterParameterEvents();
21      DiscoverOperators();
22    }
23
24    private Encoding(Encoding original, Cloner cloner)
25      : base(original, cloner) {
26      RegisterParameterEvents();
27    }
28
29
30    public override IDeepCloneable Clone(Cloner cloner) {
31      return new Encoding(this, cloner);
32    }
33
34    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
35      // TODO
36    }
37
38    #region Operator discovery
39    private static readonly IEnumerable<Type> encodingSpecificOperatorTypes;
40    static Encoding() {
41      encodingSpecificOperatorTypes = new List<Type>
42      {
43        typeof(IPackingSequenceOperator),         // TODO move (also namespace)
44      };
45    }
46    private void DiscoverOperators() {
47      var assembly = typeof(IPackingSequenceCreator).Assembly;
48      var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false);
49      var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t));
50      var newOperators = operators.Except(Operators, new TypeEqualityComparer<IOperator>()).ToList();
51
52      ConfigureOperators(newOperators);
53      foreach (var @operator in newOperators)
54        AddOperator(@operator);
55    }
56    #endregion
57
58    private void RegisterParameterEvents() {
59      // nothing to do
60    }
61
62
63  }
64
65  public static class IndividualExtensionMethods {
66    public static PackingSequence PackingSequence(this Individual individual) {
67      var encoding = individual.GetEncoding<Encoding>();
68      return individual.PackingSequence(encoding.Name);
69    }
70
71    public static PackingSequence PackingSequence(this Individual individual, string name) {
72      return (PackingSequence)individual[name];
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.