Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestSolution/BestVRPSolutionAnalyzer.cs @ 17709

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

#2521: working on VRP

File size: 3.8 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
22using System.Linq;
23using HEAL.Attic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
30
31namespace HeuristicLab.Problems.VehicleRouting {
32  /// <summary>
33  /// An operator for analyzing the best solution of Vehicle Routing Problems.
34  /// </summary>
35  [Item("BestVRPSolutionAnalyzer", "An operator for analyzing the best solution of Vehicle Routing Problems.")]
36  [StorableType("3e1bb409-0b8f-4324-826c-2190aa5fb2b6")]
37  public sealed class BestVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
38
39    [Storable] private IScopeTreeLookupParameter<VRPEvaluation> evaluationParameter;
40    public IScopeTreeLookupParameter<VRPEvaluation> EvaluationParameter => evaluationParameter;
41
42    [Storable] private ILookupParameter<VRPSolution> bestSolutionParameter;
43    public ILookupParameter<VRPSolution> BestSolutionParameter => bestSolutionParameter;
44
45    [Storable] private IResultParameter<VRPEvaluation> bestEvaluationParameter;
46    public IResultParameter<VRPEvaluation> BestSolutionEvaluationParameter => bestEvaluationParameter;
47
48    public bool EnabledByDefault {
49      get { return true; }
50    }
51
52    [StorableConstructor]
53    private BestVRPSolutionAnalyzer(StorableConstructorFlag _) : base(_) { }
54    private BestVRPSolutionAnalyzer(BestVRPSolutionAnalyzer original, Cloner cloner)
55      : base(original, cloner) {
56      evaluationParameter = cloner.Clone(original.evaluationParameter);
57      bestSolutionParameter = cloner.Clone(original.bestSolutionParameter);
58      bestEvaluationParameter = cloner.Clone(original.bestEvaluationParameter);
59    }
60    public BestVRPSolutionAnalyzer()
61      : base() {
62      Parameters.Add(evaluationParameter = new ScopeTreeLookupParameter<VRPEvaluation>("EvaluationResult", "The qualities of the VRP solutions which should be analyzed."));
63      Parameters.Add(bestSolutionParameter = new LookupParameter<VRPSolution>("BestSolution", "The best-so-far solution."));
64      Parameters.Add(bestEvaluationParameter = new ResultParameter<VRPEvaluation>("Best VRP Evaluation", "The best VRP evaluation.", "Results"));
65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new BestVRPSolutionAnalyzer(this, cloner);
69    }
70
71    public override IOperation Apply() {
72      var evaluations = EvaluationParameter.ActualValue;
73
74      int i = evaluations.Select((x, index) => new { index, Eval = x }).OrderBy(x => x.Eval.Quality).First().index;
75
76      var bestEvaluation = BestSolutionEvaluationParameter.ActualValue;
77     
78      var bestSolution = BestSolutionParameter.ActualValue;
79      if (bestSolution == null || evaluations[i].Quality <= bestSolution.Quality.Value) {
80        BestSolutionEvaluationParameter.ActualValue = (VRPEvaluation)evaluations[i].Clone();
81      }
82
83      return base.Apply();
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.