コード例 #1
0
        private void select()
        {
            //Reload the world. Update StartEndPairs
            int selection = MapSelect.SelectedIndex;

            if (selection == -1)
            {
                return;
            }
            GridHelper.readInputs(selection, mvm.world, mvm.startPairs, mvm.endPairs, mvm.hardPairs);
            //Visualize the world.
            Visualize();
            mvm.RefreshPairs();
            //Recalculate Algorithm
            float wght;

            if (!float.TryParse(Weight.Text, out wght))
            {
                wght = 1;
            }
            search    = new Naiive(mvm.world);
            seqsearch = new Sequential(mvm.world);
            intsearch = new Integrated(mvm.world);
            //bool result = search.hSearch(Heuristic.SelectedIndex, Algo.SelectedIndex, wght, mvm.startPairs[0, 0], mvm.startPairs[0, 1], mvm.endPairs[0, 0], mvm.endPairs[0, 1]);
        }
コード例 #2
0
 public Sequential(int[,] world)
 {
     searches = new Naiive[5];
     for (int i = 0; i < 5; i++)
     {
         searches[i] = new Naiive(world);
     }
 }
コード例 #3
0
        public Naiive seqSearch()
        {
            sw = Stopwatch.StartNew();
            if (!initialized)
            {
                return(null);
            }
            int consistent = 2;

            while (searches[consistent].minkey() < 30000)
            {
                for (int i = 0; i < 5; i++)
                {
                    if (i == consistent)
                    {
                        continue;
                    }
                    Naiive a = searches[i];
                    if (a.minkey() <= weight2 * searches[consistent].minkey())
                    {
                        if (a.worldNodes[endx, endy].g <= a.minkey())
                        {
                            if (a.worldNodes[endx, endy].g < 30000)
                            {
                                sw.Stop();
                                return(a);
                            }
                        }
                        else
                        {
                            a.expandNode(a.fringe.Dequeue());
                        }
                    }
                    else
                    {
                        if (searches[consistent].worldNodes[endx, endy].g <= searches[consistent].minkey())
                        {
                            if (searches[consistent].worldNodes[endx, endy].g < 30000)
                            {
                                return(searches[consistent]);
                            }
                        }
                        else
                        {
                            searches[consistent].expandNode(searches[consistent].fringe.Dequeue());
                        }
                    }
                }
            }
            return(null);
        }
コード例 #4
0
        private static void snapshot(Naiive searchLocal, string filename, int w, int p, int a, int h, float w1, float w2)
        {
            StringBuilder buffer = new StringBuilder();
            worldNode     aaa    = searchLocal.end;

            float path = searchLocal.end.g;

            //World number, s/e pair index, algo index, heuristic index, weight, weight2, pathlength, nodesexpanded, runtime, Memory,
            buffer.Append($"{w},{p},{a},{h},{w1},{w2},{path},{searchLocal.expanded},{searchLocal.sw.ElapsedMilliseconds},{GC.GetTotalMemory(false)}");

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(filename, true))
            {//Append to existing files
                file.WriteLine(buffer.ToString());
            }
        }
コード例 #5
0
        void generatePairs()
        {
            bool incomplete;

            //Start and goal pairs.
            int[] sp = new int[2];
            int[] gp = new int[2];

            for (int i = 0; i < 10; i++)
            {
                incomplete = true;
                while (incomplete)
                {
                    createPair(sp);
                    for (int j = 0; j < i; j++)
                    {
                        if (startPairs[j, 0] == sp[0] && startPairs[j, 1] == sp[1])
                        {
                            j = 0; createPair(sp);
                        }
                    }
                    createPair(gp);
                    if ((Math.Abs(sp[0] - gp[0]) + Math.Abs(sp[1] - gp[1])) < 100)
                    {
                        incomplete = true;
                    }
                    else
                    {
                        Naiive search = new Naiive(world);
                        search.initAttr(2, 0, 1, sp[0], sp[1], gp[0], gp[1]);
                        incomplete = !search.hSearch();
                    }
                }
                startPairs[i, 0] = sp[0];
                startPairs[i, 1] = sp[1];
                endPairs[i, 0]   = gp[0];
                endPairs[i, 1]   = gp[1];
            }
        }
コード例 #6
0
 public Integrated(int[,] world)
 {
     anchor = new Naiive(world);
     inad   = new Naiive(world);
 }
