public override double GetDistance(int start, int end, IVRPEncoding solution)
        {
            if (start == 0 && end == 0)
            {
                return(0);
            }

            if (start == 0)
            {
                start = GetDepot(end, solution);
                end  += Depots.Value - 1;
            }
            else if (end == 0)
            {
                end    = GetDepot(start, solution);
                start += Depots.Value - 1;
            }
            else
            {
                start += Depots.Value - 1;
                end   += Depots.Value - 1;
            }

            return(base.GetDistance(start, end, solution));
        }
예제 #2
0
        public VRPEvaluation EvaluateTour(IVRPProblemInstance instance, Tour tour, IVRPEncoding solution)
        {
            VRPEvaluation evaluation = CreateTourEvaluation();

            EvaluateTour(evaluation, instance, tour, solution);
            return(evaluation);
        }
예제 #3
0
    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 distance = 0.0;
      double quality = 0.0;

      //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;

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

      //Fleet usage
      quality += instance.FleetUsageFactor.Value;
      //Distance
      quality += instance.DistanceFactor.Value * distance;

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

      tourInfo.Quality = quality;
      eval.Quality += quality;
    }
        public override IOperation Apply()
        {
            IVRPProblemInstance      problemInstance = ProblemInstanceParameter.ActualValue;
            ItemArray <IVRPEncoding> solutions       = VRPToursParameter.ActualValue;
            ResultCollection         results         = ResultsParameter.ActualValue;

            ItemArray <DoubleValue> qualities        = QualityParameter.ActualValue;
            ItemArray <IntValue>    pickupViolations = PickupViolationsParameter.ActualValue;

            int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;

            IVRPEncoding best     = solutions[i] as IVRPEncoding;
            VRPSolution  solution = BestSolutionParameter.ActualValue;

            if (solution != null)
            {
                if (!results.ContainsKey("Best VRP Solution PickupViolations"))
                {
                    results.Add(new Result("Best VRP Solution PickupViolations", new DoubleValue(pickupViolations[i].Value)));
                }
                else
                {
                    VRPEvaluation eval = problemInstance.Evaluate(solution.Solution);
                    if (qualities[i].Value <= eval.Quality)
                    {
                        (results["Best VRP Solution PickupViolations"].Value as DoubleValue).Value = pickupViolations[i].Value;
                    }
                }
            }

            return(base.Apply());
        }
예제 #5
0
        public override IOperation InstrumentedApply()
        {
            IOperation next = base.InstrumentedApply();

            IVRPEncoding solution = VRPToursParameter.ActualValue;

            generator.PermutationParameter.ActualName = VRPToursParameter.ActualName;
            IAtomicOperation op = this.ExecutionContext.CreateChildOperation(generator);

            op.Operator.Execute((IExecutionContext)op, CancellationToken);

            foreach (IScope scope in this.ExecutionContext.Scope.SubScopes)
            {
                IVariable moveVariable = scope.Variables[
                    TranslocationMoveParameter.ActualName];

                if (moveVariable.Value is TranslocationMove &&
                    !(moveVariable.Value is AlbaTranslocationMove))
                {
                    TranslocationMove move = moveVariable.Value as TranslocationMove;
                    moveVariable.Value =
                        new AlbaTranslocationMove(
                            move.Index1, move.Index2, move.Index3, solution as AlbaEncoding);
                }
            }

            return(next);
        }
예제 #6
0
        public override IOperation InstrumentedApply()
        {
            ItemArray <IVRPEncoding> parents = new ItemArray <IVRPEncoding>(ParentsParameter.ActualValue.Length);

            for (int i = 0; i < ParentsParameter.ActualValue.Length; i++)
            {
                IVRPEncoding solution = ParentsParameter.ActualValue[i];

                if (!(solution is AlbaEncoding))
                {
                    parents[i] = AlbaEncoding.ConvertFrom(solution, ProblemInstance);
                }
                else
                {
                    parents[i] = solution;
                }
            }
            ParentsParameter.ActualValue = parents;

            ChildParameter.ActualValue =
                Crossover(RandomParameter.ActualValue, parents[0] as AlbaEncoding, parents[1] as AlbaEncoding);
            (ChildParameter.ActualValue as AlbaEncoding).Repair();

            return(base.InstrumentedApply());
        }
