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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Optimization {
|
---|
31 | [Item("Encoding", "Base class for describing different encodings.")]
|
---|
32 | [StorableClass]
|
---|
33 | public abstract class Encoding<T> : ParameterizedNamedItem, IEncoding
|
---|
34 | where T : class,ISolutionCreator {
|
---|
35 | public override sealed bool CanChangeName {
|
---|
36 | get { return false; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | private ItemSet<IOperator> encodingOperators = new ItemSet<IOperator>(new TypeEqualityComparer<IOperator>());
|
---|
40 |
|
---|
41 | [Storable(Name = "Operators")]
|
---|
42 | private IEnumerable<IOperator> StorableOperators {
|
---|
43 | get { return encodingOperators; }
|
---|
44 | set { encodingOperators = new ItemSet<IOperator>(value, new TypeEqualityComparer<IOperator>()); ; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public IEnumerable<IOperator> Operators {
|
---|
48 | get { return encodingOperators; }
|
---|
49 | set {
|
---|
50 | if (!value.OfType<T>().Any())
|
---|
51 | throw new ArgumentException("The provided operators contain no suitable solution creator");
|
---|
52 | encodingOperators.Clear();
|
---|
53 | foreach (var op in value) encodingOperators.Add(op);
|
---|
54 |
|
---|
55 | T newSolutionCreator = (T)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType()) ??
|
---|
56 | encodingOperators.OfType<T>().First();
|
---|
57 | SolutionCreator = newSolutionCreator;
|
---|
58 | OnOperatorsChanged();
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | ISolutionCreator IEncoding.SolutionCreator {
|
---|
63 | get { return SolutionCreator; }
|
---|
64 | set {
|
---|
65 | if (!(value is T)) throw new ArgumentException(string.Format("Cannot assign the solution creator {0} to the encoding {1}.", value.GetType().GetPrettyName(), GetType().GetPrettyName()));
|
---|
66 | SolutionCreator = (T)value;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | private T solutionCreator;
|
---|
70 | public T SolutionCreator {
|
---|
71 | get {
|
---|
72 | return solutionCreator;
|
---|
73 | }
|
---|
74 | set {
|
---|
75 | if (value == null) throw new ArgumentNullException("SolutionCreator must not be null.");
|
---|
76 | if (solutionCreator == value) return;
|
---|
77 | encodingOperators.Remove(solutionCreator);
|
---|
78 | encodingOperators.Add(value);
|
---|
79 | solutionCreator = value;
|
---|
80 | OnSolutionCreatorChanged();
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | [StorableConstructor]
|
---|
85 | protected Encoding(bool deserializing) : base(deserializing) { }
|
---|
86 | protected Encoding(Encoding<T> original, Cloner cloner)
|
---|
87 | : base(original, cloner) {
|
---|
88 | encodingOperators = cloner.Clone(original.encodingOperators);
|
---|
89 | solutionCreator = cloner.Clone(original.solutionCreator);
|
---|
90 | }
|
---|
91 | protected Encoding(string name)
|
---|
92 | : base(name) {
|
---|
93 | Parameters.Add(new FixedValueParameter<ReadOnlyItemSet<IOperator>>(name + ".Operators", "The operators that the encoding specifies.", encodingOperators.AsReadOnly()));
|
---|
94 | }
|
---|
95 |
|
---|
96 | public virtual Individual GetIndividual(IScope scope) {
|
---|
97 | return new SingleEncodingIndividual(this, scope);
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected bool AddOperator(IOperator @operator) {
|
---|
101 | return encodingOperators.Add(@operator);
|
---|
102 | }
|
---|
103 |
|
---|
104 | protected bool RemoveOperator(IOperator @operator) {
|
---|
105 | return encodingOperators.Remove(@operator);
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void ConfigureOperator(IOperator @operator) { ConfigureOperators(new[] { @operator }); }
|
---|
109 | public abstract void ConfigureOperators(IEnumerable<IOperator> operators);
|
---|
110 |
|
---|
111 | public event EventHandler SolutionCreatorChanged;
|
---|
112 | protected virtual void OnSolutionCreatorChanged() {
|
---|
113 | ConfigureOperator(SolutionCreator);
|
---|
114 | var handler = SolutionCreatorChanged;
|
---|
115 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
116 | }
|
---|
117 |
|
---|
118 | public event EventHandler OperatorsChanged;
|
---|
119 | protected virtual void OnOperatorsChanged() {
|
---|
120 | ConfigureOperators(Operators);
|
---|
121 | var handler = OperatorsChanged;
|
---|
122 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|