Skip to content

Commit

Permalink
Fixing tables
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickOHara committed Jan 8, 2024
1 parent 9936ec7 commit 545ab9b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions pctsp/apps/dataset_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def get_graph_stats(graph: nx.Graph, root_vertex: int) -> Dict[str, float]:
biggest_vertex = u
instance_stats["biggest_disjoint_prize"] = biggest_prize
instance_stats["disjoint_prize_ratio"] = float(biggest_prize) / float(og_prize)
instance_stats["max_disjoint_paths_cost"] = max(vertex_disjoint_cost_map(tree, biggest_vertex).values())

# preprocessing
graph = remove_one_connected_components(graph, root_vertex)
Expand Down
11 changes: 8 additions & 3 deletions pctsp/apps/plot_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ..vial import DatasetName, ShortAlgorithmName
from .options import LabDirOption
from .tables_app import get_heuristics_df
from ..utils import get_pctsp_logger

plot_app = typer.Typer(name="plot", help="Plotting results")

Expand All @@ -29,8 +30,11 @@ def plot_heuristics_figure(
lab_dir: Path = LabDirOption,
) -> None:
"""Plot a figure showing the performance of heuristics on a dataset"""
logger = get_pctsp_logger("plot-heuristics")
figures_dir.mkdir(exist_ok=True, parents=False)
logger.info("Reading heuristics results from %s.", lab_dir)
tspwplib_df = get_heuristics_df(DatasetName.tspwplib, lab_dir)
logger.info("TSPLIB heuristics dataframe has %s rows.", len(tspwplib_df))
londonaq_df = get_heuristics_df(DatasetName.londonaq, lab_dir)

# give short names to algorithms
Expand Down Expand Up @@ -86,6 +90,7 @@ def plot_heuristics_figure(
kappa_df = kappa_df.iloc[
kappa_df.index.get_level_values("cost_function") == cost_function
]
logger.info("Plotting %s points for cost function %s and kappa %s.", len(kappa_df), cost_function.value, kappa)
for algorithm in [
ShortAlgorithmName.bfs_extension_collapse,
ShortAlgorithmName.bfs_path_extension_collapse,
Expand Down Expand Up @@ -113,9 +118,9 @@ def plot_heuristics_figure(
"x": 1,
},
)
bottom_fig.write_image(
str(figures_dir / f"{DatasetName.tspwplib}_{cost_function}_heuristics.pdf")
)
figure_path = figures_dir / f"{DatasetName.tspwplib}_{cost_function}_heuristics.pdf"
logger.info("Writing figure for cost function %s to %s", cost_function.value, figure_path)
bottom_fig.write_image(str(figure_path))


def add_traces_heuristic(
Expand Down
16 changes: 11 additions & 5 deletions pctsp/apps/tables_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ def get_heuristics_df(
heuristic_df["gap"] = (
heuristic_df["objective"] - heuristic_df["lower_bound"]
) / heuristic_df["lower_bound"]
heuristic_df["is_optimal"] = heuristic_df["gap"] == 0.0
return heuristic_df


Expand All @@ -419,18 +420,23 @@ def heuristics_table(
tables_dir: Path,
lab_dir: Path = LabDirOption,
exact_experiment_name: ExperimentName = ExperimentName.cost_cover,
sbl_ec_only: bool = False,
) -> None:
"""Write a table of heuristic performance to a LaTeX file"""
logger = get_pctsp_logger("heuristics-table")
heuristic_df = get_heuristics_df(dataset, lab_dir, exact_experiment_name=exact_experiment_name, heuristic_experiment_name=experiment_name)

# NOTE remove SBL-EC heuristic!
heuristic_df = heuristic_df.loc[heuristic_df["algorithm"] != AlgorithmName.suurballes_extension_collapse]

if sbl_ec_only:
heuristic_df = heuristic_df.loc[heuristic_df["algorithm"] == AlgorithmName.suurballes_extension_collapse]
table_tex_filepath = tables_dir / f"{dataset.value}_{experiment_name.value}_{ShortAlgorithmName.suurballes_extension_collapse.value}.tex"
else:
heuristic_df = heuristic_df.loc[heuristic_df["algorithm"] != AlgorithmName.suurballes_extension_collapse]
table_tex_filepath = tables_dir / f"{dataset.value}_{experiment_name.value}.tex"
heuristic_df = heuristic_df[
heuristic_df.index.get_level_values("graph_name") != LondonaqGraphName.laqtinyA
]
table_tex_filepath = tables_dir / f"{dataset.value}_{experiment_name.value}.tex"

if dataset == DatasetName.tspwplib:
cols = ["cost_function", "kappa", "algorithm"]
heuristic_df = heuristic_df[
Expand All @@ -446,7 +452,7 @@ def heuristics_table(
raise ValueError(f"Dataset {dataset} not recognized.")
heuristic_gb = heuristic_df.groupby(cols)
summary_df = heuristic_gb.agg(
# num_optimal_solutions=("gap", lambda x: x==0.0),
num_optimal_solutions=("is_optimal", sum),
num_feasible_solutions=("feasible", sum),
mean_gap=("gap", np.mean),
mean_duration=("duration", np.mean),
Expand Down Expand Up @@ -483,7 +489,7 @@ def heuristics_table(
)
print(table_str)
logger.info("Writing table to LaTeX file: %s", table_tex_filepath)
table_tex_filepath.write_text(table_str, encoding="utf-8")
# table_tex_filepath.write_text(table_str, encoding="utf-8")


@tables_app.command(name="all")
Expand Down

0 comments on commit 545ab9b

Please sign in to comment.