[11484] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[11587] | 24 | using System.Linq;
|
---|
[11484] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[11587] | 28 | using HeuristicLab.PluginInfrastructure;
|
---|
[11598] | 29 | using HeuristicLab.Problems.Programmable.Encodings;
|
---|
[11587] | 30 | using HeuristicLab.Problems.Programmable.Interfaces;
|
---|
[11484] | 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.Programmable {
|
---|
| 33 | [Item("MultiEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")]
|
---|
| 34 | [StorableClass]
|
---|
[11598] | 35 | public sealed class MultiEncoding : Encoding<MultiEncodingCreator> {
|
---|
[11587] | 36 |
|
---|
| 37 | private readonly List<IEncoding> encodings;
|
---|
[11484] | 38 | [Storable]
|
---|
[11587] | 39 | public IEnumerable<IEncoding> Encodings {
|
---|
| 40 | get { return encodings; }
|
---|
| 41 | private set { encodings.AddRange(value); }
|
---|
| 42 | }
|
---|
[11484] | 43 |
|
---|
[11587] | 44 |
|
---|
[11484] | 45 | [StorableConstructor]
|
---|
[11598] | 46 | private MultiEncoding(bool deserializing)
|
---|
[11587] | 47 | : base(deserializing) {
|
---|
| 48 | encodings = new List<IEncoding>();
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncoding(this, cloner); }
|
---|
[11598] | 52 | private MultiEncoding(MultiEncoding original, Cloner cloner)
|
---|
[11484] | 53 | : base(original, cloner) {
|
---|
[11587] | 54 | encodings = new List<IEncoding>(original.Encodings.Select(cloner.Clone));
|
---|
[11484] | 55 | }
|
---|
| 56 | public MultiEncoding()
|
---|
| 57 | : base("MultiEncoding") {
|
---|
[11587] | 58 | encodings = new List<IEncoding>();
|
---|
| 59 | SolutionCreator = new MultiEncodingCreator();
|
---|
[11484] | 60 |
|
---|
[11587] | 61 | foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>())
|
---|
| 62 | encodingOperators.Add(@operator);
|
---|
[11484] | 63 | }
|
---|
| 64 |
|
---|
[11598] | 65 | public override Individual CreateIndividual(IScope scope) {
|
---|
| 66 | return new MultiEncodingIndividual(this, scope);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[11587] | 69 | public MultiEncoding Add(IEncoding encoding) {
|
---|
| 70 | if (encoding is MultiEncoding) throw new InvalidOperationException("Nesting of MultiEncodings is not supported.");
|
---|
| 71 | if (Encodings.Any(e => e.Name == encoding.Name)) throw new ArgumentException("Encoding name must be unique", "encoding.Name");
|
---|
| 72 | encodings.Add(encoding);
|
---|
[11484] | 73 |
|
---|
[11588] | 74 | Parameters.AddRange(encoding.Parameters);
|
---|
| 75 |
|
---|
[11587] | 76 | foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {
|
---|
| 77 | @operator.AddEncoding(encoding);
|
---|
| 78 | }
|
---|
[11484] | 79 | return this;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[11587] | 82 | public bool Remove(IEncoding encoding) {
|
---|
| 83 | var success = encodings.Remove(encoding);
|
---|
[11588] | 84 | Parameters.RemoveRange(encoding.Parameters);
|
---|
[11587] | 85 | foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {
|
---|
| 86 | @operator.RemoveEncoding(encoding);
|
---|
| 87 | }
|
---|
| 88 | return success;
|
---|
[11484] | 89 | }
|
---|
| 90 |
|
---|
[11587] | 91 | public override void ConfigureOperators(IEnumerable<IOperator> operators) {
|
---|
[11484] | 92 |
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|