Free cookie consent management tool by TermsFeed Policy Generator

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

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

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