コード例 #1
0
        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            _problem = new Problems.QueensPuzzle(trackBar1.Value);

            _currentState = new Problems.QueensPuzzle.State(Enumerable.Range(0, _problem.Size).Select(x => (x * 393342739) % _problem.Size).ToArray());
            panel1.Invalidate();
        }
コード例 #2
0
        public static List <int> GetPossibleActions(Problems.QueensPuzzle.State state)
        {
            var result       = new List <int>();
            var newPlacement = state.PlacementsCount;

            for (var action = 0; action < state.ColumnPlacements.Length; action++)
            {
                var isAttacking = false;
                for (var placement = 0; placement < newPlacement; placement++)
                {
                    if (Problems.QueensPuzzle.State.IsAttacking(placement, state.ColumnPlacements[placement], newPlacement, action))
                    {
                        isAttacking = true;
                        break;
                    }
                }

                if (!isAttacking)
                {
                    result.Add(action);
                }
            }

            return(result.OrderBy(x => rnd.Next()).ToList());
        }
コード例 #3
0
        public static Problems.QueensPuzzle.State ApplyAction(Problems.QueensPuzzle.State state, int action)
        {
            var copy = new int[state.ColumnPlacements.Length];

            state.ColumnPlacements.CopyTo(copy, 0);
            copy[state.PlacementsCount] = action;
            return(new Problems.QueensPuzzle.State(copy));
        }
コード例 #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            var i = 0;
            while (true)
            {
                _currentState = new Problems.QueensPuzzle.State(
                    Enumerable.Range(0, _problem.Size)
                    .Select(x => _random.Next(_problem.Size)).ToArray());

                if (++i % 500000 == 0 || _currentState.AttackCount == 0)
                {
                    panel1.Invalidate();
                    Application.DoEvents();
                }
                if (_currentState.AttackCount == 0) break;
            }
        }
コード例 #5
0
        private void button1_Click(object sender, EventArgs e)
        {
            var i = 0;

            while (true)
            {
                _currentState = new Problems.QueensPuzzle.State(
                    Enumerable.Range(0, _problem.Size)
                    .Select(x => _random.Next(_problem.Size)).ToArray());

                if (++i % 500000 == 0 || _currentState.AttackCount == 0)
                {
                    panel1.Invalidate();
                    Application.DoEvents();
                }
                if (_currentState.AttackCount == 0)
                {
                    break;
                }
            }
        }
コード例 #6
0
        private void button2_Click(object sender, EventArgs e)
        {
            var initialState = Enumerable.Range(0, _problem.Size).Select(x => - 1).ToArray();
            var tree         = new SearchTree <Problems.QueensPuzzle.State>(_problem.CreateState(initialState));
            var dfs          = new DepthFirstSearcher <Problems.QueensPuzzle.State, int>(
                tree,
                GetPossibleActions,
                ApplyAction,
                (x, a) => 1,
                x => x.IsSolution);


            var           updateCount  = 0;
            var           start        = DateTime.Now;
            Action <bool> updateCounts = (force) =>
            {
                if (!force && ++updateCount % 10000 > 0)
                {
                    return;
                }
                NodesGeneratedLabel.Text = "Generated = " + dfs.NodesGeneratedCount + " ";
                ExpandedNodesLabel.Text  = "Expanded = " + dfs.NodesExpandedCount + " ";
                RetainedNodesLabel.Text  = "Retained = " + dfs.NodesRetainedCount + " ";
                SecondsLabel.Text        = DateTime.Now.Subtract(start).TotalSeconds.ToString();
                _currentState            = dfs.CurrentNode.Data;
                panel1.Invalidate();
                Application.DoEvents();
            };

            dfs.NodeExpanded  += (s, a) => updateCounts(false);
            dfs.NodeGenerated += (s, a) => updateCounts(false);

            var solution = dfs.Search();

            updateCounts(true);
        }
コード例 #7
0
        private void button2_Click(object sender, EventArgs e)
        {
            var initialState = Enumerable.Range(0, _problem.Size).Select(x => -1).ToArray();
            var tree = new SearchTree<Problems.QueensPuzzle.State>(_problem.CreateState(initialState));
            var dfs = new DepthFirstSearcher<Problems.QueensPuzzle.State, int>(
                tree,
                GetPossibleActions,
                ApplyAction,
                (x, a) => 1,
                x => x.IsSolution);

            var updateCount = 0;
            var start = DateTime.Now;
            Action<bool> updateCounts = (force) =>
            {
                if (!force && ++updateCount % 10000 > 0) return;
                NodesGeneratedLabel.Text = "Generated = " + dfs.NodesGeneratedCount + " ";
                ExpandedNodesLabel.Text = "Expanded = " + dfs.NodesExpandedCount + " ";
                RetainedNodesLabel.Text = "Retained = " + dfs.NodesRetainedCount + " ";
                SecondsLabel.Text = DateTime.Now.Subtract(start).TotalSeconds.ToString();
                _currentState = dfs.CurrentNode.Data;
                panel1.Invalidate();
                Application.DoEvents();
            };

            dfs.NodeExpanded += (s,a) => updateCounts(false);
            dfs.NodeGenerated += (s, a) => updateCounts(false);

            var solution = dfs.Search();

            updateCounts(true);
        }
コード例 #8
0
        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            _problem = new Problems.QueensPuzzle(trackBar1.Value);

            _currentState = new Problems.QueensPuzzle.State(Enumerable.Range(0, _problem.Size).Select(x => (x * 393342739) % _problem.Size).ToArray());
            panel1.Invalidate();
        }