예제 #1
0
        }//end of vs

        public void Analys(object sender, EventArgs e)
        {
            if (tgx.Text == "" || tgy.Text == "" || tsx.Text == "" || tsy.Text == "")
            {
                MessageBox.Show("Erorr in Entries of Goal or Start Coordinates");
            }
            else
            {
                Form1.ActiveForm.Refresh();
                starttime = DateTime.Now;
                gx        = int.Parse(tgx.Text);
                gy        = int.Parse(tgy.Text);

                sx = int.Parse(tsx.Text);
                sy = int.Parse(tsy.Text);

                for (int j = 0; j < hp; j++)
                {
                    for (int i = 0; i < wp; i++)
                    {
                        state[i, j] = new node();
                    }
                }

                setdimval(state, hp, wp);
                settag(state, hp, wp);
                setstatus(state, hp, wp, pixel, gx, gy, sx, sy);

                state[gx, gy].h = 0.0;
                state[gx, gy].k = 0.0;
                insert(OpenList, state[gx, gy]);

                double kold;
                int    iold, jold, ind;
                node   d  = new node();
                int[]  si = new int[8];
                int[]  sj = new int[8];

                do
                {
                    d    = (node)OpenList.Dequeue();
                    kold = d.k;
                    iold = d.ni;
                    jold = d.nj;

                    state[iold, jold].tag = "CLOSE";

                    vs(si, sj, iold, jold);

                    for (int i = 0; i < 8; i++)
                    {
                        int row = si[i], col = sj[i];

                        if ((row <= wp - 1 && row >= 0) && (col <= hp - 1 && col >= 0))
                        {
                            if (row != iold && col != jold)
                            {
                                crossmove(row, col, state[row, col], state[iold, jold], OpenList);
                            }

                            else
                            {
                                linemove(row, col, state[row, col], state[iold, jold], OpenList);
                            }
                        }
                    }

                    ind = OpenList.Count;
                    sortqueue(OpenList, state);

                    text2.Text = text2.Text + "[" + iold.ToString() + "," + jold.ToString() + "] " + state[iold, jold].k.ToString() + " - ";
                } while (state[iold, jold].status != "START");


                correnti = iold;
                correntj = jold;

                textBox4.Text = "Finished";
                text2.Visible = true;
            }

            findpath(sender, e);
        }