コード例 #7
0
        private static void threadbench(int m)
        {
            string        filename = $"C:\\Users\\Public\\Gridworld_Heuristics\\testresults_world{m}_{DateTime.Now.ToFileTime()}";
            MainViewModel benchvm  = new MainViewModel();
            float         w1       = 1;
            float         w2       = 1;

            GridHelper.readInputs(m, benchvm.world, benchvm.startPairs, benchvm.endPairs, benchvm.hardPairs);
            Naiive     searchLocal    = new Naiive(benchvm.world);
            Sequential seqsearchLocal = new Sequential(benchvm.world);
            Integrated intSearchLocal = new Integrated(benchvm.world);

            for (int j = 0; j < 5; j++)     //Startend pairs
            {
                for (int k = 0; k < 5; k++) //Algos
                {
                    if (k < 2)
                    {
                        for (int f = 0; f < 5; f++) // Heuristics
                        {
                            searchLocal.initAttr(f, k, w1, benchvm.startPairs[j, 0], benchvm.startPairs[j, 1],
                                                 benchvm.endPairs[j, 0], benchvm.endPairs[j, 1]);
                            searchLocal.hSearch();
                            snapshot(searchLocal, filename, m, j, k, f, w1, w2);
                            if (k == 1)
                            {//Weighted a*
                                w1 = 1.25f;
                                searchLocal.initAttr(f, k, w1, benchvm.startPairs[j, 0], benchvm.startPairs[j, 1],
                                                     benchvm.endPairs[j, 0], benchvm.endPairs[j, 1]);
                                searchLocal.hSearch();
                                snapshot(searchLocal, filename, m, j, k, f, w1, w2);
                                w1 = 2f;
                                searchLocal.initAttr(f, k, w1, benchvm.startPairs[j, 0], benchvm.startPairs[j, 1],
                                                     benchvm.endPairs[j, 0], benchvm.endPairs[j, 1]);
                                searchLocal.hSearch();
                                snapshot(searchLocal, filename, m, j, k, f, w1, w2);
                            }
                        }
                    }
                    if (k == 2)
                    {
                        searchLocal.initAttr(-1, k, w1, benchvm.startPairs[j, 0], benchvm.startPairs[j, 1],
                                             benchvm.endPairs[j, 0], benchvm.endPairs[j, 1]);
                        searchLocal.hSearch();
                        snapshot(searchLocal, filename, m, j, k, -1, w1, w2);
                    }
                    if (k == 3)
                    {
                        w1 = 1.25f;
                        w2 = 1.25f;
                        seqsearchLocal.initAttr(w1, w2, benchvm.startPairs[j, 0], benchvm.startPairs[j, 1],
                                                benchvm.endPairs[j, 0], benchvm.endPairs[j, 1]);
                        searchLocal = seqsearchLocal.seqSearch();
                        snapshot(searchLocal, filename, m, j, k, -1, w1, w2);

                        w2 = 2f;
                        seqsearchLocal.initAttr(w1, w2, benchvm.startPairs[j, 0], benchvm.startPairs[j, 1],
                                                benchvm.endPairs[j, 0], benchvm.endPairs[j, 1]);
                        searchLocal = seqsearchLocal.seqSearch();
                        snapshot(searchLocal, filename, m, j, k, -1, w1, w2);

                        w1 = 2f;
                        seqsearchLocal.initAttr(w1, w2, benchvm.startPairs[j, 0], benchvm.startPairs[j, 1],
                                                benchvm.endPairs[j, 0], benchvm.endPairs[j, 1]);
                        searchLocal = seqsearchLocal.seqSearch();
                        snapshot(searchLocal, filename, m, j, k, -1, w1, w2);

                        w2 = 1.25f;
                        seqsearchLocal.initAttr(w1, w2, benchvm.startPairs[j, 0], benchvm.startPairs[j, 1],
                                                benchvm.endPairs[j, 0], benchvm.endPairs[j, 1]);
                        searchLocal = seqsearchLocal.seqSearch();
                        snapshot(searchLocal, filename, m, j, k, -1, w1, w2);
                    }
                    else
                    {
                        w1 = 1.25f;
                        w2 = 1.25f;
                        intSearchLocal.initAttr(w1, w2, benchvm.startPairs[j, 0], benchvm.startPairs[j, 1],
                                                benchvm.endPairs[j, 0], benchvm.endPairs[j, 1]);
                        searchLocal = intSearchLocal.intSearch();
                        snapshot(searchLocal, filename, m, j, k, -1, w1, w2);
                        w2 = 2f;
                        intSearchLocal.initAttr(w1, w2, benchvm.startPairs[j, 0], benchvm.startPairs[j, 1],
                                                benchvm.endPairs[j, 0], benchvm.endPairs[j, 1]);
                        searchLocal = intSearchLocal.intSearch();
                        snapshot(searchLocal, filename, m, j, k, -1, w1, w2);
                        w1 = 2f;
                        intSearchLocal.initAttr(w1, w2, benchvm.startPairs[j, 0], benchvm.startPairs[j, 1],
                                                benchvm.endPairs[j, 0], benchvm.endPairs[j, 1]);
                        searchLocal = intSearchLocal.intSearch();
                        snapshot(searchLocal, filename, m, j, k, -1, w1, w2);
                        w2 = 1.25f;
                        intSearchLocal.initAttr(w1, w2, benchvm.startPairs[j, 0], benchvm.startPairs[j, 1],
                                                benchvm.endPairs[j, 0], benchvm.endPairs[j, 1]);
                        searchLocal = intSearchLocal.intSearch();
                        snapshot(searchLocal, filename, m, j, k, -1, w1, w2);
                    }
                }
            }
        }
