-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
10 changed files
with
219 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import logging | ||
from pathlib import Path | ||
|
||
|
||
class Logger: | ||
logger: logging.Logger | ||
log_folder = Path(__file__).parent / "logs" | ||
|
||
def __init__(self, clazz): | ||
if not self.log_folder.exists(): | ||
self.log_folder.mkdir() | ||
|
||
self.logger = logging.getLogger(type(clazz).__name__) | ||
self.logger.setLevel(logging.DEBUG) | ||
|
||
self.formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | ||
|
||
self.stream_handler = logging.StreamHandler() | ||
self.stream_handler.setLevel(logging.INFO) | ||
self.stream_handler.setFormatter(self.formatter) | ||
self.logger.addHandler(self.stream_handler) | ||
|
||
self.file_handler = logging.FileHandler(self.log_folder / f"{type(clazz).__name__}.log") | ||
self.file_handler.setLevel(logging.DEBUG) | ||
self.file_handler.setFormatter(self.formatter) | ||
self.logger.addHandler(self.file_handler) | ||
|
||
def debug(self, message): | ||
self.logger.debug(message) | ||
|
||
def info(self, message): | ||
self.logger.info(message) | ||
|
||
def warning(self, message): | ||
self.logger.warning(message) | ||
|
||
def error(self, message): | ||
self.logger.error(message) | ||
|
||
def critical(self, message): | ||
self.logger.critical(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,42 @@ | ||
import subprocess | ||
import logging | ||
from pathlib import Path | ||
|
||
from eternitylib.board import Board | ||
from logger import Logger | ||
from solverlib.execution import Execution | ||
|
||
|
||
class Solver: | ||
def __init__(self): | ||
# exe is in the same dir as this file | ||
self.logger = Logger(self) | ||
self.exe_path = Path(__file__).parent / "e2solver" | ||
|
||
def link_solver(self, path: Path = Path(__file__).parent.parent.parent / "cmake-build-debug/solver/Solver"): | ||
# If file does not exist, raise an error | ||
if not path.exists(): | ||
self.logger.error(f"Solver executable not found at {path}") | ||
raise FileNotFoundError(f"Solver executable not found at {path}") | ||
|
||
if self.exe_path.exists(): | ||
self.logger.debug(f"Deleting existing solver at {path}") | ||
self.exe_path.unlink() | ||
|
||
self.logger.debug(f"Symlinking {path} to {self.exe_path}.") | ||
self.exe_path.symlink_to(path) | ||
|
||
def solve(self, board: Board, timeout: int = 60) -> Execution: | ||
def solve(self, board: Board, timeout: float = 60) -> Execution: | ||
# Write the board to a file | ||
self.logger.info(f"Writing board to tmp/board.{board.hash()}.txt.") | ||
board_path = Path(__file__).parent.parent / f"tmp/board.{board.hash()}.txt" | ||
board_path.parent.mkdir(parents=True, exist_ok=True) | ||
board_path.write_text(board.to_csv()) | ||
|
||
# print info to log | ||
logging. | ||
# Run the solver | ||
try: | ||
logging.info(f"Running solver with timeout of {timeout} seconds") | ||
self.logger.info(f"Running solver with timeout of {timeout} seconds") | ||
result = subprocess.run([self.exe_path, str(board_path)], capture_output=True, timeout=timeout) | ||
logging.info("Solver finished.") | ||
self.logger.info(f"Solver returned with code {result.returncode}") | ||
except subprocess.TimeoutExpired: | ||
logging.warning(f"Solver timed out after {timeout} seconds") | ||
return Execution("") | ||
self.logger.warning(f"Solver timed out after {timeout} seconds") | ||
return Execution("", timeout=timeout) | ||
return Execution(result.stdout.decode("utf-8")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.