Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/CombinedEncoding.cs @ 17317

Last change on this file since 17317 was 17226, checked in by mkommend, 5 years ago

#2521: Merged trunk changes into problem refactoring branch.

File size: 3.6 KB
RevLine 
[11484]1#region License Information
2/* HeuristicLab
[17226]3 * Copyright (C) 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;
[17226]25using HEAL.Attic;
[11484]26using HeuristicLab.Common;
27using HeuristicLab.Core;
[11587]28using HeuristicLab.PluginInfrastructure;
[11484]29
[11949]30namespace HeuristicLab.Optimization {
[13376]31  [Item("CombinedEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")]
[16723]32  [StorableType("359E2173-4D0C-40E5-A2F3-E42E59840345")]
[13376]33  public sealed class CombinedEncoding : Encoding<CombinedSolution> {
[11587]34
[13359]35    private ItemCollection<IEncoding> encodings;
[13356]36
[11484]37    [Storable]
[13356]38    private IEnumerable<IEncoding> StorableEncodings {
[11587]39      get { return encodings; }
[13359]40      set { encodings = new ItemCollection<IEncoding>(value); }
[11587]41    }
[11484]42
[13356]43    public ReadOnlyItemCollection<IEncoding> Encodings {
44      get { return encodings.AsReadOnly(); }
45    }
46
[11484]47    [StorableConstructor]
[16723]48    private CombinedEncoding(StorableConstructorFlag _) : base(_) { }
[13376]49    public override IDeepCloneable Clone(Cloner cloner) { return new CombinedEncoding(this, cloner); }
50    private CombinedEncoding(CombinedEncoding original, Cloner cloner)
[11484]51      : base(original, cloner) {
[13356]52      encodings = new ItemCollection<IEncoding>(original.Encodings.Select(cloner.Clone));
[11484]53    }
[13376]54    public CombinedEncoding()
55      : base("CombinedEncoding") {
[13356]56      encodings = new ItemCollection<IEncoding>();
[13359]57      SolutionCreator = new MultiEncodingCreator() { SolutionParameter = { ActualName = Name } };
[13356]58      foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>()) {
[13359]59        @operator.SolutionParameter.ActualName = Name;
[11952]60        AddOperator(@operator);
[13356]61      }
[11484]62    }
63
[13376]64    public CombinedEncoding Add(IEncoding encoding) {
65      if (encoding is CombinedEncoding) throw new InvalidOperationException("Nesting of CombinedEncodings is not supported.");
[11587]66      if (Encodings.Any(e => e.Name == encoding.Name)) throw new ArgumentException("Encoding name must be unique", "encoding.Name");
67      encodings.Add(encoding);
[11588]68      Parameters.AddRange(encoding.Parameters);
[13359]69
70      foreach (var @operator in Operators.OfType<IMultiEncodingOperator>())
71        @operator.AddEncoding(encoding);
72
[11484]73      return this;
74    }
75
[11587]76    public bool Remove(IEncoding encoding) {
77      var success = encodings.Remove(encoding);
[11588]78      Parameters.RemoveRange(encoding.Parameters);
[13359]79
80      foreach (var @operator in Operators.OfType<IMultiEncodingOperator>())
81        @operator.RemoveEncoding(encoding);
82
[11587]83      return success;
[11484]84    }
85
[13365]86    public void Clear() {
87      foreach (var enc in encodings.ToList()) Remove(enc);
88    }
89
[13396]90    public override void ConfigureOperators(IEnumerable<IItem> operators) {
[13359]91      foreach (var encOp in operators.OfType<IMultiEncodingOperator>())
92        encOp.SolutionParameter.ActualName = Name;
93    }
[11484]94  }
95}
Note: See TracBrowser for help on using the repository browser.