예제 #7
0
        public static GVREncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance problemInstance)
        {
            GVREncoding solution = new GVREncoding(problemInstance);

            TourEncoding.ConvertFrom(encoding, solution, problemInstance);

            return(solution);
        }
예제 #8
0
    public VRPSolution(IVRPProblemInstance problemInstance, IVRPEncoding solution, DoubleValue quality)
      : base() {
      this.problemInstance = problemInstance;
      this.solution = solution;
      this.quality = quality;

      Initialize();
    }
예제 #9
0
    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;

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

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

      double capacity = cvrpInstance.Capacity.Value;
      for (int i = 0; i <= tour.Stops.Count; i++) {
        int end = 0;
        if (i < tour.Stops.Count)
          end = tour.Stops[i];

        delivered += demand[end];
      }

      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;
    }
예제 #10
0
        public VRPSolution(IVRPProblemInstance problemInstance, IVRPEncoding solution, DoubleValue quality)
            : base()
        {
            this.problemInstance = problemInstance;
            this.solution        = solution;
            this.quality         = quality;

            Initialize();
        }
        private int SelectCityBiasedByNeighborDistance(IRandom random, Tour tour, IVRPEncoding solution)
        {
            int cityIndex = -1;

            double sum = 0.0;

            double[] probabilities = new double[tour.Stops.Count];
            for (int i = 0; i < tour.Stops.Count; i++)
            {
                int next;
                if (i + 1 >= tour.Stops.Count)
                {
                    next = 0;
                }
                else
                {
                    next = tour.Stops[i + 1];
                }
                double distance = ProblemInstance.GetDistance(
                    tour.Stops[i], next, solution);

                int prev;
                if (i - 1 < 0)
                {
                    prev = 0;
                }
                else
                {
                    prev = tour.Stops[i - 1];
                }
                distance += ProblemInstance.GetDistance(
                    tour.Stops[i], prev, solution);

                probabilities[i] = distance;
                sum += probabilities[i];
            }

            double rand = random.NextDouble() * sum;
            double cumulatedProbabilities = 0.0;
            int    index = 0;

            while (cityIndex == -1 && index < probabilities.Length)
            {
                if (cumulatedProbabilities <= rand && rand <= cumulatedProbabilities + probabilities[index])
                {
                    cityIndex = index;
                }

                cumulatedProbabilities += probabilities[index];
                index++;
            }

            return(cityIndex);
        }
예제 #12
0
        public override IOperation InstrumentedApply()
        {
            IVRPEncoding solution = VRPToursParameter.ActualValue;

            if (!(solution is PotvinEncoding))
            {
                VRPToursParameter.ActualValue = PotvinEncoding.ConvertFrom(solution, ProblemInstance);
            }

            return(base.InstrumentedApply());
        }
    public override IOperation InstrumentedApply() {
      IOperation next = base.InstrumentedApply();

      IVRPEncoding solution = VRPToursParameter.ActualValue;

      PermutationMoveOperatorParameter.PermutationParameter.ActualName = VRPToursParameter.ActualName;
      IAtomicOperation op = this.ExecutionContext.CreateChildOperation(PermutationMoveOperatorParameter);
      op.Operator.Execute((IExecutionContext)op, CancellationToken);

      return next;
    }
예제 #14
0
        public virtual VRPEvaluation Evaluate(IVRPProblemInstance instance, IVRPEncoding solution)
        {
            VRPEvaluation evaluation = CreateTourEvaluation();

            foreach (Tour tour in solution.GetTours())
            {
                EvaluateTour(evaluation, instance, tour, solution);
            }

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

      double costs = 0;
      feasible = true;
      double startDistance, endDistance;

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

      return costs;
    }
예제 #16
0
        public virtual double GetInsertionDistance(int start, int customer, int end, IVRPEncoding solution,
                                                   out double startDistance, out double endDistance)
        {
            double distance = GetDistance(start, end, solution);

            startDistance = GetDistance(start, customer, solution);
            endDistance   = GetDistance(customer, end, solution);

            double newDistance = startDistance + endDistance;

            return(newDistance - distance);
        }
