Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17709 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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