Advanced Clustering Approach for Peer-to-Peer Local Energy Markets Considering Prosumers’ Preference Vectors

Local energy markets (LEMs) are utilized in a bottom-up power systems approach for reducing the complexity of the traditional, centralized power system and to enable better integration of decentralized renewable energy resources (RES). Peer-to-peer (P2P) energy trading creates opportunities for prosumers to trade their RES with other prosumers in the LEM. Although several scenarios were proposed in the literature for modelling P2P energy trading, there is still a gap in the literature considering the heterogeneous characteristics of prosumers’ bidding preferences during P2P matching in the LEM. In this paper, we present heterogeneous characteristics of bidding preferences for prosumers considering energy quantity, bid/offer price, geographic location, location of agents on the local community and cluster welfare. Moreover, this paper proposes an advanced clustering model for P2P matching in the energy community considering the heterogeneous characteristics of bidding preferences for prosumers. For evaluating our proposed model performance, two German real case scenarios of a small and large communities were studied. The simulations results show that using price preference, as the criterion for clustering, offers more technical and economic benefits to energy communities compared to other clustering scenarios. On the other hand, clustering scenarios based on location of prosumers ensure that energy is traded among prosumers who are closer to each other.

P2P energy trading is a transactional structure which is based on prosumers and consumers directly or indirectly negotiating their electricity requirements to exchange electricity among each other [8]. In most countries, the electricity market is based on time series. Therefore, the consumers and prosumers are required to consistently bid/offer and negotiate their trade(s) every time slot to achieve a full P2P trading with direct negotiation between consumers and prosumers. This is time consuming and inefficient. Thus, modelling agents and P2P matching algorithms considering prosumers' energy requirements, choices, preferences and price are developed.
In terms of negotiation, P2P energy trading can be classified into mediated and non-mediated P2P negotiations [9]. In a mediated P2P negotiation, a neutral mediator expedites the mediation of the trade between the consumers and the prosumers [10]. This can be based on a many-to-one P2P negotiation between prosumers with an aggregator [11] or a virtual agent as a mediator [12]. Authors of [13] presented a many-to-one P2P energy trading where the distributions system operator mediates between prosumers and consumers and uses a double auction mechanism to decide the P2P winners of the auction. In a non-mediated or full P2P trading, prosumers and consumers directly negotiate with each other for electricity trading. This can be a oneto-one negotiation between a buyer and seller or many-tomany negotiation between all prosumers and consumers [10]. Ref. [14] proposed a one-to-one full P2P trading model based on bilateral trading coefficients. The authors of [15] proposed a many-to-many P2P energy trading model for local energy and flexibility trading based on decentralized negotiation.
Moreover, P2P energy trading can be classified based on the matching or decision approaches. Different approaches used for P2P energy trading are based on game-theory, algorithms, optimization models and reinforcement learning [7]. Ref. [16] proposed Stackelberg game-based energy sharing framework for P2P energy trading within in a multi-sharing region. Ref. [17] proposed a P2P multi-energy market mechanism for electricity and heat trading based on cooperative behaviors between the peers in an LEM. Authors of [18] in their work proposed a model based on Nash bargaining fair sharing of trading benefits for P2P energy trading in interconnected LEMs. Ref. [19] proposed a two-tier P2P trading model based on dual decomposition and distributed consensus mechanism for a double layer hierarchical LEM.
The authors of [20] proposed a P2P model, based on P2P coordination for energy. In recent research works, reinforcement learning is used for P2P model. Ref. [21] proposed a multi-agent deep reinforcement learning model based on the combination of the multi-agent deep deterministic policy gradient algorithm and technique of parameter sharing for P2P energy trading. Their simulation study show that the proposed model reduced daily electricity cost and demand peak.
Additionally, recent researches in the field of LEM focused on prosumers preference vectors. In order to consider and satisfy LEM participants' willingness, Ref. [22] proposed a new auction-based LEM model that takes into account the participants' preferences and willingness to pay more for a certain energy quality. The model promised to increase the coverage of local demand and local supply compared to the conventional periodic double auction mechanism. Authors of [14] proposed LEM model which provide opportunity for the prosumers to select their preferred trading partners in a P2P LEM platform. The work of Ref. [21] is based on grouping prosumers into different multiple clusters based on the type of their distributed energy resources(DERs). Ref. [23] proposed a two stage preference based merit-order market mechanism for valuation between green, local, and energy source. The model showed that different DERs supply prosumers according to their willingness to pay and market situation.
Clustering as an unsupervised learning algorithm is used in most studies to identify similarities between set of large data and group the large data into sets of smaller data with similar properties. Clustering is recently used in P2P energy trading to identify similarities in different groups of microgrid, and for coordinating trades in LEM [24]. This further results to proper decentralization of energy markets and access to renewable energy resources [24]. Ref. [25] proposed a virtual LEM that clusters prosumers daily based on their load profiles. Ref. [26] in their work proposed a game theoretic approach for clustering microgrids using particle swarm optimization in a P2P energy trading system. The concept of adaptive segmentation for P2P market clearing was proposed by Ref. [27]. The model use balanced k-means clustering algorithm to cluster energy players into different segments to negotiate energy trading separately among themselves.
The concept of clustering has been used mainly in power markets for classifying loads and forecasting the future electricity demands of the users within a cluster [28]. Ref. [29] used the known traditional K-means clustering to cluster electricity customers demand in order to make a forecast of the future customers load demand. Ref. [30] used the K-means and Fuzzy C-means clustering algorithms to model the electricity price time series patterns for forecasting future electricity price. Ref. [31] developed a semi-supervised automatic clustering algorithm based on a self-adapting metric learning process for determining the household electricity consumers demand patterns.
The literature contains several studies proposing different models for LEMs, P2P trading approaches, and prosumers preference vectors for LEM design. However, there is still a gap in literature concerning models considering the heterogeneous characteristics (i. e. preferences) of prosumers within the bids/offers during P2P matching. In this paper, we develop a non-mediated P2P matching model based on advanced clustering model that simultaneously takes into account the geographic location, location on the local community, bid/offer price, bid/offer quantity, and cluster welfare for prosumers in the energy community. The literature [32], [33], [34] contains several studies on clustering and application of clustering in energy systems, however, this is the first of its kind where clusters are built with developed weights instead of the distance or values of the sets. Hence, in this model, the developer can decide on a set of variables within the multi set that is of more importance to him or her and decide to manually give more weight to this variable before the start of clustering. By doing so, the model will perform the clustering while considering that a certain variable is of more importance to the model compared to others and therefore perform according to this instruction. The model was implemented on an interface and open source code-base of the Grid Singularity Exchange to simulate, and optimize energy trading in local communities [35]. Furthermore, we evaluate the model using performance indicators such as self-sufficiency, self-consumption ratio, share of market savings and traded energy quantity. Our model can be used for any multi set functions where the user wishes to cluster multi variables while attributing more importance to some variable compared to others. Thus the application of the developed novel clustering algorithm extends beyond LEM but also to other applications where similar multi sets need to be classified. The main contributions of the paper are summarized in the following: • Proposing an advanced clustering algorithm for grouping of heterogeneous characteristics of bidding preferences for prosumers considering energy quantity, geographic location, location on the community and bid/offer price.
• Presenting novel P2P matching in energy communities based on our proposed advanced clustering algorithm.
• Implementation of the proposed model in a real case German community.
• Assessing the performance of our proposed P2P model based on performance indicators for the LEM. The remaining sections of this work are structured as follows. Section II introduces clustering and the proposed advanced clustering algorithm. The proposed LEM model is described in Section III and sample discussion of the proposed model with example presented in Section IV. The community setup, data and price components are presented in Section V. Section VI discusses the results of our case studies and the findings in details. Finally, the paper is concluded in Section VII.

