Example #1
0
        private void GeneratePointsToBeMoved(IDictionary<byte, IList<Point>> pointsDict, out List<Point> pointsToBeMoved, out IList<Path> paths, out IList<Path> target)
        {
            int k = TspInstanceConstants.K_VALUE;

            Random r = new Random();
            int id = r.Next(NumberOfPoints);
            //id = (firstToBeMoved++)%100;
            pointsToBeMoved = new List<Point>();
            Group toBeMoved = new Group(5);
            var allPoints = CreateAllPoints(pointsDict);

            //pointsDict.ke
            Point firstToMove = allPoints[id];
            pointsToBeMoved.Add(firstToMove);
            toBeMoved.AddPoint(firstToMove);

            //pointsToBeMoved.AddRange(allPoints.Except(pointsToBeMoved).OrderBy(p => p.Distance(firstToMove)).Take(k - 1));
            Point tmp;
            for (int i = 0; i < k - 1; i++)
            {
                tmp = allPoints.Except(pointsToBeMoved).OrderBy(p => p.Distance(toBeMoved.centerOfMass)).First();
                toBeMoved.AddPoint(tmp);
                pointsToBeMoved.Add(tmp);
            }

            paths = new List<Path>();
            target = new List<Path>();
            var pointsFromJoinedPaths = new List<Point>();
            foreach (var item in pointsToBeMoved)
            {
                bool added = false;
                foreach (var path in paths)
                {
                    if (path.points.First().id == item.id)
                    {
                        int pos = pointsDict[item.groupId].IndexOf(item);
                        if (pos == 0)
                            pos = TspInstanceConstants.NUMBER_OF_POINTS_PER_GROUP;
                        path.points.AddFirst(pointsDict[item.groupId][pos - 1]);
                        added = true;
                    }
                    else if (path.points.Last().id == item.id)
                    {
                        int pos = pointsDict[item.groupId].IndexOf(item);
                        if (pos == TspInstanceConstants.NUMBER_OF_POINTS_PER_GROUP - 1)
                            pos = -1;
                        path.points.AddLast(pointsDict[item.groupId][pos + 1]);
                        added = true;
                    }
                    else if (path.points.Contains(item))
                    {
                        added = true;
                    }
                }
                if (!added)
                {

                    Path newPath = new Path();
                    int prev = pointsDict[item.groupId].IndexOf(item);
                    int next = prev;
                    if (prev == 0)
                        prev = TspInstanceConstants.NUMBER_OF_POINTS_PER_GROUP;

                    newPath.points.AddFirst(pointsDict[item.groupId][prev - 1]);
                    newPath.points.AddLast(item);

                    if (next == TspInstanceConstants.NUMBER_OF_POINTS_PER_GROUP - 1)
                        next = -1;
                    newPath.points.AddLast(pointsDict[item.groupId][next + 1]);
                    paths.Add(newPath);
                }
                while (true)
                {
                    Path toRemove = null;
                    foreach (var path in paths)
                    {
                        foreach (var innerPath in paths)
                            if (path.points.Last.Value.id == innerPath.points.First.Value.id)
                            {
                                pointsFromJoinedPaths.Add(path.points.Last.Value);
                                k++;
                                foreach (var point in innerPath.points.Skip(1))
                                {
                                    path.points.AddLast(point);
                                }
                                toRemove = innerPath;
                            }
                        if (toRemove != null)
                            break;
                    }

                    if (toRemove == null)
                        break;
                    else
                        paths.Remove(toRemove);
                }

            }

            foreach (var path in paths)
            {
                if (pointsToBeMoved.Contains(path.points.First.Value) || pointsToBeMoved.Contains(path.points.Last.Value))
                    throw new Exception("Unknown error! PLEASE TRY AGAIN!");
            }

            pointsToBeMoved.AddRange(pointsFromJoinedPaths);

            foreach (var path in paths)
                target.Add(new Path(path.points.First.Value));

            //setting groupId for all points
            for (byte i = 0; i < 4; i++)
            {
                foreach (var point in pointsDict[i])
                {
                    point.groupId = i;
                }
            }
        }
Example #2
0
        //private IDictionary<byte, IList<Point>> CreateRandomGroups(IList<Point> allPoints)
        //{
        //    IDictionary<byte, IList<Point>> pointsDict = new Dictionary<byte, IList<Point>>();
        //    pointsDict[0] = new List<Point>();
        //    pointsDict[1] = new List<Point>();
        //    pointsDict[2] = new List<Point>();
        //    pointsDict[3] = new List<Point>();
        //    for (var i = 0; i < allPoints.Count; i++)
        //    {
        //        allPoints[i].groupId = (byte)(i / 25);
        //        pointsDict[(byte)(i / 25)].Add(allPoints[i]);
        //    }
        //    var changes = 100;
        //    var rand = new Random();
        //    while (changes > 0)
        //    {
        //        var g1 = (byte)rand.Next(4);
        //        var g2 = (byte)rand.Next(4);
        //        var e1 = rand.Next(TspInstanceConstants.NUMBER_OF_POINTS_PER_GROUP);
        //        var e2 = rand.Next(TspInstanceConstants.NUMBER_OF_POINTS_PER_GROUP);
        //        var tmp = pointsDict[g1][e1];
        //        tmp.groupId = g2;
        //        pointsDict[g1][e1] = pointsDict[g2][e2];
        //        pointsDict[g1][e1].groupId = g1;
        //        pointsDict[g2][e2] = tmp;
        //        changes--;
        //    }
        //    return pointsDict;
        //}
        private IDictionary<byte, IList<Point>> CreateGroups(IList<Point> allPoints)
        {
            IDictionary<byte, IList<Point>> pointsDict = new Dictionary<byte, IList<Point>>();
            Random rand = new Random();
            int pos = rand.Next(NumberOfPoints);
            //int pos = 91;
            groups[0] = new Group(0);
            pointsDict[0] = new List<Point>();
            pointsDict[0].Add(allPoints[pos]);
            allPoints[pos].groupId = 0;
            groups[0].AddPoint(allPoints[pos]);
            centerOfMass.AddPoint(allPoints[pos]);
            allPoints.RemoveAt(pos);
            Point point;
            for (byte groupIndex = 1; groupIndex < 4; groupIndex++)
            {
                pointsDict[groupIndex] = new List<Point>();
                groups[groupIndex] = new Group(groupIndex);
                point = allPoints.OrderBy(p => p.Distance(centerOfMass)).Last();
                point.groupId = groupIndex;
                //int max = allPoints.Max(p => p.Distance(centerOfMass));
                groups[groupIndex].AddPoint(point);
                pointsDict[groupIndex].Add(point);
                centerOfMass.AddPoint(point);
                allPoints.Remove(point);
            }

            while (allPoints.Any())
            {
                // 4 | allPoints.Count()
                for (byte groupIndex = 0; groupIndex < 4; groupIndex++)
                {
                    point = allPoints.OrderBy(p => p.Distance(groups[groupIndex].centerOfMass)).First();
                    point.groupId = groupIndex;
                    groups[groupIndex].AddPoint(point);
                    pointsDict[groupIndex].Add(point);
                    allPoints.Remove(point);
                }
            }

            return pointsDict;
        }