[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 |
|
---|
| 22 | using System;
|
---|
[11543] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[16751] | 25 | using HEAL.Attic;
|
---|
[11484] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[11952] | 28 | using HeuristicLab.Parameters;
|
---|
[11484] | 29 |
|
---|
[11949] | 30 | namespace HeuristicLab.Optimization {
|
---|
[11484] | 31 | [Item("Encoding", "Base class for describing different encodings.")]
|
---|
[16723] | 32 | [StorableType("395B1372-FA54-4649-9EBE-5402A0AA9494")]
|
---|
[16751] | 33 | public abstract class Encoding<TEncodedSolution> : ParameterizedNamedItem, IEncoding<TEncodedSolution>
|
---|
| 34 | where TEncodedSolution : class, IEncodedSolution {
|
---|
| 35 | public sealed override 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 {
|
---|
[16751] | 50 | if (!value.OfType<ISolutionCreator<TEncodedSolution>>().Any())
|
---|
[11753] | 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 |
|
---|
[16751] | 55 | ISolutionCreator<TEncodedSolution> newSolutionCreator = (ISolutionCreator<TEncodedSolution>)encodingOperators.FirstOrDefault(o => o.GetType() == SolutionCreator.GetType()) ??
|
---|
| 56 | encodingOperators.OfType<ISolutionCreator<TEncodedSolution>>().First();
|
---|
[11753] | 57 | SolutionCreator = newSolutionCreator;
|
---|
| 58 | OnOperatorsChanged();
|
---|
| 59 | }
|
---|
[11550] | 60 | }
|
---|
| 61 |
|
---|
[13469] | 62 | public IValueParameter SolutionCreatorParameter {
|
---|
| 63 | get { return (IValueParameter)Parameters[Name + ".SolutionCreator"]; }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[13351] | 66 | ISolutionCreator IEncoding.SolutionCreator {
|
---|
[13469] | 67 | get { return SolutionCreator; }
|
---|
[13351] | 68 | }
|
---|
[16751] | 69 | public ISolutionCreator<TEncodedSolution> SolutionCreator {
|
---|
| 70 | get { return (ISolutionCreator<TEncodedSolution>)SolutionCreatorParameter.Value; }
|
---|
[11559] | 71 | set {
|
---|
| 72 | if (value == null) throw new ArgumentNullException("SolutionCreator must not be null.");
|
---|
[13469] | 73 | encodingOperators.Remove(SolutionCreator);
|
---|
[11587] | 74 | encodingOperators.Add(value);
|
---|
[13469] | 75 | SolutionCreatorParameter.Value = value;
|
---|
[11559] | 76 | OnSolutionCreatorChanged();
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[13469] | 80 |
|
---|
[11553] | 81 | [StorableConstructor]
|
---|
[16723] | 82 | protected Encoding(StorableConstructorFlag _) : base(_) { }
|
---|
[13469] | 83 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 84 | private void AfterDeserialization() {
|
---|
| 85 | RegisterEventHandlers();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[16751] | 88 | protected Encoding(Encoding<TEncodedSolution> original, Cloner cloner)
|
---|
[11559] | 89 | : base(original, cloner) {
|
---|
[11952] | 90 | encodingOperators = cloner.Clone(original.encodingOperators);
|
---|
[13469] | 91 |
|
---|
| 92 | RegisterEventHandlers();
|
---|
[11559] | 93 | }
|
---|
[13469] | 94 |
|
---|
[11952] | 95 | protected Encoding(string name)
|
---|
| 96 | : base(name) {
|
---|
[16751] | 97 | Parameters.Add(new ValueParameter<ISolutionCreator<TEncodedSolution>>(name + ".SolutionCreator", "The operator to create a solution."));
|
---|
[16946] | 98 | Parameters.Add(new FixedValueParameter<ReadOnlyItemSet<IOperator>>(name + ".Operators", "The operators that the encoding specifies.", encodingOperators.AsReadOnly()) {
|
---|
| 99 | GetsCollected = false
|
---|
| 100 | });
|
---|
[13469] | 101 |
|
---|
| 102 | RegisterEventHandlers();
|
---|
[11952] | 103 | }
|
---|
[11553] | 104 |
|
---|
[13469] | 105 | private void RegisterEventHandlers() {
|
---|
| 106 | SolutionCreatorParameter.ValueChanged += (o, e) => OnSolutionCreatorChanged();
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[11952] | 109 | protected bool AddOperator(IOperator @operator) {
|
---|
| 110 | return encodingOperators.Add(@operator);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | protected bool RemoveOperator(IOperator @operator) {
|
---|
| 114 | return encodingOperators.Remove(@operator);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[13396] | 117 | public void ConfigureOperator(IItem @operator) { ConfigureOperators(new[] { @operator }); }
|
---|
| 118 | public abstract void ConfigureOperators(IEnumerable<IItem> operators);
|
---|
[11559] | 119 |
|
---|
[11587] | 120 | public event EventHandler SolutionCreatorChanged;
|
---|
[11559] | 121 | protected virtual void OnSolutionCreatorChanged() {
|
---|
| 122 | ConfigureOperator(SolutionCreator);
|
---|
[11587] | 123 | var handler = SolutionCreatorChanged;
|
---|
| 124 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[11559] | 125 | }
|
---|
[11753] | 126 |
|
---|
| 127 | public event EventHandler OperatorsChanged;
|
---|
| 128 | protected virtual void OnOperatorsChanged() {
|
---|
| 129 | ConfigureOperators(Operators);
|
---|
| 130 | var handler = OperatorsChanged;
|
---|
| 131 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 132 | }
|
---|
[11484] | 133 | }
|
---|
| 134 | }
|
---|