diff --git a/scripts/cluster_network.py b/scripts/cluster_network.py index d4b3b139..1c55234c 100644 --- a/scripts/cluster_network.py +++ b/scripts/cluster_network.py @@ -190,9 +190,11 @@ def get_feature_for_hac(n, buses_i=None, feature=None): feature_data = pd.DataFrame(index=buses_i, columns=carriers) for carrier in carriers: try: + # without simpl wildcard (bus names are "X X"): feature_data[carrier] = (n.generators_t.p_max_pu.filter(like=carrier).mean() .rename(index=lambda x: x.split(' ')[0])) except: + # with simpl wildcard (bus names are "X X X"): feature_data[carrier] = (n.generators_t.p_max_pu.filter(like=carrier).mean() .rename(index=lambda x: x.split(' ')[0] + ' ' + x.split(' ')[1])) @@ -208,7 +210,7 @@ def get_feature_for_hac(n, buses_i=None, feature=None): feature_data = feature_data.append(n.generators_t.p_max_pu.filter(like=carrier) .rename(columns=lambda x: x.split(' ')[0] + ' ' + x.split(' ')[1]))[buses_i] feature_data = feature_data.T - feature_data.columns = feature_data.columns.astype(str) # Timestamp will raise error in sklearn>=v1.2 + feature_data.columns = feature_data.columns.astype(str) # timestamp raises error in sklearn>=v1.2 feature_data = feature_data.fillna(0) @@ -309,9 +311,9 @@ def busmap_for_n_clusters(n, n_clusters, solver_name, focus_weights=None, algori def clustering_for_n_clusters(n, n_clusters, custom_busmap=False, aggregate_carriers=None, line_length_factor=1.25, potential_mode='simple', solver_name="cbc", - algorithm="kmeans", feature=None, extended_link_costs=0, focus_weights=None): + algorithm="hac", feature=None, extended_link_costs=0, focus_weights=None): - logger.info(f"Clustering network using algorithm {algorithm} and feature {feature}...") + logger.info(f"Clustering network using algorithm ``{algorithm}`` and feature ``{feature}``...") if potential_mode == 'simple': p_nom_max_strategy = np.sum @@ -424,10 +426,13 @@ if __name__ == "__main__": custom_busmap.index = custom_busmap.index.astype(str) logger.info(f"Imported custom busmap from {snakemake.input.custom_busmap}") + cluster_config = snakemake.config.get('clustering', {}).get('cluster_network', {}) clustering = clustering_for_n_clusters(n, n_clusters, custom_busmap, aggregate_carriers, line_length_factor, potential_mode, snakemake.config['solving']['solver']['name'], - "kmeans", hvac_overhead_cost, focus_weights) + cluster_config.get("algorithm", "hac"), + cluster_config.get("feature", "solar+onwind-time"), + hvac_overhead_cost, focus_weights) update_p_nom_max(n) diff --git a/scripts/simplify_network.py b/scripts/simplify_network.py index 0e9dd385..23facc79 100644 --- a/scripts/simplify_network.py +++ b/scripts/simplify_network.py @@ -353,11 +353,10 @@ def aggregate_to_substations(n, buses_i=None): line_length_factor=1.0, generator_strategies={'p_nom_max': 'sum'}, scale_link_capital_costs=False) - return clustering.network, busmap -def cluster(n, n_clusters, config, algorithm="kmeans", feature=None): +def cluster(n, n_clusters, config, algorithm="hac", feature=None): logger.info(f"Clustering to {n_clusters} buses") focus_weights = config.get('focus_weights', None) @@ -403,7 +402,8 @@ if __name__ == "__main__": busmaps = [trafo_map, simplify_links_map, stub_map] - if snakemake.config.get('clustering', {}).get('simplify_network', {}).get('to_substations', False): + cluster_config = snakemake.config.get('clustering', {}).get('simplify_network', {}) + if cluster_config.get('to_substations', False): n, substation_map = aggregate_to_substations(n) busmaps.append(substation_map)