A stochastic approach to dynamic participation in energy communities

With energy communities and local electricity markets on the rise, the possibilities for prosumers to be actively involved in the energy system increase, creating more complex settings for energy communities. This paper addresses the following research question: Does having knowledge about the future development in energy communities help make better decisions selecting new participants than without consideration of any future developments? Each year, the community is faced with the exit of existing members and a portfolio of possible new entrants with different characteristics. For this purpose, a bi-level optimization model for dynamic participation in local energy communities with peer-to-peer electricity trading, which is able to select the most suitable new entrants based on the preferences of the members of the original community, is extended to a stochastic dynamic program. The community wants to plan a few years ahead, which includes the following uncertainties: (i) which members leave after each period, and (ii) which are the potential new members willing to join the community. This paper’s contribution is a stochastic optimization approach to evaluate possible future developments and scenarios. The focus lies on the contractual design between the energy community and new entrants; the model calculates the duration of contracts endogenously. The results show a sample energy community’s decision-making process over a horizon of several years, comparing the stochastic approach with a simple deterministic alternative solution.


Motivation
Decentralized electricity production creates an opportunity for traditional consumers such as households or small businesses to become producers at the same time (called prosumers) and thereby become active participants in the energy system. Because a single prosumer is only a very small player in the system, a step forward for prosumers is to collectively organize themselves in so-called energy communities, where members have the opportunity to share or trade electricity with each other. A common trading approach in scientific literature is peer-to-peer trading, 1 where participants directly buy and sell electricity from/to their "peers" ( [44] and [48]). The objectives of energy community members are mostly to increase their economic benefits and to contribute to climate change mitigation ( [42] and [3]). Photovoltaic (PV) systems play a major role in the production of clean electricity [23], and the number of prosumers in the energy system is rises steadily. In the European Union, the REDII [12] paves the way to enable renewable energy communities (REC). The therein defined measures will lead to higher acceptance and a better establishment of energy communities in the future, which means not only that the formation of energy communities is facilitated and that entry barriers are reduced, but also that stabilization, medium-and long-term developments, and selection processes in energy communities should be better understood. The analyses of this paper consider existing energy communities wherein a community manager selects optimal new participants for the community in order to maximize the environmental benefits of its members.

Core objective and research question
The core objective of this work is to optimize the selection process of an energy community wherein the prosumers' PV electricity generation is allocated using a peer-to-peer trading scheme. The research question is the following: Does having knowledge about the future development in energy communities help a community manager make better decisions selecting new participants than without consideration of any future developments? The decision considers a portfolio of possible new entrants to the community, who might or might not join in the future.

Applied method
For the purpose of answering the research question defined above, a stochastic dynamic program with a look-ahead policy is developed. The model is based on the bi-level optimization model presented by the authors in [33], which is able to select optimal new members for an energy community while simultaneously optimally allocating the PV generation between the members considering their individual willingnessto-pay. In this paper, that model is further developed such that the decision made here and now includes a time horizon peaking into the future. Future parameters are stochastic and scenarios are used to adequately represent possible future developments.

Structure of the paper
The next Sect. 2 provides a comprehensive review of relevant scientific literature and concludes with the paper's contributions beyond state-of-the-art. Sect. 3 describes the method and modeling approach of the dynamic program in detail, including data and further empirical assumptions. The results of an illustrative case study analyzing an energy community of 20 prosumers in Austria are shown in Sect. 4. A conclusion and the outlook for future research needs in Sect. 5 complete the paper.

State-of-the-art and progress beyond
This chapter provides a discussion of recent scientific literature relevant to energy communities and peerto-peer trading. Sect. 2.1 evaluates papers that study participation in energy communities, business models and contracts developed in the context of energy communities. Sect. 2.2 gives an overview of models that include stochastic approaches in modeling of energy communities. Sect. 2.3 presents this paper's contribution beyond state-of-the-art.

