Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestSolution/Capacitated/BestCapacitatedVRPSolutionAnalyzer.cs @ 15769

Last change on this file since 15769 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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("BestCapacitatedVRPSolutionAnalyzer", "An operator for analyzing the best solution of capacitated Vehicle Routing Problems.")]
39  [StorableClass]
40  public sealed class BestCapacitatedVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ICapacitatedOperator, 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<DoubleValue> OverloadParameter {
52      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Overload"]; }
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 BestCapacitatedVRPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
68
69    public BestCapacitatedVRPSolutionAnalyzer()
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<DoubleValue>("Overload", "The overloads 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 BestCapacitatedVRPSolutionAnalyzer(this, cloner);
83    }
84
85    private BestCapacitatedVRPSolutionAnalyzer(BestCapacitatedVRPSolutionAnalyzer 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<DoubleValue> overloads = OverloadParameter.ActualValue;
96
97      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
98
99      VRPSolution solution = BestSolutionParameter.ActualValue;
100      if (solution != null) {
101        if (!results.ContainsKey("Best VRP Solution Overload")) {
102          results.Add(new Result("Best VRP Solution Overload", new DoubleValue(overloads[i].Value)));
103        } else {
104          VRPEvaluation eval = problemInstance.Evaluate(solution.Solution);
105          if (qualities[i].Value <= eval.Quality) {
106            (results["Best VRP Solution Overload"].Value as DoubleValue).Value = overloads[i].Value;
107          }
108        }
109      }
110
111      return base.Apply();
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.