Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiEncoding.cs @ 13351

Last change on this file since 13351 was 13351, checked in by abeham, 8 years ago

#2521: Adapted multi-encoding for new infrastructure

TODO: Evaluator, Analyzer, ... need to be copied

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Optimization {
31  [Item("MultiEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")]
32  [StorableClass]
33  public sealed class MultiEncoding : Encoding<MultiSolution> {
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(bool deserializing)
44      : base(deserializing) {
45      encodings = new List<IEncoding>();
46    }
47    public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncoding(this, cloner); }
48    private MultiEncoding(MultiEncoding original, Cloner cloner)
49      : base(original, cloner) {
50      encodings = new List<IEncoding>(original.Encodings.Select(cloner.Clone));
51    }
52    public MultiEncoding()
53      : base("MultiEncoding") {
54      encodings = new List<IEncoding>();
55      SolutionCreator = new MultiEncodingCreator();
56
57      foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>())
58        AddOperator(@operator);
59    }
60
61    public MultiEncoding Add(IEncoding encoding) {
62      if (encoding is MultiEncoding) throw new InvalidOperationException("Nesting of MultiEncodings is not supported.");
63      if (Encodings.Any(e => e.Name == encoding.Name)) throw new ArgumentException("Encoding name must be unique", "encoding.Name");
64      encodings.Add(encoding);
65
66      Parameters.AddRange(encoding.Parameters);
67
68      foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {
69        @operator.AddEncoding(encoding);
70      }
71      OnEncodingsChanged();
72      return this;
73    }
74
75    public bool Remove(IEncoding encoding) {
76      var success = encodings.Remove(encoding);
77      Parameters.RemoveRange(encoding.Parameters);
78      foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {
79        @operator.RemoveEncoding(encoding);
80      }
81      OnEncodingsChanged();
82      return success;
83    }
84
85    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
86    }
87
88    public event EventHandler EncodingsChanged;
89    private void OnEncodingsChanged() {
90      var handler = EncodingsChanged;
91      if (handler != null) handler(this, EventArgs.Empty);
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.