Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4068 was 4068, checked in by swagner, 13 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 8.3 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;
29
30namespace HeuristicLab.Problems.VehicleRouting {
31  /// <summary>
32  /// An operator for analyzing the best solution of Vehicle Routing Problems.
33  /// </summary>
34  [Item("BestVRPSolutionAnalyzer", "An operator for analyzing the best solution of Vehicle Routing Problems.")]
35  [StorableClass]
36  public sealed class BestVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
37    public ScopeTreeLookupParameter<IVRPEncoding> VRPSolutionParameter {
38      get { return (ScopeTreeLookupParameter<IVRPEncoding>)Parameters["VRPSolution"]; }
39    }
40    public ILookupParameter<DoubleMatrix> DistanceMatrixParameter {
41      get { return (ILookupParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
42    }
43    public ILookupParameter<BoolValue> UseDistanceMatrixParameter {
44      get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
45    }
46    public LookupParameter<DoubleMatrix> CoordinatesParameter {
47      get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }
48    }
49    public ILookupParameter<DoubleArray> ReadyTimeParameter {
50      get { return (ILookupParameter<DoubleArray>)Parameters["ReadyTime"]; }
51    }
52    public ILookupParameter<DoubleArray> DueTimeParameter {
53      get { return (ILookupParameter<DoubleArray>)Parameters["DueTime"]; }
54    }
55    public ILookupParameter<DoubleArray> ServiceTimeParameter {
56      get { return (ILookupParameter<DoubleArray>)Parameters["ServiceTime"]; }
57    }
58    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
59      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
60    }
61    public ScopeTreeLookupParameter<DoubleValue> OverloadParameter {
62      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Overload"]; }
63    }
64    public ScopeTreeLookupParameter<DoubleValue> TardinessParameter {
65      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Tardiness"]; }
66    }
67    public ScopeTreeLookupParameter<DoubleValue> DistanceParameter {
68      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Distance"]; }
69    }
70    public ScopeTreeLookupParameter<DoubleValue> TravelTimeParameter {
71      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["TravelTime"]; }
72    }
73    public ScopeTreeLookupParameter<DoubleValue> VehiclesUtilizedParameter {
74      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["VehiclesUtilized"]; }
75    }
76    public LookupParameter<VRPSolution> BestSolutionParameter {
77      get { return (LookupParameter<VRPSolution>)Parameters["BestSolution"]; }
78    }
79    public ValueLookupParameter<ResultCollection> ResultsParameter {
80      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
81    }
82
83    public BestVRPSolutionAnalyzer()
84      : base() {
85      Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("VRPSolution", "The VRP solutions which should be evaluated."));
86      Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
87      Parameters.Add(new LookupParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
88      Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false."));
89      Parameters.Add(new LookupParameter<DoubleArray>("ReadyTime", "The ready time of each customer."));
90      Parameters.Add(new LookupParameter<DoubleArray>("DueTime", "The due time of each customer."));
91      Parameters.Add(new LookupParameter<DoubleArray>("ServiceTime", "The service time of each customer."));
92
93      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the VRP solutions which should be analyzed."));
94      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Distance", "The distances of the VRP solutions which should be analyzed."));
95      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Overload", "The overloads of the VRP solutions which should be analyzed."));
96      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Tardiness", "The tardiness of the VRP solutions which should be analyzed."));
97      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("TravelTime", "The travel times of the VRP solutions which should be analyzed."));
98      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("VehiclesUtilized", "The utilized vehicles of the VRP solutions which should be analyzed."));
99      Parameters.Add(new LookupParameter<VRPSolution>("BestSolution", "The best TSP solution."));
100      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best VRP solution should be stored."));
101    }
102
103    public override IOperation Apply() {
104      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
105      ItemArray<IVRPEncoding> solutions = VRPSolutionParameter.ActualValue;
106      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
107      ItemArray<DoubleValue> overloads = OverloadParameter.ActualValue;
108      ItemArray<DoubleValue> tardinesses = TardinessParameter.ActualValue;
109      ResultCollection results = ResultsParameter.ActualValue;
110      ItemArray<DoubleValue> distances = DistanceParameter.ActualValue;
111      ItemArray<DoubleValue> travelTimes = TravelTimeParameter.ActualValue;
112      ItemArray<DoubleValue> vehiclesUtilizations = VehiclesUtilizedParameter.ActualValue;
113
114      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
115
116      IVRPEncoding best = solutions[i].Clone() as IVRPEncoding;
117      VRPSolution solution = BestSolutionParameter.ActualValue;
118      if (solution == null) {
119        solution = new VRPSolution(coordinates, best.Clone() as IVRPEncoding, new DoubleValue(qualities[i].Value),
120          new DoubleValue(distances[i].Value), new DoubleValue(overloads[i].Value), new DoubleValue(tardinesses[i].Value),
121          new DoubleValue(travelTimes[i].Value), new DoubleValue(vehiclesUtilizations[i].Value),
122          DistanceMatrixParameter.ActualValue, UseDistanceMatrixParameter.ActualValue,
123          ReadyTimeParameter.ActualValue, DueTimeParameter.ActualValue, ServiceTimeParameter.ActualValue);
124        BestSolutionParameter.ActualValue = solution;
125        results.Add(new Result("Best VRP Solution", solution));
126      } else {
127        if (solution.Quality.Value > qualities[i].Value) {
128          solution.Coordinates = coordinates;
129          solution.Solution = best.Clone() as IVRPEncoding;
130          solution.Quality.Value = qualities[i].Value;
131          solution.Distance.Value = distances[i].Value;
132          solution.Overload.Value = overloads[i].Value;
133          solution.Tardiness.Value = tardinesses[i].Value;
134          solution.TravelTime.Value = travelTimes[i].Value;
135          solution.VehicleUtilization.Value = vehiclesUtilizations[i].Value;
136          solution.DistanceMatrix = DistanceMatrixParameter.ActualValue;
137          solution.UseDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
138          solution.ReadyTime = ReadyTimeParameter.ActualValue;
139          solution.DueTime = DueTimeParameter.ActualValue;
140          solution.ServiceTime = ServiceTimeParameter.ActualValue;
141        }
142      }
143
144      return base.Apply();
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.