About participation and contracts in energy communities
Main research topics within the field of energy communities and local electricity markets are the barriers and incentives to participation of prosumers in energy communities. In this regard, the contracts and formation of energy communities are key. A literature review summarizing recent publications to de-A stochastic approach to dynamic participation in energy communities K rive challenges and barriers in energy communities from a consumer perspective is found in [28]. At European level, [4] provide a qualitative overview of energy community concepts and strategies that lead to their creation and growth. [3] make a distinction between incentives of members of small and large communities: Financial motives are most important for members of large communities, while non-economic drivers (environmental, social, and other) dominate for members of smaller, local communities. According to the analysis in [18] focusing on intentions of private households to participate in peer-to-peer trading mechanisms in Germany, highly interested potential participants exhibit environmental rather than economic preferences, and are drawn to innovative pricing schemes. [43] find that reliability is a key component and that citizens recognize the added nonmonetary values of renewable energy communities.
To ensure a just energy transition to a carbon-neutral economy, energy community projects should be observed from a social perspective [29] as well. How vulnerable groups might benefit from renewable energy communities is explored in [19], who investigated 71 RECs in Europe. Policy advice for new European rules for RECs are derived in [22]. Fair revenue sharing and exit clauses are examined in [15], to identify the optimal sizing of energy communities. [39] investigate how energy communities and climate city contracts are key interventions to face the ambitious goal of implementing citizens centered and climate-neutral cities.
Energy communities are opportunities to possibly create new (sustainable) business models [14]. An optimistic outlook on possible business models in the context of energy communities is brought by [7], where sizing of PV systems and electrochemical energy storage is optimized solving a mixed integer linear program leading to an internal rate of return of 11%. Investments via consumer stock ownership plans as the prototype business model for renewable energy communities are introduced in [30].
In local electricity markets and especially in peerto-peer trading, dynamics and diversity of the actors involved have to be considered. Creating dynamic peer-to-peer clusters for virtual local electricity markets to optimally match load and renewable generation profiles for an electric vehicle (EV) flexibility marketplace is presented in [21]. Diverse distributed energy resources (DER) portfolio characteristics of prosumers are included in the study of [37], who developed a multi-agent deep reinforcement learning approach to address the peer-to-peer trading problem. The concept of so-called (smart) contracts in energy communities or peer-to-peer trading is described, among others, in the following literature: [27] reviews smart contracts in energy systems, which are applied, e.g., in peer-to-peer trading, electric vehicle charging, and demand-side response. [27] propose a systematic model of the smart contracting process to guide researcher and practitioners in this field. [6] developed an automated peer-to-peer negotiation strategy for settling energy contracts under consideration of prosumers' individual and heterogeneous preferences over societal and environmental criteria. [50] propose an energy contract based on Shapley values to allocate profits among participants in a fair way. Another automated negotiation process of bilateral energy contracts is presented in [36].
An energy community is a small, tangible social unit, wherein trust and confidence in the community are key. Automated, smart contracts for trading, as seen in [6,27,36,50] and virtual energy communities [21] are useful and supporting instruments. Our work goes beyond these short-term optimal allocation and trading contracts; we also consider the medium-to long-term development of an energy community.

Stochastic modeling and optimization of energy communities
In the field of energy systems, there are many decisions that require dealing with uncertainty, especially due to growing volatile renewable generation (wind and solar) and price variations. [51] identified four methods to tackle uncertainties: Monte Carlo analysis, stochastic programming, robust optimization, and modeling to generate alternatives. About one third of the studies reviewed in [51] apply formal uncertainty techniques. The majority of energy system models use sensitivity or scenario analyses to include effects of uncertainty. We find different stochastic optimization approaches within microgrids and (smart) energy communities in scientific literature. Energy management of a smart community with EV charging using a scenario-based stochastic model predictive control framework is presented in [52]. Among other stochastic parameters, moving-horizon probabilistic models are applied for the prediction of the arrival time of EVs. [25] show a pooled local flexibility market design under demand uncertainty and stochastic bidding process, which can reduce the costs of grid operation. Net-zero communities are modeled in [26] using a fuzzy multi-criteria decision making approach: Renewable energies are selected based on a life-cycle perspective and under uncertainty. [31] analyze smart local networks, where customers can choose between alternative solutions of energy supply according to their own preferences. Customers' decisions are addressed by a stochastic modeling approach. Robust optimal on-line scheduling of an energy community, where renewable energy sources including a community storage are shared, is accomplished in [40] using a stochastic model predictive control (MPC) approach. Uncertainty from forecast of inflexible demand profiles and renewable production curves are included. In [8], the operating strategy for the flexibility of end-users is modeled using a rolling K A stochastic approach to dynamic participation in energy communities horizon approach, including trades at Day-Ahead and Intraday spot markets. A scenario-based stochastic multi-energy microgrid investment planning model to minimize costs is presented in [10]. Again regarding a microgrid, a two-stage program for unit commitment is combined with a Markov decision process in [41] considering wind uncertainties. [1] developed a bi-level stochastic optimization for microgrids. [24] present a combined robust and stochastic MPC for EV charging stations in microgrids.
In this section, we introduced models that include uncertainty in the planning and the operation of energy communities. We found that stochastic parameters concern, among others, renewable generation profiles, energy demand of prosumers, or EV charging. Some models include individual preferences of prosumers, e.g., in [31], where preferences of customers to choose from alternative energy sources are included in their modeling approach. We found that little attention is paid to individual preferences of prosumers and their willingness to participate in energy communities or local electricity markets.

