Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestAverageWorstTours/BasicTourAnalyzer.cs @ 17716

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

#2521: changed storable type guids

File size: 5.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.Collections.Generic;
23using System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Problems.VehicleRouting.Interfaces;
32using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
33using HeuristicLab.Problems.VehicleRouting.Variants;
34
35namespace HeuristicLab.Problems.VehicleRouting {
36  /// <summary>
37  /// An operator which analyzes the best, average and worst quality of solutions in the scope tree.
38  /// </summary>
39  [Item("Basic Tour Analyzer", "An operator which analyzes the best, average and worst properties of the VRP tours in the scope tree.")]
40  [StorableType("7a135e25-fec8-4960-b412-249c56bb3aab")]
41  public sealed class BasicVRPTourAnalyzer : InstrumentedOperator, IAnalyzer, IGeneralVRPOperator {
42    [Storable] public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter { get; private set; }
43    [Storable] public IScopeTreeLookupParameter<VRPEvaluation> EvaluationResultParameter { get; private set; }
44
45    [Storable] public IResultParameter<DataTable> DistancesResult { get; private set; }
46    [Storable] public IResultParameter<DataTable> VehiclesUtilizedResult { get; private set; }
47
48    public bool EnabledByDefault {
49      get { return true; }
50    }
51
52    [StorableConstructor]
53    private BasicVRPTourAnalyzer(StorableConstructorFlag _) : base(_) { }
54    private BasicVRPTourAnalyzer(BasicVRPTourAnalyzer original, Cloner cloner)
55      : base(original, cloner) {
56      ProblemInstanceParameter = cloner.Clone(original.ProblemInstanceParameter);
57      EvaluationResultParameter = cloner.Clone(original.EvaluationResultParameter);
58      DistancesResult = cloner.Clone(original.DistancesResult);
59      VehiclesUtilizedResult = cloner.Clone(original.VehiclesUtilizedResult);
60    }
61    public BasicVRPTourAnalyzer()
62      : base() {
63      Parameters.Add(ProblemInstanceParameter = new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The problem instance."));
64      Parameters.Add(EvaluationResultParameter = new ScopeTreeLookupParameter<VRPEvaluation>("EvaluationResult", "The evaluations of the VRP solutions which should be analyzed."));
65      DataTable defaultDistanceTable = new DataTable("Distances"), defaultVehiclesUtilizedTable = new DataTable("Vehicles Utilized");
66      Parameters.Add(DistancesResult = new ResultParameter<DataTable>("Distances", "The solution distance progress in each iteration.", defaultDistanceTable));
67      Parameters.Add(VehiclesUtilizedResult = new ResultParameter<DataTable>("Vehicles Utilized", "The solution vehicle utilization progress in each iteration.", defaultVehiclesUtilizedTable));
68
69      defaultDistanceTable.Rows.Add(new DataRow("Best (monotonic)"));
70      defaultDistanceTable.Rows.Add(new DataRow("Best"));
71      defaultDistanceTable.Rows.Add(new DataRow("Average"));
72      defaultDistanceTable.Rows.Add(new DataRow("Worst"));
73
74      defaultVehiclesUtilizedTable.Rows.Add(new DataRow("Best (monotonic)"));
75      defaultVehiclesUtilizedTable.Rows.Add(new DataRow("Best"));
76      defaultVehiclesUtilizedTable.Rows.Add(new DataRow("Average"));
77      defaultVehiclesUtilizedTable.Rows.Add(new DataRow("Worst"));
78    }
79
80    public override IDeepCloneable Clone(Cloner cloner) {
81      return new BasicVRPTourAnalyzer(this, cloner);
82    }
83
84    public override IOperation InstrumentedApply() {
85      var evaluations = EvaluationResultParameter.ActualValue;
86      var distances = evaluations.Select(x => x.Distance);
87      var vehicles = evaluations.Select(x => (double)x.VehicleUtilization);
88
89      Analyze(distances, DistancesResult.ActualValue);
90      Analyze(vehicles, VehiclesUtilizedResult.ActualValue);
91
92      return base.InstrumentedApply();
93    }
94
95    internal static void Analyze(IEnumerable<double> sequence, DataTable table) {
96      var bestSoFar = table.Rows["Best (monotonic)"];
97      var best = table.Rows["Best"];
98      var average = table.Rows["Average"];
99      var worst = table.Rows["Worst"];
100
101      double min = double.MaxValue, max = double.MinValue, sum = 0.0;
102      double minmin = bestSoFar.Values.Count > 0 ? bestSoFar.Values.Last() : double.MaxValue;
103      int count = 0;
104
105      foreach (var s in sequence) {
106        if (min > s) min = s;
107        if (minmin > s) minmin = s;
108        if (max < s) max = s;
109        sum += s;
110        count++;
111      }
112
113      bestSoFar.Values.Add(minmin);
114      best.Values.Add(min);
115      average.Values.Add(sum / count);
116      worst.Values.Add(max);
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.