Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/15/20 13:53:11 (4 years ago)
Author:
mkommend
Message:

#2971: Added first draft of results implementation and problem adaptation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/VehicleRoutingProblem.cs

    r17718 r17745  
    105105    }
    106106
    107     public override void Analyze(ISingleObjectiveSolutionContext<IVRPEncodedSolution>[] solutionContexts, ResultCollection results, IRandom random) {
    108       base.Analyze(solutionContexts, results, random);
    109       var evaluations = solutionContexts.Select(x => (VRPEvaluation)x.EvaluationResult);
    110 
    111       var bestInPop = evaluations.Select((x, index) => new { index, Eval = x }).OrderBy(x => x.Eval.Quality).First();
    112       IResult bestSolutionResult;
    113       if (!results.TryGetValue("Best VRP Solution", out bestSolutionResult) || !(bestSolutionResult.Value is VRPSolution)) {
    114         var best = new VRPSolution(ProblemInstance, solutionContexts[bestInPop.index].EncodedSolution, (VRPEvaluation)bestInPop.Eval.Clone());
    115         if (bestSolutionResult != null)
    116           bestSolutionResult.Value = best;
    117         else results.Add(bestSolutionResult = new Result("Best VRP Solution", best));
    118       }
    119 
    120       var bestSolution = (VRPSolution)bestSolutionResult.Value;
    121       if (bestSolution == null || bestInPop.Eval.Quality < bestSolution.Evaluation.Quality) {
    122         var best = new VRPSolution(ProblemInstance,
    123           (IVRPEncodedSolution)solutionContexts[bestInPop.index].EncodedSolution.Clone(),
    124           (VRPEvaluation)bestInPop.Eval.Clone());
    125         bestSolutionResult.Value = best;
    126       };
    127 
    128       var bestValidInPop = evaluations.Select((x, index) => new { index, Eval = x }).Where(x => x.Eval.IsFeasible).OrderBy(x => x.Eval.Quality).FirstOrDefault();
    129       IResult bestValidSolutionResult;
    130       if (!results.TryGetValue("Best Valid VRP Solution", out bestValidSolutionResult) || !(bestValidSolutionResult.Value is VRPSolution)) {
    131         var bestValid = new VRPSolution(ProblemInstance, solutionContexts[bestValidInPop.index].EncodedSolution, (VRPEvaluation)bestValidInPop.Eval.Clone());
    132         if (bestValidSolutionResult != null)
    133           bestValidSolutionResult.Value = bestValid;
    134         else results.Add(bestValidSolutionResult = new Result("Best Valid VRP Solution", bestValid));
    135       }
    136 
    137       if (bestValidInPop != null) {
    138         var bestValidSolution = (VRPSolution)bestValidSolutionResult.Value;
    139         if (bestValidSolution == null || bestValidInPop.Eval.Quality < bestValidSolution.Evaluation.Quality) {
    140           var best = new VRPSolution(ProblemInstance,
    141             (IVRPEncodedSolution)solutionContexts[bestValidInPop.index].EncodedSolution.Clone(),
    142             (VRPEvaluation)bestValidInPop.Eval.Clone());
    143           bestValidSolutionResult.Value = best;
    144         };
    145       }
     107    public override void Analyze(ISingleObjectiveSolutionContext<IVRPEncodedSolution>[] solutionContexts, IRandom random) {
     108      base.Analyze(solutionContexts, random);
     109
     110      //TODO reimplement using results directly
     111
     112      //var evaluations = solutionContexts.Select(x => (VRPEvaluation)x.EvaluationResult);
     113
     114      //var bestInPop = evaluations.Select((x, index) => new { index, Eval = x }).OrderBy(x => x.Eval.Quality).First();
     115      //IResult bestSolutionResult;
     116      //if (!results.TryGetValue("Best VRP Solution", out bestSolutionResult) || !(bestSolutionResult.Value is VRPSolution)) {
     117      //  var best = new VRPSolution(ProblemInstance, solutionContexts[bestInPop.index].EncodedSolution, (VRPEvaluation)bestInPop.Eval.Clone());
     118      //  if (bestSolutionResult != null)
     119      //    bestSolutionResult.Value = best;
     120      //  else results.Add(bestSolutionResult = new Result("Best VRP Solution", best));
     121      //}
     122
     123      //var bestSolution = (VRPSolution)bestSolutionResult.Value;
     124      //if (bestSolution == null || bestInPop.Eval.Quality < bestSolution.Evaluation.Quality) {
     125      //  var best = new VRPSolution(ProblemInstance,
     126      //    (IVRPEncodedSolution)solutionContexts[bestInPop.index].EncodedSolution.Clone(),
     127      //    (VRPEvaluation)bestInPop.Eval.Clone());
     128      //  bestSolutionResult.Value = best;
     129      //};
     130
     131      //var bestValidInPop = evaluations.Select((x, index) => new { index, Eval = x }).Where(x => x.Eval.IsFeasible).OrderBy(x => x.Eval.Quality).FirstOrDefault();
     132      //IResult bestValidSolutionResult;
     133      //if (!results.TryGetValue("Best Valid VRP Solution", out bestValidSolutionResult) || !(bestValidSolutionResult.Value is VRPSolution)) {
     134      //  var bestValid = new VRPSolution(ProblemInstance, solutionContexts[bestValidInPop.index].EncodedSolution, (VRPEvaluation)bestValidInPop.Eval.Clone());
     135      //  if (bestValidSolutionResult != null)
     136      //    bestValidSolutionResult.Value = bestValid;
     137      //  else results.Add(bestValidSolutionResult = new Result("Best Valid VRP Solution", bestValid));
     138      //}
     139
     140      //if (bestValidInPop != null) {
     141      //  var bestValidSolution = (VRPSolution)bestValidSolutionResult.Value;
     142      //  if (bestValidSolution == null || bestValidInPop.Eval.Quality < bestValidSolution.Evaluation.Quality) {
     143      //    var best = new VRPSolution(ProblemInstance,
     144      //      (IVRPEncodedSolution)solutionContexts[bestValidInPop.index].EncodedSolution.Clone(),
     145      //      (VRPEvaluation)bestValidInPop.Eval.Clone());
     146      //    bestValidSolutionResult.Value = best;
     147      //  };
     148      //}
    146149    }
    147150
     
    164167    }
    165168
    166     void BestKnownSolutionParameter_ValueChanged(object sender, EventArgs e) {
     169    private void BestKnownSolutionParameter_ValueChanged(object sender, EventArgs e) {
    167170      EvalBestKnownSolution();
    168171    }
    169172
    170     void ProblemInstance_EvaluationChanged(object sender, EventArgs e) {
     173    private void ProblemInstance_EvaluationChanged(object sender, EventArgs e) {
    171174      BestKnownQuality = double.NaN;
    172175      if (BestKnownSolution != null) {
     
    191194    }
    192195
    193     void ProblemInstanceParameter_ValueChanged(object sender, EventArgs e) {
     196    private void ProblemInstanceParameter_ValueChanged(object sender, EventArgs e) {
    194197      InitializeOperators();
    195198      AttachProblemInstanceEventHandlers();
     
    219222        .Where(x => operatorTypes.Add(x)).Select(t => (IOperator)Activator.CreateInstance(t)).ToList();
    220223      newOps.AddRange(ProblemInstance.FilterOperators(analyzers));
    221      
     224
    222225      Operators.Replace(newOps);
    223226    }
Note: See TracChangeset for help on using the changeset viewer.