Skip to content

Commit

Permalink
Decorate user-facing, not internal, functions
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbowly committed Jun 7, 2024
1 parent 1e6e18d commit 86b9583
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/gurobi_optimods/mwis.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class MWISResult:
f: float


def maximum_weighted_independent_set(graph, weights, **kwargs):
@optimod()
def maximum_weighted_independent_set(graph, weights, *, create_env):
"""Find a set of mutually non-adjacent vertices with maximum weighted sum.
Parameters
Expand All @@ -57,22 +58,21 @@ def maximum_weighted_independent_set(graph, weights, **kwargs):
Returns
-------
Result
MWISResult
A data class representing the maximum weighted independent set array
and its weight.
"""
if sp.issparse(graph):
return _maximum_weighted_independent_set_scipy(graph, weights, **kwargs)
return _maximum_weighted_independent_set_scipy(graph, weights, create_env)
elif isinstance(graph, pd.DataFrame):
return _maximum_weighted_independent_set_pandas(graph, weights, **kwargs)
return _maximum_weighted_independent_set_pandas(graph, weights, create_env)
elif nx is not None and isinstance(graph, nx.Graph):
return _maximum_weighted_independent_set_networkx(graph, weights, **kwargs)
return _maximum_weighted_independent_set_networkx(graph, weights, create_env)
else:
raise ValueError(f"Unknown graph type: {type(graph)}")


@optimod()
def _maximum_weighted_independent_set_scipy(adjacency_matrix, weights, *, create_env):
def _maximum_weighted_independent_set_scipy(adjacency_matrix, weights, create_env):
"""This implementation uses the gurobipy matrix friendly APIs which are well
suited for the input data in scipy data structures."""
with create_env() as env, gp.Model("mwis", env=env) as model:
Expand Down Expand Up @@ -103,8 +103,7 @@ def _maximum_weighted_independent_set_scipy(adjacency_matrix, weights, *, create
return MWISResult(mwis, sum(weights[mwis]))


@optimod()
def _maximum_weighted_independent_set_pandas(frame, weights, *, create_env):
def _maximum_weighted_independent_set_pandas(frame, weights, create_env):
"""This implementation uses the gurobipy-pandas APIs which are well
suited for the input data in pandas dataframes structures."""
with create_env() as env, gp.Model("mwis", env=env) as model:
Expand All @@ -128,8 +127,7 @@ def _maximum_weighted_independent_set_pandas(frame, weights, *, create_env):
return MWISResult(mwis, weights["weights"].iloc[mwis].sum())


@optimod()
def _maximum_weighted_independent_set_networkx(graph, weights, *, create_env):
def _maximum_weighted_independent_set_networkx(graph, weights, create_env):
"""This implementation uses the gurobipy term-based APIs which are well
suited for the input data in networkx data structures."""
with create_env() as env, gp.Model("mwis", env=env) as model:
Expand All @@ -151,7 +149,8 @@ def _maximum_weighted_independent_set_networkx(graph, weights, *, create_env):
return MWISResult(mwis, sum(weights[mwis]))


def maximum_weighted_clique(graph, weights, **kwargs):
@optimod()
def maximum_weighted_clique(graph, weights, *, create_env):
"""Find a set of fully connected vertices with maximum weighted sum.
Parameters
Expand All @@ -170,15 +169,15 @@ def maximum_weighted_clique(graph, weights, **kwargs):
Returns
-------
Result
MWISResult
A data class representing the maximum weighted clique array
and its weight.
"""
if sp.issparse(graph):
num_vertices, _ = graph.shape
complement_matrix = sp.triu(np.ones((num_vertices, num_vertices)), k=1) - graph
return _maximum_weighted_independent_set_scipy(
complement_matrix, weights, **kwargs
complement_matrix, weights, create_env
)
elif isinstance(graph, pd.DataFrame):
num_vertices = len(weights)
Expand All @@ -190,11 +189,11 @@ def maximum_weighted_clique(graph, weights, **kwargs):
)
complement_frame = pd.DataFrame(data, columns=["node1", "node2"])
return _maximum_weighted_independent_set_pandas(
complement_frame, weights, **kwargs
complement_frame, weights, create_env
)
elif nx is not None and isinstance(graph, nx.Graph):
return _maximum_weighted_independent_set_networkx(
nx.complement(graph), weights, **kwargs
nx.complement(graph), weights, create_env
)
else:
raise ValueError(f"Unknown graph type: {type(graph)}")

0 comments on commit 86b9583

Please sign in to comment.