Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2901_StaticSelectionMethods/HeuristicLab.Clients.OKB/3.3/RunCreation/SingleObjectiveOKBSolution.cs @ 17740

Last change on this file since 17740 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25using HeuristicLab.Persistence.Default.Xml;
26using System;
27using System.IO;
28
29namespace HeuristicLab.Clients.OKB.RunCreation {
30  [Item("OKB Solution (single-objective)", "")]
31  [StorableClass]
32  public sealed class SingleObjectiveOKBSolution : OKBSolution {
33    [Storable]
34    private double quality;
35    public double Quality {
36      get { return quality; }
37      set {
38        if (quality == value) return;
39        quality = value;
40        OnQualityChanged();
41      }
42    }
43
44    [StorableConstructor]
45    private SingleObjectiveOKBSolution(bool deserializing) : base(deserializing) { }
46    private SingleObjectiveOKBSolution(SingleObjectiveOKBSolution original, Cloner cloner)
47      : base(original, cloner) {
48      quality = original.quality;
49    }
50    public SingleObjectiveOKBSolution(long problemId) : base(problemId) {
51      quality = double.NaN;
52    }
53    public SingleObjectiveOKBSolution(Solution solution) : base(solution) {
54      var soSolution = solution as SingleObjectiveSolution;
55      if (soSolution == null) throw new ArgumentException("Solution is not of type SingleObjectiveSolution", "solution");
56      Quality = soSolution.Quality;
57    }
58
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new SingleObjectiveOKBSolution(this, cloner);
61    }
62
63    public override void Download(long solutionId) {
64      if (SolutionId == solutionId) return;
65      byte[] solutionData = RunCreationClient.Instance.GetSolutionData(solutionId);
66      using (var stream = new MemoryStream(solutionData)) {
67        Solution = XmlParser.Deserialize<IItem>(stream);
68        SolutionId = solutionId;
69      }
70      var sol = RunCreationClient.Instance.GetSolution(solutionId);
71      ProblemId = sol.ProblemId;
72      var soSol = sol as SingleObjectiveSolution;
73      if (soSol == null) throw new InvalidOperationException(string.Format("Solution with id {0} is not a single-objective solution.", solutionId));
74      Quality = soSol.Quality;
75    }
76
77    public override void DownloadData() {
78      using (var stream = new MemoryStream(RunCreationClient.Instance.GetSolutionData(SolutionId))) {
79        Solution = XmlParser.Deserialize<IItem>(stream);
80      }
81    }
82
83    public override void Upload() {
84      if (SolutionId != -1) throw new InvalidOperationException("Solution exists already.");
85      using (var stream = new MemoryStream()) {
86        if (Solution != null) XmlGenerator.Serialize(Solution, stream);
87        SolutionId = RunCreationClient.Instance.AddSolution(
88          new SingleObjectiveSolution() {
89            ProblemId = ProblemId,
90            Quality = Quality,
91            DataType = Solution != null ? new DataType() {
92              Name = Solution.GetType().Name,
93              TypeName = Solution.GetType().FullName
94            } : null
95          }, stream.ToArray());
96      }
97    }
98
99    public event EventHandler QualityChanged;
100    private void OnQualityChanged() {
101      var handler = QualityChanged;
102      if  (handler != null) handler(this, EventArgs.Empty);
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.