Browse Source

added NextNumberChooser

master
Robin Thoni 7 years ago
parent
commit
290363b38c

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

@@ -0,0 +1,9 @@
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,6 +2,7 @@
2 2
 using uqac_ia_sudoku_csp.Interfaces;
3 3
 using uqac_ia_sudoku_csp.Solver;
4 4
 using uqac_ia_sudoku_csp.Solver.Generators;
5
+using uqac_ia_sudoku_csp.Solver.NextNumberChoosers;
5 6
 using uqac_ia_sudoku_csp.Solver.NextValueChoosers;
6 7
 
7 8
 namespace uqac_ia_sudoku_csp
@@ -20,10 +21,15 @@ namespace uqac_ia_sudoku_csp
20 21
             {
21 22
                 valueChooser = args[1];
22 23
             }
23
-            var emptyCharacters = "0 .";
24
+            var numberChooser = "basic";
24 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 35
             object data;
@@ -67,7 +73,18 @@ namespace uqac_ia_sudoku_csp
67 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 88
             var resolved = solver.Resolve(board);
72 89
 
73 90
             if (resolved.Success)

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

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

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

@@ -0,0 +1,14 @@
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