Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/13/11 16:00:19 (13 years ago)
Author:
svonolfe
Message:

Added support for incremental evaluation to improve performance (#1177)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/SingleDepotVRP/CVRP/CVRPTW/CVRPPDTW/CVRPPDTWEvaluator.cs

    r6710 r6752  
    4848
    4949    protected override void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour) {
     50      TourInsertionInfo tourInfo = new TourInsertionInfo();
     51      eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
     52
    5053      IHomogenousCapacitatedProblemInstance cvrpInstance = instance as IHomogenousCapacitatedProblemInstance;
    5154      DoubleArray demand = instance.Demand;
     
    8689        distance += currentDistace;
    8790
     91        double arrivalTime = time;
     92
    8893        //check if it was serviced on time
    8994        if (time > dueTime[end])
     
    9499        if (time < readyTime[end])
    95100          currentWaitingTime = readyTime[end] - time;
     101
     102        double waitTime = readyTime[end] - time;
     103
    96104        waitingTime += currentWaitingTime;
    97105        time += currentWaitingTime;
     106
     107        double spareTime = dueTime[end] - time;
    98108
    99109        //service
     
    103113
    104114        //Pickup / deliver
     115        double arrivalSpareCapacity = capacity - currentLoad;
     116
    105117        bool validPickupDelivery =
    106118          validPickupDelivery =
     
    110122        if (validPickupDelivery) {
    111123          currentLoad += demand[end];
    112 
    113           if (currentLoad > capacity)
    114             overweight += currentLoad - capacity;
    115124        } else {
    116125          pickupViolations++;
    117126        }
     127
     128        if (currentLoad > capacity)
     129          overweight += currentLoad - capacity;
     130
     131        double spareCapacity = capacity - currentLoad;
     132        CVRPPDTWInsertionInfo stopInfo = new CVRPPDTWInsertionInfo(start, end, spareCapacity, arrivalTime, time, spareTime, waitTime, new List<int>(stops.Keys), arrivalSpareCapacity);
     133        tourInfo.AddStopInsertionInfo(stopInfo);
    118134
    119135        stops.Add(end, true);
     
    126142
    127143      (eval as CVRPEvaluation).Overload += overweight;
     144      double tourPenalty = 0;
    128145      double penalty = overweight * cvrpInstance.OverloadPenalty.Value;
    129146      eval.Penalty += penalty;
    130147      eval.Quality += penalty;
     148      tourPenalty += penalty;
    131149
    132150      (eval as CVRPTWEvaluation).Tardiness += tardiness;
     
    136154      eval.Penalty += penalty;
    137155      eval.Quality += penalty;
     156      tourPenalty += penalty;
    138157
    139158      (eval as CVRPPDTWEvaluation).PickupViolations += pickupViolations;
    140159      penalty = pickupViolations * pdp.PickupViolationPenalty.Value;
    141160      eval.Penalty += penalty;
     161      tourPenalty += penalty;
    142162
    143163      eval.Quality += penalty;
    144164      eval.Quality += time * vrptw.TimeFactor.Value;
     165      tourInfo.Penalty = tourPenalty;
     166    }
     167
     168    protected override double GetTourInsertionCosts(IVRPProblemInstance instance, TourInsertionInfo tourInsertionInfo, int index, int customer,
     169      out bool feasible) {
     170      CVRPPDTWInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPPDTWInsertionInfo;
     171
     172      double costs = 0;
     173      feasible = tourInsertionInfo.Penalty < double.Epsilon;
     174      bool tourFeasible = true;
     175
     176      ICapacitatedProblemInstance cvrp = instance as ICapacitatedProblemInstance;
     177      double overloadPenalty = cvrp.OverloadPenalty.Value;
     178
     179      ITimeWindowedProblemInstance vrptw = instance as ITimeWindowedProblemInstance;
     180      DoubleArray dueTime = vrptw.DueTime;
     181      DoubleArray readyTime = vrptw.ReadyTime;
     182      DoubleArray serviceTimes = vrptw.ServiceTime;
     183      double tardinessPenalty = vrptw.TardinessPenalty.Value;
     184
     185      IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
     186      IntArray pickupDeliveryLocation = pdp.PickupDeliveryLocation;
     187      double pickupPenalty = pdp.PickupViolationPenalty.Value;
     188
     189      double distance = instance.GetDistance(insertionInfo.Start, insertionInfo.End);
     190      double newDistance =
     191        instance.GetDistance(insertionInfo.Start, customer) +
     192        instance.GetDistance(customer, insertionInfo.End);
     193      costs += instance.DistanceFactor.Value * (newDistance - distance);
     194
     195      double demand = instance.Demand[customer];
     196      if (demand > insertionInfo.ArrivalSpareCapacity) {
     197        tourFeasible = feasible = false;
     198        if (insertionInfo.ArrivalSpareCapacity >= 0)
     199          costs += (demand - insertionInfo.ArrivalSpareCapacity) * overloadPenalty;
     200        else
     201          costs += demand * overloadPenalty;
     202      }
     203      int destination = pickupDeliveryLocation[customer];
     204
     205      bool validPickup = true;
     206      if (demand < 0 && !insertionInfo.Visited.Contains(destination)) {
     207        tourFeasible = feasible = false;
     208        validPickup = false;
     209        costs += pickupPenalty;
     210      }
     211
     212      double time = 0;
     213      double tardiness = 0;
     214
     215      if (index > 0)
     216        time = (tourInsertionInfo.GetStopInsertionInfo(index - 1) as CVRPTWInsertionInfo).LeaveTime;
     217      time += instance.GetDistance(insertionInfo.Start, customer);
     218      if (time > dueTime[customer]) {
     219        tardiness += time - dueTime[customer];
     220      }
     221      if (time < readyTime[customer])
     222        time += readyTime[customer] - time;
     223      time += serviceTimes[customer];
     224      time += instance.GetDistance(customer, insertionInfo.End);
     225
     226      double additionalTime = time - (tourInsertionInfo.GetStopInsertionInfo(index) as CVRPTWInsertionInfo).ArrivalTime;
     227      for (int i = index; i < tourInsertionInfo.GetStopCount(); i++) {
     228        CVRPTWInsertionInfo nextStop = tourInsertionInfo.GetStopInsertionInfo(i) as CVRPTWInsertionInfo;
     229
     230        if (demand >= 0) {
     231          if (nextStop.End == destination) {
     232            demand = 0;
     233            costs -= pickupPenalty;
     234            if (tourInsertionInfo.Penalty == pickupPenalty && tourFeasible)
     235              feasible = true;
     236          } else if (nextStop.SpareCapacity < 0) {
     237            costs += demand * overloadPenalty;
     238          } else if (nextStop.SpareCapacity < demand) {
     239            tourFeasible = feasible = false;
     240            costs += (demand - nextStop.SpareCapacity) * overloadPenalty;
     241          }
     242        } else if (validPickup) {
     243          if (nextStop.SpareCapacity < 0) {
     244            costs += Math.Max(demand, nextStop.SpareCapacity) * overloadPenalty;
     245          }
     246        }
     247
     248        if (additionalTime < 0) {
     249          //arrive earlier than before
     250          //wait probably
     251          if (nextStop.WaitingTime < 0) {
     252            double wait = nextStop.WaitingTime - additionalTime;
     253            if (wait > 0)
     254              additionalTime += wait;
     255          } else {
     256            additionalTime = 0;
     257          }
     258
     259          //check due date, decrease tardiness
     260          if (nextStop.SpareTime < 0) {
     261            costs += Math.Max(nextStop.SpareTime, additionalTime) * tardinessPenalty;
     262          }
     263        } else {
     264          //arrive later than before, probably don't have to wait
     265          if (nextStop.WaitingTime > 0) {
     266            additionalTime -= Math.Min(additionalTime, nextStop.WaitingTime);
     267          }
     268
     269          //check due date
     270          if (nextStop.SpareTime > 0) {
     271            double spare = nextStop.SpareTime - additionalTime;
     272            if (spare < 0)
     273              tardiness += -spare;
     274          } else {
     275            tardiness += additionalTime;
     276          }
     277        }
     278      }
     279
     280      costs += additionalTime * vrptw.TimeFactor.Value;
     281
     282      if (tardiness > 0) {
     283        tourFeasible = feasible = false;
     284      }
     285
     286      costs += tardiness * tardinessPenalty;
     287
     288      return costs;
    145289    }
    146290
Note: See TracChangeset for help on using the changeset viewer.