Untitled

 avatar
unknown
plain_text
a year ago
1.7 kB
6
Indexable
import time

def compare_algorithms(test_cases):
    results = []

    for kb, alpha in test_cases:
        test_result = {"KB": kb, "alpha": alpha}

        # PL-Resolution
        start_time = time.time()
        pl_result = pl_resolution(kb, alpha)
        end_time = time.time()
        test_result["PL-Resolution"] = {"result": pl_result, "time": end_time - start_time}

        # DPLL
        start_time = time.time()
        dpll_result, _ = dpll(kb)
        end_time = time.time()
        test_result["DPLL"] = {"result": dpll_result, "time": end_time - start_time}

        # WalkSAT
        start_time = time.time()
        walksat_result, _ = walksat(kb, 1000)  # Sử dụng 1000 flips cho WalkSAT
        end_time = time.time()
        test_result["WalkSAT"] = {"result": walksat_result, "time": end_time - start_time}

        results.append(test_result)

    return results

# Ví dụ sử dụng với các testcase mẫu
test_cases = [
    ([
        ['p', '~q'],
        ['q', 'r'],
        ['~r', 's'],
        ['~p', '~s']
    ], 'r'),
    ([
        ['a', '~b', 'c'],
        ['~a', 'b'],
        ['~b', '~c'],
        ['b', 'c']
    ], 'a'),
    ([
        ['x', '~y'],
        ['~x', 'z'],
        ['y', 'w'],
        ['~z', '~w']
    ], 'x'),
    ([
        ['m', 'n'],
        ['~m', 'o'],
        ['~n', 'p'],
        ['~o', '~p']
    ], 'm')
]

results = compare_algorithms(test_cases)

for result in results:
    print(f"Test case: KB={result['KB']}, alpha={result['alpha']}")
    for algo in ['PL-Resolution', 'DPLL', 'WalkSAT']:
        res = result[algo]['result']
        time_taken = result[algo]['time']
        print(f"{algo}: {'Yes' if res else 'No'}, Time taken: {time_taken:.6f} seconds")
    print()
Editor is loading...
Leave a Comment