Example #1
0
        public object Clone()
        {
            var toReturn = new DynamicCSP();

            foreach (Variable variable in this.getVariables().toArray())
            {
                toReturn.addVariable(variable);
                toReturn.setDomain(variable, new Domain(this.getDomain(variable).asList()));
            }

            foreach (Constraint constraint in this.getConstraints().toArray())
            {
                toReturn.addConstraint(constraint);
            }

            return(toReturn);
        }
Example #2
0
        // CSP de Sudoku sans masque (les règles)
        public static CSP GetSudokuBaseCSP()
        {
            if (_BaseSudokuCSP == null)
            {
                lock (_BaseSudokuCSPLock)
                {
                    if (_BaseSudokuCSP == null)
                    {
                        var toReturn = new DynamicCSP();


                        //Domaine

                        var cellPossibleValues = Enumerable.Range(1, 9);
                        var cellDomain         = new Domain(cellPossibleValues.Cast <object>().ToArray());



                        //Variables

                        var variables = new Dictionary <int, Dictionary <int, Variable> >();
                        for (int rowIndex = 0; rowIndex < 9; rowIndex++)
                        {
                            var rowVars = new Dictionary <int, Variable>();
                            for (int colIndex = 0; colIndex < 9; colIndex++)
                            {
                                var varName      = GetVarName(rowIndex, colIndex);
                                var cellVariable = new Variable(varName);
                                toReturn.AddNewVariable(cellVariable);
                                toReturn.setDomain(cellVariable, cellDomain);
                                rowVars[colIndex] = cellVariable;
                            }

                            variables[rowIndex] = rowVars;
                        }



                        //Contraintes

                        var contraints = new List <Constraint>();

                        // Lignes
                        foreach (var objPair in variables)
                        {
                            var ligneVars = objPair.Value.Values.ToList();

                            var ligneContraintes = SudokuCSPHelper.GetAllDiffConstraints(ligneVars);
                            contraints.AddRange(ligneContraintes);
                            //var objContrainte = new AllDiffConstraint<int>(ligneVars);
                            //toReturn.addConstraint(objContrainte);
                        }

                        //colonnes
                        for (int j = 0; j < 9; j++)
                        {
                            var jClosure       = j;
                            var colVars        = variables.Values.SelectMany(x => { return(new Variable[] { x[jClosure] }); }).ToList();
                            var colContraintes = SudokuCSPHelper.GetAllDiffConstraints(colVars);
                            contraints.AddRange(colContraintes);
                            //var objContrainte = new AllDiffConstraint<int>(colVars);
                            //                     toReturn.addConstraint(objContrainte);
                        }

                        //Boites

                        for (int b = 0; b < 9; b++)
                        {
                            var boiteVars = new List <Variable>();
                            var iStart    = 3 * (b / 3);
                            var jStart    = 3 * (b % 3);
                            for (int i = 0; i < 3; i++)
                            {
                                for (int j = 0; j < 3; j++)
                                {
                                    boiteVars.Add(variables[iStart + i][jStart + j]);
                                }
                            }
                            var boitesContraintes = SudokuCSPHelper.GetAllDiffConstraints(boiteVars);
                            contraints.AddRange(boitesContraintes);

                            //var objContrainte = new AllDiffConstraint<int>(boiteVars);
                            //toReturn.addConstraint(objContrainte);
                        }


                        //Ajout de toutes les contraintes
                        foreach (var constraint in contraints)
                        {
                            toReturn.addConstraint(constraint);
                        }

                        //return toReturn;
                        _BaseSudokuCSP = toReturn;
                    }
                }
            }

            return((CSP)_BaseSudokuCSP.Clone());
        }