public Grid[] GetInitialPopulation(Grid initialGrid)
 {
     var population = new List<Grid>( );
     for(int i = 0; i < PopulationSize; i++)
         population.Add(GetNewInitialSolution(initialGrid));
     return population.OrderBy(GetFitness).ToArray();
 }
Ejemplo n.º 2
0
 public Grid(Grid initialGrid)
 {
     for (int i = 0; i < initialGrid.Size*initialGrid.Size; i++)
     {
         _grid[i] = initialGrid._grid[i];
     }
 }
Ejemplo n.º 3
0
 public void cannot_solve_puzzle_too_hard()
 {
     string hardGrid = "100060007070054900400800000240500060000000000010002043000005006003920070500070008";
     Grid grid = new Grid(hardGrid);
     solver.Grid = grid;
     solver.Strategies.RemoveAll(s => s.Difficulty > Solver.Strategies.Difficulty.medium);
     solver.Solve();
 }
Ejemplo n.º 4
0
 public void TestNumPlaced()
 {
     Grid g = new Grid(9, 0);
     Assert.AreEqual(0, g.GetNumPlaced());
     g.Place(3, 1, 1);
     g.Place(4, 0, 1);
     Assert.AreEqual(2, g.GetNumPlaced());
 }
Ejemplo n.º 5
0
 public void load_strategies()
 {
     Assert.IsNull(solver.Strategies);
     string easyGrid = "407650030090700000000029000520007840060000001104003020005090084000500003200800000";
     Grid grid = new Grid(easyGrid);
     solver.Grid = grid;
     Assert.AreNotEqual(0, solver.Strategies.Count);
 }
Ejemplo n.º 6
0
 public void cannot_solve_invalid_puzzle()
 {
     string evilGrid = "100060007070054900406800000240500060000000500010002043004005006003920070500070008";
     Grid grid = new Grid(evilGrid);
     solver.Grid = grid;
     solver.Strategies.RemoveAll(s => s.Difficulty > Solver.Strategies.Difficulty.basic &&
         s.Difficulty < Solver.Strategies.Difficulty.evil);
     Assert.IsTrue( solver.Solve());
 }
 public void TestRandomSolver1()
 {
     Grid g = new Grid(9,10);
     BacktrackingSolver rs = new BacktrackingSolver();
     Grid solution = rs.Solve(g);
     Assert.IsNotNull(solution);
     Assert.AreEqual(solution.GetNumMistakes(), 0);
     Assert.AreEqual(solution.GetNumPlaced(),81);
     Console.WriteLine(g);
 }
Ejemplo n.º 8
0
 public void TestNumErrors()
 {
     Grid g = new Grid(9, 0);
     Assert.AreEqual(0, g.GetNumMistakes());
     g.Place(3,1,1);
     g.Place(3,0,1);
     Assert.AreEqual(2, g.GetNumMistakes());
     g.Place(0, 0, 1);
     Assert.AreEqual(0, g.GetNumMistakes());
 }
 public void TestFitness()
 {
     Grid g = new Grid(9,10);
     EvolutionarySolver es = new EvolutionarySolver();
     Assert.AreEqual(71,es.GetFitness(g));
     g.Place(8,g.GetEmptyPositions().First());
     Assert.AreEqual(70, es.GetFitness(g));
     g.Place(8, g.GetEmptyPositions().First());
     Assert.AreEqual(2069, es.GetFitness(g));
 }
Ejemplo n.º 10
0
 public void TestCanPlace()
 {
     Grid g = new Grid(9,0);
     g.Place(3,1,1);
     for (int i = 0; i < 9; i++)
     {
         Assert.AreEqual(false, g.CanPlace(3, i, 1)); //test row
         Assert.AreEqual(false, g.CanPlace(3, 1, i)); //test col
     }
     for (int i = 0; i < 3; i++)
         for (int j = 0; j < 3; j++ )
             Assert.AreEqual(false, g.CanPlace(3, i, j)); //test row
     Assert.AreEqual(true, g.CanPlace(3, 5, 5)); //test col
 }
        private bool SolveAux(Grid initial)
        {
            if (initial.IsSolved())
                return true;
            int pos = GetNextPosition(initial);
            var listMoves = initial.GetAvailableNumbersFor(pos);
            foreach (var move in listMoves)
            {
                initial.Place(move,pos);
                if (SolveAux(initial))
                    return true;
                initial.Place(0,pos);

            }
            return false;
        }
        public override Grid Solve(Grid initialGrid)
        {
            Random r = new Random();
            var population = GetInitialPopulation(initialGrid);
            while (true)
            {
                var nextGeneration = Crossover(SelectForCrossover(population));
                foreach (var individual in nextGeneration)
                {
                    if (r.NextDouble() < MutationProbability)
                        Mutate(individual);
                }
                population = SelectNewPopulation(population, nextGeneration);

            }
        }
        public Grid GetNewInitialSolution(Grid initialGrid)
        {
            Random r = new Random();
            Grid g = new Grid(initialGrid);
            for (int i = 0; i < InitiPopulationIterationCount;i++ )
            {
                var n = r.Next(1, g.Size+1);
                var row = r.Next(0, g.Size);
                var col = r.Next(0, g.Size);
                if (g.Get(row, col) != 0)
                {
                    g.Place(n,row,col);

                }
            }
            return g;
        }
        public void TestInitialPopulation()
        {
            Grid g = new Grid(9,8);
            EvolutionarySolver es = new EvolutionarySolver();
            var initPop = es.GetInitialPopulation(g);
            foreach (var grid in initPop)
            {
                  Assert.IsTrue(grid.GetNumPlaced() >= g.GetNumPlaced());
                var empty = grid.GetEmptyPositions();
                  var isect = g.GetEmptyPositions().Intersect(empty).ToList();

                foreach (var i in isect)
                {
                    Assert.IsTrue(empty.Contains(i));
                }

            }
        }
        public int GetNextPosition(Grid g)
        {
            int min = int.MaxValue;
            int minPos = -1;
            for(int i = 0; i < g.Size; i++)
                for (int j = 0; j < g.Size; j++)
                {
                    if (g.Get(i, j) != 0)
                        continue;
                    var l = g.GetAvailableNumbersFor(i, j);
                    if (l.Count < min)
                    {
                        min = l.Count;
                        minPos = i*g.Size + j;

                    }
                    if (min == 1)
                        return minPos;
                }

            return minPos;
        }
