From f1243c3e0cc5fc1f4d6b5de03f6489f6109c9769 Mon Sep 17 00:00:00 2001 From: Max Parzen Date: Wed, 24 Nov 2021 14:16:24 +0100 Subject: [PATCH] Fix distribute clustering with cbc/glpk/ipopt Assume you have 10 nodes that need to be distributed between 2 countries. What can happen with some of the open source solvers is that one country gets assigned to 9.01 (float) nodes, and the other one to 0.99. Now using .astype(int) would lead to a node distribution of 0 and 9, as the `astype(int)` function round down by default (0.99 -> 0). This assigned zero value breaks the code in case open source solvers are used. Gurobi somehow does deal with it. --- scripts/cluster_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/cluster_network.py b/scripts/cluster_network.py index 980b73b0..1a976cd1 100644 --- a/scripts/cluster_network.py +++ b/scripts/cluster_network.py @@ -218,7 +218,7 @@ def distribute_clusters(n, n_clusters, focus_weights=None, solver_name=None): results = opt.solve(m) assert results['Solver'][0]['Status'] == 'ok', f"Solver returned non-optimally: {results}" - return pd.Series(m.n.get_values(), index=L.index).astype(int) + return pd.Series(m.n.get_values(), index=L.index).round().astype(int) def busmap_for_n_clusters(n, n_clusters, solver_name, focus_weights=None, algorithm="kmeans", **algorithm_kwds):