Progress beyond state-of-the-art
The progress beyond state-of-the-art can be summarized as following: To our knowledge, preferences of prosumers to join or leave an energy community as stochastic input are not analyzed in any other paper. We consider the medium-to long-term development and stabilization of an energy community. We ask how to assign contracts in energy communities, such that participants are assured that the community is evolving according to their needs, and trust is strengthened. Finally, the explicit search for optimal participants for an energy community instead of searching for an optimal technology portfolio, as it is state-of-the-art in most papers, is a prominent aspect of this work. With increasing number of prosumers in the energy system and energy communities as an established instrument, selection of participants will become more and more standard practice.

Method
The following chapter describes the methodology that is developed in this paper. An overview of the methodology is provided in Sect. 3.1, followed by a detailed description of the stochastic dynamic program in Sect. 3.2. Details on data and assumptions of a case study and the scenarios used are presented in Sect. 3.3 and Sect. 3.4, respectively.

Overview on the methodology
The purpose of this work is to develop a sound framework for energy communities to select from a portfolio of potential members under consideration of uncertainties, which is why a stochastic dynamic programming approach is developed. We consider the (potential) members' preferences to stay, leave, or wanting to join the community as the main uncertainty. Therefore, scenarios are developed and we use probabilities of possible future entries and exits in/from the community. A community manager has to decide what kind of contracts to offer to each of the prosumers. These contracts are binding from the perspective of the community manager (members are not allowed to be kicked out), but members can decide to leave the community before the end of the contract.
The procedure can be summarized as follows: Each year, the community manager captures the existing members and their contracts. Next, information on new possible entrants and their willingness to join the community is collected. Finally, we check if there are any existing members who want to early phase out of their contract and leave the community. Now the community manager has collected all of the certain (deterministic) information. Stochastic input data of future developments are then estimated, considering the following uncertainties: (i) which members are leaving after each period, and (ii) which are the potential new members willing to join the community. A set of scenarios is designed to represent these uncertainties and include them in the optimization problem.

Stochastic dynamic program
This section presents the core of the method, the stochastic dynamic program. The procedure introduced in Sect. 3.1 is now mathematically explained. The dynamic program needs a policy, which is a function to determine decisions given available information in a state. We choose a look-ahead policy: Decisions are made explicitly optimizing over a certain time horizon with stochastic forecasts. Fig. 1 shows an overview of the structure of the dynamic program. The planning horizon corresponds to n years in a set N , the scenarios ω are of a finite sample of potential outcomes Ω, and i ∈ I are all (possible) prosumers of a portfolio. The optimization model solves two main problems simultaneously: (i) selecting optimal new participants from the portfolio of possible entrants and assigning contracts to them, and (ii) optimally allocating the trading between participants considering their individual willingness-to-pay. Optimal allocation in (ii) means maximizing the community welfare (see Sect. 3.2.4) considering the participants chosen in (i). Therefore, the problem can be formulated as bi-level problem, wherein the leader (i) anticipates the reaction of the follower (ii).

Upper-level problem
The problem is divided into two steps: The first one, year n = 1, represents the "here and now" decision. We know the status-quo of the community and the A stochastic approach to dynamic participation in energy communities K Fig. 1 Overview on the stochastic dynamic program portfolio of new members, who might or might not want to join, at this time. The second step starts at n = 2 until n = N , where we use scenarios such that the decision at n = 1 can "see" the future within a certain horizon.

