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/VehicleRoutingProblem.cs

    r17709 r17710  
    9696    }
    9797
     98    public override void Evaluate(ISingleObjectiveSolutionContext<IVRPEncodedSolution> solutionContext, IRandom random, CancellationToken cancellationToken) {
     99      solutionContext.EvaluationResult = ProblemInstance.Evaluate(solutionContext.EncodedSolution);
     100    }
    98101    public override ISingleObjectiveEvaluationResult Evaluate(IVRPEncodedSolution solution, IRandom random, CancellationToken cancellationToken) {
    99102      return ProblemInstance.Evaluate(solution);
     103    }
     104
     105    public override void Analyze(ISingleObjectiveSolutionContext<IVRPEncodedSolution>[] solutionContexts, ResultCollection results, IRandom random) {
     106      base.Analyze(solutionContexts, results, random);
     107      var evaluations = solutionContexts.Select(x => (VRPEvaluation)x.EvaluationResult);
     108
     109      var bestInPop = evaluations.Select((x, index) => new { index, Eval = x }).OrderBy(x => x.Eval.Quality).First();
     110      IResult bestSolutionResult;
     111      if (!results.TryGetValue("Best VRP Solution", out bestSolutionResult) || !(bestSolutionResult.Value is VRPSolution)) {
     112        var best = new VRPSolution(ProblemInstance, solutionContexts[bestInPop.index].EncodedSolution, (VRPEvaluation)bestInPop.Eval.Clone());
     113        if (bestSolutionResult != null)
     114          bestSolutionResult.Value = best;
     115        else results.Add(bestSolutionResult = new Result("Best VRP Solution", best));
     116      }
     117
     118      var bestSolution = (VRPSolution)bestSolutionResult.Value;
     119      if (bestSolution == null || bestInPop.Eval.Quality < bestSolution.Evaluation.Quality) {
     120        var best = new VRPSolution(ProblemInstance,
     121          (IVRPEncodedSolution)solutionContexts[bestInPop.index].EncodedSolution.Clone(),
     122          (VRPEvaluation)bestInPop.Eval.Clone());
     123        bestSolutionResult.Value = best;
     124      };
     125
     126      var bestValidInPop = evaluations.Select((x, index) => new { index, Eval = x }).Where(x => x.Eval.IsFeasible).OrderBy(x => x.Eval.Quality).FirstOrDefault();
     127      IResult bestValidSolutionResult;
     128      if (!results.TryGetValue("Best Valid VRP Solution", out bestValidSolutionResult) || !(bestValidSolutionResult.Value is VRPSolution)) {
     129        var bestValid = new VRPSolution(ProblemInstance, solutionContexts[bestValidInPop.index].EncodedSolution, (VRPEvaluation)bestValidInPop.Eval.Clone());
     130        if (bestValidSolutionResult != null)
     131          bestValidSolutionResult.Value = bestValid;
     132        else results.Add(bestValidSolutionResult = new Result("Best Valid VRP Solution", bestValid));
     133      }
     134
     135      if (bestValidInPop != null) {
     136        var bestValidSolution = (VRPSolution)bestValidSolutionResult.Value;
     137        if (bestValidSolution == null || bestValidInPop.Eval.Quality < bestValidSolution.Evaluation.Quality) {
     138          var best = new VRPSolution(ProblemInstance,
     139            (IVRPEncodedSolution)solutionContexts[bestValidInPop.index].EncodedSolution.Clone(),
     140            (VRPEvaluation)bestValidInPop.Eval.Clone());
     141          bestValidSolutionResult.Value = best;
     142        };
     143      }
    100144    }
    101145
     
    127171      try {
    128172        //call evaluator
    129         BestKnownQuality = ProblemInstance.Evaluate(BestKnownSolution.Solution).Quality;
    130         BestKnownSolution.Quality = new DoubleValue(BestKnownQuality);
     173        var evaluation = ProblemInstance.Evaluate(BestKnownSolution.Solution);
     174        BestKnownQuality = evaluation.Quality;
     175        BestKnownSolution.Evaluation = evaluation;
    131176      } catch {
    132177        BestKnownQuality = double.NaN;
     
    169214      Operators.Add(new QualitySimilarityCalculator());
    170215      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
    171       Operators.AddRange(ProblemInstance.Operators.OfType<IAnalyzer>());
     216      //Operators.AddRange(ProblemInstance.Operators.OfType<IAnalyzer>());
    172217    }
    173218
     
    241286        ErrorHandling.ShowErrorDialog(new Exception("The optimal solution does not seem to correspond with the problem data"));
    242287      else {
    243         VRPSolution solution = new VRPSolution(ProblemInstance, encoding, new DoubleValue(0));
     288        VRPSolution solution = new VRPSolution(ProblemInstance, encoding, ProblemInstance.Evaluate(encoding));
    244289        BestKnownSolutionParameter.Value = solution;
    245290      }
     
    269314
    270315      if (instance.BestKnownSolution != null) {
    271         VRPSolution solution = new VRPSolution(ProblemInstance, instance.BestKnownSolution, new DoubleValue(0));
     316        VRPSolution solution = new VRPSolution(ProblemInstance, instance.BestKnownSolution, ProblemInstance.Evaluate(instance.BestKnownSolution));
    272317        BestKnownSolution = solution;
    273318      }
Note: See TracChangeset for help on using the changeset viewer.