Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11559 was 11559, checked in by mkommend, 10 years ago

#2174: Adapted IEncoding and Encoding base class.

File size: 3.5 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> : NamedItem, IEncoding
34    where T : class,ISolutionCreator {
35    public override sealed bool CanChangeName {
36      get { return false; }
37    }
38
39    public virtual IEnumerable<IValueParameter> Parameters {
40      get { return Enumerable.Empty<IValueParameter>(); }
41    }
42
43    protected List<IOperator> encodingOperators = new List<IOperator>();
44    [Storable]
45    public IEnumerable<IOperator> Operators {
46      get { return encodingOperators; }
47      protected set { encodingOperators = new List<IOperator>(value); }
48    }
49
50
51    ISolutionCreator IEncoding.DefaultSolutionCreator { get { return DefaultSolutionCreator; } }
52    public virtual T DefaultSolutionCreator {
53      get { return null; }
54    }
55
56
57    ISolutionCreator IEncoding.SolutionCreator {
58      get { return SolutionCreator; }
59      set {
60        if (!(value is T)) throw new ArgumentException("???");
61        SolutionCreator = (T)value;
62      }
63    }
64    private T solutionCreator;
65    public T SolutionCreator {
66      get {
67        if (solutionCreator == null) return DefaultSolutionCreator;
68        return solutionCreator;
69      }
70      set {
71        if (value == null) throw new ArgumentNullException("SolutionCreator must not be null.");
72        if (solutionCreator == value) return;
73        solutionCreator = value;
74        OnSolutionCreatorChanged();
75      }
76    }
77
78    [StorableConstructor]
79    protected Encoding(bool deserializing) : base(deserializing) { }
80
81    protected Encoding(Encoding<T> original, Cloner cloner)
82      : base(original, cloner) {
83      encodingOperators = original.Operators.Select(cloner.Clone).ToList();
84      solutionCreator = cloner.Clone(original.solutionCreator);
85    }
86    protected Encoding(string name) : base(name) { }
87
88
89
90    public void ConfigureOperator(IOperator @operator) {
91      ConfigureOperators(new[] { @operator });
92    }
93    public virtual void ConfigureOperators(IEnumerable<IOperator> operators) {
94
95    }
96
97    protected virtual void OnSolutionCreatorChanged() {
98      ConfigureOperator(SolutionCreator);
99    }
100
101
102    public event EventHandler ParameterConfigurationChanged;
103    protected virtual void OnParameterConfigurationChanged() {
104      var handler = ParameterConfigurationChanged;
105      if (handler != null) handler(this, EventArgs.Empty);
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.