Exemple #1
0
        /// <summary>
        /// Creates a new Guided Variable Neighbourhood Search solver.
        /// </summary>
        /// <param name="router"></param>
        /// <param name="max"></param>
        /// <param name="deliveryTime"></param>
        /// <param name="thresholdPrecentage"></param>
        /// <param name="lambda"></param>
        /// <param name="sigma"></param>
        public GuidedVNS(Router router, Second max, Second deliveryTime, 
            float thresholdPrecentage, float lambda, float sigma)
            : base(max, deliveryTime)
        {
            _thresholdPercentage = thresholdPrecentage;
            _lambda = lambda;
            //            _sigma = sigma;

            _intra_improvements = new List<IImprovement>();
            //_intra_improvements.Add(
            //    new ArbitraryInsertionSolver());
            _intra_improvements.Add(
                new HillClimbing3OptSolver(true, true));

            _inter_improvements = new List<IInterImprovement>();
            _inter_improvements.Add(
                new RelocateImprovement());
            _inter_improvements.Add(
                new ExchangeInterImprovement());
            //_inter_improvements.Add(
            //    new TwoOptInterImprovement());
            _inter_improvements.Add(
                new RelocateExchangeInterImprovement());
            _inter_improvements.Add(
                new CrossExchangeInterImprovement());
        }
        /// <summary>
        /// Creates a new best placement min max no depot vrp router.
        /// </summary>
        /// <param name="max"></param>
        /// <param name="delivery_time"></param>
        /// <param name="k"></param>
        public CheapestInsertionSolverWithSeeds(Second max, Second delivery_time, int k)
            : base(max, delivery_time)
        {
            _seed_selector = new SimpleSeeds();

            _k = k;
        }
        /// <summary>
        /// Creates a new genetic min max no depot vrp router.
        /// </summary>
        /// <param name="max"></param>
        /// <param name="delivery_time"></param>
        public RouterGeneticSimple(Second max, Second delivery_time)
            : base(max, delivery_time)
        {
            _population = 100;
            _stagnation = 2000;

            _elitism_percentage = 10;
            _cross_percentage = 60;
            _mutation_percentage = 30;
        }
Exemple #4
0
 /// <summary>
 /// Creates a new problem.
 /// </summary>
 /// <param name="cities"></param>
 /// <param name="minimum"></param>
 /// <param name="maximum"></param>
 public Problem(int cities,
     Second minimum,
     Second maximum)
 {
     this.InitialVehicles = 3;
     this.Cities = cities;
     this.TargetTime = (minimum.Value + maximum.Value) / 2.0;
     this.Tolerance = 0;
     this.MaximumTime = maximum;
     this.MinimumTime = minimum;
 }
Exemple #5
0
        /// <summary>
        /// Creates a new max time problem.
        /// </summary>
        /// <param name="weights"></param>
        /// <param name="max"></param>
        /// <param name="delivery_time"></param>
        /// <param name="cost_per_second"></param>
        /// <param name="cost_per_vehicle"></param>
        public MaxTimeProblem(IProblemWeights weights, Second max, Second delivery_time, 
            double cost_per_second, double cost_per_vehicle)
        {
            this.Max = max;
            this.DeliveryTime = delivery_time;
            _cost_per_second = cost_per_second;
            _cost_per_vehicle = cost_per_vehicle;

            _weights = weights;

            _calculator = new MaxTimeCalculator(this);
            _customer_positions = new List<GeoCoordinate>();
        }
        /// <summary>
        /// Creates a new best placement min max no depot vrp router.
        /// </summary>
        /// <param name="router"></param>
        /// <param name="max"></param>
        /// <param name="deliveryTime"></param>
        /// <param name="k"></param>
        /// <param name="deltaPercentage"></param>
        /// <param name="useSeedCost"></param>
        /// <param name="thresholdPrecentage"></param>
        public CheapestInsertionSolverWithImprovements(Router router,
            Second max, Second deliveryTime, int k, float deltaPercentage, bool useSeedCost, 
            float thresholdPrecentage)
            : base(max, deliveryTime)
        {
            _k = k;
            _deltaPercentage = deltaPercentage;
            _useSeedCost = useSeedCost;
            //            _thresholdPercentage = thresholdPrecentage;

            _intraImprovements = new List<IImprovement>();
            //_intra_improvements.Add(
            //    new OsmSharp.Math.TSP.ArbitraryInsertion.ArbitraryInsertionSolver());
            _intraImprovements.Add(
                new HillClimbing3OptSolver(true, true));

            _interImprovements = new List<IInterImprovement>();
            _interImprovements.Add(
                new ExchangeInterImprovement());
            _interImprovements.Add(
                new RelocateImprovement());
            //_inter_improvements.Add(
            //    new TwoOptInterImprovement());
            _interImprovements.Add(
                new RelocateExchangeInterImprovement());
            _interImprovements.Add(
                new CrossExchangeInterImprovement());
        }
 /// <summary>
 /// Creates a solver based on a construction heuristic.
 /// </summary>
 /// <param name="max"></param>
 /// <param name="delivery_time"></param>
 public SavingsHeuristicSolver(Second max, Second delivery_time)
     : base(max, delivery_time)
 {
 }