コード例 #8
0
        private void recalculateAlgorithm()
        {
            SolidColorBrush whiteBrush = new SolidColorBrush();

            whiteBrush.Color = Colors.White;
            SolidColorBrush blackBrush = new SolidColorBrush();

            blackBrush.Color = Colors.Black;
            SolidColorBrush chartreuseBrush = new SolidColorBrush();

            chartreuseBrush.Color = Colors.Chartreuse;
            SolidColorBrush beigeBrush = new SolidColorBrush();

            beigeBrush.Color = Colors.Beige;
            SolidColorBrush grayBrush = new SolidColorBrush();

            grayBrush.Color = Colors.Gray;
            SolidColorBrush pathBrush = new SolidColorBrush();

            pathBrush.Color = Colors.BlueViolet;
            SolidColorBrush startBrush = new SolidColorBrush();

            startBrush.Color = Colors.Green;
            SolidColorBrush endBrush = new SolidColorBrush();

            endBrush.Color = Colors.Red;

            SolidColorBrush transparent = new SolidColorBrush();

            transparent.Color = Colors.Transparent;


            int spi = StartEndPairs.SelectedIndex;

            if (spi < 0)
            {
                spi = 0;
            }
            int startx = mvm.startPairs[spi, 0];
            int starty = mvm.startPairs[spi, 1];
            int endx   = mvm.endPairs[spi, 0];
            int endy   = mvm.endPairs[spi, 1];

            //Rerun the algorithm, update runtime
            int pairIdx = StartEndPairs.SelectedIndex;

            if (pairIdx < 0)
            {
                pairIdx = 0;
            }
            float wght;

            if (!float.TryParse(Weight.Text, out wght))
            {
                wght = 1;
            }
            float wght2;

            if (!float.TryParse(Weight2.Text, out wght2))
            {
                wght2 = 1;
            }
            if (Algo.SelectedIndex < 3)
            {
                search.initAttr(Heuristic.SelectedIndex, Algo.SelectedIndex, wght, mvm.startPairs[pairIdx, 0], mvm.startPairs[pairIdx, 1],
                                mvm.endPairs[pairIdx, 0], mvm.endPairs[pairIdx, 1]);
                search.hSearch();
            }
            else if (Algo.SelectedIndex == 3)
            {
                seqsearch.initAttr(wght, wght2, mvm.startPairs[pairIdx, 0], mvm.startPairs[pairIdx, 1],
                                   mvm.endPairs[pairIdx, 0], mvm.endPairs[pairIdx, 1]);
                search = seqsearch.seqSearch();
            }
            else
            {
                intsearch.initAttr(wght, wght2, mvm.startPairs[pairIdx, 0], mvm.startPairs[pairIdx, 1],
                                   mvm.endPairs[pairIdx, 0], mvm.endPairs[pairIdx, 1]);
                search = intsearch.intSearch();
            }

            mvm.Expanded = search.expanded;
            myPath.Children.Clear();
            //Interpret search data.
            Button    chunk;
            worldNode a = search.end;

            //int path = 1;
            while (a.parent != null)
            {
                //path++;
                chunk = new Button();
                if (a.x == endx && a.y == endy)
                {
                    chunk.BorderBrush = endBrush;
                }
                else
                {
                    chunk.BorderBrush = pathBrush;
                }
                chunk.Background      = transparent;
                chunk.BorderThickness = new Thickness(1);
                chunk.Content         = new int[] { a.x, a.y };
                chunk.Click          += chunkClick;
                Grid.SetColumn(chunk, a.y);
                Grid.SetRow(chunk, a.x);

                a = a.parent;
                myPath.Children.Add(chunk);
            }
            //mvm.PathLen = path;
            mvm.PathLen           = search.end.g;
            chunk                 = new Button();
            chunk.Background      = transparent;
            chunk.BorderBrush     = startBrush;
            chunk.BorderThickness = new Thickness(1);
            chunk.Content         = new int[] { a.x, a.y };
            chunk.Click          += chunkClick;
            Grid.SetColumn(chunk, a.y);
            Grid.SetRow(chunk, a.x);
            myPath.Children.Add(chunk);
            //Update Start/end pair appearance
        }