Objective function
The objective function is minimized considering scenarios and planning horizon: F 1 is the the value of the objective function at n = 1 (deterministic; scenarios are not included). F n (ω) is the value of the objective function of a certain forecast year n and scenario ω, and p(ω) is the probability that ω happens.
As reference, we calculate the emissions of all possible members as if they were stand-alone prosumers (not part of the community; hence, no electricity trad-ing with anyone else but the grid, with the objective of maximizing their own self-consumption). The objective function measures the improvement of the community members' emission balances. Therefore, the optimal selection of new members should improve the emission balance of the existing participants. The emissions of each community member i over a year n are calculated as following: This definition considers the purchases q G in i ,t ,n from the grid only, as the production of PV electricity does not generate marginal emissions. F n (ω) is composed of emissions n,i (ω) and emissions out,i ; the latter are an-K A stochastic approach to dynamic participation in energy communities Originalarbeit nual emissions of member i as a stand-alone prosumer, as mentioned above.
Let us describe Equation (3) in detail: We use b 0,i and s n,i (ω) 2 to exclude prosumers, who were not part of the original community (i.e., b 0,i = 0) and those who want to leave the community in scenario ω (i.e., s n,i (ω) = 0), from the calculations. In addition, we use b n,i (ω) to ensure that the share of prosumer i 's emission balance in F n (ω) is zero if prosumer i is not part of the new community (b n,i (ω) = 0) in year n and scenario ω. 3 Thus, linearity of the problem, apart from binary variables, is maintained.

Transition function
A so-called transition function reflects the system dynamics of a dynamic program. In this work, the transition function calculates the remaining contract length (state variable x n,i (ω) ≥ 0) of each prosumer i . It depends on the number of years remaining from the previous year (x n−1,i (ω)) and the control variable u n,i (ω) ≥ 0, which is the possible extension of the contract. The transition function is defined as: valid for ∀i ∈ I , n > 1 ∈ N , ω ∈ Ω. s n,i (ω) is an exogenous parameter from the scenarios, representing the (possible) choices of the portfolio: staying/joining (s n,i (ω) = 1), or leaving/not joining (s n,i (ω) = 0). Note that when s n,i (ω) = 0, then x n,i (ω) = 0. The binary vari- ,1} serves two ends: (i) in transition function (4), b n,i decreases the contract length of the previous year x n−1,i (ω) by one year; (ii) b n,i (ω) can set the lower-level constraints (16b) and (16c) to zero, thus excluding a prosumer (refer to Sect. 3.2.4 for better understanding). The relationship between x n,i (ω) and b n,i (ω) can be expressed by using a big-M approach. For n = 1, we use the initial values x 0,i and b 0,i for the transition function: Eq. (7) means that we have to choose one decision u 0,i for the contract length of prosumer i regardless of the outcome ω; hence, we are not allowed to see into the future. Non-anticipativity constraints are included for all other variables too: There is also a rule implemented that prosumers, that wanted to join the community (s n,i (ω) = 1), but were rejected (b n,i (ω) = 0), are not considered anymore in the following years; hence, b m,i (ω) = 0 ∀m > n. We assume that once a prosumer was rejected, they search for other, alternative energy communities to join.

Lower-level problem
The dynamic program has to solve a lower-level problem to optimally allocate PV electricity generation within the community according to the participants' individual willingness-to-pay. The lower-level problem is adopted from [33]; therefore, a very brief overview is presented in the following. For details refer to the original publication.

Willingness-to-pay
The willingness-to-pay of prosumer j at time t to buy from prosumer i , w t p i , j ,t , is as follows:

Community welfare
The aim of peer-to-peer electricity trade is to maximize community welfare, which is defined in two parts. Part I of community welfare measures the optimal resource allocation at community level, maximizing self-consumption of the community as a whole over a year. Part II optimally assigns PV generated electricity to each member in consideration of their individual willingness-to-pay; thus, part II represents peer-to-peer trading from one prosumer to another, q share i , j ,t . Community welfare (CW) within scenario ω over year n is defined as following: A stochastic approach to dynamic participation in energy communities K Originalarbeit The set of variables i,t ,n (ω),SoC i,t ,n (ω) (13) are the lower level primal decision variables. The formulation is found in the Appendix. The lower level problem is reformulated to its corresponding Karush-Kuhn-Tucker (KKT) conditions in order to solve the bi-level problem.

