Browse Source

added NextNumberChooser

master
Robin Thoni 7 years ago
parent
commit
290363b38c

+ 9
- 0
uqac-ia-sudoku-csp/Interfaces/INextNumberChooser.cs View File

1
+using System.Collections.Generic;
2
+
3
+namespace uqac_ia_sudoku_csp.Interfaces
4
+{
5
+    public interface INextNumberChooser
6
+    {
7
+        IEnumerable<int> ChooseNext(Board board, int x, int y);
8
+    }
9
+}

+ 20
- 3
uqac-ia-sudoku-csp/Program.cs View File

2
 using uqac_ia_sudoku_csp.Interfaces;
2
 using uqac_ia_sudoku_csp.Interfaces;
3
 using uqac_ia_sudoku_csp.Solver;
3
 using uqac_ia_sudoku_csp.Solver;
4
 using uqac_ia_sudoku_csp.Solver.Generators;
4
 using uqac_ia_sudoku_csp.Solver.Generators;
5
+using uqac_ia_sudoku_csp.Solver.NextNumberChoosers;
5
 using uqac_ia_sudoku_csp.Solver.NextValueChoosers;
6
 using uqac_ia_sudoku_csp.Solver.NextValueChoosers;
6
 
7
 
7
 namespace uqac_ia_sudoku_csp
8
 namespace uqac_ia_sudoku_csp
20
             {
21
             {
21
                 valueChooser = args[1];
22
                 valueChooser = args[1];
22
             }
23
             }
23
-            var emptyCharacters = "0 .";
24
+            var numberChooser = "basic";
24
             if (args.Length > 2)
25
             if (args.Length > 2)
25
             {
26
             {
26
-                emptyCharacters = args[2];
27
+                numberChooser = args[2];
28
+            }
29
+            var emptyCharacters = "0 .";
30
+            if (args.Length > 3)
31
+            {
32
+                emptyCharacters = args[3];
27
             }
33
             }
28
 
34
 
29
             object data;
35
             object data;
67
                 Environment.Exit(64);
73
                 Environment.Exit(64);
68
             }
74
             }
69
 
75
 
70
-            var solver = new BacktrackSearch(nextCellChooser);
76
+            INextNumberChooser nextNumberChooser = null;
77
+            if (numberChooser == "basic")
78
+            {
79
+                nextNumberChooser = new BasicNextNumberChooser();
80
+            }
81
+            else
82
+            {
83
+                Console.Error.WriteLine("Invalid next number chooser");
84
+                Environment.Exit(64);
85
+            }
86
+
87
+            var solver = new BacktrackSearch(nextCellChooser, nextNumberChooser);
71
             var resolved = solver.Resolve(board);
88
             var resolved = solver.Resolve(board);
72
 
89
 
73
             if (resolved.Success)
90
             if (resolved.Success)

+ 7
- 7
uqac-ia-sudoku-csp/Solver/BacktrackSearch.cs View File

1
 using System;
1
 using System;
2
 using System.Collections.Generic;
2
 using System.Collections.Generic;
3
-using System.Linq;
4
 using uqac_ia_sudoku_csp.Interfaces;
3
 using uqac_ia_sudoku_csp.Interfaces;
5
 using uqac_ia_sudoku_csp.Solver.Constraints;
4
 using uqac_ia_sudoku_csp.Solver.Constraints;
6
 
5
 
9
     public class BacktrackSearch : ISolver
8
     public class BacktrackSearch : ISolver
10
     {
9
     {
11
         private readonly INextCellChooser _nextCellChooser;
10
         private readonly INextCellChooser _nextCellChooser;
11
+        private readonly INextNumberChooser _nextNumberChooser;
12
         protected IList<IConstraint> Constraints;
12
         protected IList<IConstraint> Constraints;
13
 
13
 
14
-        public BacktrackSearch(INextCellChooser nextCellChooser)
14
+        public BacktrackSearch(INextCellChooser nextCellChooser, INextNumberChooser nextNumberChooser)
15
         {
15
         {
16
             _nextCellChooser = nextCellChooser;
16
             _nextCellChooser = nextCellChooser;
17
+            _nextNumberChooser = nextNumberChooser;
17
             Constraints = new List<IConstraint>
18
             Constraints = new List<IConstraint>
18
             {
19
             {
19
                 new LineContraint(),
20
                 new LineContraint(),
24
 
25
 
25
         public SolverResult Resolve(Board board)
26
         public SolverResult Resolve(Board board)
26
         {
27
         {
27
-            var domain = Enumerable.Range(0, board.Size).ToList();
28
             var result = new SolverResult();
28
             var result = new SolverResult();
29
             var start = DateTime.Now;
29
             var start = DateTime.Now;
30
-            result.Success = RecursiveResolve(board, result, domain);
30
+            result.Success = RecursiveResolve(board, result);
31
             var end = DateTime.Now;
31
             var end = DateTime.Now;
32
             result.Elapsed = end - start;
32
             result.Elapsed = end - start;
33
             return result;
33
             return result;
34
         }
34
         }
35
 
35
 
36
-        protected bool RecursiveResolve(Board board, SolverResult result, IList<int> domain)
36
+        protected bool RecursiveResolve(Board board, SolverResult result)
37
         {
37
         {
38
             if (board.IsComplete())
38
             if (board.IsComplete())
39
             {
39
             {
43
             _nextCellChooser.SelectVariable(board, out x, out y, Constraints);
43
             _nextCellChooser.SelectVariable(board, out x, out y, Constraints);
44
             if (x != -1 && y != -1)
44
             if (x != -1 && y != -1)
45
             {
45
             {
46
-                foreach (var value in domain)
46
+                foreach (var value in _nextNumberChooser.ChooseNext(board, x, y))
47
                 {
47
                 {
48
                     ++result.TryCount;
48
                     ++result.TryCount;
49
                     board.SetNumber(x, y, value);
49
                     board.SetNumber(x, y, value);
50
                     if (board.IsConsistent(x, y, Constraints))
50
                     if (board.IsConsistent(x, y, Constraints))
51
                     {
51
                     {
52
                         ++result.ConsistentTryCount;
52
                         ++result.ConsistentTryCount;
53
-                        if (RecursiveResolve(board, result, domain))
53
+                        if (RecursiveResolve(board, result))
54
                         {
54
                         {
55
                             return true;
55
                             return true;
56
                         }
56
                         }

+ 14
- 0
uqac-ia-sudoku-csp/Solver/NextNumberChoosers/BasicNextNumberChooser.cs View File

1
+using System.Collections.Generic;
2
+using System.Linq;
3
+using uqac_ia_sudoku_csp.Interfaces;
4
+
5
+namespace uqac_ia_sudoku_csp.Solver.NextNumberChoosers
6
+{
7
+    public class BasicNextNumberChooser : INextNumberChooser
8
+    {
9
+        public IEnumerable<int> ChooseNext(Board board, int x, int y)
10
+        {
11
+            return Enumerable.Range(0, board.Size);
12
+        }
13
+    }
14
+}

Loading…
Cancel
Save