Free cookie consent management tool by TermsFeed Policy Generator

source: branches/EnhancedProgress/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingOperator.cs @ 15405

Last change on this file since 15405 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Optimization {
32  [StorableClass]
33  public abstract class MultiEncodingOperator<T> : Operator, IMultiEncodingOperator where T : class,IOperator {
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
41    [StorableConstructor]
42    protected MultiEncodingOperator(bool deserializing)
43      : base(deserializing) {
44    }
45
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
53    protected MultiEncodingOperator() : base() { }
54
55    [StorableHook(HookType.AfterDeserialization)]
56    private void AfterDeserialization() {
57      foreach (var encoding in encodings)
58        encoding.OperatorsChanged += Encoding_OperatorsChanged;
59    }
60
61
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));
69
70      encodings.Add(encoding);
71      encoding.OperatorsChanged += Encoding_OperatorsChanged;
72
73      var param = new ConstrainedValueParameter<T>(encoding.Name, new ItemSet<T>(encoding.Operators.OfType<T>()));
74      param.Value = param.ValidValues.First();
75      Parameters.Add(param);
76    }
77
78    public virtual bool RemoveEncoding(IEncoding encoding) {
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;
81      return Parameters.Remove(encoding.Name);
82    }
83
84    protected IConstrainedValueParameter<T> GetParameter(IEncoding encoding) {
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
90    private void Encoding_OperatorsChanged(object sender, EventArgs e) {
91      var encoding = (IEncoding)sender;
92      var param = GetParameter(encoding);
93
94      var oldParameterValue = param.Value;
95      param.ValidValues.Clear();
96      foreach (var op in encoding.Operators.OfType<T>())
97        param.ValidValues.Add(op);
98
99      var newValue = param.ValidValues.FirstOrDefault(op => op.GetType() == oldParameterValue.GetType());
100      if (newValue == null) newValue = param.ValidValues.First();
101      param.Value = newValue;
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.