Esempio n. 1
0
        protected double airDistance(int point1, int point2, TSPInput input)
        {
            var p1 = input.getPoint(point1);
            var p2 = input.getPoint(point2);

            return(Math.Sqrt(Math.Sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y))));
        }
Esempio n. 2
0
        public void draw(TSPInput inp, bool clear = true)
        {
            if (clear)
            {
                g.Clear(Color.WhiteSmoke);
            }
            xStretch = width / (inp.maxValueX + 20);
            yStretch = height / (inp.maxValueY + 20);
            if (inp.nodesCount > 2000)
            {
                nodeSize--;
            }
            if (inp.nodesCount > 10000)
            {
                nodeSize--;
            }


            for (int i = 0; i < inp.nodesCount; i++)
            {
                var node = inp.getPoint(i);
                drawNode(node);
            }
            fillNode(inp.getPoint(0), Color.Green);
            screen.Refresh();
        }
Esempio n. 3
0
 public Edge(int from, int to, TSPInput inp, double weight)
 {
     this.from   = from;
     this.to     = to;
     this.weight = weight;
     this.inp    = inp;
     this.a      = (inp.getPoint(to).y - inp.getPoint(from).y) / (inp.getPoint(to).x - inp.getPoint(from).x);
     this.b      = inp.getPoint(to).y - inp.getPoint(to).x *a;
 }
Esempio n. 4
0
 public Edge(int from, int to, TSPInput inp)
 {
     this.from   = from;
     this.to     = to;
     this.weight = inp.getDistance(from, to);
     this.inp    = inp;
     this.a      = (inp.getPoint(to).y - inp.getPoint(from).y) / (inp.getPoint(to).x - inp.getPoint(from).x);
     this.b      = inp.getPoint(to).y - inp.getPoint(to).x *a;
 }
Esempio n. 5
0
        public bool intersectsWith(Edge other)
        {
            //return isCrossing(inp.getPoint(from), inp.getPoint(to), other.inp.getPoint(other.from), other.inp.getPoint(other.to));

            return((a * inp.getPoint(other.from).x + b < inp.getPoint(other.from).y ^
                    a * inp.getPoint(other.to).x + b < inp.getPoint(other.to).y) &&
                   (other.a * inp.getPoint(this.from).x + other.b < inp.getPoint(this.from).y ^
                    other.a * inp.getPoint(this.to).x + other.b < inp.getPoint(this.to).y));
        }
Esempio n. 6
0
        private void computeCentroid(TSPInput input)
        {
            angles = new double[input.nodesCount];
            double sumX = 0, sumY = 0;

            for (int i = 0; i < input.nodesCount; i++)
            {
                sumX += input.getPoint(i).x;
                sumY += input.getPoint(i).y;
            }
            centroidX = sumX / input.nodesCount;
            centroidY = sumY / input.nodesCount;
            for (int i = 0; i < input.nodesCount; i++)
            {
                angles[i] = Math.Atan2(input.getPoint(i).x - centroidX, input.getPoint(i).y - centroidY);
            }
            this.comparer = new referenceComparer(angles);
        }
Esempio n. 7
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();
        }
Esempio n. 8
0
File: Form1.cs Progetto: vvancak/ai
        private void Extern_button_Click(object sender, EventArgs e)
        {
            Process p = new Process();

            //p.StartInfo.FileName = @"C:\Documents and Settings\Ota\Dokumenty\Visual Studio 2010\Projects\MinesVisualizer\bin\Debug\MinesVisualizer.exe";
            p.StartInfo.FileName               = @".\solver.exe";
            p.StartInfo.UseShellExecute        = false;
            p.StartInfo.RedirectStandardInput  = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.Start();
            p.StandardInput.WriteLine(inp.nodesCount);
            for (int i = 0; i < inp.nodesCount; i++)
            {
                p.StandardInput.WriteLine((int)(Math.Floor(inp.getPoint(i).x)) + " " + (int)(Math.Floor(inp.getPoint(i).y)));
            }
            string result = p.StandardOutput.ReadLine();

            solution = TSPSolution.fromString(result, inp);
            solution.computeDistance();
            Length_label.Text = solution.totalDistance.ToString();
            vis.draw(solution);
        }
Esempio n. 9
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);
        }
Esempio n. 10
0
 private double getDistanceSquared(int pointIndex)
 {
     return((inp.getPoint(pointIndex).x - centroidX) * (inp.getPoint(pointIndex).x - centroidX) +
            (inp.getPoint(pointIndex).y - centroidY) * (inp.getPoint(pointIndex).y - centroidY));
 }