예제 #17
0
        public override IOperation InstrumentedApply()
        {
            IVRPEncoding solution = VRPToursParameter.ActualValue;

            if (!(solution is ZhuEncoding))
            {
                VRPToursParameter.ActualValue = ZhuEncoding.ConvertFrom(solution, ProblemInstance);
            }

            Manipulate(RandomParameter.ActualValue, VRPToursParameter.ActualValue as ZhuEncoding);

            return(base.InstrumentedApply());
        }
예제 #18
0
        public virtual double GetDistance(int start, int end, IVRPEncoding solution)
        {
            if (distanceMatrix == null && UseDistanceMatrix.Value)
            {
                distanceMatrix = DistanceMatrix ?? CreateDistanceMatrix();
            }

            if (distanceMatrix != null)
            {
                return(distanceMatrix[start, end]);
            }
            return(CalculateDistance(start, end));
        }
예제 #19
0
        public double GetInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, VRPEvaluation eval, int customer, int tour, int index, out bool feasible)
        {
            bool   tourFeasible;
            double costs = GetTourInsertionCosts(
                instance,
                solution,
                eval.InsertionInfo.GetTourInsertionInfo(tour),
                index,
                customer, out tourFeasible);

            feasible = tourFeasible;

            return(costs);
        }
예제 #20
0
    //helper method to evaluate an updated individual
    protected void UpdateEvaluation(IVRPEncoding updatedTours) {
      IVRPEvaluator evaluator = ProblemInstance.MoveEvaluator;

      try {
        this.ExecutionContext.Scope.Variables.Add(new Variable(evaluator.VRPToursParameter.ActualName,
          updatedTours));

        IAtomicOperation op = this.ExecutionContext.CreateChildOperation(evaluator);
        op.Operator.Execute((IExecutionContext)op, CancellationToken);
      }
      finally {
        this.ExecutionContext.Scope.Variables.Remove(evaluator.VRPToursParameter.ActualName);
      }
    }
예제 #21
0
        //helper method to evaluate an updated individual
        protected void UpdateEvaluation(IVRPEncoding updatedTours)
        {
            IVRPEvaluator evaluator = ProblemInstance.MoveEvaluator;

            try {
                this.ExecutionContext.Scope.Variables.Add(new Variable(evaluator.VRPToursParameter.ActualName,
                                                                       updatedTours));

                IAtomicOperation op = this.ExecutionContext.CreateChildOperation(evaluator);
                op.Operator.Execute((IExecutionContext)op, CancellationToken);
            }
            finally {
                this.ExecutionContext.Scope.Variables.Remove(evaluator.VRPToursParameter.ActualName);
            }
        }
        public int GetDepot(int customer, IVRPEncoding solution)
        {
            int depot = -1;

            Tour tour =
                solution.GetTours().FirstOrDefault(t => t.Stops.Contains(customer));

            if (tour != null)
            {
                int tourIndex = solution.GetTourIndex(tour);
                int vehicle   = solution.GetVehicleAssignment(tourIndex);
                depot = VehicleDepotAssignment[vehicle];
            }

            return(depot);
        }
예제 #23
0
        public static PrinsEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance problemInstance)
        {
            List <Tour> tours = encoding.GetTours();
            List <int>  route = new List <int>();

            foreach (Tour tour in tours)
            {
                foreach (int city in tour.Stops)
                {
                    route.Add(city - 1);
                }
            }

            return(new PrinsEncoding(
                       new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), problemInstance));
        }
예제 #24
0
    protected override double GetTourInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
      out bool feasible) {
      StopInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index);

      double costs = 0;
      feasible = true;

      double distance = instance.GetDistance(insertionInfo.Start, insertionInfo.End, solution);
      double newDistance =
        instance.GetDistance(insertionInfo.Start, customer, solution) +
        instance.GetDistance(customer, insertionInfo.End, solution);

      costs += instance.DistanceFactor.Value * (newDistance - distance);

      return costs;
    }
