Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingOperator.cs @ 18171

Last change on this file since 18171 was 18153, checked in by mkommend, 3 years ago

#3144: Ordered discovered parameters of MultiEncodingOperator before operation creation.

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 operators = Parameters.OrderBy(p => p.Name).Select(p => p.ActualValue).OfType<IOperator>();
74      var operations = operators.Select(op => ExecutionContext.CreateChildOperation(op));
75      return new OperationCollection(operations);
76    }
77
78    public virtual void AddEncoding(IEncoding encoding) {
79      if (Parameters.ContainsKey(OperatorPrefix + "." + encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name));
80
81      encodings.Add(encoding);
82      encoding.OperatorsChanged += Encoding_OperatorsChanged;
83
84      var param = new ConstrainedValueParameter<T>(OperatorPrefix + "." + encoding.Name, new ItemSet<T>(encoding.Operators.OfType<T>()));
85      param.Value = param.ValidValues.First();
86      Parameters.Add(param);
87    }
88
89    public virtual bool RemoveEncoding(IEncoding encoding) {
90      if (!encodings.Remove(encoding)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
91      encoding.OperatorsChanged -= Encoding_OperatorsChanged;
92      return Parameters.Remove(OperatorPrefix + "." + encoding.Name);
93    }
94
95    protected IConstrainedValueParameter<T> GetParameter(IEncoding encoding) {
96      if (!Parameters.ContainsKey(OperatorPrefix + "." + encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
97
98      return (IConstrainedValueParameter<T>)Parameters[OperatorPrefix + "." + encoding.Name];
99    }
100
101    private void Encoding_OperatorsChanged(object sender, EventArgs e) {
102      var encoding = (IEncoding)sender;
103      var param = GetParameter(encoding);
104
105      var oldParameterValue = param.Value;
106      param.ValidValues.Clear();
107      foreach (var op in encoding.Operators.OfType<T>())
108        param.ValidValues.Add(op);
109
110      var newValue = param.ValidValues.FirstOrDefault(op => op.GetType() == oldParameterValue.GetType());
111      if (newValue == null) newValue = param.ValidValues.First();
112      param.Value = newValue;
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.