Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Optimization/3.3/BasicProblems/Encoding.cs

Last change on this file was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Parameters;
29
30namespace HeuristicLab.Optimization {
31  [Item("Encoding", "Base class for describing different encodings.")]
32  [StorableType("395B1372-FA54-4649-9EBE-5402A0AA9494")]
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    private ItemSet<IOperator> encodingOperators = new ItemSet<IOperator>(new TypeEqualityComparer<IOperator>());
40
41    [Storable(Name = "Operators")]
42    private IEnumerable<IOperator> StorableOperators {
43      get { return encodingOperators; }
44      set { encodingOperators = new ItemSet<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.Clear();
53        foreach (var op in value) encodingOperators.Add(op);
54
55        T newSolutionCreator = (T)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType()) ??
56                               encodingOperators.OfType<T>().First();
57        SolutionCreator = newSolutionCreator;
58        OnOperatorsChanged();
59      }
60    }
61
62    ISolutionCreator IEncoding.SolutionCreator {
63      get { return SolutionCreator; }
64      set {
65        if (!(value is T)) throw new ArgumentException(string.Format("Cannot assign the solution creator {0} to the encoding {1}.", value.GetType().GetPrettyName(), GetType().GetPrettyName()));
66        SolutionCreator = (T)value;
67      }
68    }
69    [Storable]
70    private T solutionCreator;
71    public T SolutionCreator {
72      get {
73        return solutionCreator;
74      }
75      set {
76        if (value == null) throw new ArgumentNullException("SolutionCreator must not be null.");
77        if (solutionCreator == value) return;
78        encodingOperators.Remove(solutionCreator);
79        encodingOperators.Add(value);
80        solutionCreator = value;
81        OnSolutionCreatorChanged();
82      }
83    }
84
85    [StorableConstructor]
86    protected Encoding(StorableConstructorFlag _) : base(_) { }
87    protected Encoding(Encoding<T> original, Cloner cloner)
88      : base(original, cloner) {
89      encodingOperators = cloner.Clone(original.encodingOperators);
90      solutionCreator = cloner.Clone(original.solutionCreator);
91    }
92    protected Encoding(string name)
93      : base(name) {
94      Parameters.Add(new FixedValueParameter<ReadOnlyItemSet<IOperator>>(name + ".Operators", "The operators that the encoding specifies.", encodingOperators.AsReadOnly()) {
95        GetsCollected = false
96      });
97    }
98
99    public virtual Individual GetIndividual(IScope scope) {
100      return new SingleEncodingIndividual(this, scope);
101    }
102
103    protected bool AddOperator(IOperator @operator) {
104      return encodingOperators.Add(@operator);
105    }
106
107    protected bool RemoveOperator(IOperator @operator) {
108      return encodingOperators.Remove(@operator);
109    }
110
111    public void ConfigureOperator(IOperator @operator) { ConfigureOperators(new[] { @operator }); }
112    public abstract void ConfigureOperators(IEnumerable<IOperator> operators);
113
114    public event EventHandler SolutionCreatorChanged;
115    protected virtual void OnSolutionCreatorChanged() {
116      ConfigureOperator(SolutionCreator);
117      var handler = SolutionCreatorChanged;
118      if (handler != null) handler(this, EventArgs.Empty);
119    }
120
121    public event EventHandler OperatorsChanged;
122    protected virtual void OnOperatorsChanged() {
123      ConfigureOperators(Operators);
124      var handler = OperatorsChanged;
125      if (handler != null) handler(this, EventArgs.Empty);
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.