예제 #25
0
        private VRPSolution(VRPSolution original, Cloner cloner)
            : base(original, cloner)
        {
            this.solution = (IVRPEncoding)cloner.Clone(original.solution);
            this.quality  = (DoubleValue)cloner.Clone(original.quality);

            if (original.ProblemInstance != null && cloner.ClonedObjectRegistered(original.ProblemInstance))
            {
                this.ProblemInstance = (IVRPProblemInstance)cloner.Clone(original.ProblemInstance);
            }
            else
            {
                this.ProblemInstance = original.ProblemInstance;
            }

            this.Initialize();
        }
        public override IOperation InstrumentedApply()
        {
            IVRPEncoding solution = VRPToursParameter.ActualValue;

            if (!(solution is PotvinEncoding))
            {
                VRPToursParameter.ActualValue = PotvinEncoding.ConvertFrom(solution, ProblemInstance);
            }

            OperationCollection next = new OperationCollection(base.InstrumentedApply());

            VehicleAssignmentParameter.ActualValue = (VRPToursParameter.ActualValue as PotvinEncoding).VehicleAssignment;
            VehicleAssignmentManipuator.Value.PermutationParameter.ActualName = VehicleAssignmentParameter.ActualName;
            next.Insert(0, ExecutionContext.CreateOperation(VehicleAssignmentManipuator.Value));

            return(next);
        }
        public static AlbaEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance)
        {
            List <Tour> tours = encoding.GetTours();

            int cities = 0;

            foreach (Tour tour in tours)
            {
                cities += tour.Stops.Count;
            }

            int emptyVehicles = instance.Vehicles.Value - tours.Count;

            int[] array      = new int[cities + tours.Count + emptyVehicles];
            int   delimiter  = 0;
            int   arrayIndex = 0;

            foreach (Tour tour in tours)
            {
                foreach (int city in tour.Stops)
                {
                    array[arrayIndex] = city - 1;
                    arrayIndex++;
                }

                if (arrayIndex != array.Length)
                {
                    array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);

                    delimiter++;
                    arrayIndex++;
                }
            }

            for (int i = 0; i < emptyVehicles; i++)
            {
                array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);

                delimiter++;
                arrayIndex++;
            }

            AlbaEncoding solution = new AlbaEncoding(new Permutation(PermutationTypes.RelativeUndirected, new IntArray(array)), instance);

            return(solution);
        }
예제 #28
0
        public override IOperation InstrumentedApply()
        {
            InitResultParameters();

            VRPEvaluation evaluation = CreateTourEvaluation();
            IVRPEncoding  solution   = VRPToursParameter.ActualValue;

            foreach (Tour tour in solution.GetTours())
            {
                EvaluateTour(evaluation, ProblemInstance, tour, solution);
            }
            SetResultParameters(evaluation);

            QualityParameter.ActualValue = new DoubleValue(evaluation.Quality);

            return(base.InstrumentedApply());
        }
예제 #29
0
파일: Tour.cs 프로젝트: t-h-e/HeuristicLab
    public double GetTourLength(IVRPProblemInstance instance, IVRPEncoding solution) {
      double length = 0;

      if (Stops.Count > 0) {
        List<int> cities = new List<int>();
        cities.Add(0);
        foreach (int city in Stops) {
          cities.Add(city);
        }
        cities.Add(0);

        for (int i = 1; i < cities.Count; i++) {
          length += instance.GetDistance(cities[i - 1], cities[i], solution);
        }
      }

      return length;
    }
예제 #30
0
        public static PotvinEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance)
        {
            PotvinEncoding solution = new PotvinEncoding(instance);

            TourEncoding.ConvertFrom(encoding, solution, instance);

            List <int> vehicles = new List <int>();

            for (int i = 0; i < instance.Vehicles.Value; i++)
            {
                vehicles.Add(i);
            }

            int[] assignment = new int[instance.Vehicles.Value];
            for (int i = 0; i < assignment.Length; i++)
            {
                assignment[i] = -1;
            }

            for (int i = 0; i < solution.Tours.Count; i++)
            {
                int vehicle = encoding.GetVehicleAssignment(i);
                assignment[i] = vehicle;
                vehicles.Remove(vehicle);
            }

            for (int i = 0; i < instance.Vehicles.Value; i++)
            {
                if (assignment[i] == -1)
                {
                    int vehicle = vehicles[0];
                    assignment[i] = vehicle;
                    vehicles.RemoveAt(0);
                }
            }

            solution.VehicleAssignment = new Permutation(PermutationTypes.Absolute, assignment);

            return(solution);
        }
