示例#1
0
 public void addPoint(TSPPoint p)
 {
     points.Add(p);
     position.Add(p.ID, points.Count - 1);
     distances.Add(new List <double>(distances.Count + 1));
     for (int i = 0; i < points.Count - 1; i++)
     {
         double distance_i_new = computeDistance(i, points.Count - 1);
         distances[i].Add(distance_i_new);
         distances[distances.Count - 1].Add(distance_i_new);
         if (distance_i_new > maximumDistance)
         {
             maximumDistance = distance_i_new;
         }
         if (distance_i_new < maximumDistance)
         {
             minimumDistance = distance_i_new;
         }
     }
     distances[distances.Count - 1].Add(0d);
     if (p.x > maxValueX)
     {
         maxValueX = p.x;
     }
     if (p.y > maxValueY)
     {
         maxValueY = p.y;
     }
 }
示例#2
0
        public void draw(TSPSolution sol, bool clear = true)
        {
            draw(sol.inp, clear);
            int j = 0, previous = 0;

            for (int i = 0; i < sol.inp.nodesCount; i++)
            {
                TSPPoint first = sol.inp.getPoint(j), second = sol.inp.getPoint(sol.getSuccessor(j, previous));
                Pen      pen = Pens.BlueViolet;
                for (int k = 0; k < sol.inp.nodesCount; k++)
                {
                    if (Edge.isCrossing(first, second, sol.inp.getPoint(k), sol.inp.getPoint(sol.getSuccessor(k))) &&
                        Edge.isCrossing(sol.inp.getPoint(k), sol.inp.getPoint(sol.getSuccessor(k)), first, second))
                    {
                        pen = Pens.Red;
                        break;
                    }
                }
                //TODO vybarvit cervene ty co se krizi - hotovo
                g.DrawLine(pen, (float)(first.x * xStretch + nodeSize / 2), (float)(first.y * yStretch + nodeSize / 2),
                           (float)(second.x * xStretch + nodeSize / 2), (float)(second.y * yStretch + nodeSize / 2));
                int pom = j;
                j        = sol.getSuccessor(j, previous);
                previous = pom;
            }
            screen.Refresh();
        }
示例#3
0
        public static bool isCrossing(TSPPoint A, TSPPoint B, TSPPoint C, TSPPoint D)
        {
            double p = (B.y - A.y) / (B.x - A.x),
                   q = A.y - p * A.x;

            return(isCrossing(A, B, C, D, p, q));
        }
示例#4
0
文件: TSPInput.cs 项目: vvancak/ai
        private Dictionary <int, int> position;  // positions of the points in "points" indexed by their IDs

        #endregion

        #region constructorsAndFactories

        private TSPInput()
        {
            TSPPoint.resetCounter();
            this.distances = new List <List <double> >();
            this.points    = new List <TSPPoint>();
            this.position  = new Dictionary <int, int>();
        }
示例#5
0
        public static TSPInput generateHybrid(int nodesCount, int clusters, int maxWidth = 1000, int maxHeight = 1000)
        {
            r = new Random();
            TSPInput      result = TSPInput.create();
            List <double> clustersX = new List <double>(), clustersY = new List <double>();
            double        x, y;

            for (int i = 0; i < clusters; i++)
            {
                do
                {
                    x = r.NextDouble() * maxWidth - maxWidth / (2 * clusters - 1);
                    y = r.NextDouble() * maxHeight - maxHeight / (2 * clusters - 1);
                } while (x < 0 || y < 0);
                clustersX.Add(x);
                clustersY.Add(y);
            }
            for (int j = 0; j < clusters; j++)
            {
                int clusterSize = r.Next(nodesCount / (clusters - 1));
                for (int i = 0; i < clusterSize; i++)
                {
                    TSPPoint p = TSPPoint.create(clustersX[j] + r.NextDouble() * maxWidth / clusters,
                                                 clustersY[j] + r.NextDouble() * maxHeight / clusters);
                    result.addPoint(p);
                }
            }
            while (result.nodesCount < nodesCount)
            {
                TSPPoint p = TSPPoint.create(r.NextDouble() * maxWidth, r.NextDouble() * maxHeight);
                result.addPoint(p);
            }
            return(result);
        }
