コード例 #1
0
        private void buttonHeuristics_Click(object sender, EventArgs e)
        {
            textBox1.Text += "Heuristics:";
            if (boxes.Count < 1)
            {
                textBox1.Text += Environment.NewLine +
                                 "No boxes - no job" +
                                 Environment.NewLine;

                return;
            }

            List <Disposition> open, closed;

            open   = new List <Disposition>();
            closed = new List <Disposition>();

            Disposition initState =
                new Disposition(null, boxes.Count, FullCost);

            float       minCost       = float.MaxValue;
            Disposition minCostDispos = null;

            // where to collect

            int sumX = 0, sumY = 0;

            for (int i = 0; i < boxes.Count; i++)
            {
                sumX += boxes[i].X;
                sumY += boxes[i].Y;
            }

            Point mid = new Point(sumX / boxes.Count, sumY / boxes.Count);

            float minFC   = float.MaxValue;
            int   i_where = -1;

            for (int i = 0; i < boxes.Count; i++)
            {
                float fc = FullCost(mid, boxes[i]);
                if (fc < minFC)
                {
                    minFC   = fc;
                    i_where = i;
                }
            }

            {
                open.Clear();

                // from which to start
                float nearLength = float.MaxValue;
                int   i_from     = -1;
                for (int i = 0; i < boxes.Count; i++)
                {
                    float fc = FullCost(robot, boxes[i]);
                    if (fc < nearLength)
                    {
                        nearLength = fc;
                        i_from     = i;
                    }
                }

                //for (int i_from = 0; i_from < boxes.Count; i_from++)
                {
                    Disposition t = new Disposition(initState);
                    t.GoTo(i_from);
                    closed.Add(t);
                    t = new Disposition(t);
                    t.DragTo(i_where);
                    open.Add(t);
                }

                while (open.Count > 0)
                {
                    int         q = 0;
                    Disposition t = open[q];

                    while (true)
                    {
                        int u = t.FindUncollected(i_where);
                        if (u == -1)
                        {
                            break;
                        }

                        t = new Disposition(t);
                        t.GoTo(u);
                        closed.Add(t);

                        t = new Disposition(t);
                        t.DragTo(i_where);
                        closed.Add(t);
                    }

                    if (minCost > t.TotalCost)
                    {
                        minCost       = t.TotalCost;
                        minCostDispos = t;
                    }

                    closed.Add(open[q]);
                    open.RemoveAt(q);
                }

                textBox1.Text += Environment.NewLine +
                                 minCostDispos.TotalCost.ToString("F2");
            }

            if (minCostDispos == null)
            {
                textBox1.Text += Environment.NewLine +
                                 "No solution" +
                                 Environment.NewLine;

                return;
            }

            textBox1.Text += Environment.NewLine +
                             minCostDispos.TotalCost.ToString("F2") +
                             Environment.NewLine;

            CreateRoads(minCostDispos);

            pictureBox1.Refresh();
        }
コード例 #2
0
        private void buttonPerebor_Click(object sender, EventArgs e)
        {
            textBox1.Text += "Traverse:";

            List <Disposition> open, closed;

            open   = new List <Disposition>();
            closed = new List <Disposition>();

            Disposition initState =
                new Disposition(null, boxes.Count, FullCost);

            float minCost = float.MaxValue;
            //int minCostFrom = -1, minCostTo = -1;
            Disposition minCostDispos = null;

            // where to collect
            for (int i_where = 0; i_where < boxes.Count; i_where++)
            {
                open.Clear();

                // from which to start
                for (int i_from = 0; i_from < boxes.Count; i_from++)
                {
                    //if (i_from == i_where) continue;

                    Disposition t = new Disposition(initState);
                    t.GoTo(i_from);
                    open.Add(t);
                }

                while (open.Count > 1)
                {
                    int         q = 0;
                    Disposition t = open[q];

                    while (true)
                    {
                        int u = t.FindUncollected(i_where);
                        if (u == -1)
                        {
                            break;
                        }

                        t = new Disposition(t);
                        t.GoTo(u);
                        closed.Add(t);

                        t = new Disposition(t);
                        t.DragTo(i_where);
                        closed.Add(t);
                    }

                    if (minCost > t.TotalCost)
                    {
                        minCost       = t.TotalCost;
                        minCostDispos = t;
                    }

                    closed.Add(open[q]);
                    open.RemoveAt(q);
                }

                if (minCostDispos == null)
                {
                    textBox1.Text += Environment.NewLine + "No solution";
                }
                else
                {
                    textBox1.Text += Environment.NewLine +
                                     minCostDispos.TotalCost.ToString("F2");
                }
            }

            if (minCostDispos == null)
            {
                textBox1.Text += Environment.NewLine +
                                 "No solution" +
                                 Environment.NewLine;

                return;
            }

            textBox1.Text += Environment.NewLine +
                             minCostDispos.TotalCost.ToString("F2") +
                             Environment.NewLine;

            CreateRoads(minCostDispos);

            pictureBox1.Refresh();
        }