Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17620 was 17577, checked in by abeham, 5 years ago

#2521: remove scope-based contexts, add helper methods to ScopeUtil, adapt operators

File size: 5.1 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    public IEnumerable<KeyValuePair<string, object>> AdditionalData { get { return data; } }
38
39    IEncodedSolution ISolutionContext.EncodedSolution { get { return EncodedSolution; } }
40
41    [Storable]
42    public TEncodedSolution EncodedSolution { get; private set; }
43
44    [Storable]
45    public IEvaluationResult EvaluationResult { get; protected set; }
46
47    public bool IsEvaluated => EvaluationResult != null;
48
49    protected SolutionContext(TEncodedSolution encodedSolution) : base() {
50      EncodedSolution = encodedSolution ?? throw new ArgumentNullException("encodedSolution");
51    }
52
53    protected SolutionContext(TEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : this(encodedSolution) {
54      EvaluationResult = evaluationResult ?? throw new ArgumentNullException("evaluationResult");
55    }
56
57    [StorableConstructor]
58    protected SolutionContext(StorableConstructorFlag _) : base(_) { }
59
60    public SolutionContext(SolutionContext<TEncodedSolution> original, Cloner cloner) : base(original, cloner) {
61      EncodedSolution = cloner.Clone(original.EncodedSolution);
62      EvaluationResult = cloner.Clone(original.EvaluationResult);
63
64
65      data = original.data.ToDictionary(entry => entry.Key,
66                                        entry => entry.Value is DeepCloneable ? cloner.Clone((DeepCloneable)entry.Value) : entry.Value);
67    }
68
69    public virtual void SetAdditionalData(string identifier, object o) {
70      data[identifier] = o;
71    }
72    public virtual object GetAdditionalData(string identifier) {
73      return data[identifier];
74    }
75  }
76
77  [StorableType("DF6DA9C9-7EF4-4DC3-9855-6C43BDEDD735")]
78  public class SingleObjectiveSolutionContext<TEncodedSolution> : SolutionContext<TEncodedSolution>, ISingleObjectiveSolutionContext<TEncodedSolution>
79   where TEncodedSolution : class, IEncodedSolution {
80    public new ISingleObjectiveEvaluationResult EvaluationResult {
81      get { return (ISingleObjectiveEvaluationResult)base.EvaluationResult; }
82      set { base.EvaluationResult = value; }
83    }
84
85    public SingleObjectiveSolutionContext(TEncodedSolution encodedSolution) : base(encodedSolution) { }
86
87
88    public SingleObjectiveSolutionContext(TEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : base(encodedSolution, evaluationResult) { }
89
90    [StorableConstructor]
91    public SingleObjectiveSolutionContext(StorableConstructorFlag _) : base(_) { }
92
93    public SingleObjectiveSolutionContext(SingleObjectiveSolutionContext<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
94
95
96    public override IDeepCloneable Clone(Cloner cloner) {
97      return new SingleObjectiveSolutionContext<TEncodedSolution>(this, cloner);
98    }
99  }
100
101  [StorableType("929868B3-8994-4D75-B363-CCF9C51410F9")]
102  public class MultiObjectiveSolutionContext<TEncodedSolution> : SolutionContext<TEncodedSolution>, IMultiObjectiveSolutionContext<TEncodedSolution>
103   where TEncodedSolution : class, IEncodedSolution {
104    public new IMultiObjectiveEvaluationResult EvaluationResult {
105      get { return (IMultiObjectiveEvaluationResult)base.EvaluationResult; }
106      set { base.EvaluationResult = value; }
107    }
108
109    public MultiObjectiveSolutionContext(TEncodedSolution encodedSolution) : base(encodedSolution) { }
110
111
112    public MultiObjectiveSolutionContext(TEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : base(encodedSolution, evaluationResult) { }
113
114    [StorableConstructor]
115    public MultiObjectiveSolutionContext(StorableConstructorFlag _) : base(_) { }
116
117    public MultiObjectiveSolutionContext(MultiObjectiveSolutionContext<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
118
119
120    public override IDeepCloneable Clone(Cloner cloner) {
121      return new MultiObjectiveSolutionContext<TEncodedSolution>(this, cloner);
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.