Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/ScopeUtil.cs @ 17699

Last change on this file since 17699 was 17699, checked in by abeham, 4 years ago

#2521: Made encodings non-generic classes (the TEncodedSolution type parameter is not actually used), this will make it considerably easier to port the VRP to the new architecture

File size: 4.3 KB
Line 
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
24using System;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27
28namespace 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    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.