Przeglądaj źródła

added arguments; added ConsistentTryCount; added Elapsed; fixed mrv for empty sudoku

master
Robin Thoni 7 lat temu
rodzic
commit
262a9e6384

+ 56
- 9
uqac-ia-sudoku-csp/Program.cs Wyświetl plik

@@ -1,4 +1,5 @@
1 1
 using System;
2
+using uqac_ia_sudoku_csp.Interfaces;
2 3
 using uqac_ia_sudoku_csp.Solver;
3 4
 using uqac_ia_sudoku_csp.Solver.Generators;
4 5
 using uqac_ia_sudoku_csp.Solver.NextValueChoosers;
@@ -9,20 +10,65 @@ namespace uqac_ia_sudoku_csp
9 10
     {
10 11
         public static void Main(string[] args)
11 12
         {
13
+            var generatorType = "-";
14
+            if (args.Length > 0)
15
+            {
16
+                generatorType = args[0];
17
+            }
18
+            var valueChooser = "mrv";
19
+            if (args.Length > 1)
20
+            {
21
+                valueChooser = args[1];
22
+            }
23
+            var emptyCharacters = "0 .";
24
+            if (args.Length > 2)
25
+            {
26
+                emptyCharacters = args[2];
27
+            }
28
+
29
+            object data;
30
+            IGenerator generator;
12 31
             var board = new Board("123456789");
13 32
 
14
-            var generator = new FileGenerator();
15
-            generator.Generate(board, new FileGeneratorDbo
33
+            if (generatorType == "-")
34
+            {
35
+                generator = new StdInGenerator();
36
+                data = emptyCharacters;
37
+            }
38
+            else if (generatorType == "0")
16 39
             {
17
-                EmptyCharacters = "0 .",
18
-                FilePath = "../sample/04.txt"
19
-            });
40
+                generator = new EmptyGenerator();
41
+                data = null;
42
+            }
43
+            else
44
+            {
45
+                generator = new FileGenerator();
46
+                data = new FileGeneratorDbo
47
+                {
48
+                    EmptyCharacters = emptyCharacters,
49
+                    FilePath = generatorType
50
+                };
51
+            }
52
+            generator.Generate(board, data);
20 53
             board.Print(Console.Out);
21 54
 
22
-            var solver = new BacktrackSearch(new MRVNextValueChooser());
23
-            var start = DateTime.Now;
55
+            INextValueChooser nextValueChooser = null;
56
+            if (valueChooser == "mrv")
57
+            {
58
+                nextValueChooser = new MRVNextValueChooser();
59
+            }
60
+            else if (valueChooser == "basic")
61
+            {
62
+                nextValueChooser = new BasicNextValueChooser();
63
+            }
64
+            else
65
+            {
66
+                Console.Error.WriteLine("Invalid next value chooser");
67
+                Environment.Exit(64);
68
+            }
69
+
70
+            var solver = new BacktrackSearch(nextValueChooser);
24 71
             var resolved = solver.Resolve(board);
25
-            var end = DateTime.Now;
26 72
 
27 73
             if (resolved.Success)
28 74
             {
@@ -34,7 +80,8 @@ namespace uqac_ia_sudoku_csp
34 80
                 Console.WriteLine("Not resolved");
35 81
             }
36 82
             Console.WriteLine($"{resolved.TryCount} tries");
37
-            Console.WriteLine($"{(end - start).TotalMilliseconds} ms");
83
+            Console.WriteLine($"{resolved.ConsistentTryCount} consistent tries");
84
+            Console.WriteLine($"{resolved.Elapsed.TotalMilliseconds} ms");
38 85
         }
39 86
     }
40 87
 }

+ 6
- 1
uqac-ia-sudoku-csp/Solver/BacktrackSearch.cs Wyświetl plik

@@ -1,4 +1,5 @@
1
-using System.Collections.Generic;
1
+using System;
2
+using System.Collections.Generic;
2 3
 using System.Linq;
3 4
 using uqac_ia_sudoku_csp.Interfaces;
4 5
 using uqac_ia_sudoku_csp.Solver.Constraints;
@@ -25,7 +26,10 @@ namespace uqac_ia_sudoku_csp.Solver
25 26
         {
26 27
             var domain = Enumerable.Range(0, board.Size).ToList();
27 28
             var result = new SolverResult();
29
+            var start = DateTime.Now;
28 30
             result.Success = RecursiveResolve(board, result, domain);
31
+            var end = DateTime.Now;
32
+            result.Elapsed = end - start;
29 33
             return result;
30 34
         }
31 35
 
@@ -45,6 +49,7 @@ namespace uqac_ia_sudoku_csp.Solver
45 49
                     board.SetNumber(x, y, value);
46 50
                     if (board.IsConsistent(x, y, Constraints))
47 51
                     {
52
+                        ++result.ConsistentTryCount;
48 53
                         if (RecursiveResolve(board, result, domain))
49 54
                         {
50 55
                             return true;

+ 0
- 10
uqac-ia-sudoku-csp/Solver/Generator.cs Wyświetl plik

@@ -1,10 +0,0 @@
1
-namespace uqac_ia_sudoku_csp
2
-{
3
-    public class Generator
4
-    {
5
-        public void Generate(Board board, object data)
6
-        {
7
-//            board.SetNumber(0, 0, 0);
8
-        }
9
-    }
10
-}

uqac-ia-sudoku-csp/Solver/NextValueChoosers/BasicValueChooser.cs → uqac-ia-sudoku-csp/Solver/NextValueChoosers/BasicNextValueChooser.cs Wyświetl plik

@@ -3,7 +3,7 @@ using uqac_ia_sudoku_csp.Interfaces;
3 3
 
4 4
 namespace uqac_ia_sudoku_csp.Solver.NextValueChoosers
5 5
 {
6
-    public class BasicValueChooser : INextValueChooser
6
+    public class BasicNextValueChooser : INextValueChooser
7 7
     {
8 8
         public void SelectVariable(Board board, out int x, out int y, IList<IConstraint> constraints)
9 9
         {

+ 1
- 1
uqac-ia-sudoku-csp/Solver/NextValueChoosers/MRVNextValueChooser.cs Wyświetl plik

@@ -8,7 +8,7 @@ namespace uqac_ia_sudoku_csp.Solver.NextValueChoosers
8 8
         public void SelectVariable(Board board, out int x, out int y, IList<IConstraint> constraints)
9 9
         {
10 10
             var avail = 1;
11
-            while (avail < board.Size)
11
+            while (avail <= board.Size)
12 12
             {
13 13
                 for (var xx = 0; xx < board.Size; ++xx)
14 14
                 {

+ 7
- 1
uqac-ia-sudoku-csp/Solver/SolverResult.cs Wyświetl plik

@@ -1,9 +1,15 @@
1
-namespace uqac_ia_sudoku_csp.Solver
1
+using System;
2
+
3
+namespace uqac_ia_sudoku_csp.Solver
2 4
 {
3 5
     public class SolverResult
4 6
     {
5 7
         public int TryCount { get; set; }
6 8
 
9
+        public int ConsistentTryCount { get; set; }
10
+
7 11
         public bool Success { get; set; }
12
+
13
+        public TimeSpan Elapsed { get; set; }
8 14
     }
9 15
 }

Ładowanie…
Anuluj
Zapisz