Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2174: Refactored individuals.

File size: 3.4 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 sealed 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    private 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    private 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 override Individual GetIndividual(IScope scope) {
65      return new MultiEncodingIndividual(this, scope);
66    }
67
68    public MultiEncoding Add(IEncoding encoding) {
69      if (encoding is MultiEncoding) throw new InvalidOperationException("Nesting of MultiEncodings is not supported.");
70      if (Encodings.Any(e => e.Name == encoding.Name)) throw new ArgumentException("Encoding name must be unique", "encoding.Name");
71      encodings.Add(encoding);
72
73      Parameters.AddRange(encoding.Parameters);
74
75      foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {
76        @operator.AddEncoding(encoding);
77      }
78      return this;
79    }
80
81    public bool Remove(IEncoding encoding) {
82      var success = encodings.Remove(encoding);
83      Parameters.RemoveRange(encoding.Parameters);
84      foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {
85        @operator.RemoveEncoding(encoding);
86      }
87      return success;
88    }
89
90    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
91
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.