Browse Source

added lcv

master
Robin Thoni 7 years ago
parent
commit
68b7cd4b7d

+ 40
- 0
uqac-ia-sudoku-csp/Board.cs View File

@@ -2,6 +2,7 @@
2 2
 using System.Collections.Generic;
3 3
 using System.IO;
4 4
 using System.Linq;
5
+using System.Security.Cryptography.X509Certificates;
5 6
 using uqac_ia_sudoku_csp.Interfaces;
6 7
 
7 8
 namespace uqac_ia_sudoku_csp
@@ -116,5 +117,44 @@ namespace uqac_ia_sudoku_csp
116 117
             }
117 118
             stream.WriteLine(new string('-', Size + 2));
118 119
         }
120
+
121
+        public IEnumerable<KeyValuePair<int, int>> GetNeighboors(int x, int y)
122
+        {
123
+            var c = new KeyValuePair<int, int>(x, y);
124
+            var cells = new List<KeyValuePair<int, int>>();
125
+            for (var i = 0; i < Size; ++i)
126
+            {
127
+                var k = new KeyValuePair<int, int>(i, y);
128
+                if (k.Equals(c) && !cells.Contains(k))
129
+                {
130
+                    cells.Add(k);
131
+                }
132
+                k = new KeyValuePair<int, int>(x, i);
133
+                if (k.Equals(c) && !cells.Contains(k))
134
+                {
135
+                    cells.Add(k);
136
+                }
137
+            }
138
+            var bs = Math.Sqrt(Size);
139
+            var sx = x / 3 * 3;
140
+            var sy = y / 3 * 3;
141
+            for (var xx = sx; xx < sx + bs; ++xx)
142
+            {
143
+                for (var yy = sy; yy < sy + bs; ++yy)
144
+                {
145
+                    var k = new KeyValuePair<int, int>(xx, yy);
146
+                    if (k.Equals(c) && !cells.Contains(k))
147
+                    {
148
+                        cells.Add(k);
149
+                    }
150
+                }
151
+            }
152
+            return cells;
153
+        }
154
+
155
+        public IEnumerable<KeyValuePair<int, int>> GetEmptyNeighboors(int x, int y)
156
+        {
157
+            return GetNeighboors(x, y).Where(pair => GetNumber(x, y) == null);
158
+        }
119 159
     }
120 160
 }

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

@@ -4,6 +4,6 @@ namespace uqac_ia_sudoku_csp.Interfaces
4 4
 {
5 5
     public interface INextNumberChooser
6 6
     {
7
-        IEnumerable<int> ChooseNext(Board board, int x, int y);
7
+        IEnumerable<int> ChooseNext(IList<IConstraint> constraints, Board board, int x, int y);
8 8
     }
9 9
 }

+ 9
- 1
uqac-ia-sudoku-csp/Program.cs View File

@@ -21,7 +21,7 @@ namespace uqac_ia_sudoku_csp
21 21
             {
22 22
                 valueChooser = args[1];
23 23
             }
24
-            var numberChooser = "basic";
24
+            var numberChooser = "lcv";
25 25
             if (args.Length > 2)
26 26
             {
27 27
                 numberChooser = args[2];
@@ -63,6 +63,10 @@ namespace uqac_ia_sudoku_csp
63 63
             {
64 64
                 nextCellChooser = new MrvNextCellChooser();
65 65
             }
66
+            else if (valueChooser == "dh")
67
+            {
68
+                nextCellChooser = new DhNextCellChooser();
69
+            }
66 70
             else if (valueChooser == "basic")
67 71
             {
68 72
                 nextCellChooser = new BasicNextCellChooser();
@@ -78,6 +82,10 @@ namespace uqac_ia_sudoku_csp
78 82
             {
79 83
                 nextNumberChooser = new BasicNextNumberChooser();
80 84
             }
85
+            else if (numberChooser == "lcv")
86
+            {
87
+                nextNumberChooser = new LcvNextNumberChooser();
88
+            }
81 89
             else
82 90
             {
83 91
                 Console.Error.WriteLine("Invalid next number chooser");

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

@@ -43,7 +43,7 @@ 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 _nextNumberChooser.ChooseNext(board, x, y))
46
+                foreach (var value in _nextNumberChooser.ChooseNext(Constraints, board, x, y))
47 47
                 {
48 48
                     ++result.TryCount;
49 49
                     board.SetNumber(x, y, value);

+ 13
- 0
uqac-ia-sudoku-csp/Solver/NextCellChoosers/DhNextCellChooser.cs View File

@@ -0,0 +1,13 @@
1
+using System.Collections.Generic;
2
+using uqac_ia_sudoku_csp.Interfaces;
3
+
4
+namespace uqac_ia_sudoku_csp.Solver.NextValueChoosers
5
+{
6
+    public class DhNextCellChooser : INextCellChooser
7
+    {
8
+        public void SelectVariable(Board board, out int x, out int y, IList<IConstraint> constraints)
9
+        {
10
+            throw new System.NotImplementedException();
11
+        }
12
+    }
13
+}

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

@@ -6,7 +6,7 @@ namespace uqac_ia_sudoku_csp.Solver.NextNumberChoosers
6 6
 {
7 7
     public class BasicNextNumberChooser : INextNumberChooser
8 8
     {
9
-        public IEnumerable<int> ChooseNext(Board board, int x, int y)
9
+        public IEnumerable<int> ChooseNext(IList<IConstraint> constraints, Board board, int x, int y)
10 10
         {
11 11
             return Enumerable.Range(0, board.Size);
12 12
         }

+ 46
- 0
uqac-ia-sudoku-csp/Solver/NextNumberChoosers/LcvNextNumberChooser.cs View File

@@ -0,0 +1,46 @@
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 LcvNextNumberChooserValue
8
+    {
9
+        public int Value { get; set; }
10
+
11
+        public int LegalValuesCount { get; set; }
12
+    }
13
+
14
+    public class LcvNextNumberChooser : INextNumberChooser
15
+    {
16
+        public IEnumerable<int> ChooseNext(IList<IConstraint> constraints, Board board, int x, int y)
17
+        {
18
+            return Enumerable.Range(0, board.Size)
19
+                .Select(v => new LcvNextNumberChooserValue {
20
+                    LegalValuesCount = ComputeLegalValues(constraints, board, v, x, y),
21
+                    Value = v
22
+                })
23
+                .OrderBy(lcvValues => lcvValues.LegalValuesCount)
24
+                .Select(lcvValues => lcvValues.Value);
25
+        }
26
+
27
+        protected int ComputeLegalValues(IList<IConstraint> constraints, Board board, int v, int x, int y)
28
+        {
29
+            return board.GetEmptyNeighboors(x, y)
30
+                .Select(pair =>
31
+                {
32
+                    var c = 0;
33
+                    for (var i = 0; i < board.Size; ++i)
34
+                    {
35
+                        board.SetNumber(x, y, i);
36
+                        if (board.IsConsistent(x, y, constraints))
37
+                        {
38
+                            ++c;
39
+                        }
40
+                    }
41
+                    return c;
42
+                })
43
+                .Aggregate(0, (i, i1) => i + i1);
44
+        }
45
+    }
46
+}

Loading…
Cancel
Save