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 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 IEncodedSolution GetEncodedSolution(IScope scope, IEncoding encoding) {
|
---|
47 | var name = encoding.Name;
|
---|
48 | if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name));
|
---|
49 | if (scope.Variables[name].Value is IEncodedSolution value)
|
---|
50 | return value;
|
---|
51 | throw new InvalidOperationException(string.Format("Value of {0} is null or not of type {1}.", name, typeof(IEncodedSolution).GetPrettyName()));
|
---|
52 | }
|
---|
53 |
|
---|
54 | public static IEncodedSolution GetEncodedSolution(IScope scope, string name) {
|
---|
55 | IVariable variable;
|
---|
56 | if (!scope.Variables.TryGetValue(name, out variable)) throw new ArgumentException(string.Format("{0} cannot be found in the provided scope.", name));
|
---|
57 | var solution = variable.Value as IEncodedSolution;
|
---|
58 | if (solution == null) throw new InvalidOperationException(string.Format("{0} is null or not of type ISolution.", name));
|
---|
59 | return solution;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public static ISingleObjectiveSolutionContext<TEncodedSolution> CreateSolutionContext<TEncodedSolution>(IScope scope, IEncoding encoding)
|
---|
63 | where TEncodedSolution : class, IEncodedSolution {
|
---|
64 | var solution = (TEncodedSolution)GetEncodedSolution(scope, encoding);
|
---|
65 | var context = new SingleObjectiveSolutionContext<TEncodedSolution>(solution);
|
---|
66 | foreach (var variable in scope.Variables) {
|
---|
67 | if (variable.Name != encoding.Name)
|
---|
68 | context.SetAdditionalData(variable.Name, variable.Value);
|
---|
69 | }
|
---|
70 | if (scope.Variables.TryGetValue(EvaluationResultName, out var variable2)) {
|
---|
71 | context.EvaluationResult = (ISingleObjectiveEvaluationResult)variable2.Value;
|
---|
72 | }
|
---|
73 | return context;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public static void CopyToScope<TEncodedSolution>(IScope scope, ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext)
|
---|
77 | where TEncodedSolution : class, IEncodedSolution {
|
---|
78 | foreach (var item in solutionContext.AdditionalData) {
|
---|
79 | if (item.Value is IItem i) {
|
---|
80 | if (!scope.Variables.TryGetValue(item.Key, out var variable))
|
---|
81 | scope.Variables.Add(new Variable(item.Key, i));
|
---|
82 | else variable.Value = i;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | if (!scope.Variables.TryGetValue(EvaluationResultName, out var variable2)) {
|
---|
86 | scope.Variables.Add(new Variable(EvaluationResultName, solutionContext.EvaluationResult));
|
---|
87 | } else variable2.Value = solutionContext.EvaluationResult;
|
---|
88 | if (solutionContext.EvaluationResult != null) {
|
---|
89 | foreach (var item in solutionContext.EvaluationResult.AdditionalData) {
|
---|
90 | if (item.Value is IItem i) {
|
---|
91 | if (!scope.Variables.TryGetValue(item.Key, out var variable))
|
---|
92 | scope.Variables.Add(new Variable(item.Key, i));
|
---|
93 | else variable.Value = i;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|