1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 HEAL.Attic;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Optimization {
|
---|
31 | [Item("Encoding", "Base class for describing different encodings.")]
|
---|
32 | [StorableType("395B1372-FA54-4649-9EBE-5402A0AA9494")]
|
---|
33 | public abstract class Encoding : ParameterizedNamedItem, IEncoding {
|
---|
34 | public sealed override bool CanChangeName {
|
---|
35 | get { return false; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | private ItemSet<IOperator> encodingOperators = new ItemSet<IOperator>(new TypeEqualityComparer<IOperator>());
|
---|
39 |
|
---|
40 | [Storable(Name = "Operators")]
|
---|
41 | private IEnumerable<IOperator> StorableOperators {
|
---|
42 | get { return encodingOperators; }
|
---|
43 | set { encodingOperators = new ItemSet<IOperator>(value, new TypeEqualityComparer<IOperator>()); ; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public IEnumerable<IOperator> Operators {
|
---|
47 | get { return encodingOperators; }
|
---|
48 | set {
|
---|
49 | // SolutionCreator is now a parameter of the algorithm, we don't care!
|
---|
50 | //if (!value.OfType<ISolutionCreator<TEncodedSolution>>().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 | OnOperatorsChanged();
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | [StorableConstructor]
|
---|
60 | protected Encoding(StorableConstructorFlag _) : base(_) { }
|
---|
61 | [StorableHook(HookType.AfterDeserialization)]
|
---|
62 | private void AfterDeserialization() { }
|
---|
63 |
|
---|
64 | protected Encoding(Encoding original, Cloner cloner)
|
---|
65 | : base(original, cloner) {
|
---|
66 | encodingOperators = cloner.Clone(original.encodingOperators);
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected Encoding(string name)
|
---|
70 | : base(name) {
|
---|
71 | Parameters.Add(new FixedValueParameter<ReadOnlyItemSet<IOperator>>(name + ".Operators", "The operators that the encoding specifies.", encodingOperators.AsReadOnly()) {
|
---|
72 | GetsCollected = false, ReadOnly = true
|
---|
73 | });
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected bool AddOperator(IOperator @operator) {
|
---|
77 | return encodingOperators.Add(@operator);
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected bool RemoveOperator(IOperator @operator) {
|
---|
81 | return encodingOperators.Remove(@operator);
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected void ReplaceOperators(IEnumerable<IOperator> operators) {
|
---|
85 | encodingOperators.Replace(operators);
|
---|
86 | }
|
---|
87 |
|
---|
88 | public void ConfigureOperator(IItem @operator) { ConfigureOperators(new[] { @operator }); }
|
---|
89 | public virtual void ConfigureOperators(IEnumerable<IItem> operators) {
|
---|
90 | ConfigureSingleObjectiveImprovementOperators(operators.OfType<ISingleObjectiveImprovementOperator>());
|
---|
91 | ConfigureSingleObjectivePathRelinker(operators.OfType<ISingleObjectivePathRelinker>());
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected virtual void ConfigureSingleObjectiveImprovementOperators(IEnumerable<ISingleObjectiveImprovementOperator> operators) {
|
---|
95 | foreach (var op in operators) {
|
---|
96 | op.SolutionParameter.ActualName = Name;
|
---|
97 | op.SolutionParameter.Hidden = true;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | protected virtual void ConfigureSingleObjectivePathRelinker(IEnumerable<ISingleObjectivePathRelinker> operators) {
|
---|
102 | foreach (var op in operators.OfType<ISingleObjectivePathRelinker>()) {
|
---|
103 | op.ParentsParameter.ActualName = Name;
|
---|
104 | op.ParentsParameter.Hidden = true;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | public event EventHandler OperatorsChanged;
|
---|
109 | protected virtual void OnOperatorsChanged() {
|
---|
110 | ConfigureOperators(Operators);
|
---|
111 | var handler = OperatorsChanged;
|
---|
112 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|