Assessing the Influence of Different Goals in Energy Communities’ Self-Sufficiency—An Optimized Multiagent Approach

: Understanding to what extent the emergence of prosumers and prosumagers organized in energy communities can impact the organization and operation of power grids has been one of the major recent research avenues at the European level. In renewable-based communities aiming to reach some level of energy self-sufficiency, a key issue to be addressed is assessing how the presence of end-users playing different roles in the system (self-consuming, producing and trading, performing demand management, etc.) can influence the overall system performance. In this setting, this paper combines Distributed Artificial Intelligence and optimization approaches to assess how prosumagers and consumers pursuing different goals can influence the energy self-sufficiency of a local energy community. The residential demand is accurately modeled, and the agents’ preferences are considered in the modeling to represent a smart community. The results show that although energy community members may have conflicting individual goals, the overall system self-sufficiency can be maximized with economic benefits for all stakeholders, thus illustrating the advantages of energy communities.


The Role of Energy Communities in the EU Agenda
The European Union (EU) is on a transition course towards a decentralized and fossil-fuel free energy system where end-users are active players contributing to the management of the power grid as asset holders, investment decision-makers and demand response (DR) programs participants. This changing paradigm empowering end-users will allow new business models and energy infrastructure ownership configurations to emerge [1,2]. In this setting, local energy communities (LEC) in which end-users become prosumers and prosumagers are gaining momentum, both in the literature [3,4] and in the EU's regulatory framework, being at the heart of the European energy policy for 2030 and 2050 [5].
Although LEC can encompass several components of the value chain (e.g., generation, distribution, supply, aggregation), in their most elementary forms they are mainly involved in local energy generation and consumption [6]. More recently, innovative business models have started to emerge, providing more integrated solutions to LEC members, favoring self-consumption and enabling the combined use of storage, local energy trading, and exchanges with the grid [7]. Thus, the emergence of LEC creates the

Distributed Artificial Intelligence in Energy Modeling
The growing penetration of renewables has created new challenges in the operation and management of power systems, also considering the pervasiveness of information and communication technologies (ICT), real-time monitoring and control devices, advanced metering infrastructures, etc. This digitalization trend is expected to generate massive amounts of data which require sophisticated data handling techniques [26]. Also, from a modeling point of view, new challenges are created due to the increasing number of actors and the dynamics of their interactions in decentralized energy systems, as LEC, marked by a noticeable socio-technical dimension [27]. In this setting, Artificial Intelligence (AI) has been identified as key to deal with modeling and decision support [26,28]. Distributed Artificial Intelligence (DAI) is a subfield of AI which is based on the interactions of intelligent agents capable of making decisions to achieve goals while co-habiting in an environment populated by other agents [29]. Based on a new programming paradigm for software engineering called Agent-Oriented Programming (AOP), multiagent systems (MAS) are a relatively new field of DAI [30]. In MAS, autonomous agents are endowed with the ability to adjust their behavior, communicate, negotiate, and collaborate, cooperate or compete to achieve their goals [31,32]. Agents take sensory input from the surrounding environment and from other agents while performing actions upon the environment (through actuators), to reach their goals. Thus, they must be able to reason and decide how to act in specific circumstances [33]. The rules driving their behavior are implemented in their architecture and consist of a set of modules used to solve subproblems defining the actions agents must perform [34]. Different architectures must be created defining the complexity of the action rules implemented. The most well-known agent architectures are described as follows [34][35][36]: • Purely reactive-agents decide uniquely based on the current situation, ignoring everything they have learned in the past. If-then-else or condition-action rules are usually adequate to define the actions performed by these agents. • Model-based-agents can make decisions based on past inputs even if they cannot observe part of the environment at a given instant. Inference mechanisms using decision states and heuristics to construct decision trees are commonly used by these agents to decide which actions they will take next.

