Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SolutionContext.cs @ 17381

Last change on this file since 17381 was 17381, checked in by mkommend, 4 years ago

#2521: Fixed single-objective evaluation results and scope contexts.

File size: 4.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
22
23using System;
24using System.Collections.Generic;
25using System.Linq;
26using HEAL.Attic;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29
30namespace HeuristicLab.Optimization {
31  [StorableType("72638F28-11DD-440D-B7A2-79E16E0EFB83")]
32  public abstract class SolutionContext<TEncodedSolution> : Item, ISolutionContext
33    where TEncodedSolution : class, IEncodedSolution {
34
35    [Storable]
36    private readonly Dictionary<string, object> data = new Dictionary<string, object>();
37
38    IEncodedSolution ISolutionContext.EncodedSolution { get { return EncodedSolution; } }
39
40    [Storable]
41    public TEncodedSolution EncodedSolution { get; private set; }
42
43    [Storable]
44    public IEvaluationResult EvaluationResult { get; protected set; }
45
46    public bool IsEvaluated => EvaluationResult != null;
47
48    protected SolutionContext(TEncodedSolution encodedSolution) : base() {
49      EncodedSolution = encodedSolution ?? throw new ArgumentNullException("encodedSolution");
50    }
51
52    protected SolutionContext(TEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : this(encodedSolution) {
53      EvaluationResult = evaluationResult ?? throw new ArgumentNullException("evaluationResult");
54    }
55
56    [StorableConstructor]
57    protected SolutionContext(StorableConstructorFlag _) : base(_) { }
58
59    public SolutionContext(SolutionContext<TEncodedSolution> original, Cloner cloner) : base(original, cloner) {
60      EncodedSolution = cloner.Clone(original.EncodedSolution);
61      EvaluationResult = cloner.Clone(original.EvaluationResult);
62
63
64      data = original.data.ToDictionary(entry => entry.Key,
65                                        entry => entry.Value is DeepCloneable ? cloner.Clone((DeepCloneable)entry.Value) : entry.Value);
66    }
67
68    public virtual void SetAdditionalData(string identifier, object o) {
69      data[identifier] = o;
70    }
71    public virtual object GetAdditionalData(string identifier) {
72      return data[identifier];
73    }
74  }
75
76  [StorableType("DF6DA9C9-7EF4-4DC3-9855-6C43BDEDD735")]
77  public class SingleObjectiveSolutionContext<TEncodedSolution> : SolutionContext<TEncodedSolution>, ISingleObjectiveSolutionContext<TEncodedSolution>
78   where TEncodedSolution : class, IEncodedSolution {
79    public new ISingleObjectiveEvaluationResult EvaluationResult { get; set; }
80
81    public SingleObjectiveSolutionContext(TEncodedSolution encodedSolution) : base(encodedSolution) { }
82
83
84    public SingleObjectiveSolutionContext(TEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : base(encodedSolution, evaluationResult) { }
85
86    [StorableConstructor]
87    public SingleObjectiveSolutionContext(StorableConstructorFlag _) : base(_) { }
88
89    public SingleObjectiveSolutionContext(SingleObjectiveSolutionContext<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
90
91
92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new SingleObjectiveSolutionContext<TEncodedSolution>(this, cloner);
94    }
95  }
96
97  [StorableType("929868B3-8994-4D75-B363-CCF9C51410F9")]
98  public class MultiObjectiveSolutionContext<TEncodedSolution> : SolutionContext<TEncodedSolution>, IMultiObjectiveSolutionContext<TEncodedSolution>
99   where TEncodedSolution : class, IEncodedSolution {
100    public new IMultiObjectiveEvaluationResult EvaluationResult { get; set; }
101
102    public MultiObjectiveSolutionContext(TEncodedSolution encodedSolution) : base(encodedSolution) { }
103
104
105    public MultiObjectiveSolutionContext(TEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : base(encodedSolution, evaluationResult) { }
106
107    [StorableConstructor]
108    public MultiObjectiveSolutionContext(StorableConstructorFlag _) : base(_) { }
109
110    public MultiObjectiveSolutionContext(MultiObjectiveSolutionContext<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
111
112
113    public override IDeepCloneable Clone(Cloner cloner) {
114      return new MultiObjectiveSolutionContext<TEncodedSolution>(this, cloner);
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.