Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Analyzers/BestVRPSolutionAnalyzer.cs @ 3938

Last change on this file since 3938 was 3938, checked in by svonolfe, 14 years ago

Added CVRP implementation using the Alba encoding (#1039)

File size: 6.6 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.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.VehicleRouting;
32
33namespace HeuristicLab.Problems.VehicleRouting {
34  /// <summary>
35  /// An operator for analyzing the best solution of Vehicle Routing Problems.
36  /// </summary>
37  [Item("BestVRPSolutionAnalyzer", "An operator for analyzing the best solution of Vehicle Routing Problems.")]
38  [StorableClass]
39  public sealed class BestVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
40    public ScopeTreeLookupParameter<IVRPEncoding> VRPSolutionParameter {
41      get { return (ScopeTreeLookupParameter<IVRPEncoding>)Parameters["VRPSolution"]; }
42    }
43    public LookupParameter<DoubleMatrix> CoordinatesParameter {
44      get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
45    }
46    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
47      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
48    }
49    public ScopeTreeLookupParameter<DoubleValue> OverloadParameter {
50      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Overload"]; }
51    }
52    public ScopeTreeLookupParameter<DoubleValue> TardinessParameter {
53      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Tardiness"]; }
54    }
55    public ScopeTreeLookupParameter<DoubleValue> DistanceParameter {
56      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Distance"]; }
57    }
58    public ScopeTreeLookupParameter<DoubleValue> TravelTimeParameter {
59      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["TravelTime"]; }
60    }
61    public ScopeTreeLookupParameter<DoubleValue> VehiclesUtilizedParameter {
62      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["VehiclesUtilized"]; }
63    }
64    public LookupParameter<VRPSolution> BestSolutionParameter {
65      get { return (LookupParameter<VRPSolution>)Parameters["BestSolution"]; }
66    }
67    public ValueLookupParameter<ResultCollection> ResultsParameter {
68      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
69    }
70
71    public BestVRPSolutionAnalyzer()
72      : base() {
73      Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("VRPSolution", "The VRP solutions which should be evaluated."));
74      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
75      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the VRP solutions which should be analyzed."));
76      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Distance", "The distances of the VRP solutions which should be analyzed."));
77      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Overload", "The overloads of the VRP solutions which should be analyzed."));
78      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Tardiness", "The tardiness of the VRP solutions which should be analyzed."));
79      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("TravelTime", "The travel times of the VRP solutions which should be analyzed."));
80      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("VehiclesUtilized", "The utilized vehicles of the VRP solutions which should be analyzed."));
81      Parameters.Add(new LookupParameter<VRPSolution>("BestSolution", "The best TSP solution."));
82      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best VRP solution should be stored."));
83    }
84
85    public override IOperation Apply() {
86      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
87      ItemArray<IVRPEncoding> solutions = VRPSolutionParameter.ActualValue;
88      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
89      ItemArray<DoubleValue> overloads = OverloadParameter.ActualValue;
90      ItemArray<DoubleValue> tardinesses = TardinessParameter.ActualValue;
91      ResultCollection results = ResultsParameter.ActualValue;
92      ItemArray<DoubleValue> distances = DistanceParameter.ActualValue;
93      ItemArray<DoubleValue> travelTimes = TravelTimeParameter.ActualValue;
94      ItemArray<DoubleValue> vehiclesUtilizations = VehiclesUtilizedParameter.ActualValue;
95
96      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
97
98      IVRPEncoding best = solutions[i].Clone() as IVRPEncoding;
99      VRPSolution solution = BestSolutionParameter.ActualValue;
100      if (solution == null) {
101        solution = new VRPSolution(coordinates, best.Clone() as IVRPEncoding, new DoubleValue(qualities[i].Value),
102          new DoubleValue(distances[i].Value), new DoubleValue(overloads[i].Value), new DoubleValue(tardinesses[i].Value),
103          new DoubleValue(travelTimes[i].Value), new DoubleValue(vehiclesUtilizations[i].Value));
104        BestSolutionParameter.ActualValue = solution;
105        results.Add(new Result("Best VRP Solution", solution));
106      } else {
107        if (solution.Quality.Value > qualities[i].Value) {
108          solution.Coordinates = coordinates;
109          solution.Solution = best.Clone() as IVRPEncoding;
110          solution.Quality.Value = qualities[i].Value;
111          solution.Distance.Value = distances[i].Value;
112          solution.Overload.Value = overloads[i].Value;
113          solution.Tardiness.Value = tardinesses[i].Value;
114          solution.TravelTime.Value = travelTimes[i].Value;
115          solution.VehicleUtilization.Value = vehiclesUtilizations[i].Value;
116
117          if (solution.Quality.Value < solution.Distance.Value) {
118          }
119        }
120      }
121
122      return base.Apply();
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.