Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13806 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.5 KB
RevLine 
[11484]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11484]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;
[11587]24using System.Linq;
[11484]25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[11587]28using HeuristicLab.PluginInfrastructure;
[11484]29
[11949]30namespace HeuristicLab.Optimization {
[11484]31  [Item("MultiEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")]
32  [StorableClass]
[11598]33  public sealed class MultiEncoding : Encoding<MultiEncodingCreator> {
[11587]34
35    private readonly List<IEncoding> encodings;
[11484]36    [Storable]
[11587]37    public IEnumerable<IEncoding> Encodings {
38      get { return encodings; }
39      private set { encodings.AddRange(value); }
40    }
[11484]41
42    [StorableConstructor]
[11598]43    private MultiEncoding(bool deserializing)
[11587]44      : base(deserializing) {
45      encodings = new List<IEncoding>();
46    }
47    public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncoding(this, cloner); }
[11598]48    private MultiEncoding(MultiEncoding original, Cloner cloner)
[11484]49      : base(original, cloner) {
[11587]50      encodings = new List<IEncoding>(original.Encodings.Select(cloner.Clone));
[11484]51    }
52    public MultiEncoding()
53      : base("MultiEncoding") {
[11587]54      encodings = new List<IEncoding>();
55      SolutionCreator = new MultiEncodingCreator();
[11484]56
[11587]57      foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>())
[11952]58        AddOperator(@operator);
[11484]59    }
60
[11619]61    public override Individual GetIndividual(IScope scope) {
[11598]62      return new MultiEncodingIndividual(this, scope);
63    }
64
[11587]65    public MultiEncoding Add(IEncoding encoding) {
66      if (encoding is MultiEncoding) throw new InvalidOperationException("Nesting of MultiEncodings is not supported.");
67      if (Encodings.Any(e => e.Name == encoding.Name)) throw new ArgumentException("Encoding name must be unique", "encoding.Name");
68      encodings.Add(encoding);
[11484]69
[11588]70      Parameters.AddRange(encoding.Parameters);
71
[11587]72      foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {
73        @operator.AddEncoding(encoding);
74      }
[11892]75      OnEncodingsChanged();
[11484]76      return this;
77    }
78
[11587]79    public bool Remove(IEncoding encoding) {
80      var success = encodings.Remove(encoding);
[11588]81      Parameters.RemoveRange(encoding.Parameters);
[11587]82      foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {
83        @operator.RemoveEncoding(encoding);
84      }
[11892]85      OnEncodingsChanged();
[11587]86      return success;
[11484]87    }
88
[11587]89    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
[11484]90    }
[11892]91
92    public event EventHandler EncodingsChanged;
93    private void OnEncodingsChanged() {
94      var handler = EncodingsChanged;
95      if (handler != null) handler(this, EventArgs.Empty);
96    }
[11484]97  }
98}
Note: See TracBrowser for help on using the repository browser.