source: branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/MultiSolution.cs @ 13359

Last change on this file since 13359 was 13359, checked in by abeham, 7 years ago

#2521: worked on multi-encoding (it works again). Some issues are still present:

  • The programmable template needs to be slightly updated for multi-encoding
  • The encoding is recreated every time it is compiled making it impossible to configure operators
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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Optimization {
30  [Item("CombinedSolution", "A solution that consists of other solutions.")]
31  [StorableClass]
32  public sealed class CombinedSolution : Item, ISolution {
33
34    private MultiEncoding Encoding { get; set; }
35    protected IScope Scope { get; private set; }
36
37    [StorableConstructor]
38    private CombinedSolution(bool deserializing) : base(deserializing) { }
39
40    private CombinedSolution(CombinedSolution original, Cloner cloner)
41      : base(original, cloner) {
42      Encoding = cloner.Clone(original.Encoding);
43      Scope = cloner.Clone(original.Scope);
44    }
45    public CombinedSolution(IScope scope, MultiEncoding encoding) {
46      Encoding = encoding;
47      Scope = scope;
48    }
49
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new CombinedSolution(this, cloner);
52    }
53
54    public ISolution this[string name] {
55      get { return ScopeUtil.GetSolution(Scope, name); }
56      set { ScopeUtil.CopySolutionToScope(Scope, name, value); }
57    }
58
59    public TEncoding GetEncoding<TEncoding>() where TEncoding : IEncoding {
60      TEncoding encoding;
61      try {
62        encoding = (TEncoding)Encoding.Encodings.SingleOrDefault(e => e is TEncoding);
63      } catch (InvalidOperationException) {
64        throw new InvalidOperationException(string.Format("The solution uses multiple {0} .", typeof(TEncoding).GetPrettyName()));
65      }
66      if (encoding == null) throw new InvalidOperationException(string.Format("The solution does not use a {0}.", typeof(TEncoding).GetPrettyName()));
67      return encoding;
68    }
69
70    public TSolution GetSolution<TSolution>(string name) where TSolution : class, ISolution {
71      return (TSolution)ScopeUtil.GetSolution(Scope, name);
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.