Exemplo n.º 1
0
    def run(self, start = 0, end = 1011):
        rules, _ = load_dimacs('rules/sudoku_rules_9x9.txt')
        sudokus = load_sudokus('sudokus/1000_sudokus_9x9.txt')[start:end]
        data = []

        heuristicDict = {
            "Random": RandomBranchDecisionFactory,
            "DLIS(True)": DynamicLargestIndividualSumTrueFactory,
            "DLIS(False)": DynamicLargestIndividualSumFalseFactory,
            "JW-OS": JeroslowWangOneSidedFactory,
            "DLCS": DynamicLargestCombinedSumFactory,
            "Dummy": DummyBranchDecisionFactory,
            "RandomFalse": RandomFalseBranchDecisionFactory,
        }

        inputValues = []
        for i in range(len(sudokus)):
            sudoku = sudokus[i]
            sudokuID = start + i + 1
            inputValues.append((sudokuID, sudoku, rules, heuristicDict))

        print("cpu_count={}".format(mp.cpu_count()))
        pool = mp.Pool(mp.cpu_count())
        results = pool.starmap(processOneSudokuWithHeuristic, inputValues)
        pool.close()
        for result in results:
            data.extend(result)

        # Save output to csv
        filename="SolverComparison_{}_{}.csv".format(start, end)
        save_csv(filename, heuristicDataHeader, data)
        print("")
Exemplo n.º 2
0
    def run(self, numOfConstraints, timeout, start=0, end=1011):
        sudokus = load_sudokus('sudokus/1000_sudokus_9x9.txt')[start:end]
        data = []

        rulesDict = self.generateRules(numOfConstraints)
        inputValues = []

        for i in range(len(sudokus)):
            sudoku = sudokus[i]
            sudokuID = i + 1
            inputValues.append(
                (sudokuID, sudoku, rulesDict, numOfConstraints, timeout))

        print("cpu_count={}".format(mp.cpu_count()))
        pool = mp.Pool(mp.cpu_count())
        results = pool.starmap(processOneSudoku, inputValues)
        pool.close()
        for result in results:
            data.extend(result)

        # Save output to csv
        header = [
            "sudokuID", "name", "numOfConstraints", "result", "totalTime",
            "loop", "backtrack", "flip", "unit"
        ]
        filename = "constraints_{}_{}_{}.csv".format(numOfConstraints, start,
                                                     end)
        save_csv(filename, header, data)
        print("")
Exemplo n.º 3
0
def main():
    rules, numOfVars = load_dimacs('rules/sudoku_rules_9x9.txt')
    sudokus = load_sudokus('sudokus/1000_sudokus_9x9.txt')

    solverSpecs = [{
        "SolverClass": BasicDPLL,
    }]

    # cnf = [[1,2,3], [1,-2],[1,-3],[-1,3]]
    # numOfVars = 3

    for solverSpec in solverSpecs:
        SolverClass = solverSpec["SolverClass"]
        overallMetrics = InMemoryMetrics()
        for sudoku in sudokus:
            cnf = rules + sudoku
            instanceMetrics = InMemoryMetrics()

            before = time.time()
            solver = SolverClass(cnf,
                numOfVars,
                # DynamicLargestIndividualSum(True),
                # JeroslowWangOneSided(),
                # DynamicLargestCombinedSum(),
                DummyBranchDecision(),
                -1,
                0,
                instanceMetrics
            )
            result, model = solver.solve()
            totalTime = time.time()-before

            overallMetrics.observeMany(instanceMetrics.getCounters())
            overallMetrics.observe("totalTime", totalTime)
            overallMetrics.observe("result", result)

            before = time.time()
            validCnf, someDontCare = validation.validateCnfModel(cnf, model)
            validCnfTime = time.time()-before
            overallMetrics.observe("someDontCare", someDontCare)
            overallMetrics.observe("validCnf", validCnf)
            overallMetrics.observe("validCnfTime", validCnfTime)

            before = time.time()
            validSudoku = validation.validateSudoku(model, 9)
            validSudokuTime = time.time()-before
            overallMetrics.observe("validSudoku", validSudoku)
            overallMetrics.observe("validSudokuTime", validSudokuTime)
        overallMetrics.printObservations()
    def run(self, numOfConstraints, timeout, start=0, end=1011):
        rules, numOfVars = load_dimacs('rules/sudoku_rules_9x9.txt')
        sudokus = load_sudokus('sudokus/1000_sudokus_9x9.txt')[start:end]
        data = []

        rulesDict = self.generateRules(numOfConstraints)

        for i in range(len(sudokus)):
            if i % 10 == 9:
                print(".", end='', flush=True)
            sudoku = sudokus[i]
            sudokuID = i + 1
            for name in rulesDict:
                rules = rulesDict[name]
                cnf = rules + sudoku
                instanceMetrics = InMemoryMetrics()

                before = time.time()
                solver = BasicDPLL(cnf, numOfVars,
                                   self.decisionHeuristicFactory, timeout, 5,
                                   instanceMetrics)
                result, _ = solver.solve()
                totalTime = time.time() - before
                counters = instanceMetrics.getCounters()
                if result == "TIMEOUT":
                    print("T", end='', flush=True)
                data.append((
                    sudokuID,
                    name,
                    numOfConstraints,
                    result,
                    totalTime,
                    counters.get("loop", 0),
                    counters.get("backtrack", 0),
                    counters.get("flip", 0),
                    counters.get("unit", 0),
                ))

        # Save output to csv
        header = [
            "sudokuID", "name", "numOfConstraints", "result", "totalTime",
            "loop", "backtrack", "flip", "unit"
        ]
        filename = "constraints_{}_{}_{}.csv".format(numOfConstraints, start,
                                                     end)
        save_csv(filename, header, data)
        print("")
Exemplo n.º 5
0
    def run(self, experimentName, rulesDict, timeout, start = 0, end = 1011):
        sudokus = load_sudokus('sudokus/1000_sudokus_9x9.txt')[start:end]
        data = []

        inputValues = []
        for i in range(len(sudokus)):
            sudoku = sudokus[i]
            sudokuID = start + i + 1
            inputValues.append((sudokuID, sudoku, rulesDict, timeout))

        print("cpu_count={}".format(mp.cpu_count()))
        pool = mp.Pool(mp.cpu_count())
        results = pool.starmap(processOneSudoku, inputValues)
        pool.close()
        for result in results:
            data.extend(result)

        # Save output to csv
        filename="{}_{}_{}_{}.csv".format(experimentName, start, end, time.time())
        save_csv(filename, dataHeader, data)
        print("")