•
Goal-based-agents have an explicitly represented model of the environment and deliberately choose to perform actions that they know to lead, with some probability, to the accomplishment of their goals. • Utility-based-agents may perform actions expected to maximize their utility (function defined to measure the performance of a given choice). Multicriteria decision-making techniques can be exploited in this setting to allow agents to express preferences or define subjective probabilities, assigning coefficients of importance to the multiple criteria, etc. • Learning-agents can improve their performance by anchoring their decisions on the knowledge gained through iterative attempts or previous experiments. Evolutionary computing or neural networks can be used if an optimal solution in a complex or large solution space is required or if an agent must decide based on patterns. • Belief-desire-intention (BDI)-the agents' reasoning is supported on concepts which can be used to predict human behavior: they observe the world, get and update information (beliefs), reason about their aims (desires), and, based on preferences, decide how to act to reach the objectives they are committed to (intentions).
Several energy related issues have been exploited by using MAS, including residential demand-side flexibility [37], electricity market transactions [38], virtual energy trading between microgrids [39,40] and grid stability issues [41]. MAS has also been used to model different strategies of automated energy demand management [37] and to represent energy demand dynamics in residential and non-residential scenarios, as in [42][43][44]. More recently, the MAS framework has been used to solve energy management problems, which are the most relevant problems in LEC ambitioning to reach self-sufficiency. In [45], autonomous agents with their own demand profiles and generation and storage systems must decide how to use locally generated energy, when to charge/discharge batteries, how to manage loads, and even when to trade electricity within the neighborhood to minimize electricity costs. Optimization algorithms, both exact methods, as in [46], and nature-inspired metaheuristic approaches (as genetic algorithms (GA) [47] and evolutionary algorithms [48,49]) can be embedded in MAS to optimize energy resources management [50,51]. For instance, reference [52] proposed a MAS to optimize the energy flows between a LEC of prosumers, revealing a good performance in enhancing community self-consumption and reducing members' costs. Decision-making was based on the Alternative Direction Method of Multipliers (ADMM) algorithm [52]. The work of [53] presented a MAS framework to coordinate and control the generation and demand in a microgrid of diversified consumers. The Tabu Search algorithm was used to mini-mize consumer agents' electricity bills, the power withdrawn from the grid and to maximize power quality.
Although approaches combining agent modeling and optimization techniques are emerging in the literature, they still fall short of considering behavioral (agents' preferences and goals), social (relationships of cooperation, collaboration or competition between agents), and organizational dynamics that may exist in community settings. Currently, MAS frameworks in energy settings often reproduce poorly the diversity of agents. Approaches focusing on consumers or prosumers are quite frequent, but diversified settings are scarce. Thus, models in which agents play different roles and have distinct preferences and conflicting individual goals, while cohabiting in the same system, are still missing in the literature.

Research Contribution and Organization
This work aims to develop a MAS approach to model a LEC in which agents representing residential prosumagers and consumers are included. Residential agents are coordinated by a central entity that is responsible for managing the community's energy resources and interfacing with an electricity retailer. The overall goal is to examine to what extent the LEC energy demand can be satisfied by locally generated resources considering different shares of prosumagers and consumers with distinct energy utilization profiles, goals and preferences. Residential loads are accurately modeled through physically-based models (PBM). Moreover, statistical data, data collected through energy audits and preferences derived from surveys are used to bring modeling closer to a real setting. Additionally, the influence of external conditions (solar radiation and outdoor temperatures) is examined by running simulations under summer and winter generation scenarios. The main contributions of this paper are summarized as follows: • The modeling of a LEC exploiting how goal-based residential agents (prosumagers and consumers) with different energy utilization profiles, goals and preferences influence the overall LEC operation; • The combination of MAS and optimization methods to model and optimize the available local energy resources at the agent and the community levels; • The evaluation of the community self-sufficiency depending on different shares of prosumager agents and the economic benefits for the agents in each scenario.
The remainder of the paper is organized as follows: In Section 1, the concept and the main dimensions associated with LEC are described, introducing the motivation of the work. In this section, the modeling perspective is presented, and the research contributions are highlighted. The methodology is thoroughly described in Section 2 and the main results are presented and discussed in Section 3. The paper concludes by presenting the main conclusions and proposing avenues for future research.

MAS Implementation
A general overview of the proposed MAS framework is displayed in Error! Reference source not found., in which energy and information flows are highlighted. Residential agents (consumers and prosumagers) are assumed to co-exist in the same collective environment. The main difference between these agents is the existence of local generation, owned by prosumagers unlike consumers. It is assumed that all agents own storage systems. A shared PV power plant coupled to a static battery owned and managed by the LEC, provides extra energy resources to community members. These resources are managed and distributed by a coordinator agent, which is also responsible for interfacing with external energy retailers to supply the remaining energy needed or to buy the surplus generation the LEC demand cannot absorb. In this model, retailers are passive agents as they do not make any action or decision. All the residential agents are expected to be physically linked to the same low-voltage distribution network and vir-tually linked to the coordinator agent, with whom only information is exchanged. Also, all the community members are assumed as being willing to cooperate to reach a collective goal-the maximization of community self-sufficiency-at the same time as they pursue their own individual goals according to their preferences. Residential agents are considered as living in smart homes, in which appliances are controlled by automated home energy management systems (AHEMS) running the optimization processes. After sensing the environmental conditions and receiving price signals from the retailer, each residential AHEMS must decide: (1) the operation of shiftable loads according to users' preferred time periods; (2) the temperature settings of thermostatically controlled loads (TCL) within thermal comfort requirements; and (3) when to store, sell or procure electricity and how to use the energy stored. The overall goal of these decisions is to minimize the agents' energy costs accounting for the possible discomfort these actions may create (e.g., for operating some loads outside the most preferred time slots to make the most of cheaper prices). The outcomes of the agents' optimization processes allow them to know how much of their demand can be supplied by self-consumption, how much energy they still need to procure and how much surplus generation they are expected to sell. The coordinator agent receives this information and manages the collective energy resources to minimize the overall community costs.

Agents Creation and Smart Homes Modeling
To create a model close to a realistic setup, agents representing different household sizes were included. According to the last Portuguese population statistics, 37.5% of the total households have one or two persons; 41.1% are households composed by a couple with one child and the remaining ones are couples with two or more children [54]. Thus, three residential agent types were created representing households having one to two persons, three persons and four or more persons. To maintain modeling coherence, different assumptions were considered regarding loads utilization as a function of the household size. Further detail is presented in Section 2.4.
The agents' energy demand include manageable loads (DSM actions may be implemented by the AHEMS that has the ability to control the equipment) and non-manageable baseloads, which include the appliances whose utilization is less flexible as is the case of kitchen equipment, lighting and entertainment (including desktops, laptops, television, etc.) [55]. In turn, within the range of manageable loads, the following appliances are comprised: • time shiftable loads, including laundry machines (LM), tumble dryers (TD) and dishwashers (DW); • TCL, as air conditioners (AC), electric water heaters (EWH) and fridges; and • interruptible loads, as electric vehicles (EV) and static batteries.
To model shiftable loads, data collected through energy audits were used to reproduce their power profiles. As these loads are characterized by continuous operation cycles, the algorithmic approach must ensure that the LM, DW and TD can fully operate within the planning period. The behavior of TCL is reproduced by PBM, which are used to compute indoor temperatures and power profiles for each interval of the planning period. For the AC, the PBM presented in [56] is used to compute, at each interval, the total heat loads in the buildings, considering heat transfer through the walls and windows, the internal heat gains and the heat losses due to indoor air renewal, according to the following expression: where , representing the internal heat gains (Hi) and heat gains through walls and windows (Hw). Typical building constructive solutions, dwelling sizes and occupation profiles were considered in dynamic building simulations performed to compute heat transfer (gains and losses) (more detail is presented in Section 2.4 and Appendix C). When the AC is operating in the cooling mode, y(t) is calculated as: where TH(t): maximum reference temperature of the thermostat at time t [°C] and TL(t); minimum reference temperature of the thermostat at time t [°C]. Similarly, the PBM presented in [57,58] is used to reproduce the EWH operation, considering the heat losses in the water container and the energy available to heat the water during a given time interval. The heat losses in the reservoir are computed as: where . In turn, the thermal energy transferred by the EWH to the water is given by: where Q(t): existing energy to heat the water at time t [Wh]; PR: power of the heating resistance of the EWH [W], ∆t: elemental time interval [s] and 3600 is the required conversion factor to maintain the units coherence (seconds to hour). The water temperature in the reservoir is calculated as: where ( ) is the binary variable representing whether the EWH is operating at time t, being defined as: where The operation of cold appliances, as fridges, was modeled by a simplified PBM based on the works of [59,60]. The temperature progress inside the fridge is computed as: where where TL(t) and TH(t) are the minimum and maximum reference temperatures of the fridge at time t [°C]. Lastly, all the agents are assumed to own static batteries and EV. These systems are used for different purposes depending on the agents: prosumagers use them to store self-produced energy, whereas consumers store energy they buy from the grid when the price is lower to operate loads or to sell and obtain benefits. At each interval of the planning period, static and EV batteries are associated with a particular state-of-charge (SoC) and each user define a minimum SoC value that cannot be disregarded. Initial SoC, charging power, minimum final and ideal final SoC should be defined. Also, the EV is assumed to work in grid-to-vehicle (G2V) mode, receiving electricity from the grid or the self-generation system and storing it, and in vehicle-to-grid (V2G) mode, providing the stored electricity to be self-consumed or sold (to the grid) whenever it is advantageous for the agent.

Optimization Framework
As explained before, the core objective of residential agents is energy cost minimization (objective 1), as this is one of the main incentives for end-users to be engaged in a collective project, alongside with energy self-sufficiency, environmental benefits, etc. [15]. AHEMS schedule DR actions to reduce demand during the periods of higher tariff prices. These actions may disturb typical energy usage practices, which may be perceived by end-users as discomfort. Therefore, discomfort minimization is also assumed as an objective function for this problem (objective 2). The bi-objective optimization problem enables to find compromise solutions as minimizing costs usually requires worsening the comfort standards and maintaining energy comfort levels imply worst cost solutions. Energy end-users display different sensitivity levels regarding price. End-users may be cost-driven, if they aim to minimize energy costs even by sacrificing their comfort standards. For these agents, better cost solutions are selected in the Pareto front at the expense of worst dissatisfaction results. In turn, other end-users may privilege comfort standards even by incurring in higher costs (comfort-driven). In this case, better dissatisfaction solutions are chosen in the Pareto front. Others may not have clear preferences about what to prioritize and seek more balanced solutions between cost and comfort. These three profiles are assigned to residential agents in equal shares. Thus, each residential agent is treated independently since each one has an individual consumption profile, preferences and goals to achieve.
The formulation of the agents' energy management problem is presented in Appendix A. The parameters were obtained from several sources, including the PBM described in the previous section. The time slot and temperature variation penalties have been established according to the previous experience of the authors in energy audits and energy efficiency studies.
The cost minimization objective function (Appendix A, Equation (A1)) has two components: (1) the cost of the electricity required by the loads minus self-consumption, and (2) the expected income from selling surplus generation. When they perform their optimization, residential agents do not know if they will use resources from the collective system or not, so the optimization assumes they will procure and sell their energy to the retailer. An eight-tiered time-of-use (ToU) tariff is announced by the retailer 24 h in advance and is perceived by agents as the grid buying price (BPt). In turn, the surplus selling is remunerated by a feed-in-tariff (FiT), whose price was set as 80% of the ToU tariff.
The dissatisfaction objective function (Appendix A, Equation (A2)) has three components, whose contribution is normalized through different scaling factors. For shiftable loads, users may define preferred time slots for their operation based on the convenience to do related tasks (such as clothes hanging). The time shift between the most convenient time slots defined by users and the periods determined by the AHEMS to operate such loads are penalized according to the time slot penalties (TSPjt) displayed in Figure  A1 in Appendix B. Data collected through surveys and interviews was used to define users' preferences [61].
For each TCL, temperature ranges are defined to express the users' thermal comfort levels. Variations between these bounds and the effective temperature computed by PBM are also accounted as discomfort. For the AC and cold appliances, the temperature variation penalty (TVPbt) is computed as: For the EWH, the TVPbt is computed as: where : temperature variation penalty for TCL b at time interval t; Tbt: temperature of TCL b at time interval t determined by the PBM (°C); HRTb: maximum reference temperature for TCL b (°C) and LRTb: minimum reference temperature for TCL b (°C). For these loads, temperature deviations have different impacts on the users' discomfort. While in the case of the AC and the fridge, going beyond the reference bounds means thermal discomfort and disturbance of the food refrigeration service, for the water heating, the user comfort is only disturbed when the water temperature does not reach the lowest temperature specified. Thus, no penalty is considered whenever the water is heated above the minimum reference temperature.
Lastly, the differences between the SoC of EV and static batteries at the end of the optimization and the ideal SoC defined by users are also considered in the dissatisfaction measurement.
The problem is constrained by the physical characteristics of the loads, which also influence the algorithm design. Shiftable loads are distinguished by continuous operation cycles. Thus, the algorithmic implementation must ensure that the operation cycles can be fully finalized within the planning period (Appendix A, Equations (A10)-(A12)). The modeling of TCL considering the upper and lower temperature bounds, which define the users' admissible comfort temperature ranges, is exploited in Appendix A, Equation (A13). Lastly, for the static and EV batteries, the AHEMS chooses a state, for each interval of the planning period, among four possible ones: self-consumption and selling surplus; selling electricity (generated locally or withdrawn from grid); idle; charging from the grid. These states are influenced by the battery capacity, as well as by the minimum SoC defined by users, according to Appendix A, Equations (A14)-(A17). The remaining constraints model the utilization of local resources and self-consumption (Appendix A, Equations (A3)-(A9)). In this model, self-consumption (Equation (A9)) may result directly from the local generation system and the electricity stored in the EV and static batteries which, in turn, may come from the local generation or the power grid.

Algorithmic Approach
The strong combinatorial nature of the mathematical models used to reproduce the operation of some loads (especially the TCL) imposes a high computational burden for the solvers; therefore, other approaches have been used to compute near-optimal solution in an acceptable timeframe [62]. Customized metaheuristics, namely GA, in which solutions converge towards a non-dominated (Pareto optimal) front where solutions of interest are located have produced sound results [57]. GA are probabilistic search and optimization methods based on the progress of a population of solutions through selection, crossover and mutation operators, which gradually converge to regions of the search space where high quality solutions for the problem are found [63]. To deal with the bi-objective problem, a Non-Dominated Sorting Genetic Algorithm (NSGA-II), based on [57,58,64], and tailored to the physical features of the problem, has been proposed. The NSGA-II is an elitist multi-objective optimization algorithm (MOOA) in which offspring populations are generated using crossover and mutation operators, and the evolving generations are selected according to non-dominated sorting and crowding distance [65].
The proposed NSGA-II flowchart is fully displayed in [66] and its operation is summarized as follows. Data on energy retail prices, weather conditions, baseload demand, preferences (favorite periods for the operation of shiftable loads, comfort temperatures for TCL and the anticipated SoC for static and EV batteries) and the algorithm parameters (population size, number of generations and probabilities of crossover and mutation operators) are used to define the agent's load profile and initiate the optimiza-tion process. Firstly, an initial parent population is randomly created considering the load features. The selection operator chooses the next generation parents through a binary tournament scheme. PBM are used at this level to obtain the temperature and power profiles for TCL, the operation of shiftable loads is reproduced by using power profiles collected from energy audits and the power profile and the SoC of storage systems are calculated according to a generic charging/discharging model displayed in [67]. Considering the optimization objectives, the solution encoding must translate the dynamics of each load. Thus, for shiftable loads, the initial operation instant within the planning period is what matters the most, while for TCL the power required at each instant relies on the operative and the desired temperatures; thus, these loads are denoted by the maximum allowable temperature in each interval. A priority rule of the LM over the TD has been set, meaning that the LM is only initialized when the TD completes its operation cycle within the remaining planning period. In each interval, the four possible operation states of the storage systems (EV and static batteries) are encoded. For these loads, the operators ensure that the minimum SoC defined by users is not disregarded and EV are only charged within a predefined convenience time.
Then, for each solution, the fitness (the fitness of a solution translates how close the given solution is to the optimum solution of the desired problem) in the initial population is appraised concerning both objective functions and, based on the Pareto dominance, a ranking is assigned. The selection operator chooses the parents based on a binary tournament, and tailored crossover and mutation operators aimed at balancing search intensification vs. diversification are used to generate the offspring population. The mutation operators: • swap the starting minute of the operation cycle of shiftable loads according to a given deviation bound; • change the maximum temperature of TCL within a specified deviation limit (the minimum temperature is also influenced since the difference between maximum and minimum temperatures is assumed to be constant); • randomly select an operation state for static and EV batteries among the four possible ones.
In turn, the crossover operators: • swap the shiftable loads starting minute between two solutions; • change the maximum temperatures of TCL of both parent solutions; • change the parent solutions operation states of static and EV batteries.
Then, parent and offspring populations are pooled, duplicating the size of the population, and the fitness of the offspring solutions for both objective functions is evaluated. A non-dominated sorting scheme is used to identify the non-dominated front while the crowding operator sets a crowding distance to solutions in the same front, thus when two solutions have the same rank in the non-dominated sorting, the one with the larger crowding distance is selected to enhance the search capability through diversity) [65]. The process is repeated until the stopping condition (number of generations) is reached. Lastly, the Pareto front is located, and the final solution is selected according to the agent's sensitivity to cost.

