Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11797 was 11797, checked in by mkommend, 9 years ago

#2174: Fixed namespaces in programmable problem plugin.

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