def clustering_for_n_clusters(n, n_clusters, aggregate_carriers=None, line_length_factor=1.25, potential_mode='simple', solver_name="cbc", algorithm="kmeans", extended_link_costs=0, focus_weights=None): if potential_mode == 'simple': p_nom_max_strategy = np.sum elif potential_mode == 'conservative': p_nom_max_strategy = np.min else: raise AttributeError("potential_mode should be one of 'simple' or 'conservative', " "but is '{}'".format(potential_mode)) clustering = get_clustering_from_busmap( n, busmap_for_n_clusters(n, n_clusters, solver_name, focus_weights, algorithm), bus_strategies=dict(country=_make_consense("Bus", "country")), aggregate_generators_weighted=True, aggregate_generators_carriers=aggregate_carriers, aggregate_one_ports=["Load", "StorageUnit"], line_length_factor=line_length_factor, generator_strategies={'p_nom_max': p_nom_max_strategy}, scale_link_capital_costs=False) nc = clustering.network nc.links['underwater_fraction'] = (n.links.eval('underwater_fraction * length') .div(nc.links.length).dropna()) nc.links['capital_cost'] = (nc.links['capital_cost'] .add((nc.links.length - n.links.length) .clip(lower=0).mul(extended_link_costs), fill_value=0)) return clustering
def clustering_for_n_clusters(n, n_clusters, aggregate_carriers=None, line_length_factor=1.25, potential_mode='simple', solver_name="cbc", algorithm="kmeans"): if potential_mode == 'simple': p_nom_max_strategy = np.sum elif potential_mode == 'conservative': p_nom_max_strategy = np.min else: raise AttributeError( "potential_mode should be one of 'simple' or 'conservative', " "but is '{}'".format(potential_mode)) clustering = get_clustering_from_busmap( 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, aggregate_one_ports=["Load", "StorageUnit"], line_length_factor=line_length_factor, generator_strategies={'p_nom_max': p_nom_max_strategy}) return clustering
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", extended_link_costs=0, focus_weights=None): if potential_mode == 'simple': p_nom_max_strategy = np.sum elif potential_mode == 'conservative': p_nom_max_strategy = np.min else: raise AttributeError( f"potential_mode should be one of 'simple' or 'conservative' but is '{potential_mode}'" ) if custom_busmap: busmap = pd.read_csv(snakemake.input.custom_busmap, index_col=0, squeeze=True) busmap.index = busmap.index.astype(str) logger.info( f"Imported custom busmap from {snakemake.input.custom_busmap}") else: busmap = busmap_for_n_clusters(n, n_clusters, solver_name, focus_weights, algorithm) clustering = get_clustering_from_busmap( n, busmap, bus_strategies=dict(country=_make_consense("Bus", "country")), aggregate_generators_weighted=True, aggregate_generators_carriers=aggregate_carriers, aggregate_one_ports=["Load", "StorageUnit"], line_length_factor=line_length_factor, generator_strategies={'p_nom_max': p_nom_max_strategy}, scale_link_capital_costs=False) if not n.links.empty: nc = clustering.network nc.links['underwater_fraction'] = ( n.links.eval('underwater_fraction * length').div( nc.links.length).dropna()) nc.links['capital_cost'] = (nc.links['capital_cost'].add( (nc.links.length - n.links.length).clip(lower=0).mul(extended_link_costs), fill_value=0)) return clustering
def clustering_for_n_clusters(n, n_clusters, aggregate_renewables=True, line_length_factor=1.25): aggregate_generators_carriers = (None if aggregate_renewables else ( pd.Index(n.generators.carrier.unique()).difference( ['onwind', 'offwind', 'solar']))) clustering = get_clustering_from_busmap( n, busmap_for_n_clusters(n, n_clusters), bus_strategies=dict(country=_make_consense("Bus", "country")), aggregate_generators_weighted=True, aggregate_generators_carriers=aggregate_generators_carriers, aggregate_one_ports=["Load", "StorageUnit"], line_length_factor=line_length_factor) return clustering
def aggregate_to_substations(n, buses_i=None): # can be used to aggregate a selection of buses to electrically closest neighbors # if no buses are given, nodes that are no substations or without offshore connection are aggregated if buses_i is None: logger.info( "Aggregating buses that are no substations or have no valid offshore connection" ) buses_i = list( set(n.buses.index) - set(n.generators.bus) - set(n.loads.bus)) weight = pd.concat({ 'Line': n.lines.length / n.lines.s_nom.clip(1e-3), 'Link': n.links.length / n.links.p_nom.clip(1e-3) }) adj = n.adjacency_matrix(branch_components=['Line', 'Link'], weights=weight) bus_indexer = n.buses.index.get_indexer(buses_i) dist = pd.DataFrame(dijkstra(adj, directed=False, indices=bus_indexer), buses_i, n.buses.index) dist[ buses_i] = np.inf # bus in buses_i should not be assigned to different bus in buses_i for c in n.buses.country.unique(): incountry_b = n.buses.country == c dist.loc[incountry_b, ~incountry_b] = np.inf busmap = n.buses.index.to_series() busmap.loc[buses_i] = dist.idxmin(1) clustering = get_clustering_from_busmap( n, busmap, bus_strategies=dict(country=_make_consense("Bus", "country")), aggregate_generators_weighted=True, aggregate_generators_carriers=None, aggregate_one_ports=["Load", "StorageUnit"], line_length_factor=1.0, generator_strategies={'p_nom_max': 'sum'}, scale_link_capital_costs=False) return clustering.network, busmap
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", extended_link_costs=0, focus_weights=None, ): if potential_mode == "simple": p_nom_max_strategy = np.sum elif potential_mode == "conservative": p_nom_max_strategy = np.min else: raise AttributeError( f"potential_mode should be one of 'simple' or 'conservative' but is '{potential_mode}'" ) if custom_busmap: busmap = pd.read_csv(snakemake.input.custom_busmap, index_col=0, squeeze=True) busmap.index = busmap.index.astype(str) logger.info( f"Imported custom busmap from {snakemake.input.custom_busmap}") else: if snakemake.config["cluster_options"]["alternative_clustering"]: n, busmap = busmap_for_gadm_clusters( n, snakemake.config["build_shape_options"]["gadm_layer_id"] ) # TODO make func only return busmap, and get level from config else: busmap = busmap_for_n_clusters(n, n_clusters, solver_name, focus_weights, algorithm) weighted_agg_gens = True clustering = get_clustering_from_busmap( n, busmap, bus_strategies=dict(country=_make_consense("Bus", "country")), aggregate_generators_weighted=weighted_agg_gens, aggregate_generators_carriers=aggregate_carriers, aggregate_one_ports=["Load", "StorageUnit"], line_length_factor=line_length_factor, generator_strategies={ "p_nom_max": p_nom_max_strategy, "p_nom_min": np.sum }, scale_link_capital_costs=False, ) if not n.links.empty: nc = clustering.network nc.links["underwater_fraction"] = ( n.links.eval("underwater_fraction * length").div( nc.links.length).dropna()) nc.links["capital_cost"] = nc.links["capital_cost"].add( (nc.links.length - n.links.length).clip(lower=0).mul(extended_link_costs), fill_value=0, ) return clustering