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());
        }
        public void Load(IVRPData data, IVRPDataInterpreter interpreter)
        {
            VRPInstanceDescription instance = interpreter.Interpret(data);

            Name        = instance.Name;
            Description = instance.Description;

            BestKnownQuality  = null;
            BestKnownSolution = null;

            if (ProblemInstance != null && instance.ProblemInstance != null &&
                instance.ProblemInstance.GetType() == ProblemInstance.GetType())
            {
                SetProblemInstance(instance.ProblemInstance);
            }
            else
            {
                ProblemInstance = instance.ProblemInstance;
            }

            OnReset();

            if (instance.BestKnownQuality != null)
            {
                BestKnownQuality = new DoubleValue((double)instance.BestKnownQuality);
            }

            if (instance.BestKnownSolution != null)
            {
                VRPSolution solution = new VRPSolution(ProblemInstance, instance.BestKnownSolution, new DoubleValue(0));
                BestKnownSolution = solution;
            }
        }
        public void ImportSolution(string solutionFileName)
        {
            SolutionParser parser = new SolutionParser(solutionFileName);

            parser.Parse();

            HeuristicLab.Problems.VehicleRouting.Encodings.Potvin.PotvinEncoding encoding = new Encodings.Potvin.PotvinEncoding(ProblemInstance);

            int cities = 0;

            foreach (List <int> route in parser.Routes)
            {
                Tour tour = new Tour();
                tour.Stops.AddRange(route);
                cities += tour.Stops.Count;

                encoding.Tours.Add(tour);
            }

            if (cities != ProblemInstance.Coordinates.Rows - 1)
            {
                ErrorHandling.ShowErrorDialog(new Exception("The optimal solution does not seem to correspond with the problem data"));
            }
            else
            {
                VRPSolution solution = new VRPSolution(ProblemInstance, encoding, new DoubleValue(0));
                BestKnownSolutionParameter.Value = solution;
            }
        }