예제 #31
0
        public double GetTourLength(IVRPProblemInstance instance, IVRPEncoding solution)
        {
            double length = 0;

            if (Stops.Count > 0)
            {
                List <int> cities = new List <int>();
                cities.Add(0);
                foreach (int city in Stops)
                {
                    cities.Add(city);
                }
                cities.Add(0);

                for (int i = 1; i < cities.Count; i++)
                {
                    length += instance.GetDistance(cities[i - 1], cities[i], solution);
                }
            }

            return(length);
        }
예제 #32
0
    public virtual double GetDistance(int start, int end, IVRPEncoding solution) {
      if (distanceMatrix == null && UseDistanceMatrix.Value) {
        if (DistanceMatrix == null) DistanceMatrix = CreateDistanceMatrix();
        distanceMatrix = DistanceMatrix;
      }

      if (distanceMatrix != null) return distanceMatrix[start, end];
      return CalculateDistance(start, end);
    }
예제 #33
0
    public static PotvinEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance) {
      PotvinEncoding solution = new PotvinEncoding(instance);

      TourEncoding.ConvertFrom(encoding, solution, instance);

      List<int> vehicles = new List<int>();
      for (int i = 0; i < instance.Vehicles.Value; i++)
        vehicles.Add(i);

      int[] assignment = new int[instance.Vehicles.Value];
      for (int i = 0; i < assignment.Length; i++)
        assignment[i] = -1;

      for (int i = 0; i < solution.Tours.Count; i++) {
        int vehicle = encoding.GetVehicleAssignment(i);
        assignment[i] = vehicle;
        vehicles.Remove(vehicle);
      }

      for (int i = 0; i < instance.Vehicles.Value; i++) {
        if (assignment[i] == -1) {
          int vehicle = vehicles[0];
          assignment[i] = vehicle;
          vehicles.RemoveAt(0);
        }
      }

      solution.VehicleAssignment = new Permutation(PermutationTypes.Absolute, assignment);

      return solution;
    }
예제 #34
0
 public VRPEvaluation EvaluateTour(Tour tour, IVRPEncoding solution)
 {
     return(evaluator.EvaluateTour(this, tour, solution));
 }
예제 #35
0
 public bool TourFeasible(Tour tour, IVRPEncoding solution)
 {
     return(evaluator.Feasible(
                evaluator.EvaluateTour(
                    this, tour, solution)));
 }
예제 #36
0
 public VRPEvaluation EvaluateTour(IVRPProblemInstance instance, Tour tour, IVRPEncoding solution) {
   VRPEvaluation evaluation = CreateTourEvaluation();
   EvaluateTour(evaluation, instance, tour, solution);
   return evaluation;
 }
예제 #37
0
 public VRPEvaluation Evaluate(IVRPEncoding solution) {
   return evaluator.Evaluate(this, solution);
 }
예제 #38
0
 protected abstract void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour, IVRPEncoding solution);
예제 #39
0
 public bool TourFeasible(Tour tour, IVRPEncoding solution) {
   return evaluator.Feasible(
     evaluator.EvaluateTour(
     this, tour, solution));
 }
예제 #40
0
 public static void ConvertFrom(IVRPEncoding encoding, TourEncoding solution, IVRPProblemInstance problemInstance) {
   solution.Tours = new ItemList<Tour>(encoding.GetTours());
   solution.Repair();
 }
 public VRPProblemInstanceView() {
   InitializeComponent();
   Solution = null;
 }
