Exemplo n.º 1
0
        public static void SaveGraph(WOGraph graph, string filePath)
        {
            using (System.IO.StreamWriter output = new System.IO.StreamWriter(filePath))
            {
                output.WriteLine(graph.VerticesNumber.ToString());

                for (int i = 0; i < graph.VerticesNumber; i++)
                {
                    WOGraphVertice vertice = graph.GetVerticeAt(i);
                    output.WriteLine("{0} {1} {2}", vertice.Label, vertice.Coordinates.X, vertice.Coordinates.Y);
                }

                for (int i = 0; i < graph.VerticesNumber; i++)
                {
                    string buffer = "";
                    for (int j = 0; j < graph.VerticesNumber; j++)
                    {
                        buffer += graph.GetEdgeWeightAt(i, j).ToString();
                        if (j != graph.VerticesNumber - 1)
                        {
                            buffer += " ";
                        }
                    }
                    output.WriteLine(buffer);
                }

                output.Close();
            }
        }
Exemplo n.º 2
0
 public Population(int capacity, WOGraph graph)
 {
     this.capacity       = capacity;
     size                = 0;
     toursPopulation     = new Tour[capacity];
     this.graph          = graph;
     random              = new ThreadSafeRandom();
     mutationProbability = 25;
 }
Exemplo n.º 3
0
        private void buttonLoadGraph_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.ShowDialog();
            if (dialog.FileName != "")
            {
                graph = FileAdapter.LoadGraph(dialog.FileName);
                textBoxVerticesNumber.Text = graph.VerticesNumber.ToString();
            }
        }
Exemplo n.º 4
0
        static public void DrawWOGraph(WOGraph graph, PictureBox canvas)
        {
            Bitmap   bitmap   = new Bitmap(canvas.Image);
            Graphics graphics = Graphics.FromImage(bitmap);

            for (int i = 0; i < graph.VerticesNumber; i++)
            {
                for (int j = 0; j < graph.VerticesNumber; j++)
                {
                    graphics.DrawLine(Pens.Black, graph.GetVerticeAt(i).Coordinates,
                                      graph.GetVerticeAt(j).Coordinates);
                }
            }

            for (int i = 0; i < graph.VerticesNumber; i++)
            {
                graphics.FillEllipse(Brushes.Red, graph.GetVerticeAt(i).Coordinates.X - 5,
                                     graph.GetVerticeAt(i).Coordinates.Y - 5, 10, 10);
            }

            canvas.Image = bitmap;
        }
Exemplo n.º 5
0
        public static WOGraph LoadGraph(string filePath)
        {
            WOGraph graph;

            using (System.IO.StreamReader input = new System.IO.StreamReader(filePath))
            {
                int verticesNumber = Convert.ToInt32(input.ReadLine());

                graph = new WOGraph(0);

                for (int i = 0; i < verticesNumber; i++)
                {
                    string   buffer      = input.ReadLine();
                    string[] bufferArray = new string[3];
                    bufferArray = buffer.Split(' ');
                    WOGraphVertice vertice = new WOGraphVertice(bufferArray[0],
                                                                Convert.ToInt32(bufferArray[1]),
                                                                Convert.ToInt32(bufferArray[2]));
                    graph.AddVertice(vertice);
                }

                for (int i = 0; i < verticesNumber; i++)
                {
                    string   buffer      = input.ReadLine();
                    string[] bufferArray = new string[verticesNumber];
                    bufferArray = buffer.Split(' ');

                    for (int j = 0; j < verticesNumber; j++)
                    {
                        graph.SetWeightAt(i, j, Convert.ToDouble(bufferArray[j]));
                    }
                }

                input.Close();
            }

            return(graph);
        }
Exemplo n.º 6
0
        static public void DrawTour(Tour tour, WOGraph graph, PictureBox canvas, bool drawLabels = false)
        {
            Bitmap   bitmap   = new Bitmap(canvas.Image);
            Graphics graphics = Graphics.FromImage(bitmap);

            float arrowCapSize          = 4;
            AdjustableArrowCap arrowCap = new AdjustableArrowCap(arrowCapSize, arrowCapSize * 2);
            Pen pen = new Pen(Brushes.Red);

            pen.CustomEndCap = arrowCap;
            pen.Width        = 2;

            for (int i = 0; i < tour.VerticesNumber; i++)
            {
                graphics.FillEllipse(Brushes.Blue, graph.GetVerticeAt(i).Coordinates.X - 5,
                                     graph.GetVerticeAt(i).Coordinates.Y - 5, 10, 10);
            }

            for (int i = 0; i < tour.VerticesNumber; i++)
            {
                graphics.DrawLine(pen, graph.GetVerticeAt(i).Coordinates,
                                  graph.GetVerticeAt(tour.IndexAt(i)).Coordinates);
            }

            if (drawLabels == true)
            {
                Font font = new Font("Arial", 9);
                for (int i = 0; i < tour.VerticesNumber; i++)
                {
                    graphics.DrawString(graph.GetVerticeAt(i).Label, font, Brushes.Black, graph.GetVerticeAt(i).Coordinates.X + 10,
                                        graph.GetVerticeAt(i).Coordinates.Y - 10);
                }
            }

            canvas.Image = bitmap;
        }
Exemplo n.º 7
0
        private void buttonGenerateGraph_Click(object sender, EventArgs e)
        {
            graph = new WOGraph(verticesNumber);
            if (drawRegion == true)
            {
                string[] temp = region.Split(',');
                graph.FillRandomDataInRegion(CANVAS_WIDTH, CANVAS_HIGHT,
                                             Convert.ToInt32(temp[0]),
                                             Convert.ToInt32(temp[1]),
                                             Convert.ToInt32(temp[2]),
                                             Convert.ToInt32(temp[3]));
            }

            if (drawBezier == true)
            {
                graph.FillRandomDataOnBezier(CANVAS_WIDTH, CANVAS_HIGHT, bezierCurve);
                Painter.DrawBezier(bezierCurve, pictureBoxCanvas);
                textBoxBezierLength.Text = graph.GetBezierLength().ToString();
            }
            if (drawBezier == false && drawRegion == false)
            {
                graph.FillRandomData(CANVAS_WIDTH, CANVAS_HIGHT);
            }
        }
Exemplo n.º 8
0
        private void pictureBoxCanvas_MouseClick(object sender, MouseEventArgs e)
        {
            for (int i = 0; graph != null && i < graph.VerticesNumber; i++)
            {
                WOGraphVertice vertice = graph.GetVerticeAt(i);

                if ((e.X < vertice.Coordinates.X + 5 && e.X > vertice.Coordinates.X - 5) &&
                    (e.Y < vertice.Coordinates.Y + 5 && e.Y > vertice.Coordinates.Y - 5))
                {
                    dragVertice   = true;
                    verticeToDrag = i;
                    break;
                }
            }

            if (dragVertice != true)
            {
                Painter.DrawPoint(new Point(e.X, e.Y), pictureBoxCanvas);

                if (graph == null)
                {
                    graph = new WOGraph(0);
                }

                graph.AddVertice(e.X, e.Y);

                textBoxVerticesNumber.Text = graph.VerticesNumber.ToString();
            }
            else
            {
                if (e.Button == MouseButtons.Right)
                {
                    dragVertice = false;
                }
            }
        }
Exemplo n.º 9
0
 private void buttonEraseGraph_Click(object sender, EventArgs e)
 {
     graph = null;
     textBoxVerticesNumber.Text = "0";
 }
Exemplo n.º 10
0
 public TourComparer(WOGraph graph)
 {
     this.graph = graph;
 }