[11484] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16728] | 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;
|
---|
[11543] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[11484] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[11952] | 27 | using HeuristicLab.Parameters;
|
---|
[16728] | 28 | using HEAL.Attic;
|
---|
[11484] | 29 |
|
---|
[11949] | 30 | namespace HeuristicLab.Optimization {
|
---|
[11484] | 31 | [Item("Encoding", "Base class for describing different encodings.")]
|
---|
[16728] | 32 | [StorableType("395B1372-FA54-4649-9EBE-5402A0AA9494")]
|
---|
[11588] | 33 | public abstract class Encoding<T> : ParameterizedNamedItem, IEncoding
|
---|
[11559] | 34 | where T : class,ISolutionCreator {
|
---|
[11543] | 35 | public override sealed bool CanChangeName {
|
---|
[11484] | 36 | get { return false; }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[11952] | 39 | private ItemSet<IOperator> encodingOperators = new ItemSet<IOperator>(new TypeEqualityComparer<IOperator>());
|
---|
[11753] | 40 |
|
---|
| 41 | [Storable(Name = "Operators")]
|
---|
| 42 | private IEnumerable<IOperator> StorableOperators {
|
---|
| 43 | get { return encodingOperators; }
|
---|
[11952] | 44 | set { encodingOperators = new ItemSet<IOperator>(value, new TypeEqualityComparer<IOperator>()); ; }
|
---|
[11753] | 45 | }
|
---|
| 46 |
|
---|
[11559] | 47 | public IEnumerable<IOperator> Operators {
|
---|
| 48 | get { return encodingOperators; }
|
---|
[11753] | 49 | set {
|
---|
| 50 | if (!value.OfType<T>().Any())
|
---|
| 51 | throw new ArgumentException("The provided operators contain no suitable solution creator");
|
---|
[11952] | 52 | encodingOperators.Clear();
|
---|
| 53 | foreach (var op in value) encodingOperators.Add(op);
|
---|
[11753] | 54 |
|
---|
[11880] | 55 | T newSolutionCreator = (T)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType()) ??
|
---|
| 56 | encodingOperators.OfType<T>().First();
|
---|
[11753] | 57 | SolutionCreator = newSolutionCreator;
|
---|
| 58 | OnOperatorsChanged();
|
---|
| 59 | }
|
---|
[11550] | 60 | }
|
---|
| 61 |
|
---|
[11559] | 62 | ISolutionCreator IEncoding.SolutionCreator {
|
---|
| 63 | get { return SolutionCreator; }
|
---|
| 64 | set {
|
---|
[11587] | 65 | if (!(value is T)) throw new ArgumentException(string.Format("Cannot assign the solution creator {0} to the encoding {1}.", value.GetType().GetPrettyName(), GetType().GetPrettyName()));
|
---|
[11559] | 66 | SolutionCreator = (T)value;
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
[12981] | 69 | [Storable]
|
---|
[11559] | 70 | private T solutionCreator;
|
---|
| 71 | public T SolutionCreator {
|
---|
| 72 | get {
|
---|
| 73 | return solutionCreator;
|
---|
| 74 | }
|
---|
| 75 | set {
|
---|
| 76 | if (value == null) throw new ArgumentNullException("SolutionCreator must not be null.");
|
---|
| 77 | if (solutionCreator == value) return;
|
---|
[11587] | 78 | encodingOperators.Remove(solutionCreator);
|
---|
| 79 | encodingOperators.Add(value);
|
---|
[11559] | 80 | solutionCreator = value;
|
---|
| 81 | OnSolutionCreatorChanged();
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[11553] | 85 | [StorableConstructor]
|
---|
[16728] | 86 | protected Encoding(StorableConstructorFlag _) : base(_) { }
|
---|
[11559] | 87 | protected Encoding(Encoding<T> original, Cloner cloner)
|
---|
| 88 | : base(original, cloner) {
|
---|
[11952] | 89 | encodingOperators = cloner.Clone(original.encodingOperators);
|
---|
[11559] | 90 | solutionCreator = cloner.Clone(original.solutionCreator);
|
---|
| 91 | }
|
---|
[11952] | 92 | protected Encoding(string name)
|
---|
| 93 | : base(name) {
|
---|
[15490] | 94 | var operatorsParam = new FixedValueParameter<ReadOnlyItemSet<IOperator>>(name + ".Operators", "The operators that the encoding specifies.", encodingOperators.AsReadOnly());
|
---|
| 95 | // ABE: disable showing the operator collection in the run by default as it wastes a lot of space
|
---|
| 96 | operatorsParam.GetsCollected = false;
|
---|
| 97 | Parameters.Add(operatorsParam);
|
---|
[11952] | 98 | }
|
---|
[11553] | 99 |
|
---|
[11619] | 100 | public virtual Individual GetIndividual(IScope scope) {
|
---|
| 101 | return new SingleEncodingIndividual(this, scope);
|
---|
[11598] | 102 | }
|
---|
[11559] | 103 |
|
---|
[11952] | 104 | protected bool AddOperator(IOperator @operator) {
|
---|
| 105 | return encodingOperators.Add(@operator);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | protected bool RemoveOperator(IOperator @operator) {
|
---|
| 109 | return encodingOperators.Remove(@operator);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[11737] | 112 | public void ConfigureOperator(IOperator @operator) { ConfigureOperators(new[] { @operator }); }
|
---|
[11587] | 113 | public abstract void ConfigureOperators(IEnumerable<IOperator> operators);
|
---|
[11559] | 114 |
|
---|
[11587] | 115 | public event EventHandler SolutionCreatorChanged;
|
---|
[11559] | 116 | protected virtual void OnSolutionCreatorChanged() {
|
---|
| 117 | ConfigureOperator(SolutionCreator);
|
---|
[11587] | 118 | var handler = SolutionCreatorChanged;
|
---|
| 119 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[11559] | 120 | }
|
---|
[11753] | 121 |
|
---|
| 122 | public event EventHandler OperatorsChanged;
|
---|
| 123 | protected virtual void OnOperatorsChanged() {
|
---|
| 124 | ConfigureOperators(Operators);
|
---|
| 125 | var handler = OperatorsChanged;
|
---|
| 126 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 127 | }
|
---|
[11484] | 128 | }
|
---|
| 129 | }
|
---|