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.PluginInfrastructure;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Optimization {
|
---|
31 | [Item("CombinedEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")]
|
---|
32 | [StorableType("359E2173-4D0C-40E5-A2F3-E42E59840345")]
|
---|
33 | public sealed class CombinedEncoding : Encoding {
|
---|
34 |
|
---|
35 | private ItemCollection<IEncoding> encodings;
|
---|
36 |
|
---|
37 | [Storable]
|
---|
38 | private IEnumerable<IEncoding> StorableEncodings {
|
---|
39 | get { return encodings; }
|
---|
40 | set { encodings = new ItemCollection<IEncoding>(value); }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public ReadOnlyItemCollection<IEncoding> Encodings {
|
---|
44 | get { return encodings.AsReadOnly(); }
|
---|
45 | }
|
---|
46 |
|
---|
47 | [StorableConstructor]
|
---|
48 | private CombinedEncoding(StorableConstructorFlag _) : base(_) { }
|
---|
49 | public override IDeepCloneable Clone(Cloner cloner) { return new CombinedEncoding(this, cloner); }
|
---|
50 | private CombinedEncoding(CombinedEncoding original, Cloner cloner)
|
---|
51 | : base(original, cloner) {
|
---|
52 | encodings = new ItemCollection<IEncoding>(original.Encodings.Select(cloner.Clone));
|
---|
53 | }
|
---|
54 | public CombinedEncoding()
|
---|
55 | : base("CombinedEncoding") {
|
---|
56 | encodings = new ItemCollection<IEncoding>();
|
---|
57 | foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>()) {
|
---|
58 | @operator.SolutionParameter.ActualName = Name;
|
---|
59 | AddOperator(@operator);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public CombinedEncoding Add(IEncoding encoding) {
|
---|
64 | if (encoding is CombinedEncoding) throw new InvalidOperationException("Nesting of CombinedEncodings is not supported.");
|
---|
65 | if (Encodings.Any(e => e.Name == encoding.Name)) throw new ArgumentException("Encoding name must be unique", "encoding.Name");
|
---|
66 | encodings.Add(encoding);
|
---|
67 | Parameters.AddRange(encoding.Parameters);
|
---|
68 |
|
---|
69 | foreach (var @operator in Operators.OfType<IMultiEncodingOperator>())
|
---|
70 | @operator.AddEncoding(encoding);
|
---|
71 |
|
---|
72 | return this;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public bool Remove(IEncoding encoding) {
|
---|
76 | var success = encodings.Remove(encoding);
|
---|
77 | Parameters.RemoveRange(encoding.Parameters);
|
---|
78 |
|
---|
79 | foreach (var @operator in Operators.OfType<IMultiEncodingOperator>())
|
---|
80 | @operator.RemoveEncoding(encoding);
|
---|
81 |
|
---|
82 | return success;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void Clear() {
|
---|
86 | foreach (var enc in encodings.ToList()) Remove(enc);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public override void ConfigureOperators(IEnumerable<IItem> operators) {
|
---|
90 | foreach (var encOp in operators.OfType<IMultiEncodingOperator>())
|
---|
91 | encOp.SolutionParameter.ActualName = Name;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|