Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 4.9 KB
RevLine 
[11587]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) 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]22using System;
[11753]23using System.Collections.Generic;
[11587]24using System.Linq;
[16782]25using HEAL.Attic;
[11587]26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30
[11949]31namespace HeuristicLab.Optimization {
[16565]32  [StorableType("43619638-9D00-4951-8138-8CCD0786E784")]
[18153]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
[16782]41    public abstract string OperatorPrefix { get; }
42
[11587]43    [StorableConstructor]
[16782]44    protected MultiEncodingOperator(StorableConstructorFlag _) : base(_) { }
[11753]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    }
[11587]51    protected MultiEncodingOperator() : base() { }
52
[11753]53    [StorableHook(HookType.AfterDeserialization)]
54    private void AfterDeserialization() {
[16782]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
[11753]68        encoding.OperatorsChanged += Encoding_OperatorsChanged;
[16782]69      }
[11753]70    }
[11587]71
72    public override IOperation Apply() {
[18153]73      var operators = Parameters.OrderBy(p => p.Name).Select(p => p.ActualValue).OfType<IOperator>();
74      var operations = operators.Select(op => ExecutionContext.CreateChildOperation(op));
[11587]75      return new OperationCollection(operations);
76    }
77
78    public virtual void AddEncoding(IEncoding encoding) {
[16782]79      if (Parameters.ContainsKey(OperatorPrefix + "." + encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name));
[11753]80
81      encodings.Add(encoding);
82      encoding.OperatorsChanged += Encoding_OperatorsChanged;
83
[16782]84      var param = new ConstrainedValueParameter<T>(OperatorPrefix + "." + encoding.Name, new ItemSet<T>(encoding.Operators.OfType<T>()));
[11593]85      param.Value = param.ValidValues.First();
[11587]86      Parameters.Add(param);
87    }
88
89    public virtual bool RemoveEncoding(IEncoding encoding) {
[11753]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;
[16782]92      return Parameters.Remove(OperatorPrefix + "." + encoding.Name);
[11587]93    }
94
[11737]95    protected IConstrainedValueParameter<T> GetParameter(IEncoding encoding) {
[16782]96      if (!Parameters.ContainsKey(OperatorPrefix + "." + encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
[11587]97
[16782]98      return (IConstrainedValueParameter<T>)Parameters[OperatorPrefix + "." + encoding.Name];
[11587]99    }
100
[11753]101    private void Encoding_OperatorsChanged(object sender, EventArgs e) {
102      var encoding = (IEncoding)sender;
103      var param = GetParameter(encoding);
[11587]104
[11753]105      var oldParameterValue = param.Value;
106      param.ValidValues.Clear();
107      foreach (var op in encoding.Operators.OfType<T>())
108        param.ValidValues.Add(op);
[11587]109
[11753]110      var newValue = param.ValidValues.FirstOrDefault(op => op.GetType() == oldParameterValue.GetType());
111      if (newValue == null) newValue = param.ValidValues.First();
112      param.Value = newValue;
113    }
[11587]114  }
115}
Note: See TracBrowser for help on using the repository browser.