Model implementation
The open-source model 4 is implemented using Python (version 3.9.7; [49]), the Pyomo package (version 6.2; see [20] and [5]), and the commercial 5 solver Gurobi (version 9.5.0; see [17]). The stochastic dynamic program is very computationally expensive; with a time horizon of five years considering four scenarios, the case study presented in the following paragraphs takes 7 hours and 36 minutes to solve on a standard computer with Intel(R) Core(TM) i7 CPU. A deterministic solution of the same problem without forecast and scenarios takes 47 seconds.

Parameters of the case study
In this case study, a portfolio of 20 artificial prosumers consisting of ten single houses (SH), eight small apartment buildings (SAB), and two small businesses (SME) is considered. Single houses have PV systems with up to 5 kWpeak installed, and apartment buildings and businesses up to 8 kWpeak. Additionally, some prosumers own a battery storage system (BESS). Not all prosumers have their own PV systems; hence, they are consumers only. The detailed data including PV system orientation and willingness-to-pay (CO2-price w j ) can be found in Table 1. w j covers a range between 0-100 EUR/tCO2, 6 depending on how strong a prosumer's environmental ambitions are. The distance preferences d i , j between prosumers are arbitrarily assigned within d i , j ∈ [0,1]. The distances are symmetric, The initial set-up consists of ten prosumer (five SHs, four SABs, and one SME); from there, the different scenarios are developed as shown in Sect. 3.4. Electricity demand data and PV production data are obtained from open-source tools. Residential demand profiles (LoadProfileGenerator version 10.4.0, see [32] and [35]) represent different living situations and demographics. Renewables.ninja (see [34,38], and [45]) provides electricity output data from PV systems; in this case study, data from Vienna, Austria from 2019 is applied. To represent demand profiles of businesses, 6 With average emissions of 132 gCO2 kWh from electricity generation in Austria and, for example, w j = 100 EUR/tCO 2 , the willingness-to-pay is 1.32 cent/kWh above the retail electricity price.

K
A stochastic approach to dynamic participation in energy communities Originalarbeit Fig. 2 Choice of the prosumers s n,i (ω) depending on the scenarios ω ∈ Ω (blues n,i (ω) = 1; yellow s n,i (ω) = 0; red highlighted -changes compared to the original community) a synthetic load profile for standard businesses (G0 "Gewerbe allgemein") is used (see [2]).
Other parameter of the case study concern electricity prices and emissions from the grid. Prosumers buy remaining electricity, which they could not buy from other community members or self-generate, from the retailer. The average residential electricity price in Austria was p G in t = 0.22 EU R/kW h in 2021 (see [13]). This value is constant over all t ∈ T and n ∈ N . The excess PV generation, which prosumers could not sell to other community members or self-consume, is sold to the grid at Day-Ahead (DA) market prices. p G out t are Austrian DA prices from 2019 (see [11]). These values are time-variant over t ∈ T ; the time series is re-used for all n ∈ N . Emissions from the grid are calculated using again data from [11] for Austria. The calculation considers the amount of electricity generated per hour and per generation type to account for the corresponding emissions. e t are hourly average values in gCO2kWh; this time series is again used for all n ∈ N .
Annual hourly data that is available for a whole year is transformed into three representative days using the Python tslearn package [46], which is based on a kmeans clustering algorithm [47]. This step is necessary to reduce computational efforts, because solving MPECs is already very time-consuming. Per year, 8760 time steps are reduced to 72 time steps only. The resulting representative days reflect a summer, a winter, and a spring/fall day. The input data sets that are clustered mainly vary during different times of the day and the year (i.e., seasons). This information is preserved in the representative time series, therefore the clustering approach is reasonable in our application.
A stochastic approach to dynamic participation in energy communities K Originalarbeit