II. PROPOSED CLUSTERING ALGORITHM A. INTRODUCTION TO CLUSTERING
Clustering is an unsupervised learning algorithm with the objective of extracting underlying information of data samples and using the information to split the data into different groups so-called clusters [36]. Clustering is usually used for analyzing market research, pattern recognition, data analysis, image processing and categorizing genes with similar functionalities [37]. K-means clustering is one of the most popular clustering algorithms used in data mining because of its simplicity and computational efficiency [36], [38]. In K-mean clustering, the Euclidean 1 is used as a means to measure data that are nearby each other and as a means for determining the centroid for group of unlabelled data [38]. Hence, K-means clustering uses a local search to group the data sample by first; randomly selecting k points {µ 1 , . . . , µ k } as the corresponding initial centers for k clusters, then optimizing them iteratively until the objective function (Eq. 3) is minimized [39]. Supposed we have a multi-set of d-dimensional vector, X, then, where p is number of observations and x p is the p-th observation. The objective of K-means clustering is to cluster the p observations into k multi-sets of homogeneous clusters by minimizing the objective function represented by Eq. (3) [39], [40].
Here, the number of clusters should be less than or equal to the number of observations (k ≤ p). Moreover, x j is the j-th observation belonging to s i as i-th cluster. It is noticeable that in K-means clustering algorithm, the number of clusters must be defined initially. Hierarchical clustering is another popular clustering algorithm used recently in data mining. Hierarchical clustering algorithm consist of nested partitions in which homogeneous observations are grouped by recursively clustering only two observations at a time [41]. Unlike K-means clustering algorithm that has fixed number of clusters, the number of clusters in hierarchical clustering changes in every iteration cycle [42]. Hierarchical clustering is classified into agglomerative and divisive clustering algorithms which can simply be explained as bottom-up and topdown approaches of the clustering, respectively [42]. In an agglomerative clustering approach, the algorithm begins with a singleton observation by pairing similar clusters at each iteration. This process is repeated every iteration until all observations are included into a single cluster or a defined criterion is met [41]. On the other hand, in divisive clustering approach, clustering begins with one big cluster obser-vation and breaks down into clusters consisting of smaller observations in a hierarchical top-down manner. Thus, for p number of observations grouped into q sets of clusters, {c 1 , c 2 , . . . , c q }, using hierarchical clustering, it is given that hence, it is either one of clusters i and j is a subset of the other one hierarchically, or a disjoint of it [43].
In hierarchical clustering, different linkage criteria such as single linkage, complete linkage, unweighted pair group method average, the weighted pair group method average, the unweighted pair group method centroid, the weighted pair group method centroid, and the ward linkage are used to measure the distance between two clusters [44]. For the single linkage, the similarity between two clusters depends on the closest pair of members in the two clusters. Therefore, the closeness of the pairs in terms of distance are considered. However, for complete linkage, the similarity between two clusters depends on the farthest pair of members in the two clusters. Therefore, farther pairs are grouped first. For the unweighted pair group method average, the average distance between all individual clusters is calculated and clusters with the least average distance are joined to form the next hierarchy [44], [45]. For a weighted pair group method average, the hierarchical clustering are formed by joining to nearby clusters, the distance between the new formed cluster and any other cluster is the arithmetic mean of the average distances between members of the formed cluster and any other cluster [34]. The unweighted pair group method centroid is similar to unweighted pair group method average, however, for unweighted pair group method centroid, the proximity between two clusters is the proximity between their geometric centroids. Also, the weighted pair group method centroid is similar to weighted pair group method average, however, for weighted pair group method centroid, the proximity between two clusters is the proximity between their geometric centroids [33], [34]. For the wards linkage, two clusters are joined to form a hierarchy once they minimize the increase of sum of square error [34], [44], [45]. For instance in [32], the the unweighted pair average distance and ward linkage methods were used for grouping the clusters. In [33], the single linkage criterio was used. In [34], the single linkage, complete linkage, unweighted pair group method average, the weighted pair group method average, the unweighted pair group method centroid, the weighted pair group method centroid, and the ward linkage were used and compared.

B. ADVANCED CLUSTERING ALGORITHM
In this section, a novel advanced clustering algorithm is proposed. Theoretically, the proposed advance clustering algorithm is derived by combining k-means and agglomerative clustering algorithms. In this way, two different multi-sets, X and W, of d-dimensional vectors each with N and M number of observations, are given in Eqs. (5) and (6), respectively.

1) STAGE I: INITIAL CLUSTERING
First, the sets from (5) and (6) are clustered into K and U number of homogeneous sets, by minimizing (7) and (8), respectively.
Subject to: Subject to: U ≤ M. Here, x y and w g are the y-th and g-th sets from the observations X and W, belonging to the k-th and u-th clusters, c x k and c w u , respectively. Hence, µ k and µ u are the initial cluster points for sets X and W as given in (5) and (6), respectively. Eq. (9) and (10) represent the solutions for (7) and (8), respectively.
where c 1 x to c k x and c 1 w to c u w represent the selected homogeneous clusters for sets X and W, respectively.

2) STAGE II: HIERARCHICAL CLUSTERING BASED ON LINKAGE CRITERIA
The homogeneous clusters from (9) and (10) are further clustered by determining the individual heterogeneous features of the clusters elements and pairing them one after the other, hierarchically. This is achieved by solving the optimization problem given in (11).
where c x k * and c w u * are clusters selected from C x and C w , respectively, as solution of (11). Moreover, ω k,u is the chosen metric which is derived based on the properties of the d-dimensional observation sets X and W. To solve the optimization problem, Eq. (11) is reduced to Eq. (12) to compute the weighted linkage ω * , and (12), and (11) are solved iteratively until (9) and (10) are reduced to a single cluster.
The pseudocode of our proposed advanced clustering is represented in Algorithm 1. The output R 1 , R 2 , . . . R N * is the result of matching two clusters c x k * and c w u * obtained as solution to Eq. (11).