Coordinator Agent
As residential agents, the coordinator is a goal-based agent aiming to manage and distribute the energy resources generated and stored in the collective energy assets (PV power park and battery), while interfacing with the retailer agent to buy and sell the remaining energy. These decisions are taken in an optimization environment aiming to minimize the overall community costs, which implies to minimize the overall power withdrawn from the grid (therefore, maximizing the community's self-sufficiency) and to maximize surplus selling. To reach this goal, the GA implemented in the coordinator agent architecture solves a single-objective cost minimization problem, similar to the residential agents' one.
As the remaining agents, the coordinator agent perceives the changes in the weather conditions and prices and uses these data to make decisions. This agent also obtains the power profiles resulting from residential agents optimization. These data are used by the algorithm implemented in the agent architecture (Error! Reference source not found.) which must decide when to store, sell or buy electricity to the retailer, how to use the energy generated and stored by the collective assets and what to do with the energy surplus from residential agents (sell to the retailer vs. store it in the collective system) according to external energy price signals received from the retailer agent. The algorithm considers the problem characteristics and use customized GA operators to guarantee that the model restrictions regarding the minimum SoC of the community battery are fulfilled during the planning period. The objective function of this problem was adjusted from Appendix A, Equation (A1) and the problem constraints (Appendix A, Equations (3)- (11) and Equation (A17)) were also adapted. In this case, the total power includes the amount of the power demanded by all residential agents, the power self-consumed and the total power injected/sold (into the grid or into the community) taking into consideration the collective PV generation and storage resources plus the individual PV generation surplus and storage. Also, some assumptions were made regarding prices. The energy distributed by the co-ordinator agent provided by the collective assets is perceived by residential agents as self-consumption, since it is assumed that these assets were financed by them. However, the use of the distribution network within the community (between the collective system and the members) has to be paid, since LEC must be charged for the use of the public infrastructure they use [11]. Thus, for each energy transaction made in the community and as, according to the model assumptions, only the low voltage distribution network is used, a Use-of-Low-Voltage-Distribution-Network tariff is charged. This tariff represents 17% of the final tariff charged to low-voltage end-users in Portugal in 2020 [68]. In the model, this means that when the coordinator agent distributes energy from the collective system to an agent, it has to pay a network use fee set as 17% of the ToU retail tariff.
If the available community resources are not sufficient to fulfill the required energy demand or if energy surplus still exists, the coordinator agent addresses the (passive) retailer agent to manage these transactions. All the technical requirements are assumed to be guaranteed (voltage, frequency, harmonic distortions, etc.) and the contracts established between the community and the retailer ensure that the latter is always available to supply the energy deficit and the purchase of surpluses. Finally, the coordinator informs the agents about how their final costs, considering the community resources they will receive, and the energy withdrawn from grid.

Scenario Description and Case Study
All the scenarios considered include a total of 100 residential agents. However, to assess how different shares of prosumagers influence the community self-sufficiency, five scenarios were exploited: 1) Scenario A: consumers only; 2) Scenario B: 25% are prosumagers and the remaining ones are consumers; 3) Scenario C: 50% are prosumagers and the remaining ones are consumers; 4) Scenario D: 75% are prosumagers and the remaining ones are consumers; 5) Scenario E: prosumagers only.
Optimizations are done for the day ahead with 1-min discretization; therefore, for each planning period, T = 1440 intervals and ∆t is given by 1/60 h. Simulations are run for seven days (one week) to better represent residential dynamics, covering weekdays (day 1 to 5) and weekends (last two simulation days) for summer (August) and winter (January) seasons. From a modeling point of view, running the simulations for several days requires initializing the optimization processes several times and, to keep modeling consistency, information between simulation days must be adjusted, namely regarding: 1) the SoC of batteries, since in the first simulation day, the initial SoC is an input but in the first interval of the second simulation day, the SoC must be coherent with the value in the last interval of the previous day and so on, and 2) the indoor temperatures of the AC and in the EWH water reservoir must also be attuned following the same reasoning.
Temperature and solar radiation profiles for the location of Coimbra, in the central region of Portugal, were used and retrieved from the Photovoltaic Geographical Information System (Error! Reference source not found.). The collective PV power plant is assumed to have an installed capacity of 175 kWpeak and is coupled to a static storage system with a capacity of 210 kWh, imitating a Tesla Powerpack system and modeled according to the manufacturer's specifications. In turn, all prosumagers were assumed to own a 10 kWpeak PV system, as according to a definition of the International Energy Agency (IEA) this should be the maximum installed capacity for residential prosumers [69]. Also, all the agents own static batteries with a 6.4 kWh capacity, e.g., a Tesla Powerwall. To determine heating and cooling needs, the thermal performance of residential dwellings with different sizes was simulated for both seasons. Three different dwelling sizes were considered: the smallest (T1) represents an area of 119 m 2 ; the medium one (T2) occupies 147 m 2 , and the largest (T3) has 182 m 2 (please see Figure A2 in Appendix C). The assignment of the dwelling sizes to the agents representing the different household sizes is displayed in Table 2. Assignment of household sizes and dwellingsError! Reference source not found.. Two different constructive solutions were included in the dynamic building simulations, representing the standard values enforced by the Portuguese Regulation Law 40/90 (solution 1) [70] and 379-A/2013 (solution 2) [71]. These values allowed to compute the heat losses and gains through the building envelope, the losses from the air renewal due to indoor ventilation, and the internal and solar gains for the different housing typologies to be included in the AC system modeling. Taking into account the Portuguese buildings statistics [72], 15% of the dwellings in the community were assigned the constructive solution 1 and 85% the constructive solution 2. The same fixed speed compressor (nominal power of 800 W) non-inverter AC system was considered for all the agents, as well as the same type of fridge (90 W). This type of AC heats or cools the spaces to the target (comfort) temperature. The hot water consumption was adjusted depending on the household dimension, and different water reservoir capacities and EWH nominal powers were assumed. For the smaller (1-2 persons) and medium (3 persons) household sizes, a water tank capacity of 100 liters and an EWH power of 1500 W were considered, while for the larger (4 or more people) a 150-L water tank and a 1550 W EWH were assumed. Also, Error! Reference source not found. displays the users' temperature conditions for all the TCL. Data retrieved from energy audits was used to reproduce the operation of shiftable loads and the number of weekly operation cycles of each one of these loads was adjusted according to the household size, based on [73,74] (Error! Reference source not found.). The baseload profiles were also created based on data collected from energy audits. For static storage systems, batteries with a capacity of 6.4 kWh and a charging capacity of 2 kW were used to comply with the manufacturer's specifications. For EV, one of the most common models in the market was reproduced (40 kWh battery capacity and a charging power of 6.6 kW). For the static battery, the minimum SoC was defined as 20% whereas for the EV this value was set at 26% (based on the results obtained in [75]). A minimum final (at the departure time) SoC was set to 75%, while the ideal SoC at the end of the charging process was defined as the full charge (100%), for EV and static batteries. Finally, EV charging operations were restricted to an availability period, representing the time interval between arriving home at the end of the day and leaving the next day. EV charging may start after 8 p.m. and be completed before 9 a.m. The data required to fully replicate the model can be found in the Appendices and in the Supplementary Material. The parameters displayed in Error! Reference source not found. were considered for the algorithms implemented in both residential agents and coordinator agent architectures. The model was implemented in the Eclipse Java Integrated Development Environment and the object-oriented Anylogic modeling software was used to display dynamic results.