Scenarios
We use a finite set of scenarios to represent possible developments of the portfolio of possible prosumers. Considering in total 20 prosumers, their possible decisions, and a time horizon of a few years, a large number of permutations are obtained. Therefore, a scenario tree with all possibilities would be very large. Due to the high computational efforts of stochastic programming, we do not aim at using the full scenario tree for our research. Instead, a relatively small set of completely different scenarios is developed to represent the wide spectrum of possibilities. 7 This decision is also justified by the fact that in the objective function in Eq. (1), the scenarios are weighted with their probabilities p(ω). As a result, with increasing number of scenarios, the probabilities of each single scenario drop.
The use case that will be shown in the results section considers different building and prosumer types: single houses (SH), small apartment buildings (SAB), and small businesses (SME). At the beginning, the initial set-up contains five SHs, four SABs, and one SME. The present contract lengths with the community x 0,i vary between zero (in the portfolio, but not a member) and three years. From there, four different scenarios are considered: ω 1 : additional SABs might want to join in the upcoming years ω 2 : the SABs might want to phase-out in the upcoming years ω 3 : additional SHs might want to join in the upcoming years ω 4 : the SHs might want to phase-out in the upcoming years Fig. 2 shows a graphical representation of each scenario ω ∈ Ω from year one to year five (blues n,i (ω) = 1; yellows n,i (ω) = 0; highlighted in red -changes compared to the original community). The original community consists of the following prosumers: SH 1, SH 2, SH 3, SH 6, SH 7, SAB 3, SAB 4, SAB 5, SAB 7, SME 1.

Results
This chapter covers the results of the case study and the corresponding scenarios introduced in Sects. 3.3 and 3.4. The first set of results in Sect. 4.1 shows the community manager's decision of one year using a horizon with stochastic forecasts. Sect. 4.2 presents the selection process over several, consecutive years, and compares the results between deterministic and stochastic decisions. 7 The values of s n,i (ω) are randomly assigned.

Selection of new members in year one using a horizon with stochastic forecasts
The energy community that is investigated in the case study considers a portfolio of 20 (possible) prosumers. The portfolio is diverse: different building types (single houses and apartment buildings), residential and commercial consumers, different PV system sizes, etc. are included. Initially, the community consists of ten members; the other ten prosumers are not members (yet), but part of the portfolio. It is up to the community manager to define who could be a potential new member in the future. Observing a neighborhood or a district, buildings currently under construction or newly constructed buildings could be potential new members in a few years or even sooner. Also, growing interest in energy communities per se is considered. Residents of existing buildings with already installed PV systems might notice the advantages of joining forces in a community. With some expertise, such portfolio can be created. The next step involves the development of plausible scenarios. If and when a potential new prosumer might announce their willingness to join the community are estimated. This does not have to be exact, because uncertainties can be represented in the different scenarios.

Original community
Initially, the status-quo of the original community is observed to create a starting point for the further evaluations of the results. Fig. 3 shows the peer-to-peer traded electricity (in kWh/year) in detail; columns represent the purchases of each member, and rows the respective sales. The allocation is based on the participants' willingness-to-pay: Prosumers sell self- generated PV electricity to those members with highest willingness-to-pay. Table 2 shows the quantitative, annual results (kilowatt-hours of electricity bought and sold, emissions, and costs) of all members. The community consists of six prosumers, who own PV systems (three of them own an additional BESS), and four consumers, who cannot sell electricity; they rely on purchases from the grid or from the community.

