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

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

#2174: Minor code cleanup in encodings.

File size: 2.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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.Programmable.Interfaces;
29
30namespace HeuristicLab.Problems.Programmable.Operators {
31  public abstract class MultiEncodingOperator<T> : Operator, IMultiEncodingOperator where T : class,IOperator {
32    [StorableConstructor]
33    protected MultiEncodingOperator(bool deserializing) : base(deserializing) { }
34    protected MultiEncodingOperator(MultiEncodingOperator<T> original, Cloner cloner) : base(original, cloner) { }
35
36    protected MultiEncodingOperator() : base() { }
37
38
39    public override IOperation Apply() {
40      var operations = Parameters.Select(p => p.ActualValue).OfType<IOperator>().Select(op => ExecutionContext.CreateOperation(op));
41      return new OperationCollection(operations);
42    }
43
44    public virtual void AddEncoding(IEncoding encoding) {
45      if (Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name));
46      var param = new ConstrainedValueParameter<T>(encoding.Name, new ItemSet<T>(encoding.Operators.OfType<T>()));
47      param.Value = param.ValidValues.First();
48      Parameters.Add(param);
49    }
50
51    public virtual bool RemoveEncoding(IEncoding encoding) {
52      return Parameters.Remove(encoding.Name);
53    }
54
55    protected IConstrainedValueParameter<T> GetParameter(IEncoding encoding) {
56      if (!Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
57
58      return (IConstrainedValueParameter<T>)Parameters[encoding.Name];
59    }
60
61
62
63  }
64}
Note: See TracBrowser for help on using the repository browser.