Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/03/20 15:50:59 (4 years ago)
Author:
abeham
Message:

#2521: working on VRP

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Analyzer/BestSolution/BestVRPSolutionAnalyzer.cs

    r17709 r17710  
    2727using HeuristicLab.Optimization;
    2828using HeuristicLab.Parameters;
     29using HeuristicLab.Problems.VehicleRouting.Interfaces;
    2930using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
    3031
     
    3334  /// An operator for analyzing the best solution of Vehicle Routing Problems.
    3435  /// </summary>
     36  //TODO: remove this operator -> part of VRP problem analyze
    3537  [Item("BestVRPSolutionAnalyzer", "An operator for analyzing the best solution of Vehicle Routing Problems.")]
    3638  [StorableType("3e1bb409-0b8f-4324-826c-2190aa5fb2b6")]
    3739  public sealed class BestVRPSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer, ISingleObjectiveOperator {
     40
     41    [Storable] private ILookupParameter<IVRPProblemInstance> problemInstanceParameter;
     42    public ILookupParameter<IVRPProblemInstance> ProblemInstanceParameter => problemInstanceParameter;
     43    [Storable] private IScopeTreeLookupParameter<IVRPEncodedSolution> vrpToursParameter;
     44    public IScopeTreeLookupParameter<IVRPEncodedSolution> VRPToursParameter => vrpToursParameter;
    3845
    3946    [Storable] private IScopeTreeLookupParameter<VRPEvaluation> evaluationParameter;
     
    4350    public ILookupParameter<VRPSolution> BestSolutionParameter => bestSolutionParameter;
    4451
    45     [Storable] private IResultParameter<VRPEvaluation> bestEvaluationParameter;
    46     public IResultParameter<VRPEvaluation> BestSolutionEvaluationParameter => bestEvaluationParameter;
     52    [Storable] private ILookupParameter<VRPSolution> bestFeasibleSolutionParameter;
     53    public ILookupParameter<VRPSolution> BestFeasibleSolutionParameter => bestFeasibleSolutionParameter;
     54
     55    [Storable] private IResultParameter<VRPEvaluation> bestEvaluationResult;
     56    public IResultParameter<VRPEvaluation> BestSolutionEvaluationResult => bestEvaluationResult;
     57    [Storable] private IResultParameter<VRPEvaluation> bestFeasibleEvaluationResult;
     58    public IResultParameter<VRPEvaluation> BestFeasibleSolutionEvaluationResult => bestFeasibleEvaluationResult;
     59    [Storable] private IResultParameter<VRPSolution> bestSolutionResult;
     60    public IResultParameter<VRPSolution> BestSolutionResult => bestSolutionResult;
     61    [Storable] private IResultParameter<VRPSolution> bestFeasibleSolutionResult;
     62    public IResultParameter<VRPSolution> BestFeasibleSolutionResult => bestFeasibleSolutionResult;
    4763
    4864    public bool EnabledByDefault {
     
    5470    private BestVRPSolutionAnalyzer(BestVRPSolutionAnalyzer original, Cloner cloner)
    5571      : base(original, cloner) {
     72      problemInstanceParameter = cloner.Clone(original.problemInstanceParameter);
     73      vrpToursParameter = cloner.Clone(original.vrpToursParameter);
    5674      evaluationParameter = cloner.Clone(original.evaluationParameter);
    5775      bestSolutionParameter = cloner.Clone(original.bestSolutionParameter);
    58       bestEvaluationParameter = cloner.Clone(original.bestEvaluationParameter);
     76      bestFeasibleSolutionParameter = cloner.Clone(original.bestFeasibleSolutionParameter);
     77      bestEvaluationResult = cloner.Clone(original.bestEvaluationResult);
     78      bestFeasibleEvaluationResult = cloner.Clone(original.bestFeasibleEvaluationResult);
     79      bestSolutionResult = cloner.Clone(original.bestSolutionResult);
     80      bestFeasibleSolutionResult = cloner.Clone(original.bestFeasibleSolutionResult);
    5981    }
    6082    public BestVRPSolutionAnalyzer()
    6183      : base() {
     84      Parameters.Add(problemInstanceParameter = new LookupParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance."));
     85      Parameters.Add(vrpToursParameter = new ScopeTreeLookupParameter<IVRPEncodedSolution>("VRPTours", "The VRP encoded solution."));
    6286      Parameters.Add(evaluationParameter = new ScopeTreeLookupParameter<VRPEvaluation>("EvaluationResult", "The qualities of the VRP solutions which should be analyzed."));
    6387      Parameters.Add(bestSolutionParameter = new LookupParameter<VRPSolution>("BestSolution", "The best-so-far solution."));
    64       Parameters.Add(bestEvaluationParameter = new ResultParameter<VRPEvaluation>("Best VRP Evaluation", "The best VRP evaluation.", "Results"));
     88      Parameters.Add(bestFeasibleSolutionParameter = new LookupParameter<VRPSolution>("BestFeasibleSolution", "The best-so-far feasible solution."));
     89      Parameters.Add(bestEvaluationResult = new ResultParameter<VRPEvaluation>("Best VRP Solution Evaluation", "The best VRP evaluation.", "Results"));
     90      Parameters.Add(bestFeasibleEvaluationResult = new ResultParameter<VRPEvaluation>("Best Feasible VRP Solution Evaluation", "The best feasible VRP evaluation.", "Results"));
     91      Parameters.Add(bestSolutionResult = new ResultParameter<VRPSolution>("Best VRP Solution", "The best-so-far VRP solution."));
     92      Parameters.Add(bestFeasibleSolutionResult = new ResultParameter<VRPSolution>("Best feasible VRP Solution", "The best-so-far feasible VRP solution."));
    6593    }
    6694
     
    72100      var evaluations = EvaluationParameter.ActualValue;
    73101
    74       int i = evaluations.Select((x, index) => new { index, Eval = x }).OrderBy(x => x.Eval.Quality).First().index;
     102      var bestInPop = evaluations.Select((x, index) => new { index, Eval = x }).OrderBy(x => x.Eval.Quality).First();
     103      var bestFeasibleInPop = evaluations.Select((x, index) => new { index, Eval = x }).Where(x => x.Eval.IsFeasible).OrderBy(x => x.Eval.Quality).FirstOrDefault();
    75104
    76       var bestEvaluation = BestSolutionEvaluationParameter.ActualValue;
     105      var bestEvaluation = BestSolutionEvaluationResult.ActualValue;
    77106     
    78107      var bestSolution = BestSolutionParameter.ActualValue;
    79       if (bestSolution == null || evaluations[i].Quality <= bestSolution.Quality.Value) {
    80         BestSolutionEvaluationParameter.ActualValue = (VRPEvaluation)evaluations[i].Clone();
     108      if (bestSolution == null || bestInPop.Eval.Quality < bestSolution.Evaluation.Quality) {
     109        var best = new VRPSolution(ProblemInstanceParameter.ActualValue,
     110          VRPToursParameter.ActualValue[bestInPop.index], (VRPEvaluation)bestInPop.Eval.Clone());
     111        BestSolutionParameter.ActualValue = best;
     112        BestSolutionResult.ActualValue = best;
     113        BestSolutionEvaluationResult.ActualValue = (VRPEvaluation)bestInPop.Eval.Clone();
     114      };
     115
     116      if (bestFeasibleInPop != null) {
     117        var bestFeasibleSolution = BestFeasibleSolutionParameter.ActualValue;
     118        if (bestFeasibleSolution == null || bestFeasibleInPop.Eval.Quality < bestFeasibleSolution.Evaluation.Quality) {
     119          var bestFeasible = new VRPSolution(ProblemInstanceParameter.ActualValue,
     120          VRPToursParameter.ActualValue[bestFeasibleInPop.index], (VRPEvaluation)bestFeasibleInPop.Eval.Clone());
     121          BestFeasibleSolutionParameter.ActualValue = bestFeasible;
     122          bestFeasibleSolutionResult.ActualValue = bestFeasible;
     123          BestFeasibleSolutionEvaluationResult.ActualValue = (VRPEvaluation)bestFeasibleInPop.Eval.Clone();
     124        }
    81125      }
    82126
Note: See TracChangeset for help on using the changeset viewer.