Skip to content

Commit

Permalink
Merge EdgeSelected constraint into resp. variable
Browse files Browse the repository at this point in the history
  • Loading branch information
funkey committed Jul 24, 2024
1 parent e033bdb commit 951efe7
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 62 deletions.
8 changes: 0 additions & 8 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,3 @@ Pin
^^^
.. autoclass:: Pin
:show-inheritance:

SelectEdgeNodes (internal use)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autoclass:: SelectEdgeNodes
:show-inheritance:



2 changes: 0 additions & 2 deletions motile/constraints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from .max_children import MaxChildren
from .max_parents import MaxParents
from .pin import Pin
from .select_edge_nodes import SelectEdgeNodes

__all__ = [
"Constraint",
Expand All @@ -13,5 +12,4 @@
"MaxChildren",
"MaxParents",
"Pin",
"SelectEdgeNodes",
]
39 changes: 0 additions & 39 deletions motile/constraints/select_edge_nodes.py

This file was deleted.

13 changes: 1 addition & 12 deletions motile/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import ilpy
import numpy as np

from .constraints import SelectEdgeNodes
from .constraints.constraint import Constraint
from .costs import Features, Weight, Weights
from .ssvm import fit_weights
Expand All @@ -28,16 +27,9 @@ class Solver:
Args:
track_graph:
The :class:`~motile.TrackGraph` of objects to track over time.
skip_core_constraints (:obj:`bool`, default=False):
If true, add no constraints to the solver at all. Otherwise, core
constraints that ensure consistencies between selected nodes and
edges are added.
"""

def __init__(
self, track_graph: TrackGraph, skip_core_constraints: bool = False
) -> None:
def __init__(self, track_graph: TrackGraph) -> None:
if not isinstance(track_graph, TrackGraph):
import networkx as nx

Expand Down Expand Up @@ -70,9 +62,6 @@ def __init__(
self._cost_instances: dict[str, Cost] = {}
self.solution: ilpy.Solution | None = None

if not skip_core_constraints:
self.add_constraint(SelectEdgeNodes())

def add_cost(self, cost: Cost, name: str | None = None) -> None:
"""Add linear cost to the value of variables in this solver.
Expand Down
15 changes: 14 additions & 1 deletion motile/variables/edge_selected.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Collection
from typing import TYPE_CHECKING, Collection, Iterable

from .node_selected import NodeSelected
from .variable import Variable

if TYPE_CHECKING:
import ilpy

from motile._types import EdgeId
from motile.solver import Solver

Expand All @@ -15,3 +18,13 @@ class EdgeSelected(Variable["EdgeId"]):
@staticmethod
def instantiate(solver: Solver) -> Collection[EdgeId]:
return solver.graph.edges

@staticmethod
def instantiate_constraints(solver: Solver) -> Iterable[ilpy.Expression]:
node_indicators = solver.get_variables(NodeSelected)
edge_indicators = solver.get_variables(EdgeSelected)

for edge in solver.graph.edges:
nodes = list(solver.graph.nodes_of(edge))
x_e = edge_indicators[edge]
yield len(nodes) * x_e - sum(node_indicators[n] for n in nodes) <= 0

0 comments on commit 951efe7

Please sign in to comment.