Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestSolution/TimeWindowed/BestTimeWindowedVRPSolutionAnalyzer.cs @ 7175

Last change on this file since 7175 was 7175, checked in by svonolfe, 12 years ago

Adapted VRP analyzers to the new IAnalyzer interface (#1584)

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.VehicleRouting.Interfaces;
30using HeuristicLab.Problems.VehicleRouting.Variants;
31using HeuristicLab.Common;
32
33namespace HeuristicLab.Problems.VehicleRouting {
34  /// <summary>
35  /// An operator for analyzing the best solution of Vehicle Routing Problems.
36  /// </summary>
37  [Item("BestTimeWindowedVRPSolutionAnalyzer", "An operator for analyzing the best solution of time windowed Vehicle Routing Problems.")]
38  [StorableClass]
39  public sealed class BestTimeWindowedVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ITimeWindowedOperator {
40    public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter {
41      get { return (ILookupParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
42    }
43    public ScopeTreeLookupParameter<IVRPEncoding> VRPToursParameter {
44      get { return (ScopeTreeLookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
45    }
46    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
47      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
48    }
49
50    public ScopeTreeLookupParameter<DoubleValue> TardinessParameter {
51      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Tardiness"]; }
52    }
53    public ScopeTreeLookupParameter<DoubleValue> TravelTimeParameter {
54      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["TravelTime"]; }
55    }
56
57    public LookupParameter<VRPSolution> BestSolutionParameter {
58      get { return (LookupParameter<VRPSolution>)Parameters["BestSolution"]; }
59    }
60    public ValueLookupParameter<ResultCollection> ResultsParameter {
61      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
62    }
63
64    public bool EnabledByDefault {
65      get { return true; }
66    }
67
68    [StorableConstructor]
69    private BestTimeWindowedVRPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
70
71    public BestTimeWindowedVRPSolutionAnalyzer()
72      : base() {
73        Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The problem instance."));
74        Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
75        Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the VRP solutions which should be analyzed."));
76
77        Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Tardiness", "The tardiness of the VRP solutions which should be analyzed."));
78        Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("TravelTime", "The travel times of the VRP solutions which should be analyzed."));
79
80        Parameters.Add(new LookupParameter<VRPSolution>("BestSolution", "The best VRP solution."));
81        Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best VRP solution should be stored."));
82    }
83
84    public override IDeepCloneable Clone(Cloner cloner) {
85      return new BestTimeWindowedVRPSolutionAnalyzer(this, cloner);
86    }
87
88    private BestTimeWindowedVRPSolutionAnalyzer(BestTimeWindowedVRPSolutionAnalyzer original, Cloner cloner)
89      : base(original, cloner) {
90    }
91
92    public override IOperation Apply() {
93      ItemArray<IVRPEncoding> solutions = VRPToursParameter.ActualValue;
94      ResultCollection results = ResultsParameter.ActualValue;
95
96      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
97      ItemArray<DoubleValue> tardinesses = TardinessParameter.ActualValue;
98      ItemArray<DoubleValue> travelTimes = TravelTimeParameter.ActualValue;
99
100      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
101
102      VRPSolution solution = BestSolutionParameter.ActualValue;
103      if (solution != null) {
104        if (!results.ContainsKey("Best VRP Solution Tardiness")) {
105          results.Add(new Result("Best VRP Solution Tardiness", new DoubleValue(tardinesses[i].Value)));
106          results.Add(new Result("Best VRP Solution TravelTime", new DoubleValue(travelTimes[i].Value)));
107        } else {
108          if (qualities[i].Value <= solution.Quality.Value) {
109            (results["Best VRP Solution Tardiness"].Value as DoubleValue).Value = tardinesses[i].Value;
110            (results["Best VRP Solution TravelTime"].Value as DoubleValue).Value = travelTimes[i].Value;
111          }
112        }
113      }
114
115      return base.Apply();
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.