Flocking-based decentralised double auction for P2P energy trading within neighbourhoods

Renewable energy sources are highly unreliable and create unreliable energy surplus and energy demands. These energy surpluses and demands can be traded frequently within neighbourhoods. This paper presents a trading algorithm to trade unreliable renewable energy frequently within neighbourhoods. We considered the flocking behaviour of birds and distributed double auction, and developed a trading algorithm to perform peer-to-peer (P2P) energy trading within neighbourhoods. Peers are prosumers which can be energy co-operatives, micro-grids, electric vehicles, charging stations, etc. We implemented our flocking-based decentralised double auction algorithm in Java and tested using a residential PV data set from California state in the USA. Our mathematical analysis shows that the peers are converging to average positions of neighbourhoods while performing P2P trading. Our experimental results showed that the proposed algorithm has a higher success rate and faster convergence to the success rate than the centralised double auction and distributed double auction presented in the literature. Moreover, nodes are converging to neighbourhoods. This guarantees trading within neighbour-hoods. Further, we have observed that successful trading reaches around 80%.


Introduction
Renewable energy generators are not reliable because they are highly affected by changing environmental factors such as the amount of sunshine, wind speed, etc. Moreover, they do not naturally have large on-site energy storages hence their output is typically referred to as nondispatchable [1]. Prosumers are proactive consumers with distributed energy resources, actively managing their consumption, production and storage of energy [2,3]. P2P energy trading among local prosumers is emerging as a promising method to manage unreliable and distributed energy resources [4] while offering three distinct value streams: energy matching, uncertainty reduction, and preference satisfaction [2].
P2P Energy trading goes from prosumer to prosumer rather than large commercial enterprise to consumer, and prosumers can sell their excess power for profit [5]. Authors in [5,6] discuss distributed double auction for P2P energy trading, and prosumers delegate their energy requirements between randomly selected neighbours. This random selection improves the anonymity of trading. The unsuccessful energy requirements will be further delegated to randomly selected neighbours of the current prosumer. Therefore, the energy requirement of a prosumer can be traded outside of its neighbourhood. That is trading within the neighbourhood is not guaranteed. A review of existing P2P energy trading projects and auction mechanisms are presented in [4,7]. A Bayesian Equilibrium solution for energy trading is presented in [8]. However, the current literature regarding the random request delegation for distributed trading does not theoretically describe and implement guaranteed trading within the neighbourhoods, and that is the main objective of this research. The Joule effect in the transmission cables accounts for energy losses of about 2.5% while the losses in transformers range between 1% and 2% [9], hence guaranteed P2P trading within a neighbourhood is important.
The flocking behaviour of birds and the convergence of birds flocking are explained using cohesion, separation and alignment properties in [10][11][12]. This paper uses properties of the flocking behaviour of birds and defines the behaviour of prosumers to perform anonymous, secure and guaranteed P2P energy trading within neighbourhoods. Our decentralised double auction uses McAfee's double auction algorithm [13]. The proposed P2P energy trading was implemented and tested for success rates, the amount of traded energy and convergence at decentralised neighbourhoods. This paper presents following contributions: • Guaranteed P2P trading between prosumers within neighbourhoods.
• The proposed flocking-based decentralised double auction preserves the anonymity of the buyer and seller hence provide improved security and trust for P2P trading. • The proposed approach shows better trading outcomes compared to distributed double auction and centralised double auction presented in the literature.
In this research, prosumers generate their energy requirements as Buying or Selling requests. A Buying request finding a successful Selling request is called successful trading. That is, one successful trading has two traded requests.
This paper was organised as follows: We discussed the relevant literature in Section 2 and introduced the flocking-based decentralised double auction for P2P energy trading in Section 3. In Section 4, we discussed the distributed double auction. The implementation, experimental evaluation, and results were discussed in Section 5. Finally, in Section 6, we presented findings and future research.

