当前位置: 首页>>代码示例>>C#>>正文


C# IVRPProblemInstance.GetDemand方法代码示例

本文整理汇总了C#中IVRPProblemInstance.GetDemand方法的典型用法代码示例。如果您正苦于以下问题:C# IVRPProblemInstance.GetDemand方法的具体用法?C# IVRPProblemInstance.GetDemand怎么用?C# IVRPProblemInstance.GetDemand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IVRPProblemInstance的用法示例。


在下文中一共展示了IVRPProblemInstance.GetDemand方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: EvaluateTour

    protected override void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour, IVRPEncoding solution) {
      TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour)));
      eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
      double originalQuality = eval.Quality;

      IHeterogenousCapacitatedProblemInstance cvrpInstance = instance as IHeterogenousCapacitatedProblemInstance;

      double delivered = 0.0;
      double overweight = 0.0;
      double distance = 0.0;

      int tourIndex = solution.GetTourIndex(tour);
      int vehicle = solution.GetVehicleAssignment(tourIndex);

      double capacity = cvrpInstance.Capacity[vehicle];
      for (int i = 0; i < tour.Stops.Count; i++) {
        delivered += instance.GetDemand(tour.Stops[i]);
      }

      double spareCapacity = capacity - delivered;

      //simulate a tour, start and end at depot
      for (int i = 0; i <= tour.Stops.Count; i++) {
        int start = 0;
        if (i > 0)
          start = tour.Stops[i - 1];
        int end = 0;
        if (i < tour.Stops.Count)
          end = tour.Stops[i];

        //drive there
        double currentDistace = instance.GetDistance(start, end, solution);
        distance += currentDistace;

        CVRPInsertionInfo stopInfo = new CVRPInsertionInfo(start, end, spareCapacity);
        tourInfo.AddStopInsertionInfo(stopInfo);
      }

      eval.Quality += instance.FleetUsageFactor.Value;
      eval.Quality += instance.DistanceFactor.Value * distance;

      eval.Distance += distance;
      eval.VehicleUtilization += 1;

      if (delivered > capacity) {
        overweight = delivered - capacity;
      }

      (eval as CVRPEvaluation).Overload += overweight;
      double penalty = overweight * cvrpInstance.OverloadPenalty.Value;
      eval.Penalty += penalty;
      eval.Quality += penalty;
      tourInfo.Penalty = penalty;
      tourInfo.Quality = eval.Quality - originalQuality;
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:55,代码来源:MDCVRPEvaluator.cs

示例2: EvaluateTour

    protected override void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour, IVRPEncoding solution) {
      TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour)));
      eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
      double originalQuality = eval.Quality;

      IHeterogenousCapacitatedProblemInstance cvrpInstance = instance as IHeterogenousCapacitatedProblemInstance;
      DoubleArray demand = instance.Demand;

      ITimeWindowedProblemInstance vrptw = instance as ITimeWindowedProblemInstance;
      DoubleArray dueTime = vrptw.DueTime;
      DoubleArray readyTime = vrptw.ReadyTime;
      DoubleArray serviceTimes = vrptw.ServiceTime;

      IMultiDepotProblemInstance mdp = instance as IMultiDepotProblemInstance;
      IntArray vehicleAssignment = mdp.VehicleDepotAssignment;
      int depots = mdp.Depots.Value;

      double time = 0.0;
      double waitingTime = 0.0;
      double serviceTime = 0.0;
      double tardiness = 0.0;
      double delivered = 0.0;
      double overweight = 0.0;
      double distance = 0.0;

      int tourIndex = solution.GetTourIndex(tour);
      int vehicle = solution.GetVehicleAssignment(tourIndex);
      int depot = vehicleAssignment[vehicle];

      double capacity = cvrpInstance.Capacity[vehicle];
      for (int i = 0; i < tour.Stops.Count; i++) {
        delivered += instance.GetDemand(tour.Stops[i]);
      }

      double spareCapacity = capacity - delivered;

      double tourStartTime = vrptw.ReadyTime[depot];
      time = tourStartTime;

      //simulate a tour, start and end at depot
      for (int i = 0; i <= tour.Stops.Count; i++) {
        int start = 0;
        if (i > 0)
          start = tour.Stops[i - 1];
        int end = 0;
        if (i < tour.Stops.Count)
          end = tour.Stops[i];

        //drive there
        double currentDistace = vrptw.GetDistance(start, end, solution);
        time += currentDistace;
        distance += currentDistace;

        double arrivalTime = time;

        int endIndex;
        if (end == 0)
          endIndex = depot;
        else
          endIndex = end + depots - 1;

        //check if it was serviced on time
        if (time > dueTime[endIndex])
          tardiness += time - dueTime[endIndex];

        //wait
        double currentWaitingTime = 0.0;
        if (time < readyTime[endIndex])
          currentWaitingTime = readyTime[endIndex] - time;

        double waitTime = readyTime[endIndex] - time;

        waitingTime += currentWaitingTime;
        time += currentWaitingTime;

        double spareTime = dueTime[endIndex] - time;

        //service
        double currentServiceTime = 0;
        if (end > 0)
          currentServiceTime = serviceTimes[end - 1];
        serviceTime += currentServiceTime;
        time += currentServiceTime;

        CVRPTWInsertionInfo stopInfo = new CVRPTWInsertionInfo(start, end, spareCapacity, tourStartTime, arrivalTime, time, spareTime, waitTime);
        tourInfo.AddStopInsertionInfo(stopInfo);
      }

      eval.Quality += instance.FleetUsageFactor.Value;
      eval.Quality += instance.DistanceFactor.Value * distance;
      eval.Distance += distance;
      eval.VehicleUtilization += 1;

      if (delivered > capacity) {
        overweight = delivered - capacity;
      }

      (eval as CVRPEvaluation).Overload += overweight;
      double tourPenalty = 0;
      double penalty = overweight * cvrpInstance.OverloadPenalty.Value;
