[11587] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11587] | 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
|
---|
[11961] | 21 |
|
---|
[11587] | 22 | using System;
|
---|
[11753] | 23 | using System.Collections.Generic;
|
---|
[11587] | 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
[11949] | 31 | namespace HeuristicLab.Optimization {
|
---|
[11767] | 32 | [StorableClass]
|
---|
[11587] | 33 | public abstract class MultiEncodingOperator<T> : Operator, IMultiEncodingOperator where T : class,IOperator {
|
---|
[11753] | 34 | private List<IEncoding> encodings = new List<IEncoding>();
|
---|
| 35 | [Storable(Name = "Encodings")]
|
---|
| 36 | private IEnumerable<IEncoding> StorableEncodings {
|
---|
| 37 | get { return encodings; }
|
---|
| 38 | set { encodings = new List<IEncoding>(value); }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[11587] | 41 | [StorableConstructor]
|
---|
[11753] | 42 | protected MultiEncodingOperator(bool deserializing)
|
---|
| 43 | : base(deserializing) {
|
---|
| 44 | }
|
---|
[11587] | 45 |
|
---|
[11753] | 46 | protected MultiEncodingOperator(MultiEncodingOperator<T> original, Cloner cloner)
|
---|
| 47 | : base(original, cloner) {
|
---|
| 48 | encodings = new List<IEncoding>(original.encodings.Select(cloner.Clone));
|
---|
| 49 | foreach (var encoding in encodings)
|
---|
| 50 | encoding.OperatorsChanged += Encoding_OperatorsChanged;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[11587] | 53 | protected MultiEncodingOperator() : base() { }
|
---|
| 54 |
|
---|
[11753] | 55 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 56 | private void AfterDeserialization() {
|
---|
| 57 | foreach (var encoding in encodings)
|
---|
| 58 | encoding.OperatorsChanged += Encoding_OperatorsChanged;
|
---|
| 59 | }
|
---|
[11587] | 60 |
|
---|
[11753] | 61 |
|
---|
[11587] | 62 | public override IOperation Apply() {
|
---|
| 63 | var operations = Parameters.Select(p => p.ActualValue).OfType<IOperator>().Select(op => ExecutionContext.CreateOperation(op));
|
---|
| 64 | return new OperationCollection(operations);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public virtual void AddEncoding(IEncoding encoding) {
|
---|
| 68 | if (Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name));
|
---|
[11753] | 69 |
|
---|
| 70 | encodings.Add(encoding);
|
---|
| 71 | encoding.OperatorsChanged += Encoding_OperatorsChanged;
|
---|
| 72 |
|
---|
[11587] | 73 | var param = new ConstrainedValueParameter<T>(encoding.Name, new ItemSet<T>(encoding.Operators.OfType<T>()));
|
---|
[11593] | 74 | param.Value = param.ValidValues.First();
|
---|
[11587] | 75 | Parameters.Add(param);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public virtual bool RemoveEncoding(IEncoding encoding) {
|
---|
[11753] | 79 | if (!encodings.Remove(encoding)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
|
---|
| 80 | encoding.OperatorsChanged -= Encoding_OperatorsChanged;
|
---|
[11587] | 81 | return Parameters.Remove(encoding.Name);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[11737] | 84 | protected IConstrainedValueParameter<T> GetParameter(IEncoding encoding) {
|
---|
[11587] | 85 | if (!Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
|
---|
| 86 |
|
---|
| 87 | return (IConstrainedValueParameter<T>)Parameters[encoding.Name];
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[11753] | 90 | private void Encoding_OperatorsChanged(object sender, EventArgs e) {
|
---|
| 91 | var encoding = (IEncoding)sender;
|
---|
| 92 | var param = GetParameter(encoding);
|
---|
[11587] | 93 |
|
---|
[11753] | 94 | var oldParameterValue = param.Value;
|
---|
| 95 | param.ValidValues.Clear();
|
---|
| 96 | foreach (var op in encoding.Operators.OfType<T>())
|
---|
| 97 | param.ValidValues.Add(op);
|
---|
[11587] | 98 |
|
---|
[11753] | 99 | var newValue = param.ValidValues.FirstOrDefault(op => op.GetType() == oldParameterValue.GetType());
|
---|
| 100 | if (newValue == null) newValue = param.ValidValues.First();
|
---|
| 101 | param.Value = newValue;
|
---|
| 102 | }
|
---|
[11587] | 103 | }
|
---|
| 104 | }
|
---|