Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Corrected compilation errors in some projects.

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