Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Optimization/3.3/BasicProblems/MultiEncoding.cs @ 17021

Last change on this file since 17021 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab.Optimization {
31  [Item("MultiEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")]
32  [StorableType("359E2173-4D0C-40E5-A2F3-E42E59840345")]
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    [StorableConstructor]
43    private MultiEncoding(StorableConstructorFlag _) : base(_) {
44      encodings = new List<IEncoding>();
45    }
46    public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncoding(this, cloner); }
47    private MultiEncoding(MultiEncoding original, Cloner cloner)
48      : base(original, cloner) {
49      encodings = new List<IEncoding>(original.Encodings.Select(cloner.Clone));
50    }
51    public MultiEncoding()
52      : base("MultiEncoding") {
53      encodings = new List<IEncoding>();
54      SolutionCreator = new MultiEncodingCreator();
55
56      foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>())
57        AddOperator(@operator);
58    }
59
60    public override Individual GetIndividual(IScope scope) {
61      return new MultiEncodingIndividual(this, scope);
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      OnEncodingsChanged();
75      return this;
76    }
77
78    public bool Remove(IEncoding encoding) {
79      var success = encodings.Remove(encoding);
80      Parameters.RemoveRange(encoding.Parameters);
81      foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {
82        @operator.RemoveEncoding(encoding);
83      }
84      OnEncodingsChanged();
85      return success;
86    }
87
88    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
89    }
90
91    public event EventHandler EncodingsChanged;
92    private void OnEncodingsChanged() {
93      var handler = EncodingsChanged;
94      if (handler != null) handler(this, EventArgs.Empty);
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.