Algorithm 1 Advanced Clustering Algorithm
Require: X ={x 1 , x 2 , . . . , x N }, W ={w 1 , w 2 , . . . , w M }. Output:{R 1 , R 2 , . . . R N * } Solve: Eq. (7) and Eq. (8), to obtain C x ={c x 1 , c x 2 , . . . c x k } and In this Section, our proposed P2P market model is presented. In the first step, each prosumer submits its offers and bids at each time slot to the energy community framework as shown in Fig. 1. Then, prosumers are clustered based on the proposed advanced clustering algorithm. P2P matching among prosumers occurs and the cluster weight matrix is updated. This process is repeated in the next time slot until end of the market as shown in Fig. 2. The motivation of clustering prosumers and consumers in P2P energy trading is to reduce mismatch during P2P matching, create cluster of markets where the members of the clusters have similar features and further reduce imbalance in the local community. By clustering prosumers, we reduce the big local market into smaller groups of markets. The participants in each of these smaller groups have similar features/properties, thereby creating opportunity for more bids and offers to be matched in the group levels which will lead to efficient LEM. Also, by creating clusters of members with the similar load profiles, the market reduce imbalance in the local community and ensure that more energy is matched at local level and that grid instability is reduced. Moreover, by creating clusters with similar bidding/offering strategies, the model ensure that there is no mismatch during the market clearing of bids and offers. Because of the establishment of previous works [6], [46], [47] in this area that P2P market for LEM trading offers economic and technical benefits to the LEM participants compared to merit order clearing mechanism, we use the P2P trading for our model. Also, P2P trading gives opportunity for consumers and prosumers to say how much they are willing to pay/receive per kW of their electricity thereby engaging both producers and consumers in the market which is the major idea of LEM unlike the merit order where only the producers gives their cost price and market is matched based on this. Also, P2P energy trading provides opportunity for more energy to be traded within the local community compared to merit order clearing mechanism.

A. STAGE I: BIDDING AND OFFERING BY PROSUMERS
For the proposed LEM model, the prosumers submit their bids and offers as a set of prosumers' preferences at each time slot to the local electricity market framework. Eq. (13) represents the bid of buyer i at time slot t.
where, b i,t represents the bidding vector of buyer i at time t which contains energy quantity (q b i,t ), bid price (p b i,t ), geographic location (l g i ) of the buyer i in terms of latitude and longitude, and the location (l d i ) of the buyer i in energy community. The location of the buyer, which is a prosumer in the energy community, is the area location consisting of the building block, street, zone and/or district location of the buyer. Hence, while l g i represents the distance location of the buyer, l d i represents the particular community the buyer belongs out of the energy communities located inside the LEM. Similar to Eq. (13), Eq. (14) represents the offer of seller j at time slot t.
where, s j,t represents the offering vector of seller j which contains the energy quantity (q s j,t ), the offer price (p s j,t ), the geographic location (l g j ) of the seller, and the location (l d j ) of the seller on the energy community. Similar to buyers, l g i represents the distance location of the seller, and l d i represents the particular community the seller belongs out of the energy communities located inside the LEM. Thus, the bidding/offering vectors submitted by buyers /sellers to the LEM frame work at time slot t are represented by (15) and (16), respectively.
where B t and S t represent bidding and offering vectors, respectively. Moreover, N and M express number of buyers and sellers, respectively.