Exemple #4
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 Apply()
        {
            IVRPProblemInstance      problemInstance = ProblemInstanceParameter.ActualValue;
            ItemArray <IVRPEncoding> solutions       = VRPToursParameter.ActualValue;
            ResultCollection         results         = ResultsParameter.ActualValue;

            ItemArray <DoubleValue> qualities            = QualityParameter.ActualValue;
            ItemArray <DoubleValue> distances            = DistanceParameter.ActualValue;
            ItemArray <DoubleValue> vehiclesUtilizations = VehiclesUtilizedParameter.ActualValue;

            int          i        = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
            IVRPEncoding best     = solutions[i].Clone() as IVRPEncoding;
            VRPSolution  solution = BestSolutionParameter.ActualValue;

            if (solution == null)
            {
                solution = new VRPSolution(problemInstance, best.Clone() as IVRPEncoding, new DoubleValue(qualities[i].Value));
                BestSolutionParameter.ActualValue = solution;
                results.Add(new Result("Best VRP Solution", solution));

                results.Add(new Result("Best VRP Solution Distance", new DoubleValue(distances[i].Value)));
                results.Add(new Result("Best VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[i].Value)));
            }
            else
            {
                VRPEvaluation eval = problemInstance.Evaluate(solution.Solution);
                if (qualities[i].Value <= eval.Quality)
                {
                    solution.ProblemInstance = problemInstance;
                    solution.Solution        = best.Clone() as IVRPEncoding;
                    solution.Quality.Value   = qualities[i].Value;
                    (results["Best VRP Solution Distance"].Value as DoubleValue).Value           = distances[i].Value;
                    (results["Best VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[i].Value;
                }
            }

            var idx = qualities.Select((x, index) => new { index, x.Value }).Where(index => problemInstance.Feasible(solutions[index.index])).OrderBy(x => x.Value).FirstOrDefault();

            if (idx != null)
            {
                int          j             = idx.index;
                IVRPEncoding bestFeasible  = solutions[j].Clone() as IVRPEncoding;
                VRPSolution  validSolution = BestValidSolutionParameter.ActualValue;
                if (validSolution == null)
                {
                    validSolution = new VRPSolution(problemInstance, best.Clone() as IVRPEncoding, new DoubleValue(qualities[j].Value));
                    BestValidSolutionParameter.ActualValue = validSolution;
                    if (results.ContainsKey("Best valid VRP Solution"))
                    {
                        results["Best valid VRP Solution"].Value = validSolution;
                    }
                    else
                    {
                        results.Add(new Result("Best valid VRP Solution", validSolution));
                    }

                    results.Add(new Result("Best valid VRP Solution Distance", new DoubleValue(distances[j].Value)));
                    results.Add(new Result("Best valid VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[j].Value)));
                }
                else
                {
                    if (qualities[j].Value <= validSolution.Quality.Value)
                    {
                        if (ProblemInstanceParameter.ActualValue.Feasible(best))
                        {
                            validSolution.ProblemInstance = problemInstance;
                            validSolution.Solution        = best.Clone() as IVRPEncoding;
                            validSolution.Quality.Value   = qualities[j].Value;
                            (results["Best valid VRP Solution Distance"].Value as DoubleValue).Value           = distances[j].Value;
                            (results["Best valid VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[j].Value;
                        }
                    }
                }

                DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
                if (bestKnownQuality == null || qualities[j].Value < bestKnownQuality.Value)
                {
                    BestKnownQualityParameter.ActualValue  = new DoubleValue(qualities[j].Value);
                    BestKnownSolutionParameter.ActualValue = (VRPSolution)validSolution.Clone();
                }
            }

            return(base.Apply());
        }
    public override IOperation Apply() {
      IVRPProblemInstance problemInstance = ProblemInstanceParameter.ActualValue;
      ItemArray<IVRPEncoding> solutions = VRPToursParameter.ActualValue;
      ResultCollection results = ResultsParameter.ActualValue;

      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
      ItemArray<DoubleValue> distances = DistanceParameter.ActualValue;
      ItemArray<DoubleValue> vehiclesUtilizations = VehiclesUtilizedParameter.ActualValue;

      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
      IVRPEncoding best = solutions[i].Clone() as IVRPEncoding;
      VRPSolution solution = BestSolutionParameter.ActualValue;
      if (solution == null) {
        solution = new VRPSolution(problemInstance, best.Clone() as IVRPEncoding, new DoubleValue(qualities[i].Value));
        BestSolutionParameter.ActualValue = solution;
        results.Add(new Result("Best VRP Solution", solution));

        results.Add(new Result("Best VRP Solution Distance", new DoubleValue(distances[i].Value)));
        results.Add(new Result("Best VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[i].Value)));
      } else {
        VRPEvaluation eval = problemInstance.Evaluate(solution.Solution);
        if (qualities[i].Value <= eval.Quality) {
          solution.ProblemInstance = problemInstance;
          solution.Solution = best.Clone() as IVRPEncoding;
          solution.Quality.Value = qualities[i].Value;
          (results["Best VRP Solution Distance"].Value as DoubleValue).Value = distances[i].Value;
          (results["Best VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[i].Value;
        }
      }

      var idx = qualities.Select((x, index) => new { index, x.Value }).Where(index => problemInstance.Feasible(solutions[index.index])).OrderBy(x => x.Value).FirstOrDefault();
      if (idx != null) {
        int j = idx.index;
        IVRPEncoding bestFeasible = solutions[j].Clone() as IVRPEncoding;
        VRPSolution validSolution = BestValidSolutionParameter.ActualValue;
        if (validSolution == null) {
          validSolution = new VRPSolution(problemInstance, best.Clone() as IVRPEncoding, new DoubleValue(qualities[j].Value));
          BestValidSolutionParameter.ActualValue = validSolution;
          if (results.ContainsKey("Best valid VRP Solution"))
            results["Best valid VRP Solution"].Value = validSolution;
          else
            results.Add(new Result("Best valid VRP Solution", validSolution));

          results.Add(new Result("Best valid VRP Solution Distance", new DoubleValue(distances[j].Value)));
          results.Add(new Result("Best valid VRP Solution VehicleUtilization", new DoubleValue(vehiclesUtilizations[j].Value)));
        } else {
          if (qualities[j].Value <= validSolution.Quality.Value) {
            if (ProblemInstanceParameter.ActualValue.Feasible(best)) {
              validSolution.ProblemInstance = problemInstance;
              validSolution.Solution = best.Clone() as IVRPEncoding;
              validSolution.Quality.Value = qualities[j].Value;
              (results["Best valid VRP Solution Distance"].Value as DoubleValue).Value = distances[j].Value;
              (results["Best valid VRP Solution VehicleUtilization"].Value as DoubleValue).Value = vehiclesUtilizations[j].Value;
            }
          }
        }

        DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
        if (bestKnownQuality == null || qualities[j].Value < bestKnownQuality.Value) {
          BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[j].Value);
          BestKnownSolutionParameter.ActualValue = (VRPSolution)validSolution.Clone();
        }
      }

      return base.Apply();
    }
    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();
    }