from itertools import combinations
############################################
+class Solver:
+ CSOLVER=1
+ SERIALISE=2
+ ALLOY=3
+ Z3=4
+ MATHSAT=5
+ SMTRAT=6
+def getSolverType(solverID):
+ return solverID-2
+
+
####
# Helper functions
###
def main():
if len(sys.argv) <= 1:
- print "usage: ./run.sh csolverHexiom.py [0-40] [--dump/--alloy]?"
+ print "usage: ./run.sh csolverHexiom.py [0-40] [--dump/--alloy/--z3/--mathsat/--smtrat]?"
exit(1)
level_no = int(sys.argv[1])
csolverlb = ps.loadCSolver();
csolver = csolverlb.createCCSolver()
if len(sys.argv) > 2 and sys.argv[-1] == "--alloy":
- csolverlb.setAlloyEncoder(csolver)
+ csolverlb.setInterpreter(csolver, getSolverType(Solver.ALLOY) )
+ elif len(sys.argv) > 2 and sys.argv[-1] == "--z3":
+ csolverlb.setInterpreter(csolver, getSolverType(Solver.Z3))
+ elif len(sys.argv) > 2 and sys.argv[-1] == "--mathsat":
+ csolverlb.setInterpreter(csolver, getSolverType(Solver.MATHSAT) )
+ elif len(sys.argv) > 2 and sys.argv[-1] == "--smtrat":
+ csolverlb.setInterpreter(csolver, getSolverType(Solver.SMTRAT) )
+ elif len(sys.argv) > 2:
+ print "Unknown command ..."
+ exit(-1)
formulation = SAT_formulation_from_board_input(board_input, csolverlb, csolver)
print '=== Done! Calling CSolver solver now ==='
class KSudokuArgParser:
- def __init__(self):
- self.parser = argparse.ArgumentParser(description='Killer Sudoku Puzzle!')
- self.parser.add_argument('--problem', '-p', metavar='problem.killer', type=str, nargs=1,help='Files generated by KillerSudokuGenerator.py')
- self.parser.add_argument('--csolver', '-cs', action='store_true',help='Encode with the constraint solver (default: uses the original hand crafted encoding)')
- self.parser.add_argument('--dump', '-dp', action='store_true',help='Dumps the problem into a file')
- self.parser.add_argument('--alloy', '-al', action='store_true',help='Solve problem via Alloy')
- self.args = self.parser.parse_args()
-
- def getProblemName(self):
- return self.args.problem
+ def __init__(self):
+ self.parser = argparse.ArgumentParser(description='Killer Sudoku Puzzle!')
+ self.parser.add_argument('--problem', '-p', metavar='problem.killer', type=str, nargs=1,help='Files generated by KillerSudokuGenerator.py')
+ self.parser.add_argument('--csolver', '-cs', action='store_true',help='Encode with the constraint solver (default: uses the original hand crafted encoding)')
+ self.parser.add_argument('--dump', '-dp', action='store_true',help='Dumps the problem into a file')
+ self.parser.add_argument('--alloy', '-al', action='store_true',help='Solve problem via Alloy')
+ self.parser.add_argument('--z3', '-z', action='store_true',help='Solve problem via z3')
+ self.parser.add_argument('--mathsat', '-ms', action='store_true',help='Solve problem via MathSat')
+ self.parser.add_argument('--smtrat', '-sr', action='store_true',help='Solve problem via SMTRat')
+ self.args = self.parser.parse_args()
- def getCSolverOption(self):
- if self.args.alloy:
- return 3
- if self.args.dump:
- return 2
- if self.args.csolver:
- return 1
- else:
- return 0
+ def getProblemName(self):
+ return self.args.problem
+
+ def getCSolverOption(self):
+ if self.args.smtrat:
+ return 6
+ if self.args.mathsat:
+ return 5
+ if self.args.z3:
+ return 4
+ if self.args.alloy:
+ return 3
+ if self.args.dump:
+ return 2
+ if self.args.csolver:
+ return 1
+ else:
+ return 0
# def main():
# print sys.argv
#
#
# if __name__ == "__main__":
-# main()
\ No newline at end of file
+# main()
from itertools import combinations, ifilter, chain
class Solver:
- CSOLVER=1
+ CSOLVER=1
SERIALISE=2
ALLOY=3
+ Z3=4
+ MATHSAT=5
+ SMTRAT=6
+
+def getSolverType(solverID):
+ return solverID-2
def solveProblem(N, killerRules, solverOption):
def generateKillerSudokuConstraints(N, killerRules, solverOption):
csolverlb = ps.loadCSolver()
solver = csolverlb.createCCSolver()
- if solverOption == Solver.ALLOY:
- csolverlb.setAlloyEncoder(solver)
+ print solverOption
+ if solverOption >= Solver.ALLOY:
+ csolverlb.setInterpreter(solver, getSolverType(solverOption) )
s1 = [ i for i in range(1, N+1)]
set1 = (c_long* len(s1))(*s1)
s1 = csolverlb.createSet(solver, c_uint(1), set1, c_uint(N))
#define NANOSEC 1000000000.0
using namespace std;
-enum SolverType {CSOLVER, DUMP, ALLOY};
-
+enum SolverType {_CSOLVER, _DUMP, _ALLOY, _Z3, _MATHSAT, _SMTRAT};
+InterpreterType convertSolverToInterpreterType(SolverType type){
+ return (InterpreterType)(type -1);
+}
void EqualOneToCNF(vector<int> literals, vector< vector<int> > & cnf){
int N = literals.size();
cnf.push_back(literals);
}
-void csolverNQueens(int N, SolverType stype = CSOLVER){
+void csolverNQueens(int N, SolverType stype = _CSOLVER){
if(N <=1){
cout<<"Q" << endl;
return;
}
CSolver *solver = new CSolver();
- if(stype = ALLOY){
- solver->setAlloyEncoder();
+ if(stype >= _ALLOY){
+ solver->setInterpreter( convertSolverToInterpreterType(stype) );
}
uint64_t domain[N];
diagonallyDifferentConstraintBothDir(solver, N, elems);
symmetryBreakingConstraint(solver, N, elems);
// solver->printConstraints();
- if(stype == DUMP){
+ if(stype == _DUMP){
solver->serialize();
} if (solver->solve() != 1){
printf("Problem is Unsolvable ...\n");
int main(int argc, char * argv[]){
if(argc < 2){
- printf("Two arguments are needed\n./nqueen <size> [--csolver/--dump/--alloy]\n");
+ printf("Two arguments are needed\n./nqueen <size> [--csolver/--dump/--alloy/--z3/--mathsat/--smtrat]\n");
exit(-1);
}
int N = atoi(argv[1]);
csolverNQueens(N);
}else if (strcmp( argv[2], "--dump") == 0){
printf("Running the CSolver encoding ...\n");
- csolverNQueens(N, DUMP);
+ csolverNQueens(N, _DUMP);
}else if (strcmp( argv[2], "--alloy") == 0){
printf("Running the Alloy encoding ...\n");
- csolverNQueens(N, ALLOY);
+ csolverNQueens(N, _ALLOY);
+ }else if (strcmp( argv[2], "--z3") == 0){
+ printf("Running the Z3 encoding ...\n");
+ csolverNQueens(N, _Z3);
+ }else if (strcmp( argv[2], "--mathsat") == 0){
+ printf("Running the mathsat encoding ...\n");
+ csolverNQueens(N, _MATHSAT);
+ } else if (strcmp( argv[2], "--smtrat") == 0){
+ printf("Running the SMTRat encoding ...\n");
+ csolverNQueens(N, _SMTRAT);
}else {
printf("Unknown argument %s", argv[2]);
}
elif "--alloy" in args:
print "Solving the problem using alloy ..."
read_problem_from_file(args[indx], 3)
+ elif "--z3" in args:
+ print "Solving the problem using alloy ..."
+ read_problem_from_file(args[indx], 4)
+ elif "--mathsat" in args:
+ print "Solving the problem using alloy ..."
+ read_problem_from_file(args[indx], 5)
+ elif "--smtrat" in args:
+ print "Solving the problem using alloy ..."
+ read_problem_from_file(args[indx], 6)
else:
read_problem_from_file(args[indx], 0)
elif opt in ( "--gen"):
print('Sudoku.py -h [or] --hard')
print('Sudoku.py -v [or] --evil')
print('Sudoku.py -b [or] --blank')
- print('Sudoku.py --file file.problem [--csolver/--dump/--alloy]')
+ print('Sudoku.py --file file.problem [--csolver/--dump/--alloy/--z3/--mathsat/--smtrat]')
print('Sudoku.py --gen 9 20 [--csolver/--dump]')
print('All problems generated by websudoku.com')
sys.exit()
CSOLVER=1
SERIALISE=2
ALLOY=3
+ Z3=4
+ MATHSAT=5
+ SMTRAT=6
+def getSolverType(solverID):
+ return solverID-2
+
+
def generateProblem(N):
return generateSudokuConstraints(N)
def generateSudokuConstraints(N, sudoku = None, solverID = -1):
csolverlb = ps.loadCSolver()
solver = csolverlb.createCCSolver()
- if solverID == Solver.ALLOY:
- csolverlb.setAlloyEncoder(solver)
+ if solverID >= Solver.ALLOY:
+ csolverlb.setInterpreter(solver, getSolverType(solverID))
s1 = [ i for i in range(1, N+1)]
set1 = (c_long* len(s1))(*s1)
s1 = csolverlb.createSet(solver, c_uint(1), set1, c_uint(N))