B. STAGE II: ADVANCED CLUSTERING OF BIDS AND OFFERS 1) STEP I: INITIAL CLUSTERING
After bidding and offering vectors are submitted to the LEM framework, they are clustered based on the proposed advanced clustering algorithm. This way, similar to K-means clustering algorithm, bidding and offering vectors are defined as multi-sets and are clustered independently based on the defined criterion by minimizing Eqs. (17) and (18), respectively: Subject to: Subject to: U ≤ M.   According to Eqs. (17) and (18), K and U are the number of clusters for the bids and offers, respectively. The number of defined bidding clusters K, must be less than or equal to the total number of bids at time step t. Also, the number of the defined offering clusters U, must be less than or equal to the total numbers of offers at time step t. Besides, b f ,t and s g,t are the f -th bid and g-th offer, belonging to the k-th and u-th clusters, c b k,t and c s u,t , respectively. Moreover, µ k and µ u are the initial cluster points for bids and offers, respectively. The initial cluster points are selected randomly and minimization of Eqs. (17) and (18) is performed iteratively until the optimal cluster points are determined. Thus, for initial k-th cluster of bids and u-th cluster of offers, it is given: where n k is the number of bids belonging to k-th cluster of bids, c b k,t , which have similar homogeneous features based on the clustering criterion at time step t. Similarly, m u is the number of offers belonging to u-th cluster of offers, c s u,t , which have similar homogeneous features based on the clustering criterion at time step t. In our proposed model, different criterion are defined for clustering bids and offers based on the elements of bids and offers submitted by prosumers, as represented by Eqs. (13) and (14), respectively, and cluster welfare. For a buyer i in c b k,t and seller j in c s u,t at time slot t, with bid and offer as represented by Eqs. (13) and (14), respectively, the welfare (π) of the pair is represented in Eq. (21), For pairs with p b i,t < p s j,t , the welfare is not considered since there will not be matching of such pairs even after clustering. The social welfare of the clusters in Eqs. (19) and (20) termed cluster welfare is represented in Eq. (22). Hence, for our model, cluster welfare means social welfare of the clusters and not the social welfare of the local community. Hence, hierarchical clustering with cluster welfare as the chosen metric criterion means clustering pairs with higher π * using the hierarchical clustering algorithm/method in the next step.
Additionally, Eqs. (23) and (24) represent the the set of all initial clusters for bids and offers: where c 1,t b to c k,t b and c 1,t s to c u,t s represent the selected homogeneous initial clusters of bids and offers at time step t resulting from Eqs. (17) and (18), respectively. Eqs. (25) and (26) show that the selected initial clusters in Eqs. (23) and (24) are from the bids and offering vectors of buyers and seller represented in Eqs. (15) and (16), respectively.
2) STEP II: HIERARCHICAL CLUSTERING BASED ON LINKAGE CRITERIA After initial clusters obtained by Eqs. (23) and (24), bids and offers are clustered hierarchically. Thus, initial clusters are inputs for the hierarchical clustering stage and its optimization problem represented by (27): where c b k * ,t and c s u * ,t are clusters selected from C b t and C s t , respectively. Moreover, ω k,u,t is the chosen metric and is defined as the weight for clustering pair of bidding cluster k and offering cluster u into a single cluster at time slot t. Accordingly, is defined as a chosen weight matrix as represented in Eq. (28).
For reducing the computational burden, Eq. (27) can be restated as given in (29) and (30): where ω * t is the linkage criteria and is defined as the maximum value of the weighted linkage from the chosen weight matrix represented in Eq. (28). From (28), the chosen metric criteria, ω k * ,u * ,t , is defined as sum of the average weights of bidding and offering elements including bid and offer prices, energy quantity, location of the prosumers on the local community for bids and offers clusters combination of c b k * ,t and c s u * ,t , respectively, which are given in (31): From (31), ω p k * ,u * ,t is the average price weight for possible matching bidding cluster, c b k * ,t , and offering cluster, c s u * ,t , as represented in the price weight matrix of (32) and given in (38).
where n k * and m u * are number of bids and offers in bidding cluster, c b k * ,t , and offering cluster, c s u * ,t , respectively. Moreover, w p i,j,t is the price weight for matching bid b i,t belonging to k * -th bidding cluster and offer s j,t belonging to u * -th offering cluster at time slot t as represented by (33), From Eq. (33), a bid and an offer pair with equal bidding and offering price has the maximum weight which is 2, because, this pair provide the maximum satisfaction to both prosumers considering their bid and offer preference price. This pair also provide the maximum welfare for the both pairs while considering the bid/offer price of the prosumers. Therefore, giving the maximum price weight to prosumers with the same bid/offer price will ensure that more energy is traded within the community. For a bidding price greater than the offering price, the price weight is 1, because, this bid and offer pair provide less price preference satisfactions to both prosumers compared to the former. For a bidding price less than the offering price, the price weight is −1 because the bid and offer pair does not satisfy the sellers requirements and validation requirements and consequently, this bid and offer pair cannot match. This negative weight reduce the possibility of matching the bid and offer pair with sellers offer price greater than the buyers bid price. Moreover, ω q k * ,u * ,t and ω l k * ,u * represent weights of average energy quantity and location for possible matching of bidding cluster, c b k * ,t , and offering cluster, c s u * ,t , as represented by (34) and (36), respectively.
where w q i,j,t represents the energy quantity weight of a buyer i and seller j belonging to clusters c b k * ,t and c s u * ,t , respectively, as given by (35): VOLUME 11, 2023 From Eq. (35), bid and offer pair with equal bidding and offering energy quantity has the maximum weight which is 2, because, this pair provides the maximum energy quantity preference satisfaction to both prosumers considering their bid and offer energy quantity. Thus, there is high probability that the buyer and seller will trade all their energy requirements with each other without requesting for another prosumer to take care of their energy requirements that is not satisfied by their pair. For pair with bidding energy quantity greater than the offering energy quantity or offering energy quantity greater than the bidding energy quantity, the price weight is 1, because, this bid and offer pair provide less energy quantity preference satisfactions to one of the prosumers unlike the former that provide maximum energy quantity preference satisfaction to both prosumers. Also, the prosumer with higher energy quantity requirements will still be paired in the next iteration meaning that all the energy requirements cannot be satisfied with the pair and hence, the reason for lesser energy quantity weight. Additionally, Eq. (37) represents the location weight (w l i,j ) of a buyer agent i and seller agent j belonging to clusters c b k * ,t and c s u * ,t , respectively.
where l b , l e , l z and l c represent apartment block, estate, zone and local community, respectively. In other words, Eq. (37) expresses that the closer the seller and buyer are on the local community, the higher their location weight. Thus, the average weight for all bidding/offering elements is determined by Eq. (38): where w (.) i,j can be weight for price (p), energy quantity (q) and location (l). In this way, the average weights calculated by Eq. (38) will be the inputs of (31).

3) STEP III: P2P MATCHING OF CLUSTERED BIDS AND OFFERS
After prosumers are clustered in bid and offer clusters separately, P2P matching occur among prosumers which their bids and offers belong to the selected clusters. This way, considering c b k * ,t and c s u * ,t are selected as optimum bidding and offering clusters from Eq. (30) which are represented by Eqs. (39) and (40), respectively.
where c b k * ,t contains n k * number of bids and c s u * ,t contains n u * number of offers at time t. Besides, bid i and offer j are vectors consisting of their corresponding bidding and offering elements as represented by Eqs. (13) and (14). Thus, prosumer i as a potential buyer and prosumer j as a potential seller are eligible to negotiate with each other directly for making P2P transaction because they belong to clusters c b k * ,t and c s u * ,t , respectively. Hence, the matching among prosumers i and j can occur if Eqs. (41) and (42) as P2P validation criteria are satisfied.
where g l i,j,t represents grid fee of P2P energy transaction among prosumers i and j in the local community l where l j and l i exist. In other words, the grid fee is amount of money in cent/kWh which the paired prosumers i and j pay for using the local grid for energy exchange in the energy community l. Eq. (43) represents the community grid fees, where g l b t , g l e t , g l z t , g l c t and g l u t represent grid fees for apartment blocks, estates, zones, local community and the upstream grid, respectively. Each grid fee is the combination of metering fee (g m ), local grid fee (g i,j,t ), upstream grid fee (g u t ) and 19% value-added-tax (VAT) as represented in Eq. (44) If Eqs. (41) and (42) are satisfied among prosumers i and j, the selected pair of bid i and offer j are matched. Thus, q i,j,t and p p2p i,j,t represent matched P2P energy quantity and price between buyer i and seller j at time slot t which are given in (45) and (46), respectively.
33614 VOLUME 11, 2023 Authorized licensed use limited to the terms of the applicable license agreement with IEEE. Restrictions apply.
where p b i,j,t is the bought price for buyer i to trade P2P energy with seller j, and p s i,j,t is the sold price for seller j to trade P2P energy with buyer i at time slot t as represent by Eqs. (47) and (48), respectively. After P2P matching between seller i and buyer j, the bid and offer are updated by subtracting the matched energy quantity from the submitted bid/offer quantity as represented in (49) and (50), respectively.
where q b ′ i,t and q s ′ j,t is the deficit/excess bid and offer energy quantity for buyer i and seller j respectively, after P2P matching between them. In case, the selected bid and offer does not satisfy constraints (41) and (42), another bid and offer pair (b i * ,t and s j * ,t ) are selected according to (51) and (52), Eqs. (51) and (52) imply that bids and offers paired earlier cannot be paired again in the same time slot, t. The proposed matching process from Eqs. (39) to (52) is repeated until all the bids and offers are selected.

