Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11642 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 5.0 KB
RevLine 
[4374]1#region License Information
2/* HeuristicLab
[11171]3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4374]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;
[8053]23using HeuristicLab.Common;
[4374]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;
[8497]32using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
[4374]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 {
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
[7175]62    public bool EnabledByDefault {
63      get { return true; }
64    }
65
[4374]66    [StorableConstructor]
67    private BestCapacitatedVRPSolutionAnalyzer(bool deserializing) : base(deserializing) { }
68
69    public BestCapacitatedVRPSolutionAnalyzer()
70      : base() {
[8053]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."));
[4374]74
[8053]75      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Overload", "The overloads of the VRP solutions which should be analyzed."));
[4374]76
[8053]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."));
[4374]79    }
80
[4752]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
[4374]89    public override IOperation Apply() {
[8497]90      IVRPProblemInstance problemInstance = ProblemInstanceParameter.ActualValue;
[4374]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;
[6710]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 {
[8497]104          VRPEvaluation eval = problemInstance.Evaluate(solution.Solution);
105          if (qualities[i].Value <= eval.Quality) {
[6710]106            (results["Best VRP Solution Overload"].Value as DoubleValue).Value = overloads[i].Value;
107          }
[4374]108        }
109      }
110
111      return base.Apply();
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.