Residential agents Optimization Results
The aggregated power profiles of all residential agents, in each scenario and season, are shown in Error! Reference source not found.. These profiles are sent to the coordinator agent and highlight the periods of demand and availability for sale. Similar patterns of demand are found across scenarios. Demand peaks are concentrated in the lower ToU price periods, corresponding to a night/dawn time, since the AHEMS, aiming to minimize costs, allocate the operation of shiftable loads, the charging of EV batteries, and part of the EWH operation to these periods. Energy can be bought in these periods for charging the static battery as well, since it can be economically beneficial to buy energy to store at a low price and consume it later to avoid high price periods. The greater or lesser presence of consumers vs. prosumagers does not change the demand patterns considerably. This occurs due to the nature of the loads considered and particularly to the agents' objective functions. Minimizing costs may not necessarily mean to maximize self-consumption. It may be advantageous to sell self-produced energy in periods of higher prices and to procure energy from the grid during lower price periods, even at the expense of some discomfort. Indeed, the periods of solar availability coincide with the higher tariff price periods. Thus, in these intervals, AHEMS avoid buying energy from the retailer to satisfy the baseload and use the existing local resources for self-consumption (local generation and storage). As these loads do not demand much energy, the AHEMS makes a share of these resources available for sale to increase the income of agents, which in turn affects their objective of minimizing costs.
Between seasons, slight demand variations are seen between scenarios, with a small demand reduction in the summer because of the increased availability of local resources allowing to enhance self-consumption. Additionally, in summer, the variation in outdoor temperatures leads to less energy being spent in space heating/cooling or water heating processes, which also slightly reduces energy demand. This amount of surplus generation in summer that is available for sale is higher than the corresponding one in winter. As expected, as more prosumagers are added to the community, more energy is made available for sale. In Scenario A, these resources include only the energy stored in consumers' static batteries, procured during lower ToU price periods and offered for sale to maximize benefits, while in the remaining scenarios the surplus from local generation is also included.  When the above results are broken down by agent, they unveil that the demand peaks, which derive from the sum of the individual demand profiles, depend mainly on the characteristics of the agents, the size of the household and their loads. The concentration of EV charging, to which the non-manageable baseload is added, the shiftable loads and the EWH operation results in individual peaks, which cause the considerable aggregated demand peaks in Error! Reference source not found.. Error! Reference source not found. displays the results for a balanced agent in the most extreme scenarios (A and E) in the first simulation day of the winter season. The trends regarding self-consumption and power available to sell are clearly presented. As expected, in Scenario A only the stored energy is used for self-consumption and sale, while in Scenario E more resources are available for those purposes. Therefore, Scenario E is expected to be the one that brings the best economic results to community members.