4) STEP IV: UPDATING THE CHOSEN WEIGHT MATRIX
After matching bidding and offering clusters c b k * ,t and c s u * ,t , respectively, according to Section III-B3, the chosen weight matrix will be updated. In this way, if all energy quantity of both bid i and offer j are fully matched, the chosen weight matrix is updated as given in Eq. (53) by deleting the corresponding bid's column and offer's row stated in Eq. (28). In other words, Eq. (28) is reduced to Eq. (53) by deleting matched bidding cluster, c b k * ,t , and offering cluster, c s u * ,t , from Eqs. (23) and (24), before solving (27) in the next iteration.
However, if only the offered energy quantity of the cluster c s u * ,t is fully cleared while the bid energy quantity of c b k * ,t is not cleared completely, then Eq. (28) is updated to (54) by deleting the corresponding offer's row stated in Eq. (28).
On the other hand, if only the energy quantity of the bid cluster c b k * ,t is cleared completely, Eq. (28) is updated to Eq. (55) by deleting the corresponding bid's column stated in Eq. (28).
Finally, if there are unmatched energy quantities of bid and offer within both (c b k * ,t and c s u * ,t ) clusters, the size of Eq. (28) remains unchanged. However, matched bids and offers are deleted leaving unmatched ones. Then the chosen weight matrix from Eq. (28) is computed again based on Eqs. (32), (34) and (36), and used to solve Eqs. (29) and (27). The process is repeated until the chosen weight matrix, represented in Eq. (28), forms a single matrix in which the input clusters cannot satisfy the matching criterion described in Eqs. (41) and (42), or all the bids and offers are fully matched. All deficit/excess bids and offers which are not cleared in the local energy market, will be traded with the utility using the upstream-grid price. The pseudocode and flowchart of our proposed advanced clustering approach for a single time slot t is shown in Algorithm 2 and Fig. 3, respectively. From Algorithm 2, R 1 , R 2 , . . . R N * is the output of the algorithm which is the result of matching two clusters c b k * ,t and c s u * ,t based on Eqs. (39) to (52).

IV. PROPOSED P2P MARKET MODEL DISCUSSION
In this Section, our proposed P2P market model is described with a simple example to illustrate how bids and offers are clustered and matched according to prosumers' preferences. VOLUME 11, 2023 Table 1 displays the sample of prosumers' bids and offers containing the prosumers preference vector for a single time step. The preference vector contains the prosumer's identification (ID), bid/offer price, energy quantity, geographic location and location of the prosumer on the energy community. The geographic location is the location of the prosumer in terms of latitude (La) and longitude (Lo). On the other hand, location of the prosumer in the energy community contains the its zone, estate and building block. According to Table 1, there are two zones consisting of zone A (ZA) and zone B (ZB). Moreover, there are three estates namely yellow (YE), ash (AS), and orange (OR), and six building blocks B1 to B6. In this way, as shown in first row of Table 1, ZA-YE-B1 means that the prosumer 1 belong to zone A, yellow estate and B1. Fig. 4 displays the clustering and matching of the sample bids and offers of Table 1 based on the proposed P2P market model using advanced clustering algorithm. The bids and offers are arranged according to the bidding/offering price and energy quantity as shown on the lowest level of Fig. 4. The bids are b 1 to b 5 , and the offers are s 1 to s 4 . In the next level, bids and offers are clustered based on the initial clustering algorithm described in Section III-B1 with cluster welfare as the clustering criterion. The number of clusters for bids and offers are set as two for each of them. The initial clusters is shown on the second level from the bottom layer of Fig. 4. For the bids, the initial clusters are represented by c b 1 and c b 2 , while the initial clusters of offers are represented by c s 1 and c s 2 . Afterward, first two optimal clusters are selected from the four initial clusters by solving Eq. (27). In this sample, the first selected optimal clusters are c s 1 and c b 2 . This way, bids and offers within these selected clusters are matched using the matching algorithm described in Section III-B3 and forms a single cluster c s 2,1 after matching. During matching, the first negotiation is between prosumers b 4 and s 4 because of their close price and energy quantity preference. Consequently, 2.85kWh is traded between both prosumers at 23.75 ct./kWh. For the next iteration, two clusters are selected from three clusters in the first hierarchy by solving Eq. (27) and are termed second selected optimal clusters. The second selected optimal clusters are c s 2,1 and c b 1 . Bids and offers within these selected clusters are matched using the same matching algorithm described in Section III-B3 and forms a single cluster c b 1,(2,1) after matching. c b 1,(2,1) contains only one bid, b 1 , which is partially matched. At this stage, only two clusters consisting of c s 2 and c b 1,(2,1) are left and form the second hierarchy. These two clusters are selected and matched to form the single cluster c s (1,(2,1)),2 which contains a single offer s 1 that is partially matched. In this way, the final cluster, c s (1,(2,1)),2 , will trade with the upstream grid using the upstream grid price as it was not able to match in the local community. Thus, our model considers similarities based on bidding/offering prices and load profiles. This way, two prosumers in an LEM with similar profiles will be able to trade P2P energy with each other. If the bidding price of the buyer is less than the offering price of the seller, there will be no matching notwithstanding the similarities in the load profiles. This shows the importance of bidding/offering price in an LEM. Bidding/offering price just likes prosumer profiles has effect in LEM trading which if not considered will lead to mismatch in trading and further result in trading with the upstream grid thereby making the market uneconomical and even cause grid instability. As our trading time step  (15 minutes) is very short, uncertainty was not considered as it does not have high impact on the LEM. In the LEM, prosumers are allowed to take strategic actions that will benefit them and the market. However, they are not allowed to take actions that will result in inequality.
Clustering brings together prosumers into pools of their fellow prosumers where they all posses similar characteristics. This results in reducing mismatch in the LEM as clusters contain pairs with similar bidding/offering preferences which is used for the P2P matching. Clustering further ensures that more energy is traded at the local levels and creates more local economy for energy community. Hence, considering heterogeneous characteristics of prosumers is relevant to the economic goal of social welfare which is mainly creating additional savings for the local participants. This additional savings results in increase in the share of market savings of the local prosumers participating in the local energy trading. Moreover, clustering is based on considering the social welfare of clusters as defined in Eqs. (21) and (22) and not the social welfare of the local community. In comparison to existing clustering algorithms, the proposed clustering algorithm provides opportunity for the LEM operator managing the local market to give more importance to certain prosumers preferences that is more importance to the prosumers. Furthermore, the algorithm converges faster in lesser time when compared to the traditional clustering algorithms like K-means clustering.

