source: branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/CombinedSolution.cs @ 13376

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

#2521: working on refactoring

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