Community Operation
The overall community performance derives from the decisions made by the coordinator agent optimization processes. The results of the different scenarios, for both seasons, are displayed in Figure A3 in Appendix D where the aggregated power requested, power to sell and self-consumption are shown. Results are very similar to those shown in Error! Reference source not found.. The community demand is concentrated in periods of the tariff lower price, to where the individual optimization processes allocate most of the load operation. These demand peaks correspond to periods with no renewable generation; thus, the coordinator agent allocates part of the energy stored in the collective storage system to smooth these peaks. Although these reductions are graphically undetectable, the examination of the results reveals that, in the summer, the distribution of community resources reduces the average demand for residential agents by about 15.5%, while in the winter the reduction is around 12.2%. These results may seem little impressive since much of the community's energy is sold while it could be allocated to residential agents. However, considering the way the cost minimization objective function is defined, it is more advantageous to let community members to buy energy from the retailer during the low-price periods (night/dawn) and distribute the collective energy resources during the higher price periods (during the day). This resource allocation is reflected in the predominance of self-consumed power during these high price periods. As the cost minimization objective function also covers sales, the coordinator's algorithm decides on the sale of energy (from collective assets and surplus from residential agents) during periods of high prices (daytime).

Energy Self-Sufficiency across Scenarios
Since in this model, self-consumption also involves energy withdrawn from the grid to be stored in EV and static batteries, the system energy self-sufficiency cannot be calculated directly. Instead, a generation/demand ratio is used and understood as a proxy for self-sufficiency, examining the relationship between local generation (including the power sold to the grid and used for self-consumption) and demand (net power requested plus self-consumption), as displayed in Equation (12): The average self-sufficiency for each simulation week in each scenario and season is shown in Error! Reference source not found.. As expected, the greater participation of prosumagers in the community model is translated into more energy available for sale and self-consumption. Thus, there is an improvement in self-sufficiency from scenarios with only consumers (Scenario A) to scenarios with more prosumagers (Scenario E). At the same time, the energy consumed is kept approximately constant, since the demand (translated by the power requested) of consumers and prosumagers is similar. The only source of fluctuation in the generated energy is due to self-consumption, which gradually increases with the incorporation of prosumagers in the LEC. These results must be interpreted at the light of the definition of energy self-sufficiency presented above and the relationship stated in Equation (12). Therefore, a self-sufficiency above 100% does not necessarily mean that the LEC could be completely autonomous from the power grid since only the total amounts of locally generated and consumed energy are evaluated and not how the energy is being effectively used (sold, exchanged, wasted, etc.). An energy balance analysis that considers the relationship between the energy generated and consumed over the simulation time, considering losses and inefficiencies, would be essential to accurately determine the energy autonomy of this community.
As mentioned previously, the objective functions of both residential agents and the coordinator were defined based on one of the main motivations pointed out in the literature for end-users to participate in LEC projects-minimizing the energy bill. Since participants are supposed to finance generation and storage assets (individual and/or collective), it seems reasonable that they want to be economically compensated for their investment. As it can be seen from the results, these objectives are not directly aligned with the maximization of community self-sufficiency. On the one hand, reducing costs requires minimizing the power purchased from external entities, which actually happens, although with little expression. On the other hand, by including the selling component in the cost minimization objective function and with prices varying over time, the economic benefit from selling easily exceeds the demand reduction component. Thus, future work may favor the maximization of self-consumption at the level of the coordinator agent without the sale of surpluses.