V. SIMULATION SET-UP A. SIMULATION FRAMEWORK
The proposed LEM advanced clustering model is developed as a Python code and implemented by integrating with the open-source Grid Singularity Exchange (GSy-E) [35], [48], [49] and bidding agent application programming interface (API) as represented in Fig. 5. Each consumer and prosumer is represented by a bidding/offering agent which communicates their bids and offers/bids, respectively, to the exchange engine. The exchange engine is responsible for (i) receiving the bids/offers from the bidding/offering agents of the prosumers, (ii) storing them in the ordered book, and (iii) sending them for onward clearing by the matching API. The matching API is responsible for matching the bids/offers among prosumer agents using the proposed advanced clustering model developed in this work. All unmatched bids and offers are transacted with the up-stream grid agent using the upstream grid price. In other words, any offer and bid of prosumers which is not matched with other prosumers in the energy community is traded with the up-stream grid. In our simulation model, each prosumer communicates their bids or offers individually to the exchange engine every 15 minutes time slot before the energy exchange time. Afterward, the results from the matching API is sent back to individual prosumers.

B. COMMUNITY SET-UP AND DATA
The proposed P2P LEM approach is verified in two simulation case studies for a period of one day. Case study I is a VOLUME 11, 2023 33617 Authorized licensed use limited to the terms of the applicable license agreement with IEEE. Restrictions apply. community with 25 prosumers consisting of 15 households with only consumption devices and 10 households with consumption and production devices. Case study II is a community with 120 prosumers consisting of 68 households consumers, 4 commercial consumers, 7 commercial prosumers, 6 industrial prosumers and 35 household prosumers. For both case studies, the load profiles are combinations of profiles from [50], LoadProfileGenerator [51], [52] and standard load profiles [53], [54]. These two case studies are chosen to show an in-depth and clear analysis of the advantage/benefits of our proposed model using case study I and further prove its application in a real-case LEM using case study II. Besides, the PV production profiles are from Renewables Ninja [55], [56] using Stuttgart region as a community. The PV system losses are also varied between 5% and 15% with a constant tilt angle of 35 • . For prosumers with storage systems, the storage capacities are between 7.5 kWh to 13.5 kWh with maximum absolute power between 3.5 kW to 5.25 kW. Additionally, two community storage systems of capacities 140 kWh and 120 kWh, with maximum power of 40 kW are included in the local community of case study II. The minimum allowed state of charge for all storage systems is 10%. The profiles are randomly fitted into a community (Scharnhausen) in Stuttgart region as shown in Fig. 6(a) and (b) for case studies I and II, respectively, using the OpenStreet map and QGIS. In this study, prosumers in the same building like P1, C1 and P2, are considered to be in the same block. Moreover, prosumers in the same neighbourhood (street) like P1, P4 and C2 are considered to be in the same estate, and prosumers in the same post code (e.g P1, C6, C9 and P3) are considered to be on the same zone.
The price components of the simulation case studies are presented in Table 2. The price components of the local electricity market consist of energy price of the electricity producer, metering fee, local grid fee and 19% Value-Added-Tax (VAT) [57]. The energy price is the minimum amount of money (cents/kWh) a seller prosumer is willing to receive per kWh of electricity produced and traded with other prosumers. The metering fee is the surcharge paid by the consumer for maintaining the metering infrastructure while the local grid fees are for maintaining the local distribution grid infrastructure. The VAT is the tax paid for transacting electricity between prosumers. It is noted that the VAT is 19% of the sum of energy price, metering, local and up-stream grid fees. Additionally, the up-stream grid fee is added if the electricity is traded with the up-stream grid. Because of the average cost of electricity in Germany, the maximum cost of electricity which is the cost of buying electricity from the grid is capped at 31.5 cents/kWh [58]. Moreover, the minimum cost of electricity is 11.00 ct/kWh which is the feed-in tariff of PV. Hence, consumers (also prosumers during bidding) set their preferred bid price between 11.38 to 29.54 ct/kWh, that is the range of total cost for buying energy within a block. However, prosumers (during offering) set their preferred offering price between 11 to 24.5 ct/kWh. This is the range of energy price for selling energy within a block as shown in Table 2. The range of energy price is between the feed-in tariff price and the maximum price that prosumers can sell their energy within the community. Each prosumer within the LEM has an agent which is a software code that submit their bidding/offering vectors on their behalf to the LEM framework. The bidding price is chosen randomly by the agent within the range provided by the prosumer while the energy quantity is selected from the consumption/production profile of the prosumer. The agents on behalf of the prosumers and consumers can only send one bid or offer per time slot. Furthermore, electricity which is not traded within the LEM is exchanged with the up-stream grid at the grid price which is less profitable compared to trading within the LEM.

A. CASE STUDY I
The analysis and discussion of the simulations results for case study I are presented in this section.

1) GENERAL ANALYSIS OF SIMULATION RESULTS
To analyze the traded electricity volume and the electricity exchanged with the upstream grid, the geographic location and location of the agent in the local community are used as criteria for initial cluster grouping and hierarchical clustering chosen metric, respectively. The simulation is analyzed and presented in this subsection. Fig. 7 shows the net electricity demand for (a) consumers, (b) prosumers and (c) the prosumers' electricity supply for 33618 VOLUME 11, 2023 Authorized licensed use limited to the terms of the applicable license agreement with IEEE. Restrictions apply.   the simulation period. As shown in Figs. 7a and 7b, the peak demand happens in the morning (around 6:00am) and late evening (around 8:00pm). This is typical of households as most people always prepare for their daily activities in the morning and consequently will need to use their electricity appliances. Moreover, at late in the evening, most people will be cooking their dinner, listening to local news, etc. Consequently, the peak electricity demand is obvious. The early morning peak electricity demand of the prosumer households is not noticeable in Fig. 7b. This is because the simulation is conducted for a summer day, and most of the prosumers' electricity demand in the morning of the simulation day is covered by the PV production and hence cutting off the morning peak demand of the prosumers. In addition, the households show an almost constant electricity demand from about 11:30pm until 5:00am. This is because most electricity consumers at this time are asleep and therefore only use their constant electricity demand appliances such as refrigerators. From Fig. 7c, net electricity supply starts early in the morning at around 5:30am with peak generation between 10:00am to 12:30pm, ending at around 4:00pm. This is a typical summer day, prior to and after this specified time, electricity generated by the prosumers is used to satisfy the internal demand of the prosumer households. Fig. 8 displays the electricity traded within the LEM for selected (a) consumers (b) prosumers and (c) energy exchange with the upstream grid for the simulation period. As shown in Fig. 8, it is evident that electricity trading happens within the LEM mainly during the net PV production time. Since the batteries are not internally controlled, the prosumers that own batteries sell their charged electricity a few time steps after the PV generation within the LEM has stopped. The stacked area of the prosumers' (Fig. 8b) traded energy is bigger compared to the stacked area of the consumers' (Fig. 8a) traded energy. This is because prosumers can act as consumers in some time steps. At this instance, the traded energy between prosumers is recorded for both prosumers (i.e. prosumers that produce the energy and those that also consume the energy) thereby increasing the area of Fig. 8b compared to Fig. 8a where two consumers cannot exchange energy. Fig. 8c displays the energy imported/exported from/to the upstream grid to/from the LEM. From Fig. 8c, there is no energy export to the upstream grid from 12:00am until about 6:00am and from about 4:00pm until 12:00am. This is because there is no net PV production at this period. The major electricity import from the upstream grid also happens at this period. Since all prosumers within the LEM may not have the same price preference, some prosumers may not be able to bid the price that will enable them buy electricity from the LEM. At same time, some prosumers many not be able to offer the price that will enable them sell all their electricity at the LEM. This is the cause of the simultaneous imports and exports witnessed at some periods around 6:00am until about 4:00pm in in Fig. 8c. Comparing Figs. 8c and 7a, it is evident that the peak demand witnessed in Fig. 7a at around 6:30am which is cut off by the LEM (as shown in Fig. 8c) from 10kW to about 4kW showing about 60% cut off. The peak import (Fig. 8c) witnessed from around 9:00pm is because of the high demand from both consumers and prosumers at late evening and there is no PV production at this time to reduce it.

