Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/03/20 18:06:16 (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/ProblemInstances/VRPProblemInstance.cs

    r17698 r17711  
    2828using HeuristicLab.Data;
    2929using HeuristicLab.Parameters;
    30 using HeuristicLab.Problems.VehicleRouting.Encodings.General;
    3130using HeuristicLab.Problems.VehicleRouting.Interfaces;
    3231
     
    3534  [StorableType("9A6CCE89-A4B6-4FA3-A150-181FC315B713")]
    3635  public abstract class VRPProblemInstance : ParameterizedNamedItem, IVRPProblemInstance, IStatefulItem {
    37     IVRPEvaluator moveEvaluator;
    38 
    39     private object locker = new object();
    40 
    41     public IVRPEvaluator MoveEvaluator {
    42       get {
    43         lock (locker) {
    44           if (evaluator == null)
    45             return null;
    46           else {
    47             if (moveEvaluator == null) {
    48               moveEvaluator = evaluator.Clone() as IVRPEvaluator;
    49 
    50               foreach (IParameter parameter in moveEvaluator.Parameters) {
    51                 if (parameter is ILookupParameter
    52                   && parameter != moveEvaluator.ProblemInstanceParameter
    53                   && parameter != moveEvaluator.VRPToursParameter) {
    54                   (parameter as ILookupParameter).ActualName =
    55                     VRPMoveEvaluator.MovePrefix +
    56                     (parameter as ILookupParameter).ActualName;
    57                 }
    58               }
    59             }
    60 
    61             return moveEvaluator;
    62           }
    63         }
    64       }
    65     }
    66 
    67     protected abstract IEnumerable<IOperator> GetOperators();
    68     protected abstract IEnumerable<IOperator> GetAnalyzers();
    69 
    70     public IEnumerable<IOperator> Operators {
    71       get {
    72         return GetOperators().Union(GetAnalyzers());
    73       }
    74     }
    7536
    7637    protected ValueParameter<DoubleMatrix> CoordinatesParameter {
     
    13192    }
    13293
     94    public virtual IEnumerable<IOperator> FilterOperators(IEnumerable<IOperator> operators) {
     95      return operators.Where(x => x is IVRPOperator);
     96    }
     97
    13398    protected virtual double CalculateDistance(int start, int end) {
    134       double distance = 0.0;
    135 
    136       distance =
     99      var distance =
    137100          Math.Sqrt(
    138101            Math.Pow(Coordinates[start, 0] - Coordinates[end, 0], 2) +
     
    172135    //cache for performance improvement
    173136    private DoubleMatrix distanceMatrix = null;
    174     private IVRPEvaluator evaluator = null;
    175 
    176     public IVRPEvaluator SolutionEvaluator {
    177       get {
    178         return evaluator;
    179       }
    180       set {
    181         lock (locker) {
    182           moveEvaluator = null;
    183           evaluator = value;
    184           EvalBestKnownSolution();
    185         }
    186       }
    187     }
     137   
    188138
    189139    public virtual double GetDistance(int start, int end, IVRPEncodedSolution solution) {
     
    208158    }
    209159
    210     public bool Feasible(IVRPEncodedSolution solution) {
    211       return evaluator.Feasible(
    212         evaluator.Evaluate(
    213           this, solution));
    214     }
    215 
    216     public bool TourFeasible(Tour tour, IVRPEncodedSolution solution) {
    217       return evaluator.Feasible(
    218         evaluator.EvaluateTour(
    219         this, tour, solution));
     160    protected virtual VRPEvaluation CreateTourEvaluation() {
     161      return new VRPEvaluation();
    220162    }
    221163
    222164    public VRPEvaluation Evaluate(IVRPEncodedSolution solution) {
    223       return evaluator.Evaluate(this, solution);
     165      VRPEvaluation evaluation = CreateTourEvaluation();
     166      evaluation.IsFeasible = true;
     167      foreach (Tour tour in solution.GetTours()) {
     168        EvaluateTour(evaluation, tour, solution);
     169      }
     170      return evaluation;
    224171    }
    225172
    226173    public VRPEvaluation EvaluateTour(Tour tour, IVRPEncodedSolution solution) {
    227       return evaluator.EvaluateTour(this, tour, solution);
    228     }
    229 
    230     public bool Feasible(VRPEvaluation eval) {
    231       return evaluator.Feasible(eval);
     174      VRPEvaluation evaluation = CreateTourEvaluation();
     175      evaluation.IsFeasible = true;
     176      EvaluateTour(evaluation, tour, solution);
     177      return evaluation;
    232178    }
    233179
    234180    public double GetInsertionCosts(VRPEvaluation eval, IVRPEncodedSolution solution, int customer, int tour, int index, out bool feasible) {
    235       return evaluator.GetInsertionCosts(this, solution, eval, customer, tour, index, out feasible);
    236     }
     181      bool tourFeasible;
     182      double costs = GetTourInsertionCosts(
     183        solution,
     184        eval.InsertionInfo.GetTourInsertionInfo(tour),
     185        index,
     186        customer, out tourFeasible);
     187
     188      feasible = tourFeasible;
     189
     190      return costs;
     191    }
     192    protected abstract double GetTourInsertionCosts(IVRPEncodedSolution solution, TourInsertionInfo tourInsertionInfo, int index, int customer, out bool feasible);
     193
     194    protected abstract void EvaluateTour(VRPEvaluation eval, Tour tour, IVRPEncodedSolution solution);
    237195
    238196
     
    241199    protected void EvalBestKnownSolution() {
    242200      EventHandler tmp = EvaluationChanged;
    243       if (tmp != null)
    244         tmp(this, null);
    245     }
    246 
    247     protected abstract IVRPEvaluator Evaluator { get; }
    248     protected abstract IVRPCreator Creator { get; }
     201      if (tmp != null) tmp(this, null);
     202    }
    249203
    250204    [StorableConstructor]
     
    262216      Parameters.Add(new ValueParameter<DoubleValue>("EvalDistanceFactor", "The distance factor considered in the evaluation.", new DoubleValue(1)));
    263217
    264       evaluator = Evaluator;
    265218      AttachEventHandlers();
    266219    }
     
    268221    protected VRPProblemInstance(VRPProblemInstance original, Cloner cloner)
    269222      : base(original, cloner) {
    270       evaluator = Evaluator;
    271223      AttachEventHandlers();
    272224    }
     
    274226    [StorableHook(HookType.AfterDeserialization)]
    275227    private void AfterDeserialization() {
    276       evaluator = Evaluator;
    277228      AttachEventHandlers();
    278229    }
Note: See TracChangeset for help on using the changeset viewer.