def aggregate_exported_imported_energy(self, area): if area.uuid not in self.imported_energy: self.imported_energy[area.uuid] = {} if area.uuid not in self.exported_energy: self.exported_energy[area.uuid] = {} past_markets = list(area._markets.past_markets.values()) if len(past_markets) > 0: current_market = past_markets[-1] else: return child_names = [ area_name_from_area_or_iaa_name(c.name) for c in area.children ] for trade in current_market.trades: if child_buys_from_area(trade, area.name, child_names): add_or_create_key(self.exported_energy[area.uuid], current_market.time_slot_str, trade.offer.energy) if area_sells_to_child(trade, area.name, child_names): add_or_create_key(self.imported_energy[area.uuid], current_market.time_slot_str, trade.offer.energy) if current_market.time_slot_str not in self.imported_energy[area.uuid]: self.imported_energy[area.uuid][current_market.time_slot_str] = 0. if current_market.time_slot_str not in self.exported_energy[area.uuid]: self.exported_energy[area.uuid][current_market.time_slot_str] = 0.
def _accumulate_storage_trade(storage, area, accumulated_trades, past_market_types): if storage.name not in accumulated_trades: accumulated_trades[storage.name] = { "type": "Storage", "produced": 0.0, "earned": 0.0, "consumedFrom": {}, "spentTo": {}, } markets = getattr(area, past_market_types) if markets is None: return accumulated_trades else: if type(markets) != list: markets = [markets] for market in markets: for trade in market.trades: if trade.buyer == storage.name: sell_id = area_name_from_area_or_iaa_name(trade.seller) accumulated_trades[ storage.name]["consumedFrom"] = add_or_create_key( accumulated_trades[storage.name]["consumedFrom"], sell_id, trade.offer.energy) accumulated_trades[ storage.name]["spentTo"] = add_or_create_key( accumulated_trades[storage.name]["spentTo"], sell_id, trade.offer.price) elif trade.offer.seller == storage.name: accumulated_trades[ storage.name]["produced"] -= trade.offer.energy accumulated_trades[ storage.name]["earned"] += trade.offer.price return accumulated_trades
def _aggregate_exported_imported_energy(self): if self._area.current_market is None: return None self.imported_traded_energy_kwh = {} self.exported_traded_energy_kwh = {} child_names = [ area_name_from_area_or_iaa_name(c.name) for c in self._area.children ] if getattr(self.current_market, 'trades', None) is not None: for trade in self.current_market.trades: if child_buys_from_area(trade, self._area.name, child_names): add_or_create_key(self.exported_traded_energy_kwh, self.current_market.time_slot, trade.offer_bid.energy) if area_sells_to_child(trade, self._area.name, child_names): add_or_create_key(self.imported_traded_energy_kwh, self.current_market.time_slot, trade.offer_bid.energy) if self.current_market.time_slot not in self.imported_traded_energy_kwh: self.imported_traded_energy_kwh[self.current_market.time_slot] = 0. if self.current_market.time_slot not in self.exported_traded_energy_kwh: self.exported_traded_energy_kwh[self.current_market.time_slot] = 0.
def _accumulate_load_trades(load, grid, accumulated_trades, is_cell_tower, past_market_types): if load.name not in accumulated_trades: accumulated_trades[load.name] = { "type": "cell_tower" if is_cell_tower else "load", "produced": 0.0, "earned": 0.0, "consumedFrom": {}, "spentTo": {}, } markets = getattr(grid, past_market_types) if markets is None: return accumulated_trades else: if type(markets) != list: markets = [markets] for market in markets: for trade in market.trades: if trade.buyer == load.name: sell_id = area_name_from_area_or_iaa_name(trade.seller) accumulated_trades[ load.name]["consumedFrom"] = add_or_create_key( accumulated_trades[load.name]["consumedFrom"], sell_id, trade.offer.energy) accumulated_trades[ load.name]["spentTo"] = add_or_create_key( accumulated_trades[load.name]["spentTo"], sell_id, trade.offer.price) return accumulated_trades
def calculate_devices_sold_bought_energy(res_dict, market): if market is None: return for trade in market.trades: trade_seller = area_name_from_area_or_iaa_name(trade.seller) trade_buyer = area_name_from_area_or_iaa_name(trade.buyer) if trade_seller not in res_dict["sold_energy"]: res_dict["sold_energy"][trade_seller] = {"accumulated": {}} if trade_buyer not in res_dict["sold_energy"][trade_seller]: res_dict["sold_energy"][trade_seller][trade_buyer] = {} if trade.offer.energy > FLOATING_POINT_TOLERANCE: add_or_create_key(res_dict["sold_energy"][trade_seller]["accumulated"], market.time_slot, trade.offer.energy) add_or_create_key(res_dict["sold_energy"][trade_seller][trade_buyer], market.time_slot, trade.offer.energy) if trade_buyer not in res_dict["bought_energy"]: res_dict["bought_energy"][trade_buyer] = {"accumulated": {}} if trade_seller not in res_dict["bought_energy"][trade_buyer]: res_dict["bought_energy"][trade_buyer][trade_seller] = {} if trade.offer.energy > FLOATING_POINT_TOLERANCE: add_or_create_key(res_dict["bought_energy"][trade_buyer]["accumulated"], market.time_slot, trade.offer.energy) add_or_create_key(res_dict["bought_energy"][trade_buyer][trade_seller], market.time_slot, trade.offer.energy)
def check_acct(self): actor_sums = {} for t in self.market.trades: actor_sums = add_or_create_key(actor_sums, t.seller, t.offer_bid.energy) actor_sums = subtract_or_create_key(actor_sums, t.buyer, t.offer_bid.energy) for actor, sum_ in actor_sums.items(): assert self.market.traded_energy[actor] == sum_ assert sum(self.market.traded_energy.values()) == 0
def _area_trade_from_parent(area, parent, accumulated_trades, past_market_types): area_IAA_name = make_iaa_name(area) parent_markets = getattr(parent, past_market_types) if parent_markets is not None: if type(parent_markets) != list: parent_markets = [parent_markets] for market in parent_markets: for trade in market.trades: if trade.buyer == area_IAA_name: seller_id = area_name_from_area_or_iaa_name(trade.seller) accumulated_trades[area.name]["consumedFrom"] = \ add_or_create_key(accumulated_trades[area.name]["consumedFrom"], seller_id, trade.offer.energy) accumulated_trades[area.name]["spentTo"] = \ add_or_create_key(accumulated_trades[area.name]["spentTo"], seller_id, trade.offer.price) return accumulated_trades
def _update_stats_after_trade(self, trade, offer, buyer, already_tracked=False): # FIXME: The following updates need to be done in response to the BC event # TODO: For now event driven blockchain updates have been disabled in favor of a # sequential approach, but once event handling is enabled this needs to be handled if not already_tracked: self.trades.append(trade) self._update_accumulated_trade_price_energy(trade) self.traded_energy = add_or_create_key(self.traded_energy, offer.seller, offer.energy) self.traded_energy = subtract_or_create_key(self.traded_energy, buyer, offer.energy) self._update_min_max_avg_trade_prices(offer.price / offer.energy) # Recalculate offer min/max price since offer was removed self._update_min_max_avg_offer_prices()
def _accumulate_area_trades(area, parent, accumulated_trades, past_market_types): if area.name not in accumulated_trades: accumulated_trades[area.name] = { "type": "house", "produced": 0.0, "earned": 0.0, "consumedFrom": {}, "spentTo": {}, "producedForExternal": {}, "earnedFromExternal": {}, "consumedFromExternal": {}, "spentToExternal": {}, } area_IAA_name = make_iaa_name(area) child_names = [ area_name_from_area_or_iaa_name(c.name) for c in area.children ] area_markets = getattr(area, past_market_types) if area_markets is not None: if type(area_markets) != list: area_markets = [area_markets] for market in area_markets: for trade in market.trades: if area_name_from_area_or_iaa_name(trade.seller) in child_names and \ area_name_from_area_or_iaa_name(trade.buyer) in child_names: # House self-consumption trade accumulated_trades[ area.name]["produced"] -= trade.offer.energy accumulated_trades[ area.name]["earned"] += trade.offer.price accumulated_trades[area.name]["consumedFrom"] = \ add_or_create_key(accumulated_trades[area.name]["consumedFrom"], area.name, trade.offer.energy) accumulated_trades[area.name]["spentTo"] = \ add_or_create_key(accumulated_trades[area.name]["spentTo"], area.name, trade.offer.price) elif trade.buyer == area_IAA_name: accumulated_trades[ area.name]["earned"] += trade.offer.price accumulated_trades[ area.name]["produced"] -= trade.offer.energy for market in area_markets: for trade in market.trades: if area_name_from_area_or_iaa_name(trade.seller) == \ area.name and area_name_from_area_or_iaa_name(trade.buyer) in child_names: accumulated_trades[area.name]["consumedFromExternal"] = \ subtract_or_create_key(accumulated_trades[area.name] ["consumedFromExternal"], area_name_from_area_or_iaa_name(trade.buyer), trade.offer.energy) accumulated_trades[area.name]["spentToExternal"] = \ add_or_create_key(accumulated_trades[area.name]["spentToExternal"], area_name_from_area_or_iaa_name(trade.buyer), trade.offer.price) elif area_name_from_area_or_iaa_name(trade.buyer) == \ area.name and area_name_from_area_or_iaa_name(trade.seller) in child_names: accumulated_trades[area.name]["producedForExternal"] = \ add_or_create_key(accumulated_trades[area.name]["producedForExternal"], area_name_from_area_or_iaa_name(trade.seller), trade.offer.energy) accumulated_trades[area.name]["earnedFromExternal"] = \ add_or_create_key(accumulated_trades[area.name]["earnedFromExternal"], area_name_from_area_or_iaa_name(trade.seller), trade.offer.price) accumulated_trades = \ _area_trade_from_parent(area, parent, accumulated_trades, past_market_types) return accumulated_trades
def _discrete_point_curve(self, obj_list, round_functor): cumulative = {} for obj in obj_list: rate = round_functor(obj.price / obj.energy) cumulative = add_or_create_key(cumulative, rate, obj.energy) return cumulative