2) ANALYSIS OF CLUSTERING SCENARIOS
The analysis and discussion of the simulation results for the different clustering scenarios are presented in this section.

a: CONSTANT CLUSTERING CRITERIA FOR INITIAL CLUSTER AND HIERARCHICAL CLUSTER CHOSEN METRIC
In this subsection, we analyze the simulation for the constant clustering scenarios. The same approach is used for both initial cluster grouping and hierarchical clustering chosen metric. In this way, four scenarios are introduced based on location, offering/bidding price, energy quantity, and cluster welfare. These scenarios are analyzed in terms of how they affect electricity trading, and the economic and technical benefits of the LEM. Fig. 9 shows (a) the traded energy, (b) average trade rate and (c) number of trade per slot for four clustering scenarios. According to our study, the four clustering scenarios show similar behaviour for traded energy, average trade rate and number of trades per slot. In the proposed scenarios, from 12:00am until about 5:00am, the energy traded within the LEM is zero and consequently, the average trade rate and the number of trades per slot are 31.5ct/kWh and 0, respectively. At this time, there is no PV generation and all the electricity is bought from the upstream grid at a higher price. In the morning, the PVs start generating and consequently, the traded volume and the number of trades per slot increase gradually which further results in gradual decrease in the average trade rate. During the day, as the PVs are generating electricity, higher volume of energy is traded within the LEM resulting in higher value of number of trades per slot and lower value of the average trade rate. On the other hand, during the evening around 8:00pm, as the sun sets, the traded volume and the number of trades per slot reduce gradually to zero. The average trade rate increases in the same way to 31.5ct/kWh. Fig. 10 displays the internal traded energy within the LEM, total energy import and export from/to the upstream grid for all clustering scenarios. The internal traded energy is the total energy traded between consumers and prosumers within the LEM. The energy imported/exported from/to the upstream grid are known as external energy exchange. As shown in Fig. 10, the price clustering scenario shows the best performance for the internal and external energy exchange compared to other clustering scenarios with location showing the least. This is evident with the high internal and lower external energy exchange of the price clustering scenario and the lower internal and higher external energy exchange of the location clustering scenario. Higher internal energy exchange means that more energy is traded within the LEM and hence, more benefits is expected to be created by this higher trade. On the other hand, higher external energy exchange means that most of the trades happens between the prosumers and the upstream grid, this is expected to create less benefits to the   LEM. The least performance of location clustering scenario compared to the price clustering scenario is because, although some consumers and prosumers may be close to each other and may be clustered to trade electricity, but they will not trade if the bidding price is less than the offering price. This way, clustering prosumers and consumers who have the same price preference range will result in higher matching of bids and offers within the LEM, thereby creating more internal energy trade and less energy exchange with the upstream grid. The self-sufficiency (SS), self consumption ratio (SC) and the share of market savings (SMS) for the four clustering scenarios are shown in Fig. 11. The SMS is the share of profit made by the local consumers and prosumers for trading within the LEM compared to when there is no LEM [59]. The SS, SC and SM of the four scenarios share a close range with each other. However, for all the performance indicators (SS, SC and SMS), the price based clustering scenario has the best performance and location has the least. This is because, the price based clustering scenario provides more internal traded energy and less external traded energy compared to other clustering scenarios. Higher internal traded energy and lower external energy exchange results in higher performance of the LEM as the LEM does not depend much on the upstream grid for its energy production/consumption. Hence, clustering prosumers and consumers, who have the same price preference range will be more beneficial to the LEM as most trades will match in this cluster scenario, thereby increasing the LEM'S performance indicators.
Figs. 12 (a) and (b) show box plots of the net cost of trading electricity for consumers and prosumers, respectively, in twelve clustering scenarios (L1 to L12). For the consumers, Fig. 12a, the mean cost for all the clustering scenarios is between 2.6 to 2.8 ct./kWh. It is evident that the consumers are household consumers as this cost is within the range of the daily electricity cost for a households in Germany and this correspond to the household data used for the simulation of this case scenario. The minimum cost is 1.5 ct./kWh and all the clustering scenarios show the same minimum cost. However, the maximum cost varies for the different scenarios with L1, L3,L4, L6, L9 and L10 showing a lesser maximum  cost value compared to L3, L5, L7, L8, L11 and L12 clustering scenarios. For the prosumers, Fig. 12b, the mean cost is between −1.8 to −1.6 ct./kWh for all the clustering scenarios. Negative mean cost means that the prosumers gained some monetary value for exchanging their electricity with the consumers. The maximum net cost of the prosumers is higher for L3, L4, and L8 compared to other clustering scenarios. On the other hand, the minimum net cost is lower for L4, L5, L6 and L7 compared to other clustering scenarios. L6, L7 and L9 show the best performance because of their lower mean, maximum, and minimum net cost. Fig. 13 shows the internal traded energy within the LEM, total energy import and export from/to the upstream grid in twelve clustering scenarios (L1 to L12). As seen in Fig. 13, L4, L5 and L6 scenarios present the best performance for the internal and external energy exchange compared to other clustering scenarios with L1, L2 and L10 having the worst performance. This is evident with the high internal and lower external energy exchange of the L4, L5 and L6 clustering scenarios and the lower internal and higher external energy exchange of the L1, L2 and L10 clustering scenarios. L4, L5 and L6 have similar cluster criterion for initial cluster grouping which is the offering/bidding price. This similarity shows why the three scenarios perform better than the others in terms of energy exchange within the LEM and upstream grid. It is also evident that clustering prosumers and consumers who have the same price range based on initial cluster grouping will result in higher matching of bids and offers within the LEM which create more internal energy trade and less energy exchange with the upstream grid. Fig. 14 shows the SS, SC and SMS for the clustering scenarios L1 to L12. The SS, SC and SMS of the twelve scenarios L1 to L12 show close range value to each other. However, for all the performance indicators, the price based initial cluster criterion's (L4, L5 and L6) clustering scenarios show the outstanding performance compared to other scenarios. The most outstanding clustering scenario is the L6 scenario which is based on initial grouping of bids and offers based on bids/offer price and advanced grouping based on location and cluster welfare. L6 has the highest SS, SC and SM compared to other clustering scenarios. This is because, the price based initial cluster grouping scenario provides more internal traded energy and less external traded energy compared to other clustering scenarios. Furthermore, the location and cluster welfare based criteria for chosen metric ensures that electricity is traded close to where they are consumed and further create additional welfare to the community. This provides additional SMS to the LEM.   Fig. 15 shows the P2P energy trade between consumers and prosumers for the L6 clustering approach. Since the prosumers can act as consumers in some time slots, and consequently buy energy from other prosumers, the prosumers are further added to the horizontal axis. Comparing Fig. 15 with Fig. 6a shows that P2P energy is traded within the LEM according to the closest cluster that can be formed from Fig. 6a. Regions on Fig. 15 where higher quantity of energy is exchanged represent a particular region or cluster of household in Fig. 6a where households are close to each other. An example is the upper most center of Fig. 15 where higher quantity of energy is exchanged between P8, P9 and P10 as prosumers (producing) and; C13, C14 and C15 as consumers. This is evident in Fig. 6a where P8, P9, P10, C13, C14 and C15 are located in the same estate known as AS-EST. The higher quantity of energy exchanged between P2 and P13 is because they belong to the same block, therefore, the cluster model matches them easily and ensure that electricity is traded close to where it is produced. In the same way, the higher quantity of energy exchanged between P10 and C15 is also because they are located in the same block, therefore, exchanging energy among them will create additional welfare to the LEM.

