Skip to content

Commit 589ada0

Browse files
Better fix_variable interface
1 parent e04175f commit 589ada0

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

examples/cycles_demo.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def test_min_flow_decomp(filename: str):
2727
subset_constraints=graph.graph["constraints"], # try with and without
2828
optimization_options={
2929
"optimize_with_safe_sequences": True, # set to false to deactivate the safe sequences optimization
30-
"optimize_with_safe_sequences_allow_geq_constraints": False,
30+
"optimize_with_safe_sequences_allow_geq_constraints": True,
31+
"optimize_with_safe_sequences_fix_via_bounds": False,
3132
},
3233
solver_options={
3334
"external_solver": "gurobi", # we can try also "highs" at some point
@@ -148,10 +149,10 @@ def process_solution(model):
148149
print("avg_size_of_non_trivial_SCC:", solve_statistics['avg_size_of_non_trivial_SCC']) # size = number of edges
149150

150151
def main():
151-
test_min_flow_decomp(filename = "tests/cyclic_graphs/gt3.kmer15.(130000.132000).V23.E32.cyc100.graph")
152-
# test_min_flow_decomp(filename = "tests/cyclic_graphs/gt5.kmer27.(655000.660000).V18.E27.mincyc4.e0.75.graph")
153-
test_least_abs_errors(filename = "tests/cyclic_graphs/gt5.kmer27.(655000.660000).V18.E27.mincyc4.e0.75.graph")
154-
test_min_path_error(filename = "tests/cyclic_graphs/gt5.kmer27.(655000.660000).V18.E27.mincyc4.e0.75.graph")
152+
# test_min_flow_decomp(filename = "tests/cyclic_graphs/gt3.kmer15.(130000.132000).V23.E32.cyc100.graph")
153+
test_min_flow_decomp(filename = "tests/cyclic_graphs/gt5.kmer27.(1300000.1400000).V809.E1091.mincyc1000.graph")
154+
# test_least_abs_errors(filename = "tests/cyclic_graphs/gt5.kmer27.(655000.660000).V18.E27.mincyc4.e0.75.graph")
155+
# test_min_path_error(filename = "tests/cyclic_graphs/gt5.kmer27.(655000.660000).V18.E27.mincyc4.e0.75.graph")
155156

156157
if __name__ == "__main__":
157158
# Configure logging

flowpaths/abstractwalkmodeldigraph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def _apply_safety_optimizations(self):
379379
try:
380380
if not self.optimize_with_safe_sequences_fix_via_bounds:
381381
raise Exception("throw")
382-
self.solver.fix_variable(self.edge_vars[(u, v, i)], 1)
382+
self.solver.fix_variable(self.edge_vars[(u, v, i)], int(1))
383383
except Exception:
384384
# Fallback: keep old behaviour if fixing fails for some backend edge case
385385
self.solver.add_constraint(

flowpaths/utils/solverwrapper.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ def fix_variable(self, var, value: Union[int, float]):
733733
value : int | float
734734
The value to which the variable should be fixed.
735735
"""
736+
736737
# Normalize to float for solvers expecting floating bounds
737738
value = float(value)
738739
if self.external_solver == "gurobi":
@@ -746,20 +747,11 @@ def fix_variable(self, var, value: Union[int, float]):
746747
elif self.external_solver == "highs":
747748
# HiGHS: change column bounds using internal index of variable
748749
try:
749-
# Attempt a few common attribute names to locate the column index
750-
idx = None
751-
for cand in ["index", "col", "idx"]:
752-
if hasattr(var, cand):
753-
idx = getattr(var, cand)
754-
break
755-
if idx is None:
756-
raise AttributeError("Variable has no index attribute among ['index','col','idx']")
757-
# Ensure numpy imported (already imported as np at top)
758750
self.solver.changeColsBounds(
759751
1,
760-
np.array([idx], dtype=np.int32),
761-
np.array([value], dtype=np.float64 if isinstance(value, float) else np.int32),
762-
np.array([value], dtype=np.float64 if isinstance(value, float) else np.int32),
752+
np.array([var.index], dtype=np.int32),
753+
np.array([value], dtype=np.float64 if isinstance(value, float) else np.int64),
754+
np.array([value], dtype=np.float64 if isinstance(value, float) else np.int64),
763755
)
764756
except Exception as e:
765757
utils.logger.error(f"{__name__}: Could not fix highs variable: {e}")

0 commit comments

Comments
 (0)