Exemplo n.º 1
0
        private void StartSearch_Click(object sender, RoutedEventArgs e)
        {
            ISearchAlgorithm sa = (ISearchAlgorithm)this.Algorithms.SelectedItem;

            _activeResults       = sa.Search(_activeProblem);
            _activeResultsMethod = sa.ToString();
            RefeshUIComponents();
        }
Exemplo n.º 2
0
        public void Search_TerminalState(ISearchAlgorithm searcher)
        {
            // Checkmate
            var state = State.ParseFen("8/8/p7/8/P7/1Kbk4/1q6/8 w - - 4 59");
            var info  = searcher.Search(state);

            LogSearch(state, searcher, info);
            Assert.Empty(info.Pv);
            Assert.InRange(info.Score, int.MinValue, -1); // getting mated should have a negative utility

            // Stalemate
            state = State.ParseFen("rn2k1nr/pp4pp/3p4/q1pP4/P1P2p1b/1b2pPRP/1P1NP1PQ/2B1KBNR w Kkq - 0 1");
            info  = searcher.Search(state);
            LogSearch(state, searcher, info);
            Assert.Empty(info.Pv);
            Assert.Equal(0, info.Score);
        }
Exemplo n.º 3
0
        public Move PickMove(State root)
        {
            var info = _searcher.Search(root);
            var move = info.Pv[0];

            _history.Add(move);
            _searchInfos.Add(info);

            return(move);
        }
        private static void TestSearchFailure(ISearchAlgorithm<int[]> searcher, char[] s)
        {
            if (s.Length < 1)
            {
                throw new ArgumentException("s.Length < 1");
            }
            int n = s.Length;

            // Build an impossible pattern by
            // modifying every character in s
            var pattern = s.Select(c => (char)(c + 1)).ToArray();
            Assert.AreEqual(n, searcher.Search(s, pattern));
        }
Exemplo n.º 5
0
        public void Search_StressTest(ISearchAlgorithm searcher)
        {
            var rsg = GetRsg();

            for (int i = 0; i < 100; i++)
            {
                var state = rsg.Next();
                var info  = searcher.Search(state);
                LogSearch(state, searcher, info);

                Assert.Equal(searcher.Depth, info.Depth);
                Assert.InRange(info.NodesSearched, 0, searcher.MaxNodes);
                Assert.InRange(info.Pv.Length, 0, searcher.Depth);

                // make sure all of the moves in the pv are valid
                var successor = state;
                foreach (var pvMove in info.Pv)
                {
                    successor = successor.Apply(pvMove);
                }
            }
        }
Exemplo n.º 6
0
        public static IList <Audio> Search(this List <Audio> source, Audio referenceItem, ISearchAlgorithm algorithm, object parameters = null)
        {
            algorithm.Search(source, referenceItem, parameters);

            return(source);
        }
