diff --git a/src/gurobi_logtools/parsers/continuous.py b/src/gurobi_logtools/parsers/continuous.py index 774939c..d70438b 100644 --- a/src/gurobi_logtools/parsers/continuous.py +++ b/src/gurobi_logtools/parsers/continuous.py @@ -1,7 +1,7 @@ import re from gurobi_logtools.parsers.barrier import BarrierParser -from gurobi_logtools.parsers.pretree_solutions import PretreeSolutionParser +from gurobi_logtools.parsers.pretree_solutions import PreTreeSolutionParser from gurobi_logtools.parsers.simplex import SimplexParser from gurobi_logtools.parsers.util import typeconvert_groupdict @@ -25,7 +25,7 @@ class ContinuousParser: re.compile(r"(?POptimal objective\s+(?P.*))$"), ] - def __init__(self, pre_tree_solution_parser: PretreeSolutionParser): + def __init__(self, pretree_solution_parser: PreTreeSolutionParser): """Initialize the Continuous parser.""" self._barrier_parser = BarrierParser() self._simplex_parser = SimplexParser() @@ -34,7 +34,7 @@ def __init__(self, pre_tree_solution_parser: PretreeSolutionParser): self._current_pattern = None - self._pre_tree_solution_parser = pre_tree_solution_parser + self._pretree_solution_parser = pretree_solution_parser def parse(self, line: str) -> bool: """Parse the given log line to populate summary and progress data. @@ -48,7 +48,7 @@ def parse(self, line: str) -> bool: bool: Return True if the given line is matched by some pattern. """ - if self._pre_tree_solution_parser.parse(line): + if self._pretree_solution_parser.parse(line): return True mip_relaxation_match = ContinuousParser.mip_relaxation_pattern.match(line) diff --git a/src/gurobi_logtools/parsers/presolve.py b/src/gurobi_logtools/parsers/presolve.py index 75525a4..452d116 100644 --- a/src/gurobi_logtools/parsers/presolve.py +++ b/src/gurobi_logtools/parsers/presolve.py @@ -1,6 +1,6 @@ import re -from gurobi_logtools.parsers.pretree_solutions import PretreeSolutionParser +from gurobi_logtools.parsers.pretree_solutions import PreTreeSolutionParser from gurobi_logtools.parsers.util import typeconvert_groupdict @@ -67,7 +67,7 @@ class PresolveParser: # Special case: model solved by presolve presolve_all_removed = re.compile(r"Presolve: All rows and columns removed") - def __init__(self, pre_tree_solution_parser: PretreeSolutionParser): + def __init__(self, pretree_solution_parser: PreTreeSolutionParser): """Initialize the Presolve parser. The PresolveParser extends beyond the lines associated with the presolved @@ -76,7 +76,7 @@ def __init__(self, pre_tree_solution_parser: PretreeSolutionParser): """ self._summary = {} self._started = False - self._pre_tree_solution_parser = pre_tree_solution_parser + self._pretree_solution_parser = pretree_solution_parser def parse(self, line: str) -> bool: """Parse the given log line to populate summary data. @@ -96,7 +96,7 @@ def parse(self, line: str) -> bool: return True return False - if self._pre_tree_solution_parser.parse(line): + if self._pretree_solution_parser.parse(line): return True for pattern in PresolveParser.presolve_intermediate_patterns: diff --git a/src/gurobi_logtools/parsers/pretree_solutions.py b/src/gurobi_logtools/parsers/pretree_solutions.py index 34a5a7b..67bedae 100644 --- a/src/gurobi_logtools/parsers/pretree_solutions.py +++ b/src/gurobi_logtools/parsers/pretree_solutions.py @@ -3,7 +3,7 @@ from gurobi_logtools.parsers.util import typeconvert_groupdict -class PretreeSolutionParser: +class PreTreeSolutionParser: pretree_solution_regex = re.compile( r"Found heuristic solution:\sobjective\s(?P[^\s]+)" ) diff --git a/src/gurobi_logtools/parsers/single_log.py b/src/gurobi_logtools/parsers/single_log.py index da1c5c2..14a13e3 100644 --- a/src/gurobi_logtools/parsers/single_log.py +++ b/src/gurobi_logtools/parsers/single_log.py @@ -5,7 +5,7 @@ from gurobi_logtools.parsers.nodelog import NodeLogParser from gurobi_logtools.parsers.norel import NoRelParser from gurobi_logtools.parsers.presolve import PresolveParser -from gurobi_logtools.parsers.pretree_solutions import PretreeSolutionParser +from gurobi_logtools.parsers.pretree_solutions import PreTreeSolutionParser from gurobi_logtools.parsers.termination import TerminationParser from gurobi_logtools.parsers.util import model_type @@ -16,16 +16,21 @@ class SingleLogParser: It expects parse to be called once for each line in a log file. """ + _NoRelParser = NoRelParser + _NodeLogParser = NodeLogParser + _TerminationParser = TerminationParser + _PreTreeSolutionParser = PreTreeSolutionParser + def __init__(self, write_to_dir=None): - self.pretree_solution_parser = self.make_pretree_solution_parser() + self.pretree_solution_parser = self._PreTreeSolutionParser() # Parsers in sequence self.header_parser = HeaderParser() self.presolve_parser = PresolveParser(self.pretree_solution_parser) - self.norel_parser = NoRelParser() + self.norel_parser = self._NoRelParser() self.continuous_parser = ContinuousParser(self.pretree_solution_parser) - self.nodelog_parser = NodeLogParser() - self.termination_parser = TerminationParser() + self.nodelog_parser = self._NodeLogParser() + self.termination_parser = self._TerminationParser() # State self.started = False @@ -42,9 +47,6 @@ def __init__(self, write_to_dir=None): self.write_to_dir = pathlib.Path(write_to_dir) if write_to_dir else None self.lines = [] if self.write_to_dir else None - def make_pretree_solution_parser(self): - return PretreeSolutionParser() - def close(self): if self.write_to_dir: paramstr = "-".join( diff --git a/tests/parsers/test_continuous.py b/tests/parsers/test_continuous.py index b1e0145..b262489 100644 --- a/tests/parsers/test_continuous.py +++ b/tests/parsers/test_continuous.py @@ -1,7 +1,7 @@ from unittest import TestCase, main from gurobi_logtools.parsers.continuous import ContinuousParser -from gurobi_logtools.parsers.pretree_solutions import PretreeSolutionParser +from gurobi_logtools.parsers.pretree_solutions import PreTreeSolutionParser from gurobi_logtools.parsers.util import parse_block example_log_barrier_with_simplex = """ @@ -129,7 +129,7 @@ class TestContinuous(TestCase): def test_last_progress_entry_barrier_with_simplex(self): - continuous_parser = ContinuousParser(PretreeSolutionParser()) + continuous_parser = ContinuousParser(PreTreeSolutionParser()) parse_block(continuous_parser, example_log_barrier_with_simplex) self.assertEqual( continuous_parser.get_progress()[-1], expected_progress_last_entry @@ -154,7 +154,7 @@ def test_get_summary_progress(self): ], ): with self.subTest(example_log=example_log): - continuous_parser = ContinuousParser(PretreeSolutionParser()) + continuous_parser = ContinuousParser(PreTreeSolutionParser()) parse_block(continuous_parser, example_log) self.assertEqual(continuous_parser.get_summary(), expected_summary) self.assertEqual(continuous_parser.get_progress(), expected_progress) diff --git a/tests/parsers/test_presolve.py b/tests/parsers/test_presolve.py index d45c14e..8bbf75a 100644 --- a/tests/parsers/test_presolve.py +++ b/tests/parsers/test_presolve.py @@ -1,7 +1,7 @@ from unittest import TestCase, main from gurobi_logtools.parsers.presolve import PresolveParser -from gurobi_logtools.parsers.pretree_solutions import PretreeSolutionParser +from gurobi_logtools.parsers.pretree_solutions import PreTreeSolutionParser from gurobi_logtools.parsers.util import parse_lines example_log_0 = """ @@ -140,7 +140,7 @@ def test_first_line_matched(self): for i, example_log in enumerate([example_log_0, example_log_1, example_log_2]): with self.subTest(example_log=example_log): - presolve_parser = PresolveParser(PretreeSolutionParser()) + presolve_parser = PresolveParser(PreTreeSolutionParser()) for line in example_log.strip().split("\n"): if presolve_parser.parse(line): self.assertEqual(line, expected_start_lines[i]) @@ -156,7 +156,7 @@ def test_get_summary(self): ] for i, example_log in enumerate([example_log_0, example_log_1, example_log_2]): with self.subTest(example_log=example_log): - presolve_parser = PresolveParser(PretreeSolutionParser()) + presolve_parser = PresolveParser(PreTreeSolutionParser()) lines = example_log.strip().split("\n") parse_lines(presolve_parser, lines) self.assertEqual(presolve_parser.get_summary(), expected_summaries[i]) diff --git a/tests/parsers/test_pretreesols.py b/tests/parsers/test_pretreesols.py index 1b8c781..a85ae39 100644 --- a/tests/parsers/test_pretreesols.py +++ b/tests/parsers/test_pretreesols.py @@ -2,7 +2,7 @@ from gurobi_logtools.parsers.continuous import ContinuousParser from gurobi_logtools.parsers.presolve import PresolveParser -from gurobi_logtools.parsers.pretree_solutions import PretreeSolutionParser +from gurobi_logtools.parsers.pretree_solutions import PreTreeSolutionParser from gurobi_logtools.parsers.util import parse_lines example_log_0 = """ @@ -74,21 +74,21 @@ def setUp(self): def test_get_summary_presolve(self): for example_log, Parser in zip(example_logs, parsers): with self.subTest(example_log=example_log): - pre_tree_sols_parser = PretreeSolutionParser() - presolve_parser = Parser(pre_tree_sols_parser) + pretree_sols_parser = PreTreeSolutionParser() + presolve_parser = Parser(pretree_sols_parser) lines = example_log.strip().split("\n") parse_lines(presolve_parser, lines) - self.assertEqual(pre_tree_sols_parser.get_summary(), expected_summary) + self.assertEqual(pretree_sols_parser.get_summary(), expected_summary) def test_get_progress_presolve(self): for example_log, Parser in zip(example_logs, parsers): with self.subTest(example_log=example_log): - pre_tree_sols_parser = PretreeSolutionParser() - presolve_parser = Parser(pre_tree_sols_parser) + pretree_sols_parser = PreTreeSolutionParser() + presolve_parser = Parser(pretree_sols_parser) lines = example_log.strip().split("\n") parse_lines(presolve_parser, lines) - pre_tree_sols_parser.get_progress() - self.assertEqual(pre_tree_sols_parser.get_progress(), expected_progress) + pretree_sols_parser.get_progress() + self.assertEqual(pretree_sols_parser.get_progress(), expected_progress) if __name__ == "__main__":