Skip to content

Commit

Permalink
Merge pull request #55 from venaturum/subparser_class_attributes
Browse files Browse the repository at this point in the history
Subparser class attributes
  • Loading branch information
mattmilten authored Jul 23, 2024
2 parents 76b1264 + 2f61586 commit 1be6a99
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 31 deletions.
8 changes: 4 additions & 4 deletions src/gurobi_logtools/parsers/continuous.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -25,7 +25,7 @@ class ContinuousParser:
re.compile(r"(?P<OPTIMAL>Optimal objective\s+(?P<ObjVal>.*))$"),
]

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()
Expand All @@ -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.
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/gurobi_logtools/parsers/presolve.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/gurobi_logtools/parsers/pretree_solutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<Incumbent>[^\s]+)"
)
Expand Down
18 changes: 10 additions & 8 deletions src/gurobi_logtools/parsers/single_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions tests/parsers/test_continuous.py
Original file line number Diff line number Diff line change
@@ -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 = """
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions tests/parsers/test_presolve.py
Original file line number Diff line number Diff line change
@@ -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 = """
Expand Down Expand Up @@ -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])
Expand All @@ -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])
Expand Down
16 changes: 8 additions & 8 deletions tests/parsers/test_pretreesols.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand Down Expand Up @@ -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__":
Expand Down

0 comments on commit 1be6a99

Please sign in to comment.