Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2521: working on VRP

File size: 7.4 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.Interfaces;
30using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
31
32namespace HeuristicLab.Problems.VehicleRouting {
33  /// <summary>
34  /// An operator for analyzing the best solution of Vehicle Routing Problems.
35  /// </summary>
36  //TODO: remove this operator -> part of VRP problem analyze
37  [Item("BestVRPSolutionAnalyzer", "An operator for analyzing the best solution of Vehicle Routing Problems.")]
38  [StorableType("3e1bb409-0b8f-4324-826c-2190aa5fb2b6")]
39  public sealed class BestVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
40
41    [Storable] private ILookupParameter<IVRPProblemInstance> problemInstanceParameter;
42    public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter => problemInstanceParameter;
43    [Storable] private IScopeTreeLookupParameter<IVRPEncodedSolution> vrpToursParameter;
44    public IScopeTreeLookupParameter<IVRPEncodedSolution> VRPToursParameter => vrpToursParameter;
45
46    [Storable] private IScopeTreeLookupParameter<VRPEvaluation> evaluationParameter;
47    public IScopeTreeLookupParameter<VRPEvaluation> EvaluationParameter => evaluationParameter;
48
49    [Storable] private ILookupParameter<VRPSolution> bestSolutionParameter;
50    public ILookupParameter<VRPSolution> BestSolutionParameter => bestSolutionParameter;
51
52    [Storable] private ILookupParameter<VRPSolution> bestFeasibleSolutionParameter;
53    public ILookupParameter<VRPSolution> BestFeasibleSolutionParameter => bestFeasibleSolutionParameter;
54
55    [Storable] private IResultParameter<VRPEvaluation> bestEvaluationResult;
56    public IResultParameter<VRPEvaluation> BestSolutionEvaluationResult => bestEvaluationResult;
57    [Storable] private IResultParameter<VRPEvaluation> bestFeasibleEvaluationResult;
58    public IResultParameter<VRPEvaluation> BestFeasibleSolutionEvaluationResult => bestFeasibleEvaluationResult;
59    [Storable] private IResultParameter<VRPSolution> bestSolutionResult;
60    public IResultParameter<VRPSolution> BestSolutionResult => bestSolutionResult;
61    [Storable] private IResultParameter<VRPSolution> bestFeasibleSolutionResult;
62    public IResultParameter<VRPSolution> BestFeasibleSolutionResult => bestFeasibleSolutionResult;
63
64    public bool EnabledByDefault {
65      get { return true; }
66    }
67
68    [StorableConstructor]
69    private BestVRPSolutionAnalyzer(StorableConstructorFlag _) : base(_) { }
70    private BestVRPSolutionAnalyzer(BestVRPSolutionAnalyzer original, Cloner cloner)
71      : base(original, cloner) {
72      problemInstanceParameter = cloner.Clone(original.problemInstanceParameter);
73      vrpToursParameter = cloner.Clone(original.vrpToursParameter);
74      evaluationParameter = cloner.Clone(original.evaluationParameter);
75      bestSolutionParameter = cloner.Clone(original.bestSolutionParameter);
76      bestFeasibleSolutionParameter = cloner.Clone(original.bestFeasibleSolutionParameter);
77      bestEvaluationResult = cloner.Clone(original.bestEvaluationResult);
78      bestFeasibleEvaluationResult = cloner.Clone(original.bestFeasibleEvaluationResult);
79      bestSolutionResult = cloner.Clone(original.bestSolutionResult);
80      bestFeasibleSolutionResult = cloner.Clone(original.bestFeasibleSolutionResult);
81    }
82    public BestVRPSolutionAnalyzer()
83      : base() {
84      Parameters.Add(problemInstanceParameter = new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance."));
85      Parameters.Add(vrpToursParameter = new ScopeTreeLookupParameter<IVRPEncodedSolution>("VRPTours", "The VRP encoded solution."));
86      Parameters.Add(evaluationParameter = new ScopeTreeLookupParameter<VRPEvaluation>("EvaluationResult", "The qualities of the VRP solutions which should be analyzed."));
87      Parameters.Add(bestSolutionParameter = new LookupParameter<VRPSolution>("BestSolution", "The best-so-far solution."));
88      Parameters.Add(bestFeasibleSolutionParameter = new LookupParameter<VRPSolution>("BestFeasibleSolution", "The best-so-far feasible solution."));
89      Parameters.Add(bestEvaluationResult = new ResultParameter<VRPEvaluation>("Best VRP Solution Evaluation", "The best VRP evaluation.", "Results"));
90      Parameters.Add(bestFeasibleEvaluationResult = new ResultParameter<VRPEvaluation>("Best Feasible VRP Solution Evaluation", "The best feasible VRP evaluation.", "Results"));
91      Parameters.Add(bestSolutionResult = new ResultParameter<VRPSolution>("Best VRP Solution", "The best-so-far VRP solution."));
92      Parameters.Add(bestFeasibleSolutionResult = new ResultParameter<VRPSolution>("Best feasible VRP Solution", "The best-so-far feasible VRP solution."));
93    }
94
95    public override IDeepCloneable Clone(Cloner cloner) {
96      return new BestVRPSolutionAnalyzer(this, cloner);
97    }
98
99    public override IOperation Apply() {
100      var evaluations = EvaluationParameter.ActualValue;
101
102      var bestInPop = evaluations.Select((x, index) => new { index, Eval = x }).OrderBy(x => x.Eval.Quality).First();
103      var bestFeasibleInPop = evaluations.Select((x, index) => new { index, Eval = x }).Where(x => x.Eval.IsFeasible).OrderBy(x => x.Eval.Quality).FirstOrDefault();
104
105      var bestEvaluation = BestSolutionEvaluationResult.ActualValue;
106     
107      var bestSolution = BestSolutionParameter.ActualValue;
108      if (bestSolution == null || bestInPop.Eval.Quality < bestSolution.Evaluation.Quality) {
109        var best = new VRPSolution(ProblemInstanceParameter.ActualValue,
110          VRPToursParameter.ActualValue[bestInPop.index], (VRPEvaluation)bestInPop.Eval.Clone());
111        BestSolutionParameter.ActualValue = best;
112        BestSolutionResult.ActualValue = best;
113        BestSolutionEvaluationResult.ActualValue = (VRPEvaluation)bestInPop.Eval.Clone();
114      };
115
116      if (bestFeasibleInPop != null) {
117        var bestFeasibleSolution = BestFeasibleSolutionParameter.ActualValue;
118        if (bestFeasibleSolution == null || bestFeasibleInPop.Eval.Quality < bestFeasibleSolution.Evaluation.Quality) {
119          var bestFeasible = new VRPSolution(ProblemInstanceParameter.ActualValue,
120          VRPToursParameter.ActualValue[bestFeasibleInPop.index], (VRPEvaluation)bestFeasibleInPop.Eval.Clone());
121          BestFeasibleSolutionParameter.ActualValue = bestFeasible;
122          bestFeasibleSolutionResult.ActualValue = bestFeasible;
123          BestFeasibleSolutionEvaluationResult.ActualValue = (VRPEvaluation)bestFeasibleInPop.Eval.Clone();
124        }
125      }
126
127      return base.Apply();
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.