예제 #42
0
    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;

      IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
      IntArray pickupDeliveryLocation = pdp.PickupDeliveryLocation;

      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 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];

      double currentLoad = 0.0;
      Dictionary<int, bool> stops = new Dictionary<int, bool>();
      int pickupViolations = 0;

      double tourStartTime = readyTime[0];
      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;
        double arrivalSpareCapacity = capacity - currentLoad;

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

          //Pickup / deliver
          bool validPickupDelivery =
            validPickupDelivery =
            ((demand[end - 1] >= 0) ||
             (stops.ContainsKey(pickupDeliveryLocation[end - 1])));

          if (validPickupDelivery) {
            currentLoad += demand[end - 1];
          } else {
            pickupViolations++;
          }

          if (currentLoad > capacity)
            overweight += currentLoad - capacity;
        }

        double spareCapacity = capacity - currentLoad;
        CVRPPDTWInsertionInfo stopInfo = new CVRPPDTWInsertionInfo(start, end, spareCapacity, tourStartTime,
          arrivalTime, time, spareTime, waitTime, new List<int>(stops.Keys), arrivalSpareCapacity);
        tourInfo.AddStopInsertionInfo(stopInfo);

        stops.Add(end, true);
      }

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

      (eval as CVRPEvaluation).Overload += overweight;
      double tourPenalty = 0;
      double penalty = overweight * cvrpInstance.OverloadPenalty.Value;
      eval.Penalty += penalty;
      eval.Quality += penalty;
      tourPenalty += penalty;

      (eval as CVRPTWEvaluation).Tardiness += tardiness;
      (eval as CVRPTWEvaluation).TravelTime += time;

      penalty = tardiness * vrptw.TardinessPenalty.Value;
      eval.Penalty += penalty;
      eval.Quality += penalty;
      tourPenalty += penalty;

      (eval as CVRPPDTWEvaluation).PickupViolations += pickupViolations;
      penalty = pickupViolations * pdp.PickupViolationPenalty.Value;
      eval.Penalty += penalty;
      tourPenalty += penalty;

      eval.Quality += penalty;
      eval.Quality += time * vrptw.TimeFactor.Value;
      tourInfo.Penalty = tourPenalty;
      tourInfo.Quality = eval.Quality - originalQuality;
    }
예제 #43
0
        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);
        }
예제 #44
0
 public VRPEvaluation EvaluateTour(Tour tour, IVRPEncoding solution) {
   return evaluator.EvaluateTour(this, tour, solution);
 }
예제 #45
0
    public virtual VRPEvaluation Evaluate(IVRPProblemInstance instance, IVRPEncoding solution) {
      VRPEvaluation evaluation = CreateTourEvaluation();

      foreach (Tour tour in solution.GetTours()) {
        EvaluateTour(evaluation, instance, tour, solution);
      }

      return evaluation;
    }
예제 #46
0
    public double GetInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, VRPEvaluation eval, int customer, int tour, int index, out bool feasible) {
      bool tourFeasible;
      double costs = GetTourInsertionCosts(
        instance,
        solution,
        eval.InsertionInfo.GetTourInsertionInfo(tour),
        index,
        customer, out tourFeasible);

      feasible = tourFeasible;

      return costs;
    }
    private int SelectCityBiasedByNeighborDistance(IRandom random, Tour tour, IVRPEncoding solution) {
      int cityIndex = -1;

      double sum = 0.0;
      double[] probabilities = new double[tour.Stops.Count];
      for (int i = 0; i < tour.Stops.Count; i++) {
        int next;
        if (i + 1 >= tour.Stops.Count)
          next = 0;
        else
          next = tour.Stops[i + 1];
        double distance = ProblemInstance.GetDistance(
          tour.Stops[i], next, solution);

        int prev;
        if (i - 1 < 0)
          prev = 0;
        else
          prev = tour.Stops[i - 1];
        distance += ProblemInstance.GetDistance(
          tour.Stops[i], prev, solution);

        probabilities[i] = distance;
        sum += probabilities[i];
      }

      double rand = random.NextDouble() * sum;
      double cumulatedProbabilities = 0.0;
      int index = 0;
      while (cityIndex == -1 && index < probabilities.Length) {
        if (cumulatedProbabilities <= rand && rand <= cumulatedProbabilities + probabilities[index])
          cityIndex = index;

        cumulatedProbabilities += probabilities[index];
        index++;
      }

      return cityIndex;
    }
