[11484] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17097] | 3 | * Copyright (C) 2002-2019 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[11587] | 24 | using System.Linq;
|
---|
[11484] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[17097] | 27 | using HEAL.Attic;
|
---|
[11587] | 28 | using HeuristicLab.PluginInfrastructure;
|
---|
[11484] | 29 |
|
---|
[11949] | 30 | namespace HeuristicLab.Optimization {
|
---|
[11484] | 31 | [Item("MultiEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")]
|
---|
[17097] | 32 | [StorableType("359E2173-4D0C-40E5-A2F3-E42E59840345")]
|
---|
[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]
|
---|
[17097] | 43 | private MultiEncoding(StorableConstructorFlag _) : base(_) {
|
---|
[11587] | 44 | encodings = new List<IEncoding>();
|
---|
| 45 | }
|
---|
| 46 | public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncoding(this, cloner); }
|
---|
[11598] | 47 | private MultiEncoding(MultiEncoding original, Cloner cloner)
|
---|
[11484] | 48 | : base(original, cloner) {
|
---|
[11587] | 49 | encodings = new List<IEncoding>(original.Encodings.Select(cloner.Clone));
|
---|
[11484] | 50 | }
|
---|
| 51 | public MultiEncoding()
|
---|
| 52 | : base("MultiEncoding") {
|
---|
[11587] | 53 | encodings = new List<IEncoding>();
|
---|
| 54 | SolutionCreator = new MultiEncodingCreator();
|
---|
[11484] | 55 |
|
---|
[11587] | 56 | foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>())
|
---|
[11952] | 57 | AddOperator(@operator);
|
---|
[11484] | 58 | }
|
---|
| 59 |
|
---|
[11619] | 60 | public override Individual GetIndividual(IScope scope) {
|
---|
[11598] | 61 | return new MultiEncodingIndividual(this, scope);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[11587] | 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);
|
---|
[11484] | 68 |
|
---|
[11588] | 69 | Parameters.AddRange(encoding.Parameters);
|
---|
| 70 |
|
---|
[11587] | 71 | foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {
|
---|
| 72 | @operator.AddEncoding(encoding);
|
---|
| 73 | }
|
---|
[11892] | 74 | OnEncodingsChanged();
|
---|
[11484] | 75 | return this;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[11587] | 78 | public bool Remove(IEncoding encoding) {
|
---|
| 79 | var success = encodings.Remove(encoding);
|
---|
[11588] | 80 | Parameters.RemoveRange(encoding.Parameters);
|
---|
[11587] | 81 | foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {
|
---|
| 82 | @operator.RemoveEncoding(encoding);
|
---|
| 83 | }
|
---|
[11892] | 84 | OnEncodingsChanged();
|
---|
[11587] | 85 | return success;
|
---|
[11484] | 86 | }
|
---|
| 87 |
|
---|
[11587] | 88 | public override void ConfigureOperators(IEnumerable<IOperator> operators) {
|
---|
[11484] | 89 | }
|
---|
[11892] | 90 |
|
---|
| 91 | public event EventHandler EncodingsChanged;
|
---|
| 92 | private void OnEncodingsChanged() {
|
---|
| 93 | var handler = EncodingsChanged;
|
---|
| 94 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 95 | }
|
---|
[11484] | 96 | }
|
---|
| 97 | }
|
---|