Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11587 was 11587, checked in by mkommend, 9 years ago

#2174: Implemented multi-encoding operators and adapated wiring of operators in the programmable problems.

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