Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/CombinedSolution.cs @ 18086

Last change on this file since 18086 was 17226, checked in by mkommend, 5 years ago

#2521: Merged trunk changes into problem refactoring branch.

File size: 2.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.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27
28namespace HeuristicLab.Optimization {
29  [Item("CombinedSolution", "A solution that consists of other solutions.")]
30  [StorableType("B5ED00CB-E533-4ED6-AB2D-95BF7A654AAD")]
31  public sealed class CombinedSolution : Item, IEncodedSolution {
32
33    private CombinedEncoding Encoding { get; set; }
34    private IScope Scope { get; set; }
35
36    [StorableConstructor]
37    private CombinedSolution(StorableConstructorFlag _) : base(_) { }
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 IEncodedSolution this[string name] {
53      get { return ScopeUtil.GetEncodedSolution(Scope, name); }
54      set { ScopeUtil.CopyEncodedSolutionToScope(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 TEncodedSolution GetEncodedSolution<TEncodedSolution>(string name) where TEncodedSolution : class, IEncodedSolution {
69      return (TEncodedSolution)ScopeUtil.GetEncodedSolution(Scope, name);
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.