Ejemplo n.º 16
0
 public void solve_easy()
 {
     string easyGrid = "407650030090700000000029000520007840060000001104003020005090084000500003200800000";
     Grid grid = new Grid(easyGrid);
     solver.Grid = grid;
     Assert.IsTrue(solver.Solve());
 }
 private Grid[] SelectNewPopulation(Grid[] population, Grid[] nextGeneration)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 18
0
 public void TestGet()
 {
     Grid g = new Grid(9,0);
     g.Place(3,1,1);
     Assert.AreEqual(3,g.Get(1,1));
 }
Ejemplo n.º 19
0
 public void solve_evil_with_brute_force_test()
 {
     string evilGrid = "100060007070054900400800000240500060000000000010002043000005006003920070500070008";
     Grid grid = new Grid(evilGrid);
     solver.Grid = grid;
     solver.Strategies.RemoveAll(s => s.Difficulty > Solver.Strategies.Difficulty.basic &&
         s.Difficulty < Solver.Strategies.Difficulty.evil);
     Assert.IsTrue( solver.Solve());
 }
Ejemplo n.º 20
0
 public void solve_medium()
 {
     string midGrid = "400008900010030750008509000070000506000000240004000093340900000107005800005000020";
     Grid grid = new Grid(midGrid);
     solver.Grid = grid;
     solver.Strategies.RemoveAll(s => s.Difficulty > Solver.Strategies.Difficulty.medium);
     Assert.IsTrue( solver.Solve());
 }
Ejemplo n.º 21
0
 public void TestGeneration()
 {
     Grid g = new Grid(9,10);
     Assert.AreEqual(0,g.GetNumMistakes());
     Assert.AreEqual(10,g.GetNumPlaced());
 }
Ejemplo n.º 22
0
 public void verify_test()
 {
     string solved = "182369457376254981459817632247538169938146725615792843721485396863921574594673218";
     Grid grid = new Grid(solved);
     Assert.IsTrue(grid.isValid);
 }
Ejemplo n.º 23
0
 public void is_solved()
 {
     string solvedGrid = "957849992998369599699999999916998943992435199949199829999999998995983299799924369";
     grid = new Grid(solvedGrid);
     Assert.IsTrue(grid.IsSolved);
 }
Ejemplo n.º 24
0
 public void solve_need_subgroup_reduction()
 {
     string medGrid = "000020100587010200009503040040000502000000000705000010070304800002060957006090000";
     Grid grid = new Grid(medGrid);
     solver.Grid = grid;
     solver.Strategies.RemoveAll(s => s.Difficulty > Solver.Strategies.Difficulty.medium);
     Assert.IsTrue( solver.Solve());
 }
Ejemplo n.º 25
0
 public void MyTestInitialize()
 {
     this.grid = new Grid(sGrid);
      Assert.IsNotNull(grid);
 }
 public override Grid Solve(Grid initial)
 {
     if (SolveAux(initial))
         return initial;
     return null;
 }
Ejemplo n.º 27
0
 public Solver(Grid grid)
 {
     this.Grid = grid;
 }
Ejemplo n.º 28
0
 public void MyTestInitialize()
 {
     this.grid = new Grid(sGrid);
     this.solver = new Solver.Solver();
     Assert.IsNotNull(grid);
 }
Ejemplo n.º 29
0
 public void solve_with_XWing()
 {
     string hardGrid = "100060007070054900400800000240500060000000000010002043000005006003920070500070008";
     Grid grid = new Grid(hardGrid);
     solver.Grid = grid;
     solver.Strategies.RemoveAll(s => s.Difficulty > Solver.Strategies.Difficulty.hard);
     Assert.IsTrue( solver.Solve());
 }
 private double[] GetPopulationFitness(Grid[] population)
 {
     double sum =
 }