Stochastic solution
The first set of results shows the selection process for one year in detail. A time horizon of five years with stochastic forecasts from year n = 2,...,5 is included in the decision at year n = 1. For each scenario within the time horizon, different decisions are made depending on which configuration is optimal within each scenario. The resulting numbers of prosumers are shown in Fig. 4, grouped into the following categories: the numbers of existing members (blue) and newly added members (green) are counted on the positive y-axis, and the numbers of prosumers, who are part of the portfolio but no members of the community (yellow), and those leaving the community (red) are counted on the negative part of y-axis. The scenarios ω 1 , ω 2 , ω 3 , ω 4 are shown one below the other. Note that in year one, there is only one joint decision for all scenarios together because of the non-anticipativity constraints imposed in Eqs. (7)-(10). As shown in Fig. 4, the decision at year one involves three prosumers who join the community, and two prosumers who leave. Prosumer SAB 3 and prosumer SAB 7 left on a voluntary basis (s 1,i = 0). At n = 1, decisions on the potential participation of prosumer SH 5, prosumer SAB 8, and prosumer SME 2, who show interest in joining the community (s 1,i = 1), are made.
The stochastic dynamic program under consideration of all four scenarios accepts the new prosumers into the community. Prosumer SH 5 and SAB 8 bring PV systems to the community, which facilitates acceptance. Prosumer SME 2 on the other hand presents an interesting case: Not owning PV systems, but having the highest electricity demand within the community, prosumer SME 2 is not the ideal candidate for this community with the objective of minimizing emissions. In our case study, there is sufficient excess PV generation available for prosumer SME 2 to be included in the community without worsening the objective function, because prosumer SAB 3 and SAB 7, who are both consumers only, left. We take a look at Fig. 5, where increase (or decrease) of annual costs and emissions comparing the original community and the community at n = 1 are illustrated. Costs and emissions of prosumers that left the community (prosumer SAB 3 and SAB 7), and of those who joined the community (prosumer SH 5, SAB 8, and SME 2), are compared with the costs/emissions of stand-alone prosumers. Without the community, emissions due to electricity consumption of prosumers SAB 3 and SAB 7 highly increase in n = 1. All other emission balances are negative except for prosumer SME 1, thus most prosumers can avoid emissions by trading electricity with other community members. The only prosumer with highly increasing costs in year one is prosumer SAB 5, who joined the community at n = 1.
Returning to Fig. 4, we now compare the scenarios from year two to year five. There is a distinct difference between the scenarios starting from year three: In scenario ω 1 and ω 3 , new prosumers show interest in joining the community, while in ω 2 and ω 4 , some existing members leave the community, without re-A stochastic approach to dynamic participation in energy communities K Originalarbeit Fig. 4 Acceptance/dropping out per scenario considering all prosumers in the portfolio -stochastic solution Fig. 5 Difference of annual costs and emissions between original community and new community at n = 1 (red -annual costs; green -annual emissions); increase of costs and emissions counted positive, decrease negative placement by new prosumers. This diversity within the scenarios is also reflected in the selection process. Single houses have higher PV capacities installed in relation to their annual electricity demand than apartment buildings or businesses. Therefore, single houses share more PV generated electricity with other members than other prosumer types. In scenario ω 4 , five single houses, which were part of the original community, leave in year n = 3. The remaining members are then left with a community without sufficient PV capacities to actually benefit from peer-topeer trading. Hence, the remaining prosumers leave too. The explanation for scenario ω 2 is similar.
Let us now discuss the development of the original community's annual emissions over five years. The contributions of each scenario to the expected emission are shown in Fig. 6. In this graph, only emissions of active members count; thus, emissions in scenarios ω 2 and ω 4 converge to zero. Additional SABs joining at n = 3 in scenario ω 1 increase emissions of the original community members, while staying well below the baseline, the emissions without sharing electricity in the community (dashed black line). In scenario ω 3 , the annual emission decrease, because the newly added SHs provide more PV generated electricity, relative to their own demand, to trade with the community.

Deterministic solution
Next, we compare the selection of the stochastic approach with a simplified, deterministic approach. The deterministic implementation is as following: First, the existing members and potential new members are captured. The optimization is executed knowing all relevant parameters of year n = 1, but not considering any future developments. The simplified version of Eq. (1) is: Constraint and lower level problem remain unchanged to those presented in Sect. 3.2, however, the scenarios ω are missing. Fig. 7 compares stochastic and deterministic solutions of the problem by showing the decision for each prosumer separately. While prosumers SH 5 and SME 2 are accepted into the community as in the stochastic solution, prosumer SAB 8 is rejected using a deterministic approach, which is the only distinction between the two cases.

Selection process over five years comparing stochastic vs. deterministic solution
Recalling the research question of this paper, we want to find out if the stochastic approach to dynamic participation in energy communities leads to different selection of prosumers than a more simple, deterministic approach. For this purpose, the optimization model is applied over several consecutive years using the deterministic implementation briefly explained in the previous section. The consecutive execution of the deterministic program is performed as following: We optimize using Eq. (14) with n = 1 as our objective function, knowing all the relevant parameters of year one, but not considering any future developments. The resulting configuration of members is the new socalled original community for the following year and the contract lengths are updated. We use scenario ω 1 A stochastic approach to dynamic participation in energy communities K Fig. 8 Acceptance/dropping out per scenario considering all prosumers in the portfolio -deterministic solution

