source: branches/ProgrammableProblem/HeuristicLab.Optimization/3.3/BasicProblems/Encoding.cs @ 11949

Last change on this file since 11949 was 11949, checked in by mkommend, 8 years ago

#2174: Distributed files in programmable problem branch to the correct plugins.

File size: 4.4 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.Optimization {
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
41    [Storable(Name = "Operators")]
42    private IEnumerable<IOperator> StorableOperators {
43      get { return encodingOperators; }
44      set { encodingOperators = new HashSet<IOperator>(value, new TypeEqualityComparer<IOperator>()); ; }
45    }
46
47    public IEnumerable<IOperator> Operators {
48      get { return encodingOperators; }
49      set {
50        if (!value.OfType<T>().Any())
51          throw new ArgumentException("The provided operators contain no suitable solution creator");
52        encodingOperators = new HashSet<IOperator>(value, new TypeEqualityComparer<IOperator>());
53
54        T newSolutionCreator = (T)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType()) ??
55                               encodingOperators.OfType<T>().First();
56        SolutionCreator = newSolutionCreator;
57        OnOperatorsChanged();
58      }
59    }
60
61    ISolutionCreator IEncoding.SolutionCreator {
62      get { return SolutionCreator; }
63      set {
64        if (!(value is T)) throw new ArgumentException(string.Format("Cannot assign the solution creator {0} to the encoding {1}.", value.GetType().GetPrettyName(), GetType().GetPrettyName()));
65        SolutionCreator = (T)value;
66      }
67    }
68    private T solutionCreator;
69    public T SolutionCreator {
70      get {
71        return solutionCreator;
72      }
73      set {
74        if (value == null) throw new ArgumentNullException("SolutionCreator must not be null.");
75        if (solutionCreator == value) return;
76        encodingOperators.Remove(solutionCreator);
77        encodingOperators.Add(value);
78        solutionCreator = value;
79        OnSolutionCreatorChanged();
80      }
81    }
82
83    [StorableConstructor]
84    protected Encoding(bool deserializing) : base(deserializing) { }
85    protected Encoding(Encoding<T> original, Cloner cloner)
86      : base(original, cloner) {
87      encodingOperators = new HashSet<IOperator>(original.Operators.Select(cloner.Clone), new TypeEqualityComparer<IOperator>());
88      solutionCreator = cloner.Clone(original.solutionCreator);
89    }
90    protected Encoding(string name) : base(name) { }
91
92    public virtual Individual GetIndividual(IScope scope) {
93      return new SingleEncodingIndividual(this, scope);
94    }
95
96    public void ConfigureOperator(IOperator @operator) { ConfigureOperators(new[] { @operator }); }
97    public abstract void ConfigureOperators(IEnumerable<IOperator> operators);
98
99    public event EventHandler SolutionCreatorChanged;
100    protected virtual void OnSolutionCreatorChanged() {
101      ConfigureOperator(SolutionCreator);
102      var handler = SolutionCreatorChanged;
103      if (handler != null) handler(this, EventArgs.Empty);
104    }
105
106    public event EventHandler OperatorsChanged;
107    protected virtual void OnOperatorsChanged() {
108      ConfigureOperators(Operators);
109      var handler = OperatorsChanged;
110      if (handler != null) handler(this, EventArgs.Empty);
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.