Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingOperator.cs

Last change on this file was 17699, checked in by abeham, 4 years ago

#2521: Made encodings non-generic classes (the TEncodedSolution type parameter is not actually used), this will make it considerably easier to port the VRP to the new architecture

File size: 4.1 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.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29
30namespace HeuristicLab.Optimization {
31  [StorableType("43619638-9D00-4951-8138-8CCD0786E784")]
32  internal abstract class MultiEncodingOperator<T> : InstrumentedOperator, IEncodingOperator, IMultiEncodingOperator where T : class, IOperator {
33    public ILookupParameter<CombinedSolution> SolutionParameter {
34      get { return (ILookupParameter<CombinedSolution>)Parameters["Solution"]; }
35    }
36
37    public ILookupParameter<IEncoding> EncodingParameter {
38      get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
39    }
40
41    public abstract string OperatorPrefix { get; }
42
43    [StorableConstructor]
44    protected MultiEncodingOperator(StorableConstructorFlag _) : base(_) { }
45    protected MultiEncodingOperator(MultiEncodingOperator<T> original, Cloner cloner) : base(original, cloner) { }
46    protected MultiEncodingOperator()
47      : base() {
48      Parameters.Add(new LookupParameter<CombinedSolution>("Solution", "The solution that gets created."));
49      Parameters.Add(new LookupParameter<IEncoding>("Encoding", "The encoding."));
50    }
51
52    public override IOperation InstrumentedApply() {
53      var operations = Parameters.Select(p => p.ActualValue).OfType<IOperator>().Select(op => ExecutionContext.CreateChildOperation(op));
54      return new OperationCollection(operations);
55    }
56
57    public virtual void AddEncoding(IEncoding encoding) {
58      if (Parameters.ContainsKey(OperatorPrefix + "." + encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name));
59
60      encoding.OperatorsChanged += Encoding_OperatorsChanged;
61
62      var param = new ConstrainedValueParameter<T>(OperatorPrefix + "." + encoding.Name, new ItemSet<T>(encoding.Operators.OfType<T>()));
63      param.Value = param.ValidValues.First();
64      Parameters.Add(param);
65    }
66
67    public virtual bool RemoveEncoding(IEncoding encoding) {
68      if (!Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
69      encoding.OperatorsChanged -= Encoding_OperatorsChanged;
70      return Parameters.Remove(OperatorPrefix + "." + encoding.Name);
71    }
72
73    protected IConstrainedValueParameter<T> GetParameter(IEncoding encoding) {
74      if (!Parameters.ContainsKey(OperatorPrefix + "." + encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
75
76      return (IConstrainedValueParameter<T>)Parameters[OperatorPrefix + "." + encoding.Name];
77    }
78
79    private void Encoding_OperatorsChanged(object sender, EventArgs e) {
80      var enc = (IEncoding)sender;
81      var param = GetParameter(enc);
82
83      var oldParameterValue = param.Value;
84      param.ValidValues.Clear();
85      foreach (var op in enc.Operators.OfType<T>())
86        param.ValidValues.Add(op);
87
88      var newValue = param.ValidValues.FirstOrDefault(op => op.GetType() == oldParameterValue.GetType());
89      if (newValue == null) newValue = param.ValidValues.First();
90      param.Value = newValue;
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.