Economic Benefits
Depending on the agents' sensitivity to cost, the individual optimization processes compute diversified solutions in the non-dominated front, i.e., with lower cost, lower dissatisfaction, or balanced solutions. The average costs obtained for each agent profile, in each scenario and season, are presented in Error! Reference source not found. and the dissatisfaction results are displayed in Error! Reference source not found.. Average weekly costs and standard deviations of cost and dissatisfaction are also provided to facilitate the comparison of results. Results are displayed as heatmaps to facilitate the understanding and focus on the most relevant trends.
As expected, lower cost solutions are assigned to cost-driven agents; lower dissatisfaction solutions are associated with comfort-driven agents and intermediate solutions are allocated to balanced agents. When residential agents perform their individual optimizations, they are not aware whether they will receive collective energy or not. Therefore, in addition to their own energy resources, they can only use energy purchased from and sold to the retailer. In scenario A, all solutions are translated into costs (shown with a negative sign) since as all the agents are consumers, the whole power needed to supply loads is procured from the grid, representing costs for the agents. For this reason, this is the scenario where the worst cost solutions for the residential agents are verified. Due to the lower availability of renewable generation in winter, also in scenarios B, C, D and E, solutions lead to costs that agents must bear. In summer, the situation is different. In scenarios B and C, cost-driven agents can reach benefits, whereas balanced and comfort-driven agents still have high costs, because of their lower comfort flexibility. In these scenarios, the amount of self-generated surplus made available for sale is still not enough for comfort-sensitive agents to enjoy benefits. In Scenario D, all the agents can achieve benefits due to a greater share of energy made available for sale. Though, scenario E is the one displaying best cost results as most agents have benefits from the sale of self-generated energy.
Regarding the comfort assessment, a closer look reveals very high values of dissatisfaction (quantified through a constructed indicator) in summer in the two last simulation days for cost-driven agents in all scenarios. These values are originated from the higher outdoor temperatures considered in the simulation of the AC operation (average temperatures of 26.4 °C, compared to a mean value of 21.5 °C in the remaining days). As the same AC equipment was considered to cope with the temperature variation and different dwelling areas were assumed, the system may not be able to keep the cooling needs within the desired limits, which gives rise to high levels of discomfort. After the coordinator agent optimization, the average economic benefits for residential agents are revealed. The differences between the average costs calculated initially by the agents and after the distribution of collective resources by the coordinator agent show that, in all scenarios and seasons, there are benefits in belonging to a community with these characteristics. In addition to the demand reduction due to the distribution of collective energy, residential agents also benefit from the distribution of the benefits obtained from the sale of collective surpluses to the retailer. These benefits are equally shared among the residential agents and are included in the final cost estimates, which allows to reduce the agents' average costs or increase their expected benefits as shown in Error! Reference source not found.. The comfort-driven agents are the ones who benefit the most from being in the community, as they are also the ones that consume more energy and are charged with higher costs to maintain their comfort standards.

