Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Encoding.cs @ 11588

Last change on this file since 11588 was 11588, checked in by mkommend, 9 years ago

#2174: Adapted encodings to store its specific parameters in the standard parameter collection.

File size: 3.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
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.Programmable {
31  [Item("Encoding", "Base class for describing different encodings.")]
32  [StorableClass]
33  public abstract class Encoding<T> : ParameterizedNamedItem, IEncoding
34    where T : class,ISolutionCreator {
35    public override sealed bool CanChangeName {
36      get { return false; }
37    }
38
39    protected HashSet<IOperator> encodingOperators = new HashSet<IOperator>(new TypeEqualityComparer<IOperator>());
40    [Storable]
41    public IEnumerable<IOperator> Operators {
42      get { return encodingOperators; }
43      private set { encodingOperators = new HashSet<IOperator>(value, new TypeEqualityComparer<IOperator>()); }
44    }
45
46    ISolutionCreator IEncoding.SolutionCreator {
47      get { return SolutionCreator; }
48      set {
49        if (!(value is T)) throw new ArgumentException(string.Format("Cannot assign the solution creator {0} to the encoding {1}.", value.GetType().GetPrettyName(), GetType().GetPrettyName()));
50        SolutionCreator = (T)value;
51      }
52    }
53    private T solutionCreator;
54    public T SolutionCreator {
55      get {
56        return solutionCreator;
57      }
58      set {
59        if (value == null) throw new ArgumentNullException("SolutionCreator must not be null.");
60        if (solutionCreator == value) return;
61        encodingOperators.Remove(solutionCreator);
62        encodingOperators.Add(value);
63        solutionCreator = value;
64        OnSolutionCreatorChanged();
65      }
66    }
67
68    [StorableConstructor]
69    protected Encoding(bool deserializing) : base(deserializing) { }
70
71    protected Encoding(Encoding<T> original, Cloner cloner)
72      : base(original, cloner) {
73      encodingOperators = new HashSet<IOperator>(original.Operators.Select(cloner.Clone));
74      solutionCreator = cloner.Clone(original.solutionCreator);
75    }
76    protected Encoding(string name) : base(name) { }
77
78
79    public abstract void ConfigureOperators(IEnumerable<IOperator> operators);
80    public void ConfigureOperator(IOperator @operator) { ConfigureOperators(new[] { @operator }); }
81
82    public event EventHandler SolutionCreatorChanged;
83    protected virtual void OnSolutionCreatorChanged() {
84      ConfigureOperator(SolutionCreator);
85      var handler = SolutionCreatorChanged;
86      if (handler != null) handler(this, EventArgs.Empty);
87    }
88
89
90    public event EventHandler ParameterConfigurationChanged;
91    protected virtual void OnParameterConfigurationChanged() {
92      var handler = ParameterConfigurationChanged;
93      if (handler != null) handler(this, EventArgs.Empty);
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.