source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/MultiEncoding.cs @ 11588

Last change on this file since 11588 was 11588, checked in by mkommend, 8 years ago

#2174: Adapted encodings to store its specific parameters in the standard parameter collection.

File size: 3.2 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.PluginInfrastructure;
29using HeuristicLab.Problems.Programmable.Interfaces;
30
31namespace HeuristicLab.Problems.Programmable {
32  [Item("MultiEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")]
33  [StorableClass]
34  public class MultiEncoding : Encoding<MultiEncodingCreator> {
35
36    private readonly List<IEncoding> encodings;
37    [Storable]
38    public IEnumerable<IEncoding> Encodings {
39      get { return encodings; }
40      private set { encodings.AddRange(value); }
41    }
42
43
44    [StorableConstructor]
45    protected MultiEncoding(bool deserializing)
46      : base(deserializing) {
47      encodings = new List<IEncoding>();
48    }
49
50    public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncoding(this, cloner); }
51    protected MultiEncoding(MultiEncoding original, Cloner cloner)
52      : base(original, cloner) {
53      encodings = new List<IEncoding>(original.Encodings.Select(cloner.Clone));
54    }
55    public MultiEncoding()
56      : base("MultiEncoding") {
57      encodings = new List<IEncoding>();
58      SolutionCreator = new MultiEncodingCreator();
59
60      foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>())
61        encodingOperators.Add(@operator);
62    }
63
64    public MultiEncoding Add(IEncoding encoding) {
65      if (encoding is MultiEncoding) throw new InvalidOperationException("Nesting of MultiEncodings is not supported.");
66      if (Encodings.Any(e => e.Name == encoding.Name)) throw new ArgumentException("Encoding name must be unique", "encoding.Name");
67      encodings.Add(encoding);
68
69      Parameters.AddRange(encoding.Parameters);
70
71      foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {
72        @operator.AddEncoding(encoding);
73      }
74      return this;
75    }
76
77    public bool Remove(IEncoding encoding) {
78      var success = encodings.Remove(encoding);
79      Parameters.RemoveRange(encoding.Parameters);
80      foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {
81        @operator.RemoveEncoding(encoding);
82      }
83      return success;
84    }
85
86    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
87
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.