Conclusions
In recent years, energy communities and collective self-consumption projects have emerged and gained relevance both in the scientific literature and at the political level, with a prominent place in the current regulatory landscape. In this setting, modeling approaches that allow for the simulation of technical, organizational, behavioral and social dynamics, underlying a optimizing rationality, are needed to assess the performance of different community configurations before putting them into practice. This work presents a MAS framework developed to reproduce the operation of a LEC formed by residential agents (consumers and prosumagers) willing to engage into DR actions to minimize energy costs while considering comfort requirements. By including optimization processes at two levels (residential agents and coordinator), the results show the optimal management of collective resources, according to the objectives defined for the LEC.
Each residential agent is analyzed individually as it represents an individual household with its own goals regarding cost and discomfort minimization and preferences regarding energy utilization. Depending on agents' goals, the benefits of belonging to the community can vary considerably. Comfort-driven agents proved to be the most benefited economically, especially in scenarios with the highest energy surplus available since the sharing of collective resources allows them to maintain their comfort standards at a lower cost. As this result emerged as relevant in the scope of this work, future approaches should exploit different ways of sharing costs and revenues. This work considered that local collective resources are managed by a centralized agent which distribute them between agents, not considering their individual contribution in terms of demand and local injection. To ensure greater equity in the allocation of collective resources, prioritization rules can be further defined to penalize agents who consume more and provide fewer surplus resources.
Different settings were studied to assess to what extent agents playing the role of consumers and prosumagers influence the overall system self-sufficiency. As expected, the greater presence of prosumagers enhances the community self-sufficiency, as more energy resources are available. However, a detailed analysis of the energy balance over time is necessary to assess the real energy self-sufficiency of the community. In addition, future research should further exploit how different goals can be encompassed in LEC, since the participants' individual goals may not be directly aligned with the general goal of energy self-sufficiency. As demonstrated in this work, although cost minimization goals are usually the most relevant for residential users, they may conflict with self-sufficiency ambitions since, depending on the price schemes, selling self-generated energy may be economically more advantageous than self-consuming. Therefore, the regulatory framework of LEC and the energy markets must be able to prevent situations that promote injections into the grid for profit purposes at the expense of self-consumption, according to the non-profit definitions of CEC and REC. In fact, this issue is quite relevant in the current context, since the number of small on-site distributed energy generation initiatives is increasing with possible negative consequences for power systems. Therefore, it is important assessing how far apart the individual objectives of community members are from the collective self-sufficiency goals, how LEC can be penalized for not managing efficiently their energy resources and the extent to which these inefficiencies can disturb the operation of power systems.
A limitation of this work is related to the validation of the results and conclusions drawn. Despite the valuable insights this work unveils, it presents a conceptual LEC model, supported by a set of assumptions informed by the existing literature on energy communities and the authors experience on smart grids as well as behavioral and tech-nical demand response research and practice. As the LEC that this work aims to model has not been materialized, there is no real basis for comparison that allows to discuss how far the model's outcomes are to represent the behavior of the target system (correctness) or to what extent the conceptual framework of the model and the target system match (consistency) [76]. Therefore, results must be understood as experimental and exploratory.
In future approaches, data from real LEC can be used to strengthen modeling. Also, in addition to residential agents, LEC may also include non-residential agents (including services, industry and cross-sectoral activities) and their flexibility profiles exploited. Additionally, a power cost component can be introduced in the modeling to exploit its effect on the minimization of the power procured to the grid and load scheduling.

Data Availability Statement:
The data presented in this study are openly available in Mendeley Data at DOI:10.17632/7g762sxszh.1, reference number [77].

Conflicts of Interest:
The authors declare no conflict of interest. Table A1. Parameters and decision-variables of the optimization problem.

Parameters
Decision variables

TSPjt
Time slot penalty for shiftable load j at interval t (presented in Fig.B1).
Power from the grid used to supply the EV battery e at interval t [kW].

TVPbt
Temperature variation penalty for TCL b at interval t (as defined in Equations (10) and (11)).  Temperature of the space being heated/cooled by the TCL b at interval t (as defined in Equation (1)