示例#6
0
        public static TSPInput generateClustersAntiGreedy(int nodesCount, int clusters, int maxWidth = 1000, int maxHeight = 1000)
        {
            r = new Random();
            TSPInput      result = TSPInput.create();
            List <double> clustersX = new List <double>(), clustersY = new List <double>();
            double        x, y;

            for (int i = 0; i < clusters; i++)
            {
                do
                {
                    x = r.NextDouble() * maxWidth - maxWidth / (2 * clusters);
                    y = r.NextDouble() * maxHeight - maxHeight / (2 * clusters);
                } while (x < 0 || y < 0);
                clustersX.Add(x);
                clustersY.Add(y);
            }
            for (int j = 0; j < clusters; j++)
            {
                for (int i = 0; i < (nodesCount - 4) / clusters; i++)
                {
                    TSPPoint p = TSPPoint.create(clustersX[j] + r.NextDouble() * maxWidth / clusters,
                                                 clustersY[j] + r.NextDouble() * maxHeight / clusters);
                    result.addPoint(p);
                }
            }
            result.addPoint(TSPPoint.create(0, 0));
            result.addPoint(TSPPoint.create(1000, 0));
            result.addPoint(TSPPoint.create(0, 1000));
            result.addPoint(TSPPoint.create(1000, 1000));
            return(result);
        }
示例#7
0
 private TSPInput()
 {
     TSPPoint.resetCounter();
     this.distances       = new List <List <double> >();
     this.points          = new List <TSPPoint>();
     this.position        = new Dictionary <int, int>();
     this.maximumDistance = double.MinValue;
     this.minimumDistance = double.MaxValue;
 }
示例#8
0
        public static TSPInput generateUniform(int nodesCount, int maxWidth = 1000, int maxHeight = 1000)
        {
            //r = new Random();
            TSPInput result = TSPInput.create();

            for (int i = 0; i < nodesCount; i++)
            {
                TSPPoint p = TSPPoint.create(r.NextDouble() * maxWidth, r.NextDouble() * maxHeight);
                result.addPoint(p);
            }
            return(result);
        }
示例#9
0
 private void drawNode(TSPPoint p)
 {
     if (showNumbers)
     {
         g.DrawRectangle(Pens.Black, (float)(p.x * xStretch), (float)(p.y * yStretch), 2, 2);
         g.DrawString(p.ID.ToString(), new Font("Arial", 15), Brushes.Blue, new PointF(
                          (float)(p.x * xStretch) - nodeSize, (float)(p.y * yStretch) - nodeSize));
     }
     else
     {
         g.DrawRectangle(Pens.Black, (float)(p.x * xStretch), (float)(p.y * yStretch), nodeSize, nodeSize);
     }
 }
示例#10
0
        public static bool isCrossing(TSPPoint A, TSPPoint B, TSPPoint C, TSPPoint D, double p, double q)
        {
            if (B.ID == C.ID || A.ID == C.ID || A.ID == D.ID || B.ID == D.ID)
            {
                return(false);
            }
            double res1 = p * C.x + q - C.y,
                   res2 = p * D.x + q - D.y;

            if ((res1 < 0 && res2 > 0) || (res1 > 0 && res2 < 0))
            {
                return(true);
            }
            return(false);
            //return (res1 < 0 && res2 > 0) || (res1 > 0 && res2 < 0);
        }
示例#11
0
        private bool isCrossing(TSPInput inp, List <int> currentEdges)
        {
            if (currentEdges.Count <= 2)
            {
                return(false);
            }
            TSPPoint A = inp.getPoint(currentEdges[currentEdges.Count - 2]),
                     B = inp.getPoint(currentEdges[currentEdges.Count - 1]);
            double p   = (B.y - A.y) / (B.x - A.x),
                   q   = A.y - p * A.x;

            for (int i = 0; i < currentEdges.Count - 2; i++)
            {
                if (Edge.isCrossing(A, B, inp.getPoint(currentEdges[i]), inp.getPoint(currentEdges[i + 1]), p, q) &&
                    Edge.isCrossing(inp.getPoint(currentEdges[i]), inp.getPoint(currentEdges[i + 1]), A, B))
                {
                    return(true);
                }
            }
            return(false);
        }
示例#12
0
        public void drawSpanningTree(TSPInput input, Dictionary <int, List <int> > successors)
        {
            draw(input);
            HashSet <int> done = new HashSet <int>();

            for (int i = 0; i < input.nodesCount; i++)
            {
                foreach (var item in successors[i])
                {
                    if (done.Contains(item))
                    {
                        continue;
                    }
                    TSPPoint first = input.getPoint(i), second = input.getPoint(item);
                    var      p = new Pen(Brushes.Yellow, 2);
                    g.DrawLine(p, (float)(first.x * xStretch + nodeSize / 2), (float)(first.y * yStretch + nodeSize / 2),
                               (float)(second.x * xStretch + nodeSize / 2), (float)(second.y * yStretch + nodeSize / 2));
                }
                done.Add(i);
            }
            screen.Refresh();
        }
示例#13
0
 public void renewID()
 {
     this.ID = TSPPoint.getID();
 }
示例#14
0
 private void fillNode(TSPPoint p, Color c)
 {
     g.FillRectangle(new SolidBrush(c), (float)(p.x * xStretch), (float)(p.y * yStretch), nodeSize, nodeSize);
     g.DrawRectangle(Pens.Black, (float)(p.x * xStretch), (float)(p.y * yStretch), nodeSize, nodeSize);
 }