Example #1
0
 public DistanceItem()
 {
     Item1          = new ItemBin();
     Item2          = new ItemBin();
     SquareDistance = 0;
     Distance       = 0;
 }
Example #2
0
 public DistanceItem(ItemBin bin1, ItemBin bin2)
 {
     Item1          = bin1;
     Item2          = bin2;
     SquareDistance = Item1.Location.GetSquareDistance(Item2.Location);
     Distance       = System.Math.Sqrt(SquareDistance);
 }
Example #3
0
 public Route(ItemBin bin, double dist = 0)
 {
     StartPoint = bin;
     Length     = dist;
 }
Example #4
0
 public Route()
 {
     StartPoint = new ItemBin();
     Length     = 0;
 }
Example #5
0
        private List <Route> NearestAddition(Request request)
        {
            // Initial result contains the starting position of the robot ang route length 0
            var result = new List <Route>()
            {
                new Route()
            };

            if (request.Targets.Count > 0)
            {
                // Create copy of targets so that the original version doesn't change
                var targets = new List <int>();
                request.Targets.ForEach(t => targets.Add(t));

                // Go through the current route and find the nearest requested bin to add
                // Add first target
                ItemBin      currentBestBin      = null;
                DistanceItem currentBestDistance = null;
                foreach (var target in targets)
                {
                    var bin          = Layout.ItemBins.Find(b => b.Identifier == target);
                    var distanceItem = Layout.DistanceTable.Find(d => (d.Item1.Identifier == 0 && d.Item2.Identifier == target) || (d.Item1.Identifier == target && d.Item2.Identifier == 0));
                    if (bin != null && distanceItem != null && (currentBestDistance == null || distanceItem.SquareDistance < currentBestDistance.SquareDistance))
                    {
                        currentBestBin      = bin;
                        currentBestDistance = distanceItem;
                    }
                }
                if (currentBestBin != null && currentBestDistance != null)
                {
                    // Update distance to adjacent bin
                    result[0].Length = currentBestDistance.Distance;
                    // Add new bin
                    result.Add(new Route(currentBestBin, currentBestDistance.Distance));
                    // Remove found target
                    targets.RemoveAll(t => t == currentBestBin.Identifier);
                }

                var allTargetsAdded = (targets.Count == 0);
                while (!allTargetsAdded)
                {
                    currentBestBin = null;
                    DistanceItem distLeft                 = null;
                    DistanceItem distRight                = null;
                    DistanceItem currentBestLeft          = null;
                    DistanceItem currentBestRight         = null;
                    double       currentBestAddedDistance = -1;
                    int          currentPosToAdd          = -1;
                    for (int i = 0; i < result.Count; i++)
                    {
                        // For each pair of adjacent points in the route,
                        // find a target that increases the route distance the least
                        var currentRouteItem = result[i];
                        var nextRouteItem    = (i + 1 >= result.Count) ? result[0] : result[i + 1];
                        foreach (var target in targets)
                        {
                            // Fing the nearest target for this pair
                            distLeft = Layout.DistanceTable.Find(d => (d.Item1.Identifier == currentRouteItem.StartPoint.Identifier && d.Item2.Identifier == target) ||
                                                                 (d.Item1.Identifier == target && d.Item2.Identifier == currentRouteItem.StartPoint.Identifier));
                            distRight = Layout.DistanceTable.Find(d => (d.Item1.Identifier == nextRouteItem.StartPoint.Identifier && d.Item2.Identifier == target) ||
                                                                  (d.Item1.Identifier == target && d.Item2.Identifier == nextRouteItem.StartPoint.Identifier));
                            if (currentBestAddedDistance < 0 || currentBestAddedDistance > (distLeft.Distance + distRight.Distance - currentRouteItem.Length))
                            {
                                currentPosToAdd          = i;
                                currentBestAddedDistance = distLeft.Distance + distRight.Distance - currentRouteItem.Length;
                                currentBestBin           = (distLeft.Item1.Identifier == target) ? distLeft.Item1 : distLeft.Item2;
                                currentBestLeft          = distLeft;
                                currentBestRight         = distRight;
                            }
                        }
                    }
                    if (currentBestBin != null && currentBestLeft != null && currentBestRight != null &&
                        currentBestAddedDistance > -1 && currentPosToAdd >= 0)
                    {
                        // Update distance to adjacent bin
                        result[currentPosToAdd].Length = currentBestLeft.Distance;
                        // Add new bin between i and i + 1
                        result.Insert(currentPosToAdd + 1, new Route(currentBestBin, currentBestRight.Distance));
                        // Remove found target
                        targets.RemoveAll(t => t == currentBestBin.Identifier);
                    }
                    allTargetsAdded = (targets.Count == 0);
                }
            }
            return(result);
        }