예제 #48
0
    public virtual double GetInsertionDistance(int start, int customer, int end, IVRPEncoding solution,
      out double startDistance, out double endDistance) {
      double distance = GetDistance(start, end, solution);

      startDistance = GetDistance(start, customer, solution);
      endDistance = GetDistance(customer, end, solution);

      double newDistance = startDistance + endDistance;

      return newDistance - distance;
    }
예제 #49
0
    public static GVREncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance problemInstance) {
      GVREncoding solution = new GVREncoding(problemInstance);

      TourEncoding.ConvertFrom(encoding, solution, problemInstance);

      return solution;
    }
예제 #50
0
    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;
    }
예제 #51
0
    public static AlbaEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance) {
      List<Tour> tours = encoding.GetTours();

      int cities = 0;
      foreach (Tour tour in tours) {
        cities += tour.Stops.Count;
      }

      int emptyVehicles = instance.Vehicles.Value - tours.Count;

      int[] array = new int[cities + tours.Count + emptyVehicles];
      int delimiter = 0;
      int arrayIndex = 0;

      foreach (Tour tour in tours) {
        foreach (int city in tour.Stops) {
          array[arrayIndex] = city - 1;
          arrayIndex++;
        }

        if (arrayIndex != array.Length) {
          array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);

          delimiter++;
          arrayIndex++;
        }
      }

      for (int i = 0; i < emptyVehicles; i++) {
        array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);

        delimiter++;
        arrayIndex++;
      }

      AlbaEncoding solution = new AlbaEncoding(new Permutation(PermutationTypes.RelativeUndirected, new IntArray(array)), instance);

      return solution;
    }
예제 #52
0
    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;
      eval.Penalty += penalty;
      eval.Quality += penalty;
      tourPenalty += penalty;

      (eval as CVRPTWEvaluation).Tardiness += tardiness;
      (eval as CVRPTWEvaluation).TravelTime += time;

      penalty = tardiness * vrptw.TardinessPenalty.Value;
      eval.Penalty += penalty;
      eval.Quality += penalty;
      tourPenalty += penalty;
      eval.Quality += time * vrptw.TimeFactor.Value;
      tourInfo.Penalty = tourPenalty;
      tourInfo.Quality = eval.Quality - originalQuality;
    }
예제 #53
0
 public VRPEvaluation Evaluate(IVRPEncoding solution)
 {
     return(evaluator.Evaluate(this, solution));
 }
예제 #54
0
 protected abstract double GetTourInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, TourInsertionInfo tourInsertionInfo, int index, int customer, out bool feasible);
예제 #55
0
 public double GetInsertionCosts(VRPEvaluation eval, IVRPEncoding solution, int customer, int tour, int index, out bool feasible)
 {
     return(evaluator.GetInsertionCosts(this, solution, eval, customer, tour, index, out feasible));
 }
예제 #56
0
    public static PrinsEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance problemInstance) {
      List<Tour> tours = encoding.GetTours();
      List<int> route = new List<int>();

      foreach (Tour tour in tours) {
        foreach (int city in tour.Stops)
          route.Add(city - 1);
      }

      return new PrinsEncoding(
        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), problemInstance);
    }
예제 #57
0
 public bool Feasible(IVRPEncoding solution) {
   return evaluator.Feasible(
     evaluator.Evaluate(
       this, solution));
 }
예제 #58
0
    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;

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

      double distance = instance.GetDistance(insertionInfo.Start, insertionInfo.End, solution);
      double newDistance =
        instance.GetDistance(insertionInfo.Start, customer, solution) +
        instance.GetDistance(customer, insertionInfo.End, solution);
      costs += instance.DistanceFactor.Value * (newDistance - distance);

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

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

      return costs;
    }
예제 #59
0
        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;
        }
예제 #60
0
 public double GetInsertionCosts(VRPEvaluation eval, IVRPEncoding solution, int customer, int tour, int index, out bool feasible) {
   return evaluator.GetInsertionCosts(this, solution, eval, customer, tour, index, out feasible);
 }