source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Operators/MultiEncodingOperator.cs @ 11753

Last change on this file since 11753 was 11753, checked in by mkommend, 5 years ago

#2174: First working version of refactored programmable problem.

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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
21using System;
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.Programmable.Interfaces;
30
31namespace HeuristicLab.Problems.Programmable.Operators {
32  public abstract class MultiEncodingOperator<T> : Operator, IMultiEncodingOperator where T : class,IOperator {
33    private List<IEncoding> encodings = new List<IEncoding>();
34    [Storable(Name = "Encodings")]
35    private IEnumerable<IEncoding> StorableEncodings {
36      get { return encodings; }
37      set { encodings = new List<IEncoding>(value); }
38    }
39
40    [StorableConstructor]
41    protected MultiEncodingOperator(bool deserializing)
42      : base(deserializing) {
43    }
44
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
52    protected MultiEncodingOperator() : base() { }
53
54    [StorableHook(HookType.AfterDeserialization)]
55    private void AfterDeserialization() {
56      foreach (var encoding in encodings)
57        encoding.OperatorsChanged += Encoding_OperatorsChanged;
58    }
59
60
61    public override IOperation Apply() {
62      var operations = Parameters.Select(p => p.ActualValue).OfType<IOperator>().Select(op => ExecutionContext.CreateOperation(op));
63      return new OperationCollection(operations);
64    }
65
66    public virtual void AddEncoding(IEncoding encoding) {
67      if (Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name));
68
69      encodings.Add(encoding);
70      encoding.OperatorsChanged += Encoding_OperatorsChanged;
71
72      var param = new ConstrainedValueParameter<T>(encoding.Name, new ItemSet<T>(encoding.Operators.OfType<T>()));
73      param.Value = param.ValidValues.First();
74      Parameters.Add(param);
75    }
76
77    public virtual bool RemoveEncoding(IEncoding encoding) {
78      if (!encodings.Remove(encoding)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
79      encoding.OperatorsChanged -= Encoding_OperatorsChanged;
80      return Parameters.Remove(encoding.Name);
81    }
82
83    public virtual void ReplaceEncoding(IEncoding oldEncoding, IEncoding newEncoding) {
84      var oldParameterValue = GetParameter(oldEncoding).Value;
85      RemoveEncoding(oldEncoding);
86      AddEncoding(newEncoding);
87
88      var parameter = GetParameter(newEncoding);
89      var newParameterValue = parameter.ValidValues.FirstOrDefault(op => op.GetType() == oldParameterValue.GetType());
90      if (newParameterValue != null) parameter.Value = newParameterValue;
91    }
92
93    protected IConstrainedValueParameter<T> GetParameter(IEncoding encoding) {
94      if (!Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
95
96      return (IConstrainedValueParameter<T>)Parameters[encoding.Name];
97    }
98
99    private void Encoding_OperatorsChanged(object sender, EventArgs e) {
100      var encoding = (IEncoding)sender;
101      var param = GetParameter(encoding);
102
103      var oldParameterValue = param.Value;
104      param.ValidValues.Clear();
105      foreach (var op in encoding.Operators.OfType<T>())
106        param.ValidValues.Add(op);
107
108      var newValue = param.ValidValues.FirstOrDefault(op => op.GetType() == oldParameterValue.GetType());
109      if (newValue == null) newValue = param.ValidValues.First();
110      param.Value = newValue;
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.