コード例 #1
0
ファイル: SolverGeneticSharp.cs プロジェクト: momo0154/Sudoku
        private IList <IList <IList <int> > > GetRowsPermutationsUncached(Sudoku Sudoku)

        {
            var toReturn = new List <IList <IList <int> > >(9);

            for (int i = 0; i < 9; i++)

            {
                var tempList = new List <IList <int> >();

                foreach (var perm in AllPermutations)

                {
                    if (!Range9.Any(j => Sudoku.Cells[i * 9 + j] > 0

                                    && (perm[j] != Sudoku.Cells[i * 9 + j])))

                    {
                        tempList.Add(perm);
                    }
                }

                toReturn.Add(tempList);
            }



            return(toReturn);
        }
コード例 #2
0
        /// <summary>
        /// generates a chromosome gene from its index containing a random row permutation
        /// amongst those respecting the target mask.
        /// </summary>
        /// <param name="geneIndex">the index for the gene</param>
        /// <returns>a gene generated for the index</returns>
        public override Gene GenerateGene(int geneIndex)
        {
            var rnd = RandomizationProvider.Current;
            int permIdx;

            do
            {
                //we randomize amongst the permutations that account for the target mask.
                permIdx = rnd.GetInt(0, TargetRowsPermutations[geneIndex].Count);
                var perm = GetPermutation(geneIndex, permIdx);

                //We pick a random previous row to check compatibility with
                if (geneIndex == 0)
                {
                    //we skip first row
                    break;
                }
                var checkRowIdx = rnd.GetInt(0, geneIndex);
                var checkPerm   = GetPermutation(checkRowIdx);
                if (Range9.All(i => perm[i] != checkPerm[i]))
                {
                    break;
                }
            } while (true);



            return(new Gene(permIdx));
        }
コード例 #3
0
        private IList <IList <IList <int> > > GetRowsPermutationsUncached(SudokuBoard sudokuBoard)
        {
            var toReturn = new List <IList <IList <int> > >(9);

            for (int i = 0; i < 9; i++)
            {
                var tempList = new List <IList <int> >();
                foreach (var perm in AllPermutations)
                {
                    if (!Range9.Any(j => sudokuBoard.GetCell(i, j) > 0 &&
                                    (perm[j] != sudokuBoard.GetCell(i, j))))
                    {
                        tempList.Add(perm);
                    }
                }
                toReturn.Add(tempList);
            }

            return(toReturn);
        }
コード例 #4
0
        private IList <IList <IList <int> > > GetRowsPermutationsUncached()
        {
            var toReturn = new List <IList <IList <int> > >(9);

            for (int i = 0; i < 9; i++)
            {
                var tempList = new List <IList <int> >();
                foreach (var perm in AllPermutations)
                {
                    // Permutation should be compatible with current row extended mask domains
                    if (Range9.All(j => ExtendedMask[i * 9 + j].Contains(perm[j])))
                    {
                        tempList.Add(perm);
                    }
                }
                toReturn.Add(tempList);
            }

            return(toReturn);
        }
コード例 #5
0
        private IList <IList <IList <int> > > GetRowsPermutationsUncached(GrilleSudoku objSudoku)
        {
            var toReturn = new List <IList <IList <int> > >(9);

            for (int i = 0; i < 9; i++)
            {
                var tempList = new List <IList <int> >();
                foreach (var perm in AllPermutations)
                {
                    // Permutation should match current mask row numbers, and have numbers different that other mask rows
                    if (!Range9.Any(rowIdx => Range9.Any(j => objSudoku.GetCellule(rowIdx, j) > 0 &&
                                                         ((rowIdx == i && perm[j] != objSudoku.GetCellule(rowIdx, j)) || (rowIdx != i && perm[j] == objSudoku.GetCellule(rowIdx, j))))))
                    {
                        tempList.Add(perm);
                    }
                }
                toReturn.Add(tempList);
            }

            return(toReturn);
        }