Exemple #8
0
        /// <summary>
        /// Calculates the closest point on the route relative to the given coordinate.
        /// </summary>
        /// <returns></returns>
        public bool ProjectOn(GeoCoordinate coordinates, out GeoCoordinate projectedCoordinates, out int entryIndex, out Meter distanceFromStart, out Second timeFromStart)
        {
            double distance = double.MaxValue;
            distanceFromStart = 0;
            timeFromStart = 0;
            double currentDistanceFromStart = 0;
            projectedCoordinates = null;
            entryIndex = -1;

            // loop over all points and try to project onto the line segments.
            GeoCoordinate projected;
            double currentDistance;
            var points = this.GetPoints();
            for (int idx = 0; idx < points.Count - 1; idx++)
            {
                var line = new GeoCoordinateLine(points[idx], points[idx + 1], true, true);
                var projectedPoint = line.ProjectOn(coordinates);
                if (projectedPoint != null)
                { // there was a projected point.
                    projected = new GeoCoordinate(projectedPoint[1], projectedPoint[0]);
                    currentDistance = coordinates.Distance(projected);
                    if (currentDistance < distance)
                    { // this point is closer.
                        projectedCoordinates = projected;
                        entryIndex = idx;
                        distance = currentDistance;

                        // calculate distance/time.
                        double localDistance = projected.DistanceReal(points[idx]).Value;
                        distanceFromStart = currentDistanceFromStart + localDistance;
                        if(this.HasTimes && idx > 0)
                        { // there should be proper timing information.
                            double timeToSegment = this.Segments[idx].Time;
                            double timeToNextSegment = this.Segments[idx + 1].Time;
                            timeFromStart = timeToSegment + ((timeToNextSegment - timeToSegment) * (localDistance / line.LengthReal.Value));
                        }
                    }
                }

                // check first point.
                projected = points[idx];
                currentDistance = coordinates.Distance(projected);
                if (currentDistance < distance)
                { // this point is closer.
                    projectedCoordinates = projected;
                    entryIndex = idx;
                    distance = currentDistance;
                    distanceFromStart = currentDistanceFromStart;
                    if (this.HasTimes)
                    { // there should be proper timing information.
                        timeFromStart = this.Segments[idx].Time;
                    }
                }
                
                // update distance from start.
                currentDistanceFromStart = currentDistanceFromStart + points[idx].DistanceReal(points[idx + 1]).Value;
            }

            // check last point.
            projected = points[points.Count - 1];
            currentDistance = coordinates.Distance(projected);
            if (currentDistance < distance)
            { // this point is closer.
                projectedCoordinates = projected;
                entryIndex = points.Count - 1;
                distance = currentDistance;
                distanceFromStart = currentDistanceFromStart;
                if (this.HasTimes)
                { // there should be proper timing information.
                    timeFromStart = this.Segments[points.Count - 1].Time;
                }
            }
            return true;
        }
Exemple #9
0
 /// <summary>
 /// Calculates the closest point on the route relative to the given coordinate.
 /// </summary>
 /// <param name="coordinates"></param>
 /// <param name="projectedCoordinates"></param>
 /// <param name="distanceToProjected"></param>
 /// <param name="timeFromStart"></param>
 /// <returns></returns>
 public bool ProjectOn(GeoCoordinate coordinates, out GeoCoordinate projectedCoordinates, out Meter distanceToProjected, out Second timeFromStart)
 {
     int entryIdx;
     return this.ProjectOn(coordinates, out projectedCoordinates, out entryIdx, out distanceToProjected, out timeFromStart);
 }
        /// <summary>
        /// Creates a new route tracker animator.
        /// </summary>
        /// <param name="mapView">The mapview.</param>
        /// <param name="routeTracker">The tracker tracking the route.</param>
        /// <param name="restartAfterTouch">The time in second to wait before resuming tracking after the mapview is touched.</param>
        public RouteTrackerAnimator(IMapView mapView, RouteTracker routeTracker, Second restartAfterTouch)
        {
            this.MinZoom = 16;
            this.MaxZoom = 30;
            this.DefaultZoom = 17.5f;

            _mapView = mapView;
            _animator = new MapViewAnimator(mapView);
            _routeTracker = routeTracker;
            _minimumTrackGap = new TimeSpan (0, 0, 0, 0, 500).Ticks;

            this.RestartAfterTouch = restartAfterTouch;

            _mapView.MapTouched += MapViewMapTouched;
        }
