Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: Fixed bugs in solution context and adapted programmable problem template to include a cancellation token in the evaluate function.

File size: 4.6 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;
25
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      //TODO clone data dictionary
61      EncodedSolution = cloner.Clone(original.EncodedSolution);
62      EvaluationResult = cloner.Clone(original.EvaluationResult);
63    }
64
65    public virtual void SetAdditionalData(string identifier, object o) {
66      data[identifier] = o;
67    }
68    public virtual object GetAdditionalData(string identifier) {
69      return data[identifier];
70    }
71  }
72
73  [StorableType("DF6DA9C9-7EF4-4DC3-9855-6C43BDEDD735")]
74  public class SingleObjectiveSolutionContext<TEncodedSolution> : SolutionContext<TEncodedSolution>, ISingleObjectiveSolutionContext<TEncodedSolution>
75   where TEncodedSolution : class, IEncodedSolution {
76    public new ISingleObjectiveEvaluationResult EvaluationResult { get; set; }
77
78    public SingleObjectiveSolutionContext(TEncodedSolution encodedSolution) : base(encodedSolution) { }
79
80
81    public SingleObjectiveSolutionContext(TEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : base(encodedSolution, evaluationResult) { }
82
83    [StorableConstructor]
84    public SingleObjectiveSolutionContext(StorableConstructorFlag _) : base(_) { }
85
86    public SingleObjectiveSolutionContext(SingleObjectiveSolutionContext<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
87
88
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new SingleObjectiveSolutionContext<TEncodedSolution>(this, cloner);
91    }
92  }
93
94  [StorableType("929868B3-8994-4D75-B363-CCF9C51410F9")]
95  public class MultiObjectiveSolutionContext<TEncodedSolution> : SolutionContext<TEncodedSolution>, IMultiObjectiveSolutionContext<TEncodedSolution>
96   where TEncodedSolution : class, IEncodedSolution {
97    public new IMultiObjectiveEvaluationResult EvaluationResult { get; set; }
98
99    public MultiObjectiveSolutionContext(TEncodedSolution encodedSolution) : base(encodedSolution) { }
100
101
102    public MultiObjectiveSolutionContext(TEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : base(encodedSolution, evaluationResult) { }
103
104    [StorableConstructor]
105    public MultiObjectiveSolutionContext(StorableConstructorFlag _) : base(_) { }
106
107    public MultiObjectiveSolutionContext(MultiObjectiveSolutionContext<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
108
109
110    public override IDeepCloneable Clone(Cloner cloner) {
111      return new MultiObjectiveSolutionContext<TEncodedSolution>(this, cloner);
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.