From 9279c7f0276504bb6fe3ec788d96e97b0722909e Mon Sep 17 00:00:00 2001 From: Jonas Hoersch Date: Wed, 20 Feb 2019 19:06:48 +0100 Subject: [PATCH] solve_network: Remove hard-coded solver fallback in `busmap_for_n_clusters` Necessary for when `clustering_for_n_clusters` is used from the simplify_network rule. Fixes #12 --- scripts/cluster_network.py | 15 ++++++--------- scripts/simplify_network.py | 7 +++---- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/scripts/cluster_network.py b/scripts/cluster_network.py index e04cbe6d..7fb339fc 100644 --- a/scripts/cluster_network.py +++ b/scripts/cluster_network.py @@ -93,7 +93,7 @@ def distribute_clusters(n, n_clusters, solver_name=None): return pd.Series(m.n.get_values(), index=L.index).astype(int) -def busmap_for_n_clusters(n, n_clusters, algorithm="kmeans", **algorithm_kwds): +def busmap_for_n_clusters(n, n_clusters, solver_name, algorithm="kmeans", **algorithm_kwds): if algorithm == "kmeans": algorithm_kwds.setdefault('n_init', 1000) algorithm_kwds.setdefault('max_iter', 30000) @@ -101,11 +101,6 @@ def busmap_for_n_clusters(n, n_clusters, algorithm="kmeans", **algorithm_kwds): n.determine_network_topology() - if 'snakemake' in globals(): - solver_name = snakemake.config['solving']['solver']['name'] - else: - solver_name = "gurobi" - n_clusters = distribute_clusters(n, n_clusters, solver_name=solver_name) def reduce_network(n, buses): @@ -140,7 +135,8 @@ def plot_busmap_for_n_clusters(n, n_clusters=50): del cs, cr def clustering_for_n_clusters(n, n_clusters, aggregate_carriers=None, - line_length_factor=1.25, potential_mode='simple', algorithm="kmeans"): + line_length_factor=1.25, potential_mode='simple', + solver_name="cbc", algorithm="kmeans"): if potential_mode == 'simple': p_nom_max_strategy = np.sum @@ -151,7 +147,7 @@ def clustering_for_n_clusters(n, n_clusters, aggregate_carriers=None, "but is '{}'".format(potential_mode)) clustering = get_clustering_from_busmap( - n, busmap_for_n_clusters(n, n_clusters, algorithm), + n, busmap_for_n_clusters(n, n_clusters, solver_name, algorithm), bus_strategies=dict(country=_make_consense("Bus", "country")), aggregate_generators_weighted=True, aggregate_generators_carriers=aggregate_carriers, @@ -233,7 +229,8 @@ if __name__ == "__main__": for tech in renewable_carriers])) clustering = clustering_for_n_clusters(n, n_clusters, aggregate_carriers, line_length_factor=line_length_factor, - potential_mode=potential_mode) + potential_mode=potential_mode, + solver_name=snakemake.config['solving']['solver']['name']) clustering.network.export_to_netcdf(snakemake.output.network) with pd.HDFStore(snakemake.output.clustermaps, mode='w') as store: diff --git a/scripts/simplify_network.py b/scripts/simplify_network.py index ba8f9bac..e419989a 100644 --- a/scripts/simplify_network.py +++ b/scripts/simplify_network.py @@ -21,9 +21,7 @@ from six.moves import reduce import pypsa from pypsa.io import import_components_from_dataframe, import_series_from_dataframe -from pypsa.networkclustering import (busmap_by_stubs, busmap_by_kmeans, - _make_consense, get_clustering_from_busmap, - aggregategenerators, aggregateoneport) +from pypsa.networkclustering import busmap_by_stubs, aggregategenerators, aggregateoneport from cluster_network import clustering_for_n_clusters, cluster_regions from add_electricity import load_costs @@ -254,7 +252,8 @@ def cluster(n, n_clusters): potential_mode = (consense(pd.Series([snakemake.config['renewable'][tech]['potential'] for tech in renewable_carriers])) if len(renewable_carriers) > 0 else 'conservative') - clustering = clustering_for_n_clusters(n, n_clusters, potential_mode=potential_mode) + clustering = clustering_for_n_clusters(n, n_clusters, potential_mode=potential_mode, + solver_name=snakemake.config['solving']['solver']['name']) return clustering.network, clustering.busmap