Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Encoding.cs @ 17571

Last change on this file since 17571 was 17571, checked in by mkommend, 4 years ago

#2521: Changed parameters to use readonly only for GUI manipulation.

File size: 6.0 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<TEncodedSolution> : ParameterizedNamedItem, IEncoding<TEncodedSolution>
34    where TEncodedSolution : class, IEncodedSolution {
35    public sealed override 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<ISolutionCreator<TEncodedSolution>>().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        ISolutionCreator<TEncodedSolution> newSolutionCreator = (ISolutionCreator<TEncodedSolution>)encodingOperators.FirstOrDefault(o => o.GetType() == SolutionCreator.GetType()) ??
56                               encodingOperators.OfType<ISolutionCreator<TEncodedSolution>>().First();
57        SolutionCreator = newSolutionCreator;
58        OnOperatorsChanged();
59      }
60    }
61
62    public IValueParameter SolutionCreatorParameter {
63      get { return (IValueParameter)Parameters[Name + ".SolutionCreator"]; }
64    }
65
66    ISolutionCreator IEncoding.SolutionCreator {
67      get { return SolutionCreator; }
68    }
69    public ISolutionCreator<TEncodedSolution> SolutionCreator {
70      get { return (ISolutionCreator<TEncodedSolution>)SolutionCreatorParameter.Value; }
71      set {
72        if (value == null) throw new ArgumentNullException("SolutionCreator must not be null.");
73        encodingOperators.Remove(SolutionCreator);
74        encodingOperators.Add(value);
75        SolutionCreatorParameter.Value = value;
76        OnSolutionCreatorChanged();
77      }
78    }
79
80
81    [StorableConstructor]
82    protected Encoding(StorableConstructorFlag _) : base(_) { }
83    [StorableHook(HookType.AfterDeserialization)]
84    private void AfterDeserialization() {
85      RegisterEventHandlers();
86    }
87
88    protected Encoding(Encoding<TEncodedSolution> original, Cloner cloner)
89      : base(original, cloner) {
90      encodingOperators = cloner.Clone(original.encodingOperators);
91
92      RegisterEventHandlers();
93    }
94
95    protected Encoding(string name)
96      : base(name) {
97      Parameters.Add(new ValueParameter<ISolutionCreator<TEncodedSolution>>(name + ".SolutionCreator", "The operator to create a solution.") {
98        ReadOnly = true
99      });
100      Parameters.Add(new FixedValueParameter<ReadOnlyItemSet<IOperator>>(name + ".Operators", "The operators that the encoding specifies.", encodingOperators.AsReadOnly()) {
101        GetsCollected = false, ReadOnly = true
102      });
103
104      RegisterEventHandlers();
105    }
106
107    private void RegisterEventHandlers() {
108      SolutionCreatorParameter.ValueChanged += (o, e) => OnSolutionCreatorChanged();
109    }
110
111    protected bool AddOperator(IOperator @operator) {
112      return encodingOperators.Add(@operator);
113    }
114
115    protected bool RemoveOperator(IOperator @operator) {
116      return encodingOperators.Remove(@operator);
117    }
118
119    public void ConfigureOperator(IItem @operator) { ConfigureOperators(new[] { @operator }); }
120    public virtual void ConfigureOperators(IEnumerable<IItem> operators) {
121      ConfigureSingleObjectiveImprovementOperators(operators.OfType<ISingleObjectiveImprovementOperator>());
122      ConfigureSingleObjectivePathRelinker(operators.OfType<ISingleObjectivePathRelinker>());
123    }
124
125    protected virtual void ConfigureSingleObjectiveImprovementOperators(IEnumerable<ISingleObjectiveImprovementOperator> operators) {
126      foreach (var op in operators) {
127        op.SolutionParameter.ActualName = Name;
128        op.SolutionParameter.Hidden = true;
129      }
130    }
131
132    protected virtual void ConfigureSingleObjectivePathRelinker(IEnumerable<ISingleObjectivePathRelinker> operators) {
133      foreach (var op in operators.OfType<ISingleObjectivePathRelinker>()) {
134        op.ParentsParameter.ActualName = Name;
135        op.ParentsParameter.Hidden = true;
136      }
137    }
138
139    public event EventHandler SolutionCreatorChanged;
140    protected virtual void OnSolutionCreatorChanged() {
141      ConfigureOperator(SolutionCreator);
142      var handler = SolutionCreatorChanged;
143      if (handler != null) handler(this, EventArgs.Empty);
144    }
145
146    public event EventHandler OperatorsChanged;
147    protected virtual void OnOperatorsChanged() {
148      ConfigureOperators(Operators);
149      var handler = OperatorsChanged;
150      if (handler != null) handler(this, EventArgs.Empty);
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.