Procházet zdrojové kódy

added DhNextCellChooser

master
Robin Thoni před 7 roky
rodič
revize
2400112ca3

+ 1
- 0
README.md Zobrazit soubor

@@ -26,6 +26,7 @@ The program allows 4 optional arguments:
26 26
     - '0': Empty sudoku
27 27
     - filename: load from the specified file
28 28
 - Next cell chooser: The way to select next cell to fill. Possible values:
29
+    - 'mrv-dh': Use the Minimum remaining values strategy then Degree heuristic strategy if multiple cells match
29 30
     - 'mrv' (default): Use the Minimum remaining values strategy
30 31
     - 'dh': Use the Degree heuristic strategy
31 32
     - 'basic': Select the first blank value, from up to down and left to right

+ 16
- 0
uqac-ia-sudoku-csp/Board.cs Zobrazit soubor

@@ -156,5 +156,21 @@ namespace uqac_ia_sudoku_csp
156 156
         {
157 157
             return GetNeighboors(x, y).Where(pair => GetNumber(x, y) == null);
158 158
         }
159
+
160
+        public IEnumerable<KeyValuePair<int, int>> GetEmptyCells()
161
+        {
162
+            var list = new List<KeyValuePair<int, int>>();
163
+            for (var y = 0; y < Size; ++y)
164
+            {
165
+                for (var x = 0; x < Size; ++x)
166
+                {
167
+                    if (GetNumber(x, y) == null)
168
+                    {
169
+                        list.Add(new KeyValuePair<int, int>(x, y));
170
+                    }
171
+                }
172
+            }
173
+            return list;
174
+        }
159 175
     }
160 176
 }

+ 6
- 2
uqac-ia-sudoku-csp/Program.cs Zobrazit soubor

@@ -59,9 +59,13 @@ namespace uqac_ia_sudoku_csp
59 59
             board.Print(Console.Out);
60 60
 
61 61
             INextCellChooser nextCellChooser = null;
62
-            if (valueChooser == "mrv")
62
+            if (valueChooser == "mrv-dh")
63 63
             {
64
-                nextCellChooser = new MrvNextCellChooser();
64
+                nextCellChooser = new MrvNextCellChooser(new DhNextCellChooser());
65
+            }
66
+            else if (valueChooser == "mrv")
67
+            {
68
+                nextCellChooser = new MrvNextCellChooser(null);
65 69
             }
66 70
             else if (valueChooser == "dh")
67 71
             {

+ 28
- 1
uqac-ia-sudoku-csp/Solver/NextCellChoosers/DhNextCellChooser.cs Zobrazit soubor

@@ -1,4 +1,5 @@
1 1
 using System.Collections.Generic;
2
+using System.Linq;
2 3
 using uqac_ia_sudoku_csp.Interfaces;
3 4
 
4 5
 namespace uqac_ia_sudoku_csp.Solver.NextValueChoosers
@@ -7,7 +8,33 @@ namespace uqac_ia_sudoku_csp.Solver.NextValueChoosers
7 8
     {
8 9
         public void SelectVariable(Board board, out int x, out int y, IList<IConstraint> constraints)
9 10
         {
10
-            throw new System.NotImplementedException();
11
+            var dict = new Dictionary<KeyValuePair<int, int>, int>();
12
+            for (var yy = 0; yy < board.Size; ++yy)
13
+            {
14
+                for (var xx = 0; xx < board.Size; ++xx)
15
+                {
16
+                    if (board.GetNumber(xx, yy) == null)
17
+                    {
18
+                        var k = new KeyValuePair<int, int>(xx, yy);
19
+                        if (!dict.ContainsKey(k))
20
+                        {
21
+                            dict.Add(k, 0);
22
+                        }
23
+                        ++dict[k];
24
+                    }
25
+                }
26
+            }
27
+            if (!dict.Any())
28
+            {
29
+                x = -1;
30
+                y = -1;
31
+            }
32
+            else
33
+            {
34
+                var k = dict.OrderByDescending(pair => pair.Value).First().Key;
35
+                x = k.Key;
36
+                y = k.Value;
37
+            }
11 38
         }
12 39
     }
13 40
 }

+ 24
- 6
uqac-ia-sudoku-csp/Solver/NextCellChoosers/MrvNextCellChooser.cs Zobrazit soubor

@@ -5,10 +5,18 @@ namespace uqac_ia_sudoku_csp.Solver.NextValueChoosers
5 5
 {
6 6
     public class MrvNextCellChooser : INextCellChooser
7 7
     {
8
+        private readonly INextCellChooser _nextCellChooser;
9
+
10
+        public MrvNextCellChooser(INextCellChooser nextCellChooser)
11
+        {
12
+            _nextCellChooser = nextCellChooser;
13
+        }
14
+
8 15
         public void SelectVariable(Board board, out int x, out int y, IList<IConstraint> constraints)
9 16
         {
10 17
             var avail = 1;
11
-            while (avail <= board.Size)
18
+            var vars = new List<KeyValuePair<int, int>>();
19
+            while (avail <= board.Size && vars.Count == 0)
12 20
             {
13 21
                 for (var xx = 0; xx < board.Size; ++xx)
14 22
                 {
@@ -19,17 +27,27 @@ namespace uqac_ia_sudoku_csp.Solver.NextValueChoosers
19 27
                             var remaining = GetRemainingValue(board, xx, yy, constraints);
20 28
                             if (remaining == avail)
21 29
                             {
22
-                                x = xx;
23
-                                y = yy;
24
-                                return;
30
+                                vars.Add(new KeyValuePair<int, int>(xx, yy));
25 31
                             }
26 32
                         }
27 33
                     }
28 34
                 }
29 35
                 ++avail;
30 36
             }
31
-            x = -1;
32
-            y = -1;
37
+            if (vars.Count == 0)
38
+            {
39
+                x = -1;
40
+                y = -1;
41
+            }
42
+            else if (vars.Count == 1 || _nextCellChooser == null)
43
+            {
44
+                x = vars[0].Key;
45
+                y = vars[0].Value;
46
+            }
47
+            else
48
+            {
49
+                _nextCellChooser.SelectVariable(board, out x, out y, constraints);
50
+            }
33 51
         }
34 52
 
35 53
         protected int GetRemainingValue(Board board, int x, int y, IList<IConstraint> constraints)

Načítá se…
Zrušit
Uložit