Exemple #11
0
        /// <summary>
        /// Updates the tracker with the given location.
        /// </summary>
        /// <param name="location">The measured location.</param>
        public void Track(GeoCoordinate location)
        {
            // set the current location.
            _currentPosition = location;

            // calculate the total distance.
            var previous = new GeoCoordinate(_route.Segments[0].Latitude, _route.Segments[0].Longitude); ;
            var totalDistance = 0.0;
            for (int idx = 1; idx < _route.Segments.Length; idx++)
            {
                GeoCoordinate next = new GeoCoordinate(_route.Segments[idx].Latitude, _route.Segments[idx].Longitude);
                totalDistance = totalDistance + previous.DistanceReal(next).Value;
                previous = next;
            }
            double totalTime = _route.TotalTime;

            // project onto the route.
            int entryIdx;
            _route.ProjectOn(_currentPosition, out _currentRoutePosition, out entryIdx, out _distanceFromStart, out _timeFromStart);
            _distanceToEnd = totalDistance - _distanceFromStart;
            _timeToEnd = totalTime - _timeFromStart.Value;

            // find the next instruction.
            _nextInstructionIdx = -1;
            for (int instructionIdx = 0; instructionIdx < _instructions.Count; instructionIdx++)
            {
                var instruction = _instructions[instructionIdx];
                if (instruction.LastSegmentIdx > entryIdx)
                { // stop here!
                    _nextInstructionIdx = instructionIdx;
                    break;
                }
            }
            if(_nextInstructionIdx < 0)
            { // no instruction was found after the entryIdx: assume last instruction.
                _nextInstructionIdx = _instructions.Count - 1;
            }

            // calculate the distance to the next instruction.
            previous = _currentRoutePosition;
            var distance = 0.0;
            for (int idx = entryIdx + 1; idx <= _instructions[_nextInstructionIdx].LastSegmentIdx && idx < _route.Segments.Length; idx++)
            {
                var next = (new GeoCoordinate(_route.Segments[idx].Latitude, _route.Segments[idx].Longitude));
                distance = distance + previous.DistanceReal(next).Value;
                previous = next;
            }
            _distanceNextInstruction = distance;
        }
