1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
9 | using HeuristicLab.Optimization;
|
---|
10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
11 | using HeuristicLab.PluginInfrastructure;
|
---|
12 | using HeuristicLab.Problems.BinPacking;
|
---|
13 |
|
---|
14 | namespace 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 | }
|
---|