//.........这里部分代码省略.........
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:101,代码来源:MDCVRPTWEvaluator.cs

示例3: GetTourInsertionCosts

    protected override double GetTourInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
      out bool feasible) {
      CVRPTWInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPTWInsertionInfo;

      double costs = 0;
      feasible = tourInsertionInfo.Penalty < double.Epsilon;

      ICapacitatedProblemInstance cvrp = instance as ICapacitatedProblemInstance;
      double overloadPenalty = cvrp.OverloadPenalty.Value;

      ITimeWindowedProblemInstance vrptw = instance as ITimeWindowedProblemInstance;
      DoubleArray dueTime = vrptw.DueTime;
      DoubleArray readyTime = vrptw.ReadyTime;
      DoubleArray serviceTimes = vrptw.ServiceTime;
      double tardinessPenalty = vrptw.TardinessPenalty.Value;

      IMultiDepotProblemInstance mdp = instance as IMultiDepotProblemInstance;
      int depots = mdp.Depots.Value;

      double startDistance, endDistance;
      costs += instance.GetInsertionDistance(insertionInfo.Start, customer, insertionInfo.End, solution, out startDistance, out endDistance);

      double demand = instance.GetDemand(customer);
      if (demand > insertionInfo.SpareCapacity) {
        feasible = false;

        if (insertionInfo.SpareCapacity >= 0)
          costs += (demand - insertionInfo.SpareCapacity) * overloadPenalty;
        else
          costs += demand * overloadPenalty;
      }

      double time = 0;
      double tardiness = 0;

      if (index > 0)
        time = (tourInsertionInfo.GetStopInsertionInfo(index - 1) as CVRPTWInsertionInfo).LeaveTime;
      else
        time = insertionInfo.TourStartTime;

      time += startDistance;

      int customerIndex = customer + depots - 1;

      if (time > dueTime[customerIndex]) {
        tardiness += time - dueTime[customerIndex];
      }
      if (time < readyTime[customerIndex])
        time += readyTime[customerIndex] - time;
      time += serviceTimes[customer - 1];
      time += endDistance;

      double additionalTime = time - (tourInsertionInfo.GetStopInsertionInfo(index) as CVRPTWInsertionInfo).ArrivalTime;
      for (int i = index; i < tourInsertionInfo.GetStopCount(); i++) {
        CVRPTWInsertionInfo nextStop = tourInsertionInfo.GetStopInsertionInfo(i) as CVRPTWInsertionInfo;

        if (additionalTime < 0) {
          //arrive earlier than before
          //wait probably
          if (nextStop.WaitingTime < 0) {
            double wait = nextStop.WaitingTime - additionalTime;
            if (wait > 0)
              additionalTime += wait;
          } else {
            additionalTime = 0;
          }

          //check due date, decrease tardiness
          if (nextStop.SpareTime < 0) {
            costs += Math.Max(nextStop.SpareTime, additionalTime) * tardinessPenalty;
          }
        } else {
          //arrive later than before, probably don't have to wait
          if (nextStop.WaitingTime > 0) {
            additionalTime -= Math.Min(additionalTime, nextStop.WaitingTime);
          }

          //check due date
          if (nextStop.SpareTime > 0) {
            double spare = nextStop.SpareTime - additionalTime;
            if (spare < 0)
              tardiness += -spare;
          } else {
            tardiness += additionalTime;
          }
        }
      }

      costs += additionalTime * vrptw.TimeFactor.Value;

      if (tardiness > 0) {
        feasible = false;
      }

      costs += tardiness * tardinessPenalty;

      return costs;
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:98,代码来源:MDCVRPTWEvaluator.cs

示例4: CreateSolution

    public static PotvinEncoding CreateSolution(IVRPProblemInstance problemInstance, IRandom random,
      double alphaValue = 0.7, double betaValue = 0.1, double gammaValue = 0.2,
      double alphaVariance = 0.5, double betaVariance = 0.07, double gammaVariance = 0.14) {
      PotvinEncoding result = new PotvinEncoding(problemInstance);

      IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
      IMultiDepotProblemInstance mdp = problemInstance as IMultiDepotProblemInstance;

      double alpha, beta, gamma;
      alpha = N(alphaValue, Math.Sqrt(alphaVariance), random);
      beta = N(betaValue, Math.Sqrt(betaVariance), random);
      gamma = N(gammaValue, Math.Sqrt(gammaVariance), random);

      List<int> unroutedCustomers = new List<int>();
      for (int i = 1; i <= problemInstance.Cities.Value; i++) {
        if (pdp == null || (problemInstance.GetDemand(i) >= 0))
          unroutedCustomers.Add(i);
      }

      List<int> depots = new List<int>();
      if (mdp != null) {
        for (int i = 0; i < mdp.Depots.Value; i++) {
          depots.Add(i);
        }
      } else {
        depots.Add(0);
      }

      Dictionary<int, List<int>> vehicles = new Dictionary<int, List<int>>();
      foreach (int depot in depots) {
        vehicles[depot] = new List<int>();

        int vehicleCount = problemInstance.Vehicles.Value;
        if (mdp != null) {
          for (int vehicle = 0; vehicle < mdp.VehicleDepotAssignment.Length; vehicle++) {
            if (mdp.VehicleDepotAssignment[vehicle] == depot) {
              vehicles[depot].Add(vehicle);
            }
          }
        } else {
          for (int vehicle = 0; vehicle < vehicleCount; vehicle++) {
            vehicles[depot].Add(vehicle);
          }
        }
      }

      RemoveUnusedDepots(depots, vehicles);
      Dictionary<int, int> depotAssignment = new Dictionary<int, int>();

      unroutedCustomers = SortCustomers(
        problemInstance, unroutedCustomers, depots, depotAssignment,
        alpha, beta, gamma);

      /////////
      Tour tour = new Tour();
      result.Tours.Add(tour);
      int currentCustomer = unroutedCustomers[0];
      unroutedCustomers.RemoveAt(0);

      int currentDepot = depotAssignment[currentCustomer];
      int currentVehicle = vehicles[currentDepot][0];
      vehicles[currentDepot].RemoveAt(0);
      if (RemoveUnusedDepots(depots, vehicles)) {
        unroutedCustomers = SortCustomers(
        problemInstance, unroutedCustomers, depots, depotAssignment,
        alpha, beta, gamma);
      }

      result.VehicleAssignment[result.Tours.Count - 1] = currentVehicle;

      tour.Stops.Add(currentCustomer);
      if (pdp != null) {
        tour.Stops.Add(pdp.GetPickupDeliveryLocation(currentCustomer));
      }
      ////////

      while (unroutedCustomers.Count > 0) {
        double minimumCost = double.MaxValue;
        int customer = -1;
        int indexOfMinimumCost = -1;
        int indexOfMinimumCost2 = -1;

        foreach (int unrouted in unroutedCustomers) {
          VRPEvaluation eval = problemInstance.EvaluateTour(tour, result);
          double originalCosts = eval.Quality;

          for (int i = 0; i <= tour.Stops.Count; i++) {
            tour.Stops.Insert(i, unrouted);
            eval = problemInstance.EvaluateTour(tour, result);
            double tourCost = eval.Quality - originalCosts;

            if (pdp != null) {
              for (int j = i + 1; j <= tour.Stops.Count; j++) {
                bool feasible;
                double cost = tourCost +
                  problemInstance.GetInsertionCosts(eval, result, pdp.GetPickupDeliveryLocation(unrouted), 0, j, out feasible);
                if (cost < minimumCost && feasible) {
                  customer = unrouted;
                  minimumCost = cost;
                  indexOfMinimumCost = i;
//.........这里部分代码省略.........
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:101,代码来源:PushForwardInsertionCreator.cs

示例5: PairwiseMove

    public static bool PairwiseMove(PotvinEncoding individual, IVRPProblemInstance instance, int city, bool allowInfeasible) {
      bool success;

      IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;

      if (pdp != null) {
        Tour route1 = individual.Tours.Find(t => t.Stops.Contains(city));
        int i = route1.Stops.IndexOf(city);

        int dest = pdp.GetPickupDeliveryLocation(city);
        Tour destRoute = individual.Tours.Find(t => t.Stops.Contains(dest));
        int j = destRoute.Stops.IndexOf(dest);

        route1.Stops.Remove(city);
        destRoute.Stops.Remove(dest);

        int routeToAvoid = -1;
        if (route1 == destRoute)
          routeToAvoid = individual.Tours.IndexOf(route1);

        int source, target;
        if (instance.GetDemand(city) >= 0) {
          source = city;
          target = dest;
        } else {
          source = dest;
          target = city;
        }

        double bestQuality = double.MaxValue;
        int bestTour = -1;
        int bestPositionSource = -1;
        int bestPositionTarget = -1;

        for (int tourIdx = 0; tourIdx < individual.Tours.Count; tourIdx++) {
          if (tourIdx != routeToAvoid) {
            Tour tour = individual.Tours[tourIdx];
            VRPEvaluation eval = instance.EvaluateTour(tour, individual);
            individual.InsertPair(tour, source, target, instance);
            VRPEvaluation evalNew = instance.EvaluateTour(tour, individual);

            double delta = evalNew.Quality - eval.Quality;

            if (delta < bestQuality &&
               (instance.Feasible(evalNew) || allowInfeasible)) {
              bestQuality = delta;
              bestTour = tourIdx;
              bestPositionSource = tour.Stops.IndexOf(source);
              bestPositionTarget = tour.Stops.IndexOf(target);
            }

            tour.Stops.Remove(source);
            tour.Stops.Remove(target);
          }
        }

        if (bestTour >= 0) {
          if (bestPositionTarget < bestPositionSource) {
            individual.Tours[bestTour].Stops.Insert(bestPositionTarget, target);
            individual.Tours[bestTour].Stops.Insert(bestPositionSource, source);
          } else {
            individual.Tours[bestTour].Stops.Insert(bestPositionSource, source);
            individual.Tours[bestTour].Stops.Insert(bestPositionTarget, target);
          }

          success = true;
        } else {
          if (j < i) {
            destRoute.Stops.Insert(j, dest);
            route1.Stops.Insert(i, city);
          } else {
            route1.Stops.Insert(i, city);
            destRoute.Stops.Insert(j, dest);
          }

          success = false;
        }
      } else {
        success = false;
      }

      return success;
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:83,代码来源:PotvinPairwiseOneLevelExchangeManipulator.cs

示例6: GetTourInsertionCosts

    protected override double GetTourInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
      out bool feasible) {
      CVRPInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPInsertionInfo;

      double costs = 0;
      feasible = tourInsertionInfo.Penalty < double.Epsilon;

      IHeterogenousCapacitatedProblemInstance cvrp = instance as IHeterogenousCapacitatedProblemInstance;
      double overloadPenalty = cvrp.OverloadPenalty.Value;

      double startDistance, endDistance;
      costs += instance.GetInsertionDistance(insertionInfo.Start, customer, insertionInfo.End, solution, out startDistance, out endDistance);

      double demand = instance.GetDemand(customer);
      if (demand > insertionInfo.SpareCapacity) {
        feasible = false;

        if (insertionInfo.SpareCapacity >= 0)
          costs += (demand - insertionInfo.SpareCapacity) * overloadPenalty;
        else
          costs += demand * overloadPenalty;
      }

      return costs;
    }
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:25,代码来源:MDCVRPEvaluator.cs


注:本文中的IVRPProblemInstance.GetDemand方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。