B. CASE STUDY II
In this Section, we analyze the performance of the L6 clustering scenario with 120 households as described in Section V-B and varying number of clusters for initial cluster grouping and compare it with P2P matching where there is no clustering. Fig. 16 displays the internal and external energy exchange for varying number of clusters. The external energy exchange are the energy export and import to/from the upstream grid. As shown in Fig. 16, the total energy exchange, that is the sum of the internal and external energy exchange, is constant for all the simulation scenarios. This is evident that the total energy requirements of the local community is constant throughout the simulations. Furthermore, the P2P matching model without advanced clustering scenario has higher external and lower internal energy exchange compared to all other scenarios. Higher external and lower internal energy exchange results in LEM depending totally on the upstream grid and impacts the LEM participants negatively. Moreover, increasing the number of clusters for initial cluster grouping increases the internal energy exchange and reduces the external energy exchange of the LEM. For the case scenario studied in this work, the simulation achieved its optimum when the number of clusters is four, therefore, increasing the numbers of clusters beyond four has no impact on the performance of the LEM. Based on the L6 advanced clustering approach, the number of initial cluster groups where the LEM reaches its optimum depends on the individual bidding strategies and location of the LEM participants on the local grid. Fig. 17 displays the SS, SC and SMS for varying number of initial cluster groups. The P2P matching model without advanced clustering scenario has lower SS, SC and SMS compared to all other scenarios. This is because, the P2P matching model without clustering scenario has more external energy exchange and lower internal energy exchange compared to all other scenarios, making the LEM less beneficial to the local markets participants. Also, increasing the number of clusters for initial cluster groups increases the SS, SC and the SMS of the local community. This is because, increasing the number of clusters for initial cluster groups reduces the external energy and increases the internal energy exchange of the LEM and thereby creating additional benefits to the  local community. The optimum SS, SC and SMS is achieved when the number of initial cluster groups is four. This is the same for internal and external energy exchange of the LEM. This is evidence that the technical and economic benefits of the local community depend on the energy exchange between the community and the upstream grid.

3) TIME COMPLEXITY
The proposed clustering algorithm takes less than 1 seconds to solve a given problem in an Ubuntu operating system computer. This means running the model only, without a market framework such as GSy-exchange. Running it with a market framework takes more time as simulation data needs to be stored and read from different files. Table 4 shows the average simulation computational time per slot to complete the matching in the GSy-exchange matching API for varying number of clusters. The P2P matching mechanism without clustering has the least computational time per slot compared to all other scenarios. This is because the P2P matching mechanism without clustering is a simple random matching and has no clustering model. With the smallest number of  clusters which is 2, the run time is 22.55 seconds per slot for a community of 120 prosumers. Increasing the number of initial cluster groups increases the computational time. This is because increasing the number of clusters will require more time for k-means clustering to determine the individual cluster centroids, additionally, it will require more time for advanced clustering to build up the hierarchical model as there will be a greater number of branches from the initial cluster groups. Also, comparing the average computational time of our model with that of a multi-layer LEM model proposed in [48], where a similar community was simulated with the Grid Singularity platform shows that our proposed model has the capability of reducing the average computational time by half when the number of clusters is less than 5. However, with large number of clusters, it may take more time for the algorithm to complete its calculation and matching. For our simulation case scenario, our model achieved its optimum when the number of clusters is four as discussed in Sections VI-B1 and VI-B2, this is evidence that the model is computational efficient when compared to previous works already published in the same area.

VII. CONCLUSION
In this paper, an advanced clustering algorithm was proposed for P2P matching in the energy community considering the heterogeneous characteristics of bidding preferences for prosumers consisting of geographic location, location on the local community, bid/offer price, bid/offer quantity, and cluster welfare. The proposed model was tested in a German real-case scenario and simulated for 120 German households. The simulations results show that the model was able to leverage the preference opportunity to ensure that electricity was traded and consumed closer to where it is produced in a P2P trading structure. Thereby, reducing the peak demand of the local community, increasing the traded energy at the local community, and reducing the energy exchange of the local community and the upstream grid. Also, the model benefit the local prosumers as it can help to increase the share of market saving, self consumption and self sufficiency of the prosumers. By ensuring that energy is traded closer to where is is produced, the model further increase the local economy of the local prosumers.
Furthermore, in the proposed advanced clustering model, using price preference as the criterion for initial cluster grouping, and location of the prosumers in the community and cluster welfare as the criteria for the chosen weight metric, offered more technical and economic benefits to the local community compared to other clustering scenarios. Also, comparing our proposed model with a P2P model without clustering shows that the computational time of both models are closer in range and hence our model can be easily adopted without increasing the market computational time. In future work, we will investigate how reinforcement learning can be used by the prosumers to intelligently decide their bidding/offering preference and how the model will be implemented in a distributed blockchain platform to ensure efficient preservation of prosumers' privacy and conformation to data protection laws.