Related literature
Distributed Energy Resources (DERs) are being integrated into the energy market by transforming the centralised and unidirectional market into a decentralised market while transitioning customers into prosumers [14]. The direct energy trading between consumers and prosumers without the use of an intermediary is called P2P energy trading which is usually implemented within a local electricity distribution system [15]. A P2P system is a self-organising decentralised system for shared usage of resources avoiding central services [16,17]. The P2P architecture is emerging as a potential and promising architecture for decentralised energy trading in local energy markets [14,15].
The P2P trading enables multi-directional trading in local communities [15]. Energy trading platforms such as vandebron 1 , Piclo 2 , etc. act as an energy supplier who link consumers and DERs, and balance the whole market [18]. Consumers can select and prioritize from which generators to buy electricity. Moreover, [18] suggests P2P energy trading within a micro-grid, within a CELL(multi-micro-grids), and among CELLS(multi-cells). A four-layer system architecture is proposed for P2P energy trading in [15]. They are two dimensional power grid layer, ICT layer, control layer and business layer. A P2P control architecture is detailed in [14] discussing a case study in Nepal.
The P2P energy trading enables trading locally generated renewable energy among local customers [2,3,1,7,18]. A Bayesian Equilibrium solution for energy trading considering utility, power loss and Bayesian strategic bidding are described in [8]. However, many of the trails have focused on the business models and it is still lacking necessary mechanisms to ensure energy trading within local prosumers [4]. In [10], Reynolds introduced three heuristic rules to explain the flocking behaviour of birds however these rules are interpreted broadly because they are not theoretically defined. A formalization of Reynolds's boids is presented in [19] while modelling the behaviour of a living being. A theoretical framework for the design and analysis of distributed flocking algorithms is presented in [12]. The leaderless coordination is theoretically defined in [20] where birds update their velocities by averaging them out over their nearest neighbours. The repeated averaging of velocities enables each bird to eventually converge to a fixed speed and heading [11].
A double auction mechanism with dominant strategies for both buyers are sellers are presented in [13]. Authors in [5,6] have discussed distributed double auction and the use of blockchain technology for P2P energy trading. Blockchain transactions of energy surplus and deficiency are sent to neighbours. However, guaranteed trading within the neighbourhood has not yet been addressed in the literature.

Flocking-based decentralisation
The flocking-based decentralisation enables prosumers to delegate their energy requirements only between neighbours during continuous auctions.

Flocking behaviour of prosumers
This section defines and explains the flocking behaviour of prosumers to assure trading within neighbourhoods, improve anonymity and improve the security for P2P energy trading. In [10], Reynolds introduced three heuristic rules to explain the flocking behaviour of birds. As in [12], Cohesion -attempt to stay close to nearby flockmates, Alignment -attempt to match velocity with nearby flockmates, and Separation -avoid collisions with nearby flockmates. We discuss the cohesion, alignment and separation properties of flocking behaviour and their applicability for prosumers to perform P2P energy trading. We define a prosumer as a node in a two-dimensional space. The position of a prosumer is determined by X and Y coordinates in the Cartesian Coordinate System. That is, the internal state of a node consists of at least its position p ∈ ℘, where ℘is a position in the Cartesian Coordinate System. Definition 1. The internal state q ∈ of a prosumer n ∈ is defined by (1), where p ∈ ℘ is its position which is defined by X and Y coordinates in the Cartesian Coordinate System, l is its label, and r * is a list of requests as r ∈ ℛ.
The position of each node is updated based on the average position of the neighbourhood. The neighbours of node i at a given time t, are those nodes that are in a circle of pre-defined radius r centred around the current position of node i. The position of node i (n i ), written in p i evolves in discrete-times in accordance with the model defined by (2), where t is a discrete-time index taking values in the non-negative integers 0, 1, 2, 3, …, and < p i (t)> r ′ is the average of the positions of n i 's neighbours at time t in radius r ′ as defined in (3).
where i (t) is the set of neighbours and M i (t) is the number of neighbours of node i (n i ) at time t. The internal state of node i (n i ), that is q ni is changed at discrete-times.

Definition 2.
The cohesion function f for a node n i is a process which is defined by the following steps; step1: select a request r ∈ ℛ from n i , step2: find the neighbourhood of n i , step3: delegate the request to a randomly selected neighbour, step4: move n i towards the centre of the neighbourhood as defined in the Eq. (2), and step5: repeat the above steps for all the requests in n i .
A new < p i (t)> r ′ is calculated for each request delegation which we defined as, where v =< p i (t)> r ′ and k > 0 ; k is a positive integer. The average position V for k number of requests is, where m is a positive integer. This further justifies, the node n i converges to the average V k when delegating requests to neighbours as defined in 6.
where f is the cohesion function as defined in definition 2. Time to converge is proportional to the number of requests defined in k. The convergence is important because that guarantees requests are delegated within neighbouring nodes all the time and that is one of the main contributions of this paper. We use radius r to define the neighbourhood, and the distance between nodes is calculated to find neighbour nodes. The energy loss of electric power systems is more complex and that involves transformers, cables, etc. Transmitting electricity over distance and via networks involves energy loss. As the Joule effect described in the introduction section, transmission cables have a higher energy loss than transformers [9]. However, for simplicity, in this research, we assume the distance between nodes is proportional to energy loss. This convergence promises the minimum energy loss which is an interesting feature for P2P energy trading within local communities in low voltage networks.
Here, the Alignment α i (t) is defined as illustrated in Fig. 1. The node n i finds its neighbourhood for each request delegation at time t as in step 2 of definition 2. Then the average position V avg (t) is calculated as in Eq. 3.