Fig. 9
Emissions over five years by scenario ω 1 , ω 2 , ω 3 , ω 4 -deterministic solution as a reference scenario, which we assume is actually taking place, meaning s n,i = s n,i (ω 1 ). (15) The optimization is repeated year by year for all n ∈ N . Afterwards, the whole procedure is again repeated for the other scenarios ω 2 , ω 3 , ω 4 . Fig. 8 presents the decisions of the deterministic approach comparing all four scenarios one below the other. In year one, all scenarios deliver the same results, because the same parameters are assumed. Comparing with Fig. 4, it is interesting to notice that in the deterministic solution for scenarios ω 2 and ω 4 , there are still members in the community at n = 5, which is not the case in the stochastic solution. This can be explained as follows: The objective function F n takes into account the emission balances of all members of the original community. The deterministic approach updates the community each year, thus the set-up of original members changes as well. The stochastic results from the previous Sect. 4.1 are obtained from the decision at year one and only considers the original community at the starting point. The corresponding emissions are shown in Fig. 9.

Conclusions
In this work, a stochastic dynamic program is developed to optimally select new members for an energy community with peer-to-peer trading scheme. Based on previous work on energy communities by the authors in [33], where a bi-level optimization model can choose optimal parameters (PV capacity and annual electricity demand) of a new member and choose between potential new members, the present work includes scenarios of possible future developments within the energy community into the decision making process.
Core characteristic of our approach to the selection process is the community members' objective to minimize emissions from electricity consumption. The peer-to-peer trading mechanism maximizes selfconsumption -and therefore also minimizing emissions from electricity consumption -of the community as a whole while considering prosumers' individual willingness-to-pay. When selecting prosumers from a portfolio of potential new members, the original community aims at further avoiding emissions. It is up for discussion if energy community members are more interested in improving economic (e.g., by saving annual costs for electricity) or environmental benefits. Because literature often indicates that environmental incentives play a particularly important role for participants of energy communities, and because individual willingness-to-pay that determine peer-topeer trading in our work include a preference to save emissions, this analysis focuses entirely on environmental interests. Therefore, we made a conscious decision not against minimizing costs, but for minimizing emissions in the objective function, which is a distinguishing feature of this particular analysis.
This leads to the next discussion point. Our model allows the energy community to reject potential members, which is in some way a contradiction to the environmental preference attested to the community members. On the one hand, an energy community should be a small, socially tangible entity of manageable size. A sense of belonging, trust, and confidence are easier maintained in a small and selective community. Therefore, boundaries are consciously drawn. On the other hand, the suggested selection process is not a one-size-fits-all approach. Energy communities can have different sizes, goals, and diversity of actors involved. Not setting boundaries and accepting all interested prosumers into the community would eventually lead to one big energy community for a whole country, which is not a socially tangible entity anymore. The possibility to actively participate and to engage in the energy system according to one's own preferences would be lost.
The analyses showed that the stochastic approach to optimize a selection process of energy community members is cumbersome. Not only are stochastic dynamic programs computationally expensive, but also the creation of adequate scenarios, data collection and estimation of existing members and potential new ones is a complex procedure in real-life situations. Though, including scenarios that are most likely to happen as a forecast in the decision process is recommended. The exact contractual design between community members and the community as a legal entity is subject to further research, which should include real test sites and legal aspects.
Funding Open access funding provided by TU Wien (TUW).
Open Access This article is licensed under a Creative Commons Attribution 4.0 International License, which permits use, sharing, adaptation, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons licence, and indicate if changes were made. The images or other third party material in this article are included in the article's Creative Commons licence, unless indicated otherwise in a credit line to the material. If material is not included in the article's Creative Commons licence and your intended use is not permitted by statutory regulation or exceeds the permitted use, you will need to obtain permission directly from the copyright holder. To view a copy of this licence, visit http://creativecommons.org/licenses/by/4.0/.

Lower-level problem formulation
The formulation of the lower-level problem is:  Table 3 Nomenclature