40 lines
1.6 KiB
Python
40 lines
1.6 KiB
Python
from typing import Any, Dict, List
|
|
from common.base_problem import BaseProblem
|
|
from problems.interval_scheduling.generator import generate_interval_scheduling_case
|
|
from problems.interval_scheduling.solvers import solve_greedy, solve_brute_force
|
|
from common.timer_utils import time_execution
|
|
|
|
class IntervalSchedulingProblem(BaseProblem):
|
|
def generate_case(self, size: int = 10, min_time: int = 0, max_time: int = 100) -> Dict[str, Any]:
|
|
intervals = generate_interval_scheduling_case(size, min_time, max_time)
|
|
return {"intervals": intervals}
|
|
|
|
def solve(self, input_data: Dict[str, Any], algorithm: str) -> Dict[str, Any]:
|
|
intervals = input_data.get("intervals", [])
|
|
# Ensure intervals are tuples
|
|
intervals = [tuple(x) for x in intervals]
|
|
|
|
result = None
|
|
duration = 0.0
|
|
|
|
if algorithm == "greedy":
|
|
result, duration = time_execution(solve_greedy)(intervals)
|
|
elif algorithm == "brute_force":
|
|
result, duration = time_execution(solve_brute_force)(intervals)
|
|
else:
|
|
raise ValueError(f"Unknown algorithm: {algorithm}")
|
|
|
|
return {
|
|
"algorithm": algorithm,
|
|
"result_count": len(result),
|
|
"result_intervals": result,
|
|
"time_seconds": duration
|
|
}
|
|
|
|
def verify(self, input_data: Any, result: Any) -> bool:
|
|
# Use Greedy as the source of truth (it is optimal for this problem)
|
|
intervals = input_data.get("intervals", [])
|
|
intervals = [tuple(x) for x in intervals]
|
|
truth, _ = time_execution(solve_greedy)(intervals)
|
|
return result.get("result_count") == len(truth)
|