Exemple #12
0
 /// <summary>
 /// Creates a new min max VRP router.
 /// </summary>
 /// <param name="max"></param>
 /// <param name="delivery_time"></param>
 public RouterMaxTime(Second max, Second delivery_time)
 {
     this.Max = max;
     this.DeliveryTime = delivery_time;
 }
        /// <summary>
        /// Creates a new genetic min max no depot vrp router.
        /// </summary>
        /// <param name="max"></param>
        /// <param name="delivery_time"></param>
        /// <param name="population"></param>
        /// <param name="stagnation"></param>
        /// <param name="elitism_percentage"></param>
        /// <param name="cross_percentage"></param>
        /// <param name="mutation_percentage"></param>
        public RouterGeneticSimple(Second max, Second delivery_time, int population, int stagnation,
            double elitism_percentage, double cross_percentage, double mutation_percentage)
            : base(max, delivery_time)
        {
            _population = population;
            _stagnation = stagnation;

            _elitism_percentage = elitism_percentage;
            _cross_percentage = cross_percentage;
            _mutation_percentage = mutation_percentage;
        }
 /// <summary>
 /// Creates a new best placement min max no depot vrp router.
 /// </summary>
 /// <param name="max"></param>
 /// <param name="delivery_time"></param>
 /// <param name="k"></param>
 /// <param name="delta_percentage"></param>
 /// <param name="use_seed_cost"></param>
 /// <param name="threshold_precentage"></param>
 /// <param name="use_seed"></param>
 /// <param name="lambda"></param>
 public CheapestInsertionSolverWithImprovements(
     Second max, Second delivery_time, int k, float delta_percentage, bool use_seed_cost,
     float threshold_precentage, bool use_seed, float lambda)
     : this(max, delivery_time, k, delta_percentage, use_seed_cost, threshold_precentage,
     use_seed, lambda, true)
 {
 }
 /// <summary>
 /// Creates a new TSP placement solver.
 /// </summary>
 /// <param name="max"></param>
 /// <param name="delivery_time"></param>
 /// <param name="tsp_solution"></param>
 public TSPPlacementSolver(Second max, Second delivery_time,
     IRoute tsp_solution)
     : base(max, delivery_time)
 {
     _tsp_solution = tsp_solution;
 }
 /// <summary>
 /// Creates a new TSP placement solver.
 /// </summary>
 /// <param name="max"></param>
 /// <param name="delivery_time"></param>
 /// <param name="tsp_solver"></param>
 public TSPPlacementSolver(Second max, Second delivery_time,
     OsmSharp.Math.TSP.ISolver tsp_solver)
     : base(max, delivery_time)
 {
     _tsp_solver = tsp_solver;
 }
 /// <summary>
 /// Creates a new best placement min max no depot vrp router.
 /// </summary>
 /// <param name="max"></param>
 /// <param name="delivery_time"></param>
 public CheapestInsertionSolver(Second max, Second delivery_time)
     : base(max, delivery_time)
 {
 }
        /// <summary>
        /// Creates a new best placement min max no depot vrp router.
        /// </summary>
        /// <param name="max"></param>
        /// <param name="delivery_time"></param>
        /// <param name="k"></param>
        /// <param name="delta_percentage"></param>
        /// <param name="use_seed_cost"></param>
        /// <param name="threshold_precentage"></param>
        /// <param name="use_seed"></param>
        /// <param name="lambda"></param>
        /// <param name="intra_improvements"></param>
        /// <param name="inter_improvements"></param>
        public CheapestInsertionSolverWithImprovements(
            Second max, Second delivery_time, int k, float delta_percentage, bool use_seed_cost, 
            float threshold_precentage, bool use_seed, float lambda, 
            List<IImprovement> intra_improvements, List<IInterImprovement> inter_improvements)
            : base(max, delivery_time)
        {
            _k = k;
            _delta_percentage = delta_percentage;
            _use_seed_cost = use_seed_cost;
            _threshold_percentage = threshold_precentage;
            _use_seed = use_seed;
            _lambda = lambda;

            _intra_improvements = new List<IImprovement>();
            if(intra_improvements != null)
            {
                _intra_improvements.AddRange(intra_improvements);
            }

            _inter_improvements = new List<IInterImprovement>();
            if (inter_improvements != null)
            {
                _inter_improvements.AddRange(inter_improvements);
            }
        }
        /// <summary>
        /// Creates a new best placement min max no depot vrp router.
        /// </summary>
        /// <param name="max"></param>
        /// <param name="delivery_time"></param>
        /// <param name="k"></param>
        /// <param name="delta_percentage"></param>
        /// <param name="use_seed_cost"></param>
        /// <param name="threshold_precentage"></param>
        /// <param name="use_seed"></param>
        /// <param name="lambda"></param>
        /// <param name="use_improvements"></param>
        public CheapestInsertionSolverWithImprovements(
            Second max, Second delivery_time, int k, float delta_percentage, bool use_seed_cost, 
            float threshold_precentage, bool use_seed, float lambda, bool use_improvements)
            : base(max, delivery_time)
        {
            _k = k;
            _delta_percentage = delta_percentage;
            _use_seed_cost = use_seed_cost;
            _threshold_percentage = threshold_precentage;
            _use_seed = use_seed;
            _lambda = lambda;

            _intra_improvements = new List<IImprovement>();
            if (use_improvements)
            {
                //_intra_improvements.Add(
                //    new OsmSharp.Math.TSP.ArbitraryInsertion.ArbitraryInsertionSolver());
                _intra_improvements.Add(
                    new HillClimbing3OptSolver(true, true));
            }

            _inter_improvements = new List<IInterImprovement>();
            if (use_improvements)
            {
                _inter_improvements.Add(
                    new ExchangeInterImprovement());
                _inter_improvements.Add(
                    new RelocateImprovement());
                //_inter_improvements.Add(
                //    new TwoOptInterImprovement());
                _inter_improvements.Add(
                    new RelocateExchangeInterImprovement());
                _inter_improvements.Add(
                    new CrossExchangeInterImprovement());
            }
        }
Exemple #20
0
        /// <summary>
        /// Implements the actual logic.
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="customers"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public MaxTimeSolution DoCalculation(MaxTimeProblem problem,
            ICollection<int> customers, Second max)
        {
            MaxTimeSolution routes = this.Solve(problem);

            return routes;
        }