Exemplo n.º 7
0
        public void BenchmarkSimulation()
        {
            try
            {
                benchmarking = true;
                this.Invoke(new Action(delegate
                {
                    _gArea.ResetNodesColor();
                    tracetxt.Text = "";
                }));
                benchmarks.Clear();
                Node <string> initial = (start_node.SelectedItem as ComboBoxItem).Tag as Node <string>;
                Node <string> final   = (goal_node.SelectedItem as ComboBoxItem).Tag as Node <string>;
                initialGraph = ScreenshotGraphView();
                this.Invoke(new Action(delegate
                {
                    _gArea.ResetInitialGoal();
                    _gArea.SetInitialGoal(initial, final);
                }));
                // set event handlers
                simulation_thread = new Thread(new ThreadStart(delegate
                {
                    try
                    {
                        this.Invoke(new Action(delegate
                        {
                            buttonX2.Enabled = false;
                            buttonX1.Enabled = false;
                        }));

                        foreach (var selected_algorithm in salgos)
                        {
                            this.Invoke(new Action(delegate
                            {
                                selected_algorithm.Logged = false;
                            }));
                            selected_algorithm.OnResultFound   += Selected_algorithm_OnResultFound;
                            selected_algorithm.OnResetRequired += Selected_algorithm_OnResetRequired;

                            selected_algorithm.Initialize();
                            current_report           = new SearchReport <string>();
                            SearchResult <string> sr = null;


                            sr = selected_algorithm.Search(initial, final.Key);
                            benchmarks.Add(
                                new KeyValuePair <ISearchAlgorithm <string>, SearchReport <string> >(
                                    selected_algorithm,
                                    current_report));
                            current_report.Result = sr;
                            current_report.Timer.Start();
                            selected_algorithm.SearchClean(initial, final.Key);
                            current_report.Timer.Stop();



                            current_report.Steps.Add(new SearchStep <string>
                            {
                                StepInformation =
                                    new KeyValuePair <INode <string>, NodeVisitAction>(null, NodeVisitAction.Reset)
                            });
                            current_report.ElapsedTime          = current_report.Timer.Elapsed;
                            selected_algorithm.OnResultFound   -= Selected_algorithm_OnResultFound;
                            selected_algorithm.OnResetRequired -= Selected_algorithm_OnResetRequired;
                        }
                        this.Invoke(new Action(delegate
                        {
                            simulation_thread = null;
                            DumpToReport();
                        }));
                    }
                    catch (Exception ex)
                    {
                        this.Invoke(new Action(delegate
                        {
                            MessageBoxEx.Show(this, ex.Message, "Benchmark Simulation Process",
                                              System.Windows.Forms.MessageBoxButtons.OK,
                                              System.Windows.Forms.MessageBoxIcon.Error);
                        }));
                    }
                    this.Invoke(new Action(delegate
                    {
                        buttonX2.Enabled = true;
                        buttonX1.Enabled = true;
                        benchmarking     = false;
                        MessageBoxEx.Show(this, "Benchmark successfully completed", "Benchmark simulation",
                                          MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }));
                }));
                simulation_thread.Start();
                //simulation_thread.Join();
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(this, ex.Message, "Benchmark Simulation Execution", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }
Exemplo n.º 8
0
        public void RunSimulation()
        {
            try
            {
                this.Invoke(new Action(delegate
                {
                    _gArea.ResetNodesColor();
                    tracetxt.Text = "";
                }));
                selected_algorithm = (algorithms.SelectedItem as ComboBoxItem).Tag as ISearchAlgorithm <string>;
                Node <string> initial = (start_node.SelectedItem as ComboBoxItem).Tag as Node <string>;
                Node <string> final   = (goal_node.SelectedItem as ComboBoxItem).Tag as Node <string>;
                this.Invoke(new Action(delegate
                {
                    _gArea.ResetInitialGoal();
                    _gArea.SetInitialGoal(initial, final);
                    selected_algorithm.Logged = checkBoxX1.Checked;
                }));
                selected_algorithm.OnResultFound   += Selected_algorithm_OnResultFound;
                selected_algorithm.OnResetRequired += Selected_algorithm_OnResetRequired;

                // set event handlers
                simulation_thread = new Thread(new ThreadStart(delegate
                {
                    sw.Start();
                    try
                    {
                        selected_algorithm.Initialize();
                        var sr = selected_algorithm.Search(initial, final.Key);
                        this.Invoke(new Action(delegate
                        {
                            selected_algorithm.OnResultFound   -= Selected_algorithm_OnResultFound;
                            selected_algorithm.OnResetRequired -= Selected_algorithm_OnResetRequired;
                            buttonX1.Text     = "Run";
                            simulation_thread = null;
                            if (sr.Found)
                            {
                                _gArea.ColorizePath(sr, Brushes.Red);
                                TracePath(sr);
                            }
                            this.Invoke(new Action(delegate
                            {
                                tracetxt.AppendText("The path cost is " + selected_algorithm.CalculateCost(sr) + Environment.NewLine);
                            }));
                            MessageBoxEx.Show(this, "Simulation complete", "Simulation",
                                              MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }));
                    }
                    catch (Exception ex)
                    {
                        this.Invoke(new Action(delegate
                        {
                            MessageBoxEx.Show(this, ex.Message, "Simulation Process", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                        }));
                    }
                    sw.Stop();
                    this.Invoke(new Action(delegate
                    {
                        tracetxt.AppendText("Elapsed time: " + sw.Elapsed);
                    }));
                }));
                simulation_thread.Start();
            }catch (Exception ex)
            {
                MessageBoxEx.Show(this, ex.Message, "Simulation Execution", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }
Exemplo n.º 9
0
 public bool Search(Node start, Node target, ISearchAlgorithm algorithm, out List <Node> path)
 {
     return(algorithm.Search(start, target, out path));
 }
Exemplo n.º 10
0
        private static void TestSearchSubstr(ISearchAlgorithm<int[]> searcher, char[] s, int start, int substrLen)
        {
            if (start < 0)
            {
                throw new ArgumentException("start < 0");
            }
            if (substrLen < 1)
            {
                throw new ArgumentException("patternLen < 1");
            }
            // Select substring as pattern from a random index
            var pattern = s.Skip(start).Take(substrLen).ToArray();
            int result = searcher.Search(s, pattern);

            // Rather than comaring result with i, which would be a
            // bad test because i may not be the first occurence,
            // create match from result and compare with pattern.
            var match = s.Skip(result)
                .Take(pattern.Length)
                .ToArray();

            start = 0;
            Assert.AreEqual(pattern.Length, match.Length);
            foreach (char c in pattern)
            {
                Assert.AreEqual(c, match[start++]);
            }
        }