Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestSolution/BestVRPSolutionAnalyzer.cs @ 4374

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

Added analyzers and views (#1177)

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;
31
32namespace HeuristicLab.Problems.VehicleRouting {
33  /// <summary>
34  /// An operator for analyzing the best solution of Vehicle Routing Problems.
35  /// </summary>
36  [Item("BestVRPSolutionAnalyzer", "An operator for analyzing the best solution of Vehicle Routing Problems.")]
37  [StorableClass]
38  public sealed class BestVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, IGeneralVRPOperator {
39    public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter {
40      get { return (ILookupParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
41    }
42    public ScopeTreeLookupParameter<IVRPEncoding> VRPToursParameter {
43      get { return (ScopeTreeLookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
44    }
45
46    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
47      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
48    }
49    public ScopeTreeLookupParameter<DoubleValue> DistanceParameter {
50      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Distance"]; }
51    }
52    public ScopeTreeLookupParameter<DoubleValue> VehiclesUtilizedParameter {
53      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["VehiclesUtilized"]; }
54    }
55
56    public LookupParameter<VRPSolution> BestSolutionParameter {
57      get { return (LookupParameter<VRPSolution>)Parameters["BestSolution"]; }
58    }
59    public ValueLookupParameter<ResultCollection> ResultsParameter {
60      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
61    }
62
63    [StorableConstructor]
64    private BestVRPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
65
66    public BestVRPSolutionAnalyzer()
67      : base() {
68        Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The problem instance."));
69        Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
70
71        Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the VRP solutions which should be analyzed."));
72        Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Distance", "The distances of the VRP solutions which should be analyzed."));
73        Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("VehiclesUtilized", "The utilized vehicles of the VRP solutions which should be analyzed."));
74
75        Parameters.Add(new LookupParameter<VRPSolution>("BestSolution", "The best TSP solution."));
76        Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best VRP solution should be stored."));
77    }
78
79    public override IOperation Apply() {
80      IVRPProblemInstance problemInstance = ProblemInstanceParameter.ActualValue;
81      ItemArray<IVRPEncoding> solutions = VRPToursParameter.ActualValue;
82      ResultCollection results = ResultsParameter.ActualValue;
83
84      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
85      ItemArray<DoubleValue> distances = DistanceParameter.ActualValue;
86      ItemArray<DoubleValue> vehiclesUtilizations = VehiclesUtilizedParameter.ActualValue;
87
88      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
89
90      IVRPEncoding best = solutions[i].Clone() as IVRPEncoding;
91      VRPSolution solution = BestSolutionParameter.ActualValue;
92      if (solution == null) {
93        solution = new VRPSolution(problemInstance, best.Clone() as IVRPEncoding, new DoubleValue(qualities[i].Value));
94        BestSolutionParameter.ActualValue = solution;
95        results.Add(new Result("Best VRP Solution", solution));
96
97        results.Add(new Result("Best VRP Solution Distance", new DoubleValue(distances[i].Value)));
98        results.Add(new Result("Best VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[i].Value)));
99      } else {
100        if (qualities[i].Value <= solution.Quality.Value) {
101          solution.ProblemInstance = problemInstance;
102          solution.Solution = best.Clone() as IVRPEncoding;
103          solution.Quality.Value = qualities[i].Value;
104          (results["Best VRP Solution Distance"].Value as DoubleValue).Value = distances[i].Value;
105          (results["Best VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[i].Value;
106        }
107      }
108
109      return base.Apply();
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.