Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestSolution/PickupAndDelivery/BestPickupAndDeliveryVRPSolutionAnalyzer.cs @ 18190

Last change on this file since 18190 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.2 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HEAL.Attic;
30using HeuristicLab.Problems.VehicleRouting.Interfaces;
31using HeuristicLab.Problems.VehicleRouting.Variants;
32using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
33
34namespace HeuristicLab.Problems.VehicleRouting {
35  /// <summary>
36  /// An operator for analyzing the best solution of Vehicle Routing Problems.
37  /// </summary>
38  [Item("BestPickupAndDeliveryVRPSolutionAnalyzer", "An operator for analyzing the best solution of Pickup and Delivery Routing Problems.")]
39  [StorableType("81CA703F-038E-4D41-BB3F-25E04EC46AF9")]
40  public sealed class BestPickupAndDeliveryVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, IPickupAndDeliveryOperator, ISingleObjectiveOperator {
41    public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter {
42      get { return (ILookupParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
43    }
44    public ScopeTreeLookupParameter<IVRPEncoding> VRPToursParameter {
45      get { return (ScopeTreeLookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
46    }
47    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
48      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
49    }
50
51    public ScopeTreeLookupParameter<IntValue> PickupViolationsParameter {
52      get { return (ScopeTreeLookupParameter<IntValue>)Parameters["PickupViolations"]; }
53    }
54
55    public LookupParameter<VRPSolution> BestSolutionParameter {
56      get { return (LookupParameter<VRPSolution>)Parameters["BestSolution"]; }
57    }
58    public ValueLookupParameter<ResultCollection> ResultsParameter {
59      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
60    }
61
62    public bool EnabledByDefault {
63      get { return true; }
64    }
65
66    [StorableConstructor]
67    private BestPickupAndDeliveryVRPSolutionAnalyzer(StorableConstructorFlag _) : base(_) { }
68
69    public BestPickupAndDeliveryVRPSolutionAnalyzer()
70      : base() {
71      Parameters.Add(new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The problem instance."));
72      Parameters.Add(new ScopeTreeLookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
73      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the VRP solutions which should be analyzed."));
74
75      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("PickupViolations", "The pickup violation of the VRP solutions which should be analyzed."));
76
77      Parameters.Add(new LookupParameter<VRPSolution>("BestSolution", "The best VRP solution."));
78      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best VRP solution should be stored."));
79    }
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new BestPickupAndDeliveryVRPSolutionAnalyzer(this, cloner);
83    }
84
85    private BestPickupAndDeliveryVRPSolutionAnalyzer(BestPickupAndDeliveryVRPSolutionAnalyzer original, Cloner cloner)
86      : base(original, cloner) {
87    }
88
89    public override IOperation Apply() {
90      IVRPProblemInstance problemInstance = ProblemInstanceParameter.ActualValue;
91      ItemArray<IVRPEncoding> solutions = VRPToursParameter.ActualValue;
92      ResultCollection results = ResultsParameter.ActualValue;
93
94      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
95      ItemArray<IntValue> pickupViolations = PickupViolationsParameter.ActualValue;
96
97      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
98
99      IVRPEncoding best = solutions[i] as IVRPEncoding;
100      VRPSolution solution = BestSolutionParameter.ActualValue;
101      if (solution != null) {
102        if (!results.ContainsKey("Best VRP Solution PickupViolations")) {
103          results.Add(new Result("Best VRP Solution PickupViolations", new DoubleValue(pickupViolations[i].Value)));
104        } else {
105          VRPEvaluation eval = problemInstance.Evaluate(solution.Solution);
106          if (qualities[i].Value <= eval.Quality) {
107            (results["Best VRP Solution PickupViolations"].Value as DoubleValue).Value = pickupViolations[i].Value;
108          }
109        }
110      }
111
112      return base.Apply();
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.