예제 #2
0
        // // // // // // // // // // // // // // // // // // // // // // // // // // // // //


        private void findpath(object sender, EventArgs e)
        {
            double[] mink      = new double[1]; mink[0] = 100000;
            int[]    mi        = new int[1]; mi[0] = 0;
            int[]    mj        = new int[1]; mj[0] = 0;
            int[]    neighbori = new int[8];
            int[]    neighborj = new int[8];
            int[]    Mi        = new int[4];
            int[]    Mj        = new int[4];
            string[] y         = new string[4];
            string[] z         = new string[4];
            double   pathcoast = 0.0;

            pathcoast += state[correnti, correntj].h;

            int  goali = gx;
            int  goalj = gy;
            bool ddd   = true;

            textBox5.Text = textBox5.Text + "(" + correnti.ToString() + ", " + correntj.ToString() + ") - ";
            do
            {
                vs(neighbori, neighborj, correnti, correntj);
                mink[0] = 10000;
                for (int i = 0; i < 8; i++)
                {
                    int row = neighbori[i]; int col = neighborj[i];
                    if ((row <= wp - 1 && row >= 0) && (col <= hp - 1 && col >= 0) && (state[row, col].status != "OBSTACLE"))
                    {
                        k_min(row, col, state[row, col], mink, mi, mj);
                    }
                }

                int mii = mi[0]; int mjj = mj[0];


                //To Here we find the minimum k value


                Mi[0] = correnti + 1; Mj[0] = correntj + 1;
                Mi[1] = correnti + 1; Mj[1] = correntj - 1;
                Mi[2] = correnti - 1; Mj[2] = correntj + 1;
                Mi[3] = correnti - 1; Mj[3] = correntj - 1;


                if (correntj + 1 > hp - 1)
                {
                    z[0] = "NULL";
                }
                else
                {
                    z[0] = state[correnti, correntj + 1].status;
                }

                if (correntj - 1 < 0)
                {
                    z[1] = "NULL";
                }
                else
                {
                    z[1] = state[correnti, correntj - 1].status;
                }

                if (correnti - 1 < 0)
                {
                    z[2] = "NULL";
                }
                else
                {
                    z[2] = state[correnti - 1, correntj].status;
                }

                z[3] = z[1];


                if (correnti + 1 > wp - 1)
                {
                    y[0] = "NULL";
                }
                else
                {
                    y[0] = state[correnti + 1, correntj].status;
                }

                y[1] = y[0];

                if (correntj + 1 > hp - 1)
                {
                    y[2] = "NULL";
                }
                else
                {
                    y[2] = state[correnti, correntj + 1].status;
                }

                if (correnti - 1 < 0)
                {
                    y[3] = "NULL";
                }
                else
                {
                    y[3] = state[correnti - 1, correntj].status;
                }


                for (int ii = 0; ii < 4; ii++)
                {
                    if (mii == Mi[ii] && mjj == Mj[ii])
                    {
                        if (y[ii] == "OBSTACLE" && z[ii] == "OBSTACLE")
                        {
                            state[mii, mjj].h      = 10000;
                            state[mii, mjj].status = "OBSTACLE";
                            insert(OpenList, state[mii, mjj]);

                            int[] si = new int[8];
                            int[] sj = new int[8];
                            vs(si, sj, mii, mjj);

                            for (int i = 0; i < 8; i++)
                            {
                                if (state[si[i], sj[i]].tag == "CLOSE")
                                {
                                    insert(OpenList, state[si[i], sj[i]]);
                                }
                            }
                            int    iold, jold;
                            double kold;
                            bool   good;
                            good = true;
                            sortqueue(OpenList, state);

                            do
                            {
                                node d = new node();
                                d    = (node)OpenList.Dequeue();
                                kold = d.k;
                                iold = d.ni;
                                jold = d.nj;
                                state[iold, jold].tag = "CLOSE";

                                if (iold == correnti && jold == correntj)
                                {
                                    good = false;
                                }

                                vs(si, sj, iold, jold);


                                if (kold == state[iold, jold].h)
                                {
                                    for (int i = 0; i < 8; i++)
                                    {
                                        int r = si[i];
                                        int c = sj[i];

                                        if ((r <= wp - 1 && r >= 0) && (c <= hp - 1 && c >= 0))
                                        {
                                            double co = cost(iold, jold, r, c);
                                            double tt = co + state[iold, jold].h;

                                            if (state[r, c].tag == "NEW")
                                            {
                                                state[r, c].b = state[iold, jold];
                                                state[r, c].h = tt;
                                                state[r, c].k = tt;
                                                insert(OpenList, state[r, c]);
                                                sortqueue(OpenList, state);
                                            }
                                            else if (((state[r, c].b == state[iold, jold]) && (state[r, c].h != tt)) || ((state[r, c].b != state[iold, jold]) && ((float)state[r, c].h > (float)tt)))
                                            {
                                                if (state[r, c].status != "OBSTACLE")
                                                {
                                                    state[r, c].b = state[iold, jold];
                                                    state[r, c].h = tt;

                                                    if (state[r, c].tag == "CLOSE")
                                                    {
                                                        insert(OpenList, state[r, c]);
                                                        sortqueue(OpenList, state);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }


                                if (kold < state[iold, jold].h)
                                {
                                    for (int i = 0; i < 8; i++)
                                    {
                                        int r = si[i];
                                        int c = sj[i];

                                        if ((r <= wp - 1 && r >= 0) && (c <= hp - 1 && c >= 0))
                                        {
                                            double co = cost(iold, jold, r, c);
                                            double tt = co + state[iold, jold].h;

                                            if (state[r, c].tag == "NEW")
                                            {
                                                state[r, c].b = state[iold, jold];
                                                state[r, c].h = tt;
                                                state[r, c].k = tt;
                                                insert(OpenList, state[r, c]);
                                            }
                                            else
                                            {
                                                if ((state[r, c].b == state[iold, jold]) && (state[r, c].h != tt))
                                                {
                                                    state[r, c].b = state[iold, jold];
                                                    if (state[r, c].tag == "CLOSE")
                                                    {
                                                        if (state[iold, jold].h == 10000)
                                                        {
                                                            state[r, c].h = 10000;
                                                        }
                                                        insert(OpenList, state[r, c]);
                                                        sortqueue(OpenList, state);
                                                    }

                                                    else
                                                    {
                                                        state[r, c].h = tt;
                                                        if (state[r, c].h > 10000)
                                                        {
                                                            state[r, c].h = 10000;
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    if ((state[r, c].b != state[iold, jold]) && (state[r, c].h > tt))
                                                    {
                                                        state[iold, jold].k = tt;
                                                        insert(OpenList, state[iold, jold]);
                                                        sortqueue(OpenList, state);
                                                    }
                                                    else
                                                    {
                                                        if (state[r, c].b != state[iold, jold])
                                                        {
                                                            if (state[iold, jold].h > state[r, c].h + cost(iold, jold, r, c))
                                                            {
                                                                if (state[r, c].tag == "CLOSE")
                                                                {
                                                                    if (state[r, c].h > kold)
                                                                    {
                                                                        insert(OpenList, state[r, c]);
                                                                        sortqueue(OpenList, state);
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            } while (good);



                            bool end = true;

                            while (end)
                            {
                                node d = new node();
                                d    = (node)OpenList.Dequeue();
                                kold = d.k;
                                iold = d.ni;
                                jold = d.nj;
                                state[iold, jold].tag = "CLOSE";

                                if (kold < state[correnti, correntj].h)
                                {
                                    vs(si, sj, iold, jold);

                                    if (kold == state[iold, jold].h)
                                    {
                                        for (int i = 0; i < 8; i++)
                                        {
                                            int r = si[i];
                                            int c = sj[i];

                                            if ((r <= wp - 1 && r >= 0) && (c <= hp - 1 && c >= 0))
                                            {
                                                double co = cost(iold, jold, r, c);
                                                double tt = state[iold, jold].h + co;

                                                if (state[r, c].tag == "NEW")
                                                {
                                                    state[r, c].b = state[iold, jold];
                                                    state[r, c].h = state[iold, jold].h + co;
                                                    state[r, c].k = state[r, c].h;
                                                    insert(OpenList, state[r, c]);
                                                    sortqueue(OpenList, state);
                                                }
                                                else if (((state[r, c].b == state[iold, jold]) && ((float)state[r, c].h != (float)tt)) || ((state[r, c].b != state[iold, jold]) && ((float)state[r, c].h > (float)tt)))
                                                {
                                                    if (state[r, c].status != "OBSTACLE")
                                                    {
                                                        state[r, c].b = state[iold, jold];
                                                        state[r, c].h = state[iold, jold].h + co;
                                                        state[r, c].k = state[r, c].h;
                                                        if (state[r, c].tag == "CLOSE")
                                                        {
                                                            insert(OpenList, state[r, c]);
                                                            sortqueue(OpenList, state);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }


                                    else if (kold < state[iold, jold].h)
                                    {
                                        for (int i = 0; i < 8; i++)
                                        {
                                            int r = si[i];
                                            int c = sj[i];

                                            if ((r <= 4 && r >= 0) && (c <= 4 && c >= 0))
                                            {
                                                double co = cost(iold, jold, r, c);
                                                double tt = state[iold, jold].h + co;

                                                if (state[r, c].tag == "NEW")
                                                {
                                                    state[r, c].b = state[iold, jold];
                                                    state[r, c].h = tt;
                                                    state[r, c].k = tt;
                                                    insert(OpenList, state[r, c]);
                                                    sortqueue(OpenList, state);
                                                }
                                                else if ((state[r, c].b == state[iold, jold]) && ((float)state[r, c].h != (float)tt))
                                                {
                                                    state[r, c].b = state[iold, jold];
                                                    if (state[r, c].tag == "CLOSE")
                                                    {
                                                        state[r, c].h = tt;
                                                        if (state[r, c].h > 10000)
                                                        {
                                                            state[r, c].h = 10000;
                                                        }
                                                        insert(OpenList, state[r, c]);
                                                        sortqueue(OpenList, state);
                                                    }
                                                }
                                                else if ((state[r, c].b != state[iold, jold]) && ((float)state[r, c].h > (float)tt))
                                                {
                                                    if (state[iold, jold].tag == "CLOSE")
                                                    {
                                                        state[iold, jold].k = state[iold, jold].h;
                                                        insert(OpenList, state[iold, jold]);
                                                        sortqueue(OpenList, state);
                                                    }
                                                }
                                                else if ((state[r, c].b != state[iold, jold]) && ((float)state[iold, jold].h > (float)(state[r, c].h + co)) && (state[r, c].tag == "CLOSE") && (state[r, c].h > (float)kold))
                                                {
                                                    insert(OpenList, state[r, c]);
                                                    sortqueue(OpenList, state);
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    end = false;
                                    OpenList.Enqueue(state[iold, jold]);
                                    sortqueue(OpenList, state);
                                }
                            }

                            mii = correnti;
                            mjj = correntj;
                        }
                    }
                }
                correnti = mii;
                correntj = mjj;
                if ((correnti == goali) && (correntj == goalj))
                {
                    ddd = false;
                }

                textBox5.Text = textBox5.Text + "(" + correnti.ToString() + ", " + correntj.ToString() + ") - ";
                orb.SetPixel(correnti, correntj, Color.Green);
                Form1.ActiveForm.Refresh();
                pathcoast += state[correnti, correntj].h;
            } while (ddd);
            orb.SetPixel(correnti, correntj, Color.Gold);
            textBox1.Text    = pathcoast.ToString();
            textBox5.Visible = true;

            endtime = DateTime.Now;

            TimeSpan duration = endtime - starttime;
            int      sec      = duration.Seconds;
            int      milli    = duration.Milliseconds;

            textBox2.Text = sec.ToString() + " Sec , " + milli.ToString() + " MS";

            Form1.ActiveForm.Refresh();
        }
예제 #3
0
        }//end of sortqueue

        //INSERT THE ELEMENT S INTO OPENLIST
        static void insert(Queue OpenList, node s)
        {
            s.tag = "OPEN";
            OpenList.Enqueue(s);
        }//end of insert