1 | #region License Information
|
---|
2 |
|
---|
3 | /* HeuristicLab
|
---|
4 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
5 | *
|
---|
6 | * This file is part of HeuristicLab.
|
---|
7 | *
|
---|
8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU General Public License as published by
|
---|
10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
11 | * (at your option) any later version.
|
---|
12 | *
|
---|
13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU General Public License
|
---|
19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #endregion
|
---|
23 |
|
---|
24 | using System;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Optimization {
|
---|
29 | public static class ScopeUtil {
|
---|
30 | private const string EvaluationResultName = "EvaluationResult";
|
---|
31 |
|
---|
32 | // TODO: Create SolutionContexts that derive from IScope to have a unified datastructure (e.g. #1614)
|
---|
33 | public static TEncodedSolution CopyEncodedSolutionToScope<TEncodedSolution>(IScope scope, IEncoding<TEncodedSolution> encoding, TEncodedSolution solution)
|
---|
34 | where TEncodedSolution : class, IEncodedSolution {
|
---|
35 | return CopyEncodedSolutionToScope(scope, encoding.Name, solution);
|
---|
36 | }
|
---|
37 |
|
---|
38 | public static TEncodedSolution CopyEncodedSolutionToScope<TEncodedSolution>(IScope scope, string name, TEncodedSolution solution)
|
---|
39 | where TEncodedSolution : class, IEncodedSolution {
|
---|
40 | var copy = (TEncodedSolution)solution.Clone();
|
---|
41 | if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, copy));
|
---|
42 | else scope.Variables[name].Value = copy;
|
---|
43 | return copy;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public static TEncodedSolution GetEncodedSolution<TEncodedSolution>(IScope scope, IEncoding<TEncodedSolution> encoding)
|
---|
47 | where TEncodedSolution : class, IEncodedSolution {
|
---|
48 | var name = encoding.Name;
|
---|
49 | if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name));
|
---|
50 | var value = scope.Variables[name].Value as TEncodedSolution;
|
---|
51 | if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null or not of type {1}.", name, typeof(TEncodedSolution).GetPrettyName()));
|
---|
52 | return value;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public static IEncodedSolution GetEncodedSolution(IScope scope, IEncoding encoding) {
|
---|
56 | return GetEncodedSolution(scope, encoding.Name);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static IEncodedSolution GetEncodedSolution(IScope scope, string name) {
|
---|
60 | IVariable variable;
|
---|
61 | if (!scope.Variables.TryGetValue(name, out variable)) throw new ArgumentException(string.Format("{0} cannot be found in the provided scope.", name));
|
---|
62 | var solution = variable.Value as IEncodedSolution;
|
---|
63 | if (solution == null) throw new InvalidOperationException(string.Format("{0} is null or not of type ISolution.", name));
|
---|
64 | return solution;
|
---|
65 | }
|
---|
66 |
|
---|
67 | public static ISingleObjectiveSolutionContext<TEncodedSolution> CreateSolutionContext<TEncodedSolution>(IScope scope, IEncoding<TEncodedSolution> encoding)
|
---|
68 | where TEncodedSolution : class, IEncodedSolution {
|
---|
69 | var solution = GetEncodedSolution(scope, encoding);
|
---|
70 | var context = new SingleObjectiveSolutionContext<TEncodedSolution>(solution);
|
---|
71 | foreach (var variable in scope.Variables) {
|
---|
72 | if (variable.Name != encoding.Name)
|
---|
73 | context.SetAdditionalData(variable.Name, variable.Value);
|
---|
74 | }
|
---|
75 | if (scope.Variables.TryGetValue(EvaluationResultName, out var variable2)) {
|
---|
76 | context.EvaluationResult = (ISingleObjectiveEvaluationResult)variable2.Value;
|
---|
77 | }
|
---|
78 | return context;
|
---|
79 | }
|
---|
80 |
|
---|
81 | public static void CopyToScope<TEncodedSolution>(IScope scope, ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext)
|
---|
82 | where TEncodedSolution : class, IEncodedSolution {
|
---|
83 | foreach (var item in solutionContext.AdditionalData) {
|
---|
84 | if (item.Value is IItem i) {
|
---|
85 | if (!scope.Variables.TryGetValue(item.Key, out var variable))
|
---|
86 | scope.Variables.Add(new Variable(item.Key, i));
|
---|
87 | else variable.Value = i;
|
---|
88 | }
|
---|
89 | }
|
---|
90 | if (!scope.Variables.TryGetValue(EvaluationResultName, out var variable2)) {
|
---|
91 | scope.Variables.Add(new Variable(EvaluationResultName, solutionContext.EvaluationResult));
|
---|
92 | } else variable2.Value = solutionContext.EvaluationResult;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|