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/SingleDepotVRP/CVRP/CVRPTW/CVRPTWProblemInstance.cs

    r17226 r17711  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HEAL.Attic;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
    2728using HeuristicLab.Data;
    28 using HeuristicLab.Optimization;
    2929using HeuristicLab.Parameters;
    30 using HEAL.Attic;
    31 using HeuristicLab.PluginInfrastructure;
    3230using HeuristicLab.Problems.VehicleRouting.Interfaces;
    3331using HeuristicLab.Problems.VehicleRouting.Variants;
     
    8987    }
    9088
    91     protected override IEnumerable<IOperator> GetOperators() {
    92       return base.GetOperators()
    93         .Where(o => o is ITimeWindowedOperator).Cast<IOperator>();
    94     }
    95 
    96     protected override IEnumerable<IOperator> GetAnalyzers() {
    97       return ApplicationManager.Manager.GetInstances<ITimeWindowedOperator>()
    98         .Where(o => o is IAnalyzer)
    99         .Cast<IOperator>().Union(base.GetAnalyzers());
    100     }
    101 
    102     protected override IVRPEvaluator Evaluator {
    103       get {
    104         return new CVRPTWEvaluator();
    105       }
     89    public override IEnumerable<IOperator> FilterOperators(IEnumerable<IOperator> operators) {
     90      return base.FilterOperators(operators).Where(x => x is ITimeWindowedOperator);
     91    }
     92
     93    protected override VRPEvaluation CreateTourEvaluation() {
     94      return new CVRPTWEvaluation();
     95    }
     96
     97    protected override void EvaluateTour(VRPEvaluation eval, Tour tour, IVRPEncodedSolution solution) {
     98      TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour)));
     99      eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
     100      double originalQuality = eval.Quality;
     101
     102      double time = 0.0;
     103      double waitingTime = 0.0;
     104      double serviceTime = 0.0;
     105      double tardiness = 0.0;
     106      double delivered = 0.0;
     107      double overweight = 0.0;
     108      double distance = 0.0;
     109
     110      double capacity = Capacity.Value;
     111      for (int i = 0; i <= tour.Stops.Count; i++) {
     112        int end = 0;
     113        if (i < tour.Stops.Count)
     114          end = tour.Stops[i];
     115
     116        delivered += Demand[end];
     117      }
     118
     119      double spareCapacity = capacity - delivered;
     120
     121      double tourStartTime = ReadyTime[0];
     122      time = tourStartTime;
     123
     124      //simulate a tour, start and end at depot
     125      for (int i = 0; i <= tour.Stops.Count; i++) {
     126        int start = 0;
     127        if (i > 0)
     128          start = tour.Stops[i - 1];
     129        int end = 0;
     130        if (i < tour.Stops.Count)
     131          end = tour.Stops[i];
     132
     133        //drive there
     134        double currentDistace = GetDistance(start, end, solution);
     135        time += currentDistace;
     136        distance += currentDistace;
     137
     138        double arrivalTime = time;
     139
     140        //check if it was serviced on time
     141        if (time > DueTime[end]) {
     142          tardiness += time - DueTime[end];
     143        }
     144
     145        //wait
     146        double currentWaitingTime = 0.0;
     147        if (time < ReadyTime[end])
     148          currentWaitingTime = ReadyTime[end] - time;
     149
     150        double waitTime = ReadyTime[end] - time;
     151
     152        waitingTime += currentWaitingTime;
     153        time += currentWaitingTime;
     154
     155        double spareTime = DueTime[end] - time;
     156
     157        //service
     158        double currentServiceTime = ServiceTime[end];
     159        serviceTime += currentServiceTime;
     160        time += currentServiceTime;
     161
     162        CVRPTWInsertionInfo stopInfo = new CVRPTWInsertionInfo(start, end, spareCapacity, tourStartTime, arrivalTime, time, spareTime, waitTime);
     163        tourInfo.AddStopInsertionInfo(stopInfo);
     164      }
     165
     166      eval.Quality += FleetUsageFactor.Value;
     167      eval.Quality += DistanceFactor.Value * distance;
     168      eval.Distance += distance;
     169      eval.VehicleUtilization += 1;
     170
     171      if (delivered > capacity) {
     172        overweight = delivered - capacity;
     173      }
     174
     175      (eval as CVRPEvaluation).Overload += overweight;
     176      double tourPenalty = 0;
     177      double penalty = overweight * OverloadPenalty.Value;
     178      eval.Penalty += penalty;
     179      eval.Quality += penalty;
     180      tourPenalty += penalty;
     181
     182      (eval as CVRPTWEvaluation).Tardiness += tardiness;
     183      (eval as CVRPTWEvaluation).TravelTime += time;
     184
     185      penalty = tardiness * TardinessPenalty.Value;
     186      eval.Penalty += penalty;
     187      eval.Quality += penalty;
     188      tourPenalty += penalty;
     189      eval.Quality += time * TimeFactor.Value;
     190      tourInfo.Penalty = tourPenalty;
     191      tourInfo.Quality = eval.Quality - originalQuality;
     192
     193      eval.IsFeasible = overweight == 0 && tardiness == 0;
     194    }
     195
     196    protected override double GetTourInsertionCosts(IVRPEncodedSolution solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
     197      out bool feasible) {
     198      CVRPTWInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPTWInsertionInfo;
     199
     200      double costs = 0;
     201      feasible = tourInsertionInfo.Penalty < double.Epsilon;
     202
     203      double overloadPenalty = OverloadPenalty.Value;
     204      double tardinessPenalty = TardinessPenalty.Value;
     205
     206      double distance = GetDistance(insertionInfo.Start, insertionInfo.End, solution);
     207      double newDistance =
     208        GetDistance(insertionInfo.Start, customer, solution) +
     209        GetDistance(customer, insertionInfo.End, solution);
     210      costs += DistanceFactor.Value * (newDistance - distance);
     211
     212      double demand = Demand[customer];
     213      if (demand > insertionInfo.SpareCapacity) {
     214        feasible = false;
     215        if (insertionInfo.SpareCapacity >= 0)
     216          costs += (demand - insertionInfo.SpareCapacity) * overloadPenalty;
     217        else
     218          costs += demand * overloadPenalty;
     219      }
     220
     221      double time = 0;
     222      double tardiness = 0;
     223
     224      if (index > 0)
     225        time = (tourInsertionInfo.GetStopInsertionInfo(index - 1) as CVRPTWInsertionInfo).LeaveTime;
     226      else
     227        time = insertionInfo.TourStartTime;
     228
     229      time += GetDistance(insertionInfo.Start, customer, solution);
     230      if (time > DueTime[customer]) {
     231        tardiness += time - DueTime[customer];
     232      }
     233      if (time < ReadyTime[customer])
     234        time += ReadyTime[customer] - time;
     235      time += ServiceTime[customer];
     236      time += GetDistance(customer, insertionInfo.End, solution);
     237
     238      double additionalTime = time - (tourInsertionInfo.GetStopInsertionInfo(index) as CVRPTWInsertionInfo).ArrivalTime;
     239      for (int i = index; i < tourInsertionInfo.GetStopCount(); i++) {
     240        CVRPTWInsertionInfo nextStop = tourInsertionInfo.GetStopInsertionInfo(i) as CVRPTWInsertionInfo;
     241
     242        if (additionalTime < 0) {
     243          //arrive earlier than before
     244          //wait probably
     245          if (nextStop.WaitingTime < 0) {
     246            double wait = nextStop.WaitingTime - additionalTime;
     247            if (wait > 0)
     248              additionalTime += wait;
     249          } else {
     250            additionalTime = 0;
     251          }
     252
     253          //check due date, decrease tardiness
     254          if (nextStop.SpareTime < 0) {
     255            costs += Math.Max(nextStop.SpareTime, additionalTime) * tardinessPenalty;
     256          }
     257        } else {
     258          //arrive later than before, probably don't have to wait
     259          if (nextStop.WaitingTime > 0) {
     260            additionalTime -= Math.Min(additionalTime, nextStop.WaitingTime);
     261          }
     262
     263          //check due date
     264          if (nextStop.SpareTime > 0) {
     265            double spare = nextStop.SpareTime - additionalTime;
     266            if (spare < 0)
     267              tardiness += -spare;
     268          } else {
     269            tardiness += additionalTime;
     270          }
     271        }
     272      }
     273
     274      costs += additionalTime * TimeFactor.Value;
     275
     276      if (tardiness > 0) {
     277        feasible = false;
     278      }
     279
     280      costs += tardiness * tardinessPenalty;
     281
     282      return costs;
    106283    }
    107284
Note: See TracChangeset for help on using the changeset viewer.