V avg
As in Fig. 1, node n i (t) move towards V avg (t) as defined in the Eq. 2 while Blockchain-based smart contract implementation of 7 and α i (t) alignment verification can strengthen the security of request delegation among neighbours and P2P trading. For example, a prosumer who is moving towards the average position of a neighbourhood to delegate a request is paying a less fee than other positions. However, secure request delegation is not part of objectives here and we will address it in our future work. We find a collision neighbourhood collision where nodes in this area have a risk of collision. Each prosumer should see their own local prosumers in collision and steer away from the centre of collision as defined in the Algorithm 1.
• Node n steer away from the centre recalculating x and y coordinates with r distance. • Repeat above steps for node n until collision is empty.
Separation is useful to differentiate prosumers while trading and simulate trading behaviours.

Decentralisation of prosumers for P2P trading
Reynolds' rules are not mathematically defined, and it is unclear when and how each rule is applied [12]. Here, we use the internal state of each node as in Eq. (1), average position as in Eq. (2), cohesion function as in Eq. (6), alignment as in Eq. (7), separation as in Algorithm 1, and defined an Algorithm 2 to run double auctions to trade local energy demands and surpluses frequently within dynamically created decentralised neighbourhoods. The unsuccessful trading requests regarding energy surplus and demand will be addressed using the Utility grid which we do not discuss in this paper. while not converged do ←nodesWithRequests(N) 4: for eachn ∈ do neighbours←findNeighbours(n) 6: ℛ←allRequests(n) for eachrequest ∈ ℛ do 8: centre←findCentre(neighbours) randomNode←select(neighbours) 10: randomNode←delegate(request) n←moveTowardsCentre(n, centre) 12: neighbours←findNeighbours(n) end for 14: n←Algorithm 1 ( , n) end for 15: ←doubleAuction(N) end while end for Here in Algorithm 2, we assume prosumers are defined by nodes, and prosumers are capable of performing double auction and trading. Nodes generate two types of requests which are Buying and Selling requests. These requests are delegating to randomly selected neighbouring nodes for double auction as defined in the Algorithm 2. At a discrete-time t, a node can have no requests, a single request or a set of requests. For example, a node defines a home that is connected to a wind farm, the utility grid and provides an electric vehicle charging facility.
Input data is a set of nodes. Each node n ∈ has a set of ℛ requests. These requests can be either Buying requests or Selling requests which will be processed at double auctions. The output is records of successfully traded transactions. Algorithm 2 can be explained as follows: • In a given discrete-time t, nodes which have requests are in a set of . • Select a node n ∈ and find the neighbourhood. If there are no neighbours exist, then go to the next node. If there are neighbours, then go to the next step.
• Find all the requests of node n and for each request r ∈ ℛ, calculate the average position of the neighbourhood, select a neighbour node, delegate r to the selected neighbour node, move n towards the calculated average position of the neighbourhood, and find the new neighbourhood for n after its move. Repeat this process for all the requests in . • Avoid collision using Algorithm 1 and repeat this process for all the nodes n ∈ • Apply double auction for all the nodes. Successful trading transactions are executed and recorded in a blockchain. Nodes having unsuccessful trading transactions are returned. Repeat above steps for number of auctions until find the convergence before going to next discrete-time t. • Repeat the process for next discrete-time t.
This algorithm generates converged decentralised nodes. This convergence is important because that guarantees the requests are delegated only within neighbour nodes and that enables the minimum energy loss at energy transmission. The energy losses are mainly due to transformers and cables [9]. In a general P2P scheme, households interact directly with other households [21]. In this work, we assume prosumers interact directly with other prosumers, the energy loss is propositional to the distance between prosumers and energy requests are generated by prosumers considering the energy losses within neighbourhoods. Our future work will address prosumers in proper infrastructure networks considering network constraints [21] and calculate energy losses between prosumers to decide potential neighbourhoods.
In the above algorithm, requests delegation among randomly selected neighbours increases the anonymity of trading nodes for requests. This is important to improve the security of trading nodes from potential planned attacks. If there are ℳ nodes in the neighbourhood, the probability of finding the trading node for a particular request is 1 ℳ . That is if ℳ increases then probability goes down and difficulty of finding the trading node goes high. Moreover, ℳ is changing dynamically for each request delegation and that further increases the anonymity of trading nodes.

