Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/MultiSolution.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: 3.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    private readonly Dictionary<string, ISolution> solutions;
38
39    [StorableConstructor]
40    private CombinedSolution(bool deserializing) : base(deserializing) { }
41
42    private CombinedSolution(CombinedSolution original, Cloner cloner)
43      : base(original, cloner) {
44      Encoding = cloner.Clone(original.Encoding);
45      Scope = cloner.Clone(original.Scope);
46      solutions = original.solutions.ToDictionary(x => x.Key, x => cloner.Clone(x.Value));
47    }
48    public CombinedSolution(IScope scope, MultiEncoding encoding) {
49      Encoding = encoding;
50      Scope = scope;
51      solutions = encoding.Encodings.Select(e => new { Name = e.Name, Solution = ScopeUtil.GetSolution(scope, e) })
52                                    .ToDictionary(x => x.Name, x => x.Solution);
53    }
54
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new CombinedSolution(this, cloner);
57    }
58
59    public ISolution this[string name] {
60      get {
61        ISolution result;
62        if (!solutions.TryGetValue(name, out result)) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
63        return result;
64      }
65      set {
66        if (!solutions.ContainsKey(name)) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
67        solutions[name] = value;
68      }
69    }
70
71    public TEncoding GetEncoding<TEncoding>() where TEncoding : IEncoding {
72      TEncoding encoding;
73      try {
74        encoding = (TEncoding)Encoding.Encodings.SingleOrDefault(e => e is TEncoding);
75      } catch (InvalidOperationException) {
76        throw new InvalidOperationException(string.Format("The solution uses multiple {0} .", typeof(TEncoding).GetPrettyName()));
77      }
78      if (encoding == null) throw new InvalidOperationException(string.Format("The solution does not use a {0}.", typeof(TEncoding).GetPrettyName()));
79      return encoding;
80    }
81
82    public TSolution GetSolution<TSolution>() where TSolution : class, ISolution {
83      TSolution solution;
84      try {
85        solution = (TSolution)solutions.SingleOrDefault(s => s.Value is TSolution).Value;
86      } catch (InvalidOperationException) {
87        throw new InvalidOperationException(string.Format("The solution uses multiple {0} .", typeof(TSolution).GetPrettyName()));
88      }
89      if (solution == null) throw new InvalidOperationException(string.Format("The solution does not use a {0}.", typeof(TSolution).GetPrettyName()));
90      return solution;
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.