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
This commit is contained in:
Jonas Hoersch 2019-02-20 19:06:48 +01:00
parent 6f1b387804
commit 9279c7f027
2 changed files with 9 additions and 13 deletions

View File

@ -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:

View File

@ -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