From 0eb90d4846971d9b6139875340f6081429e9e862 Mon Sep 17 00:00:00 2001 From: Willy Mroczowski Date: Mon, 8 Apr 2024 09:39:08 +0200 Subject: [PATCH] fix(core): catch missing reduced costs and shadow prices. (#1383) * fix(core): catch missing reduced costs and shadow prices. Fixes #1381 and #1372 * Added fix to next release --- release-notes/next-release.md | 1 + src/cobra/core/solution.py | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/release-notes/next-release.md b/release-notes/next-release.md index 843063be5..36ef2f5a0 100644 --- a/release-notes/next-release.md +++ b/release-notes/next-release.md @@ -5,6 +5,7 @@ ## Fixes * Fixes the incorrect bounds in the CycleFree loop removal. +* Fixes reduced costs and shadow prices not available when using non-convex models. ## Other diff --git a/src/cobra/core/solution.py b/src/cobra/core/solution.py index a23d86db8..6f6689078 100644 --- a/src/cobra/core/solution.py +++ b/src/cobra/core/solution.py @@ -174,7 +174,15 @@ def get_solution( reduced = np.empty(len(reactions)) var_primals = model.solver.primal_values shadow = np.empty(len(metabolites)) - if model.solver.is_integer: + + try: + var_duals = model.solver.reduced_costs + constr_duals = model.solver.shadow_prices + duals_available = True + except Exception: + duals_available = False + + if model.solver.is_integer or not duals_available: reduced.fill(np.nan) shadow.fill(np.nan) for i, rxn in enumerate(reactions):