[11484] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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;
|
---|
[11484] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
[11949] | 30 | namespace HeuristicLab.Optimization {
|
---|
[11484] | 31 | [Item("Encoding", "Base class for describing different encodings.")]
|
---|
| 32 | [StorableClass]
|
---|
[13336] | 33 | public abstract class Encoding<TSolution> : ParameterizedNamedItem, IEncoding<TSolution>
|
---|
| 34 | where TSolution : class,ISolution {
|
---|
[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 {
|
---|
[13336] | 50 | if (!value.OfType<ISolutionCreator<TSolution>>().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 |
|
---|
[13336] | 55 | ISolutionCreator<TSolution> newSolutionCreator = (ISolutionCreator<TSolution>)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType()) ??
|
---|
| 56 | encodingOperators.OfType<ISolutionCreator<TSolution>>().First();
|
---|
[11753] | 57 | SolutionCreator = newSolutionCreator;
|
---|
| 58 | OnOperatorsChanged();
|
---|
| 59 | }
|
---|
[11550] | 60 | }
|
---|
| 61 |
|
---|
[13351] | 62 | ISolutionCreator IEncoding.SolutionCreator {
|
---|
| 63 | get { return solutionCreator; }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[12981] | 66 | [Storable]
|
---|
[13336] | 67 | private ISolutionCreator<TSolution> solutionCreator;
|
---|
| 68 | public ISolutionCreator<TSolution> SolutionCreator {
|
---|
[11559] | 69 | get {
|
---|
| 70 | return solutionCreator;
|
---|
| 71 | }
|
---|
| 72 | set {
|
---|
| 73 | if (value == null) throw new ArgumentNullException("SolutionCreator must not be null.");
|
---|
| 74 | if (solutionCreator == value) return;
|
---|
[11587] | 75 | encodingOperators.Remove(solutionCreator);
|
---|
| 76 | encodingOperators.Add(value);
|
---|
[11559] | 77 | solutionCreator = value;
|
---|
| 78 | OnSolutionCreatorChanged();
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[11553] | 82 | [StorableConstructor]
|
---|
| 83 | protected Encoding(bool deserializing) : base(deserializing) { }
|
---|
[13336] | 84 | protected Encoding(Encoding<TSolution> original, Cloner cloner)
|
---|
[11559] | 85 | : base(original, cloner) {
|
---|
[11952] | 86 | encodingOperators = cloner.Clone(original.encodingOperators);
|
---|
[11559] | 87 | solutionCreator = cloner.Clone(original.solutionCreator);
|
---|
| 88 | }
|
---|
[11952] | 89 | protected Encoding(string name)
|
---|
| 90 | : base(name) {
|
---|
| 91 | Parameters.Add(new FixedValueParameter<ReadOnlyItemSet<IOperator>>(name + ".Operators", "The operators that the encoding specifies.", encodingOperators.AsReadOnly()));
|
---|
| 92 | }
|
---|
[11553] | 93 |
|
---|
[11952] | 94 | protected bool AddOperator(IOperator @operator) {
|
---|
| 95 | return encodingOperators.Add(@operator);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | protected bool RemoveOperator(IOperator @operator) {
|
---|
| 99 | return encodingOperators.Remove(@operator);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[11737] | 102 | public void ConfigureOperator(IOperator @operator) { ConfigureOperators(new[] { @operator }); }
|
---|
[11587] | 103 | public abstract void ConfigureOperators(IEnumerable<IOperator> operators);
|
---|
[11559] | 104 |
|
---|
[11587] | 105 | public event EventHandler SolutionCreatorChanged;
|
---|
[11559] | 106 | protected virtual void OnSolutionCreatorChanged() {
|
---|
| 107 | ConfigureOperator(SolutionCreator);
|
---|
[11587] | 108 | var handler = SolutionCreatorChanged;
|
---|
| 109 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[11559] | 110 | }
|
---|
[11753] | 111 |
|
---|
| 112 | public event EventHandler OperatorsChanged;
|
---|
| 113 | protected virtual void OnOperatorsChanged() {
|
---|
| 114 | ConfigureOperators(Operators);
|
---|
| 115 | var handler = OperatorsChanged;
|
---|
| 116 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 117 | }
|
---|
[11484] | 118 | }
|
---|
| 119 | }
|
---|