Double auction and trading
This section discusses double auction for energy trading at each node which has both Buying and Selling requests. In this research, we used the double auction defined in [13] with a trade-reduction mechanism for P2P energy trading.
δ ta (N i ) < 0 indicates that N i needs δ ta amount of energy at the next time instance. Similarly δ ta (N i ) > 0 indicates that N i has a surplus of δ ta amount of energy to sell at the next time instance. Both Buying and Selling requests are generated accordingly. In our double auction market, each node N i will have a set of Buying requests from buyers (B 1 , B 2 , …, B p ) and a set of Selling requests from sellers (S 1 , S 2 , …, S q ) at a given time t where p and q are positive integers which are greater than or equal to 0. Each node solves the double auc-tion as follows: • Sort the Selling requests in the increasing order as SR 1 ⩽SR 2 ⩽…⩽SR q .
The list of sorted Selling requests contributes to the seller curve. • Sort the Buying requests in the decreasing order as BR 1 ⩾BR 2 ⩾…⩾BR p . The list of sorted Buying requests contributes to the buyer curve. • Let curves of Buying requests and Selling requests to cross at index β so that BR k ⩾SR k and k⩽β. The break-even index is k. Calculate p so that, p = (SR k+1 + BR k+1 )/2. • If BR k ⩾p⩾SR k , then all the k requests of both types trade for price p.
• Otherwise, the Selling requests SR 1 ⩽SR 2 ⩽…⩽SR k− 1 trade for SR k and the Buying requests BR 1 ⩽BR 2 ⩽…⩽BR k− 1 trade for BR k as in the trade reduction mechanism.
The buyers and sellers curves are illustrated in Fig. 2. In our trading platform, double auction is processing as defined in Algorithm 2. Nodes to perform double auctions should have both buying and selling requests.

Data processing
We used road networks data from the California state from [22] and processed locations, demand profiles(energy needs for houses) and PV profiles(energy generation information) of houses fitted with PVs using data sources from [23,24,5]. We created clusters of houses in close proximity. In each cluster, the maximum difference in house locations (difference in latitude or longitude) is.1 which corresponds to 11 km. We created 5000 clusters of houses where each cluster has at most 50 houses. Fig. 3 shows the locations of houses with fitted PVs and houses in  each cluster is coloured with the same colour. This presents a high-level view regarding the density of the number of PVs and their locations. From this PV locations, we have selected 100 houses in the range of latitudes between 33 and 34, and longitudes between − 119 and − 118. Then we created demand profiles and PV production profiles for each house every 5 min for 24 h. Using this information, we created an excess energy profile for each house in every 5 min for 24 h. We created two types of energy requests (Buying request, Selling request) for each house based on the excess energy profile.

