Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingCreator.cs @ 13356

Last change on this file since 13356 was 13356, checked in by abeham, 8 years ago

#2521: working on multi-encoding

File size: 2.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Optimization {
30  [Item("MultiEncodingCreator", "Contains solution creators that together create a multi-encoding.")]
31  [StorableClass]
32  public sealed class MultiEncodingCreator : MultiEncodingOperator<ISolutionCreator>, ISolutionCreator<CombinedSolution> {
33
34    [StorableConstructor]
35    private MultiEncodingCreator(bool deserializing) : base(deserializing) { }
36    private MultiEncodingCreator(MultiEncodingCreator original, Cloner cloner) : base(original, cloner) { }
37    public MultiEncodingCreator() : base() { }
38
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new MultiEncodingCreator(this, cloner);
41    }
42
43    protected override void AddEncoding(IEncoding encoding) {
44      base.AddEncoding(encoding);
45      var parameter = GetParameter(encoding);
46      parameter.Value = encoding.SolutionCreator;
47      encoding.SolutionCreatorChanged += Encoding_SolutionCreatorChanged;
48    }
49
50    protected override bool RemoveEncoding(IEncoding encoding) {
51      var success = base.RemoveEncoding(encoding);
52      encoding.SolutionCreatorChanged -= Encoding_SolutionCreatorChanged;
53      return success;
54    }
55
56    private void Encoding_SolutionCreatorChanged(object sender, EventArgs e) {
57      var encoding = (IEncoding)sender;
58      var parameter = GetParameter(encoding);
59
60      var oldCreator = parameter.ValidValues.Single(creator => creator.GetType() == encoding.SolutionCreator.GetType());
61      parameter.ValidValues.Remove(oldCreator);
62      parameter.ValidValues.Add(encoding.SolutionCreator);
63      parameter.Value = encoding.SolutionCreator;
64    }
65
66
67    public override IOperation InstrumentedApply() {
68      CombinedSolutionParameter.ActualValue = new CombinedSolution(ExecutionContext.Scope, Encoding);
69      return base.Apply();
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.