Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1966_HeuristicLab.BinPacking/old files/PackingSequence/Encoding.cs @ 18183

Last change on this file since 18183 was 14128, checked in by gkronber, 8 years ago

#1966: refactoring of bin packing implementation

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