Experimental evaluation
We created 100 nodes for 100 houses having buying/selling requests for each house based on the excess energy profiles in every 5 min for 24 h. Our flocking algorithm considers houses within a radius of 2 km as a neighbourhood. That is, the difference between the two latitudes or two longitudes is 0.02 for 2 km. We compare the number of buying requests (NGs) and selling requests(PGs) throughout the day for 24 h as in Fig. 4. We implemented centralised double auction [13], distributed double auction [5] and the proposed flocking-based decentralised double auction using java, tested with the above data set, and compared their results as in Fig. 5. Here, one successful trading has one buying request and one selling request that is two traded requests. The results showed that both the FDDA and DDA in [5] have a higher success rate than CDA in [13]. However, the proposed FDDA showed the highest success rate throughout the day as shown in Fig. 5. When comparing Fig. 5 and Fig. 4, we can observe that a higher number of requests show higher success rates in all three algorithms. Moreover, we compared success rates in various auctions at various discrete-times. Here we present a comparison of successful trading percentages when t = 210 (t is an integer in the range 0 to 287 for 24 h) in Fig. 6. As in Fig. 6, at the first auction, we have observed the number of successful tradings given by the CDA is higher than that of FDDA and DDA. However, both FDDA and DDA enable several distributed auctions at a discrete-time and contribute to overall higher success rates than CDA. In a centralised   auction, bids from all the peers are used to determine the price for energy trade, but in distributed auctions, each local auction uses bids from a subset of peers. Hence the price for energy trade can differ for these local auctions. Moreover, if a prosumer fails to find a winner for an energy requirement, it will forward that energy requirement to a neighbouring prosumer. The energy requirements are processed in several distributed auctions with various trading prices at a discretetime. As in Fig. 2 and experimental results in Fig. 6, the CDA shows around 38% of success. However, at a discrete-time, both DDA and FDDA enable distributed auctions on prosumers, various trading prices, and several iterations showing a higher success rate than CDA. We have observed around 80% of energy requirements were traded in DDA and FDDA using 5 distributed auctions at a discrete-time. The results showed that the FDDA converges to 80% successful trading quicker than DDA in [5] having a higher success rate.
We have observed that the flocking-based decentralised double   auction guarantees trading within neighbourhoods because of the flocking behaviour of neighbour nodes. In [5], when a request does not achieve successful trading, it is forwarded to a neighbour. However, it is not guaranteed that the 3rd neighbour is a neighbour of the request generator. That is, distributed double auction in [5] does not guarantee to trade within the neighbourhoods. Here we present the behaviour of nodes in the 2nd auction at a discrete-time, t = 210. The behaviour of nodes using the proposed flocking-based algorithm is illustrated in Fig. 7 and distributed double auction in [5] is illustrated in Fig. 8. The flocking-based algorithm creates decentralised flocks considering energy demands and distance between PV locations while nodes in distributed double auction in [5] are not flocking around the neighbourhoods.
As defined in lines 7 to 13 in Algorithm 2, prosumers are moving towards the centre of the neighbourhood while delegating requests until all the requests are delegated. This process will be repeated for all the prosumers which have energy requirements. The double auction will be applied at prosumers after delegating requests. The unsuccessful requests will be processed again following the above procedures as defined in lines 2 to 17 of Algorithm 2. Our experimental results showed that both FDDA and DDA converged in 5 distributed auctions hence we repeated the above processes for 5 times. At convergence, most of the trading possible requests have been traded as illustrated in Fig. 5. Moreover, we have compared the convergence times of DDA and FDDA throughout the day and illustrated in Fig. 9. The results show that FDDA has a better performance than DDA at most of the discrete-times.
DERs can trade the excess energy. We have observed that the energy trading is successful mostly during the day time when the DERs generate energy. We have analysed the amount of energy traded using FDDA, DDA and CDA within the day time from 6 am to 6 pm using a summertime dataset. We have observed that FDDA has the highest amount of energy traded and that is 70.60461% of the total energy requirement. DDA and CDA can satisfy 69.64921% and 58.58982% of the total energy requirement. Fig. 10 illustrates the amount of energy traded using FDDA, DDA and CDA compared to the amount of energy needed from 6am to 6 pm.

Discussion
Renewable energy sources are highly unreliable hence unreliable energy surplus and demands should be addressed frequently within neighbourhoods. In this research, we used the flocking behaviour of birds to define the dynamic behaviour of prosumers for P2P trading. We defined, implemented and tested a flocking-based decentralised double auction algorithm(FDDA) to perform P2P trading within neighbourhoods. We compared our algorithm with the centralised double auction (CDA) and the distributed double auction algorithm(DDA) defined in the literature. We used a PV data set from California state and tested algorithms for successful tradings, trading percentages and trading within neighbourhoods at every 5 min for 24 h. We have observed that anonymous trading nodes are delegating requests to neighbours and converging to average positions of neighbourhoods at discrete-times. This guarantee that both Buying and Selling requests are delegated and traded within the neighbourhoods. The random request delegation preserves the anonymity of trading nodes. Further, the test results showed that FDDA has the highest success rate, faster convergence than DDA, and around 80% successful trading is achieved in five distributed auctions. In this work, we assumed the distance between two nodes was proportional to energy loss. Our future work will enrich a flocking-based distributed double auction to consider energy transmission loss based on the energy infrastructure network.

Declaration of Competing Interest
The authors declare that they have no known competing financial interests or personal relationships that could have appeared to influence the work reported in this paper.