Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingOperator.cs

Last change on this file was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30
31namespace HeuristicLab.Optimization {
32  [StorableType("43619638-9D00-4951-8138-8CCD0786E784")]
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    public abstract string OperatorPrefix { get; }
42
43    [StorableConstructor]
44    protected MultiEncodingOperator(StorableConstructorFlag _) : base(_) { }
45    protected MultiEncodingOperator(MultiEncodingOperator<T> original, Cloner cloner)
46      : base(original, cloner) {
47      encodings = new List<IEncoding>(original.encodings.Select(cloner.Clone));
48      foreach (var encoding in encodings)
49        encoding.OperatorsChanged += Encoding_OperatorsChanged;
50    }
51    protected MultiEncodingOperator() : base() { }
52
53    [StorableHook(HookType.AfterDeserialization)]
54    private void AfterDeserialization() {
55      foreach (var encoding in encodings) {
56        // BackwardsCompatibility3.3
57        #region Backwards compatible code, remove with 3.4
58        if (Parameters.ContainsKey(encoding.Name) && !Parameters.ContainsKey(OperatorPrefix + "." + encoding.Name)) {
59          var oldParam = (IConstrainedValueParameter<T>)Parameters[encoding.Name];
60          var selected = oldParam.Value;
61          Parameters.Remove(oldParam);
62          var newParam = new ConstrainedValueParameter<T>(OperatorPrefix + "." + encoding.Name, new ItemSet<T>(oldParam.ValidValues));
63          newParam.Value = selected;
64          Parameters.Add(newParam);
65          oldParam.ValidValues.Clear();
66        }
67        #endregion
68        encoding.OperatorsChanged += Encoding_OperatorsChanged;
69      }
70    }
71
72    public override IOperation Apply() {
73      var operations = Parameters.Select(p => p.ActualValue).OfType<IOperator>().Select(op => ExecutionContext.CreateChildOperation(op));
74      return new OperationCollection(operations);
75    }
76
77    public virtual void AddEncoding(IEncoding encoding) {
78      if (Parameters.ContainsKey(OperatorPrefix + "." + encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name));
79
80      encodings.Add(encoding);
81      encoding.OperatorsChanged += Encoding_OperatorsChanged;
82
83      var param = new ConstrainedValueParameter<T>(OperatorPrefix + "." + encoding.Name, new ItemSet<T>(encoding.Operators.OfType<T>()));
84      param.Value = param.ValidValues.First();
85      Parameters.Add(param);
86    }
87
88    public virtual bool RemoveEncoding(IEncoding encoding) {
89      if (!encodings.Remove(encoding)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
90      encoding.OperatorsChanged -= Encoding_OperatorsChanged;
91      return Parameters.Remove(OperatorPrefix + "." + encoding.Name);
92    }
93
94    protected IConstrainedValueParameter<T> GetParameter(IEncoding encoding) {
95      if (!Parameters.ContainsKey(OperatorPrefix + "." + encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
96
97      return (IConstrainedValueParameter<T>)Parameters[OperatorPrefix + "." + encoding.Name];
98    }
99
100    private void Encoding_OperatorsChanged(object sender, EventArgs e) {
101      var encoding = (IEncoding)sender;
102      var param = GetParameter(encoding);
103
104      var oldParameterValue = param.Value;
105      param.ValidValues.Clear();
106      foreach (var op in encoding.Operators.OfType<T>())
107        param.ValidValues.Add(op);
108
109      var newValue = param.ValidValues.FirstOrDefault(op => op.GetType() == oldParameterValue.GetType());
110      if (newValue == null) newValue = param.ValidValues.First();
111      param.Value = newValue;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.