Microgrid Trading Game Model Based on Blockchain Technology and Optimized Particle Swarm Algorithm

With the increase in the number of microgrids and the maturity of technology, microgrids will also participate in power market bidding while ensuring their own consumption. This paper establishes a microgrid transaction model based on the blockchain platform and uses the Optimized Particle Swarm Algorithm (OPSO) to solve the optimal bidding strategy in the transaction, which achieves the respective profit optimization of each subject. The model proposed in this paper realizes the organic integration of blockchain and microgrid to solve the problem of underutilization of energy in a microgrid gaming competition. The proposed optimized particle swarm algorithm has high convergence accuracy and strong search capability. Also, the two major market players are used as research objects to establish an actual " Wind Turbine - Energy Storage System - Electric Vehicle " mini-microgrid system to solve the real-time electricity price problem. A comparative analysis of the model is carried out utilizing an example verification.


I. INTRODUCTION
As the economy and society continue to develop, distributed power producers and sellers are gradually participating in electricity market transactions [1]. It is hoped that distributed renewable energy (such as wind power, photovoltaic power generation, etc.) can be fully utilized to address the shortage of energy demand.
As an important part of the smart grid, the microgrid can efficiently integrate various distributed energy sources and increase the penetration rate of renewable energy. After the collapse of the large power grid, the supply of power can be quickly restored to ensure uninterrupted power supply for important users and make up for the shortcomings of the centralized power supply of the large power grid [2]- [3].
In the electricity market, microgrids form multiple subjects involved in the competition playing bidding against each other. In blockchain microgrid transactions, each subject is pro-consumer. The purpose of studying real-time microgrid trading is to complement microgrids with different characteristics, to fully utilize the surplus power of microgrids, and make efficient use of energy. How to fully guarantee the balance of interests of the various market entities and increase the utilization ratio of renewable energy is a problem that needs to be solved [4]. Therefore, the construction of the microgrid power trading model is of great significance to energy consumption.
However, the research on the electric energy trading of microgrid still faces many obstacles. There are still multiple obstacles to the study of electricity trading in microgrids. For example, the lack of clarity of the trading entity, the opacity of the price, and the complexity of the algorithm. As a distributed database and decentralized P2P network, the blockchain has the characteristics of smart contracts, distributed decision-making, collaborative autonomy, high security and openness, and transparency that are tamperproof. The blockchain network can support the construction and protection of the power spot trading market.
In recent years, the trading model has been studied in microgrid market trading supported by blockchain technology. Most of the current adoption is using blockchain technology for platform building and data recording. Reference [5] proposes an energy framework for P2P multisettlement transactions. A multi-settlement market is implemented using blockchain technology to make optimal decisions in selling/buying energy. There is a lack of more specific elaboration on practical application. Reference [6] develops a V2G secure energy trading mechanism based on the alliance blockchain. And the optimal pricing strategy is obtained using the game knowledge. However, it is not sufficiently connected to the internal microgrid. Reference [7] discusses the application of smart grid and blockchain technologies to smart cars, which is valuable to learn from. Reference [8] proposes a decentralized distributed energy trading mechanism based on blockchain smart contract technology as a way to enable P2P energy trading between producers. The question of how to further facilitate and motivate transactions between energy sources is one that requires further study. Reference [9] investigates scheduling schemes between microgrids using the PSO algorithm to minimize carbon emissions and cost per microgrid based on traditional scheduling. But improvements can be made to the algorithms for decentralized trading. Reference [10] presents a decentralized energy management system for electric microgrids that allows microgrid participants to trade with the distribution network and between participants. It enables users to get better profits directly. But it is further possible to construct a game model of multiparty competition.
The above research has studied the scheduling between microgrids and explored the application of various blockchain technologies to power sales, based on the development needs of the existing power system. However, the competitive relationship between the internal entities has not yet been clearly explained, and the practical application of blockchain technology in microgrids has not been explicitly analyzed in a case study. How to maximize the profit of the respective entities between microgrids under a decentralized trading mechanism is a problem to be solved.
The main contributions of this paper are as follows: 1) Microgrid transaction design: analyze the coupling between blockchain technology and microgrid transactions, and provide decentralized transaction support using blockchain and smart contract technology. 2) Trading platform model construction: The trading relationship between microgrids is investigated, and the subjects are divided into operators, storage providers, and large users, and the demands of each subject are identified and mathematical function models are established. 3) Solving the model: The optimal particle swarm algorithm is used to realize the demand for electricity and tariff from operators, storage providers, and large users at various times of the day. The feasibility and validity of the constructed model and the solution algorithm are verified by applying them to the actual model. The rest of the paper is organized as follows: in Sec. II, a brief introduction to the concepts related to blockchain technology and a coupled analysis of microgrid group transactions and blockchain technology are presented; in Sec. III, a multi-party game model based on microgrid operators, large users, and distributed storage providers is developed; in Sec. IV, the OPSO is used to solve the above multi-body game model; in Sec. V, using the wind turbine, energy storage device, and electric vehicle combination model as a practical example of a small-scale microgrid for verification of the algorithm; in Sec.VI, conclusion, and summary.

A. BLOCKCHAIN TECHNICAL
Blockchain technology is also known as distributed ledger technology. As the underlying technology and infrastructure of Bitcoin, blockchain has the characteristics of decentralization, information security and transparency, smart contracts, and verifiability. When the blockchain is used on different occasions, the hierarchical structure of the blockchain will be fine-tuned according to the specific application occasions, but the application structure of the blockchain basically consists of six levels [11]- [12].
The bottom layer is the data layer, which uses a chain structure to store the underlying data blocks; the second layer is the network layer, including network transmission mechanisms and verification mechanisms; the third layer is the consensus layer, which encapsulates algorithms to allow distributed blocks to reach consensus; the fourth layer is the incentive layer, which mainly includes the issuance mechanism and distribution mechanism; the fifth layer is the contract layer, which generates smart contracts; the top layer is the application layer, which is the actual case of blockchain application in power transactions.

B. COUPLING ANALYSIS BETWEEN BLOCKCHAIN AND MICROGRID TRANSACTION
In addition to its application in the financial field such as digital currency, blockchain is also developing rapidly in energy transactions in the power sector. Its decentralized nature naturally corresponds to the distributed nature of the main body of power purchase and sale in the power grid. And the features of data transparency, traceability, and antitampering can improve the security and reliability of transactions [13]. For the characteristics of the blockchain, the corresponding section in the microgrid is shown in table 1: Encrypt the data and information in the transaction process, and ensure the safety and reliability of the transaction High efficiency and realtime Smart contracts do not require the participation of third-party central institutions, and can automatically respond to the needs of various entities in real-time, Due to a large number of microgrid participants, with the blockchain technology, contracts are signed directly between consumers and producers, which also helps to enhance the status of consumers and market influence. In microgrid trading, operators and users publish contracts with bidding information into the trading area, and the system generates smart contracts based on the information from both parties and feeds back to them.
In this paper, the microgrid market players include Microgrid operators (MOs), Distributed storage providers (DPs), and Large users (LUs). To maximize their interests, the three players will influence the market changes by formulating different strategies to buy and sell electricity at different stages of the game.
Microgrid operators: Mainly responsible for generating and buying and selling electricity.
Distributed storage providers: Mainly collects and stores the distributed electric energy of all distributed users.
Large users: Only consider obtaining the required power at a lower cost. Acting as a consumer, buying electricity but not selling electricity. Fig.1 shows the game structure of the three-party market players. Each market subject is equipped with a corresponding block, and each subject block will be connected at the same time to form an energy block network. Each block conducts transactions, blockchain searches, and links work in different periods to form a blockchain. In a large energy block network, each main module will actively submit its identity authentication or logo, such as identity ID, account, energy form, geographic location, and other basic information as the core basis of power transmission data and market transaction data [14].
And through the analysis and mining of the energy block network, the market game equilibrium can be realized. In the case of multiple parties participating in the competition at the same time, each subject can choose a plan that is more in line with its interests based on the quotation. All block information is transparent and open, and each entity judges the transaction based on its needs and benefits. If both parties meet the transaction conditions, a consensus between the two nodes can be generated and the transaction can be completed.

III. MATHEMATICAL MODEL
In the microgrid system, to maximize their own interests, the microgrid operators, distributed storage providers, and large users participating in the market game will influence market changes by formulating different strategies for purchasing and selling electricity at different stages. All three have different objectives and needs, and therefore a functional relationship needs to be established separately.

A. LUS DEMAND MODEL
The goal of Large users is to obtain the target power at the lowest cost and put it into production. In order to simplify the model, this article assumes that the main users of the multimicrogrid system do not have the conditions to produce electricity. The goals of Large users to purchase electricity mainly include the cost of electricity purchase from MOs

MtoU buy
Ct and the cost of electricity purchase from DPs

DtoU buy
Ct . The specific form of electricity purchase depends on the electricity sales strategy of each entity, and the mathematical expression is as follows:

B. MOS MATHEMATICAL MODEL
Since the microgrid contains intermittent energy sources (wind power, solar energy, etc.), within a unit time period, its content processes are not necessarily balanced with its own load, and external power is usually surplus or lack of power. When the system has surplus electricity, the microgrid operator will sell electricity to other users to obtain revenue ()

MO sell
Pt . When the operator lacks electricity, it will purchase This work is licensed under a Creative Commons Attribution 4.0 License. For more information, see https://creativecommons.org/licenses/by/4.0/ This article has been accepted for publication in a future issue of this journal, but has not been fully edited. Content may change prior to final publication. Citation information: DOI 10.1109/ACCESS.2020.3044980, IEEE Access 9 electricity externally at a cost of ()

MO buy
Ct . It is described in mathematical form as follows:

1) ELECTRICITY SALES PROFIT
The MO's profit on electricity sales is the sum of the electricity sold by each MO to DP, LU and other MOs multiplied by the real-time electricity price.

2) ELECTRICITY PURCHASES COST
The MO's cost on power purchases is the sum of the power purchased by each MO from DP, and other MOs multiplied by the real-time electricity price.

C. DPS MATHEMATICAL MODEL
Distributed storage providers can not only inspire ordinary users to actively participate in the electricity market, but also increase the utilization rate of renewable energy and provide the market with more energy sources and purchase channels.
Distributed storage providers mainly use the bid-ask price difference to maximize their profits. On the one hand, when the market lacks electricity, they sell electricity to obtain income ()

DP sell
Pt . On the other hand, they purchase electricity at the lowest cost ()

DP buy
Ct . The specific mathematical description is as follows:

D. ACTUAL MATHEMATICAL MODEL
On the premise of ensuring the balance of power and the continuous and stable operation of the power system, the new round of power reform will open up some power generation and power consumption plans, and establish a peer-to-peer power trading market model. Qualified power generation companies, power sales companies, or large customers can determine transaction capabilities and transaction prices through bilateral or multilateral transactions, thereby realizing the organic connection between dispatching business and market transactions.
The virtual power plant in the microgrid can integrate different resources in the distributed power generation system to form an overall participating power market. At present, due to the intermittent and volatility of renewable energy, when it is directly connected to the grid, it will cause operation disorder and quality degradation [15]. The microgrid can organically combine distributed power generation systems based on renewable energy, user loads, energy storage systems, and control systems [16]. Therefore, an actual transaction model of the microgrid can be constructed to validate the effectiveness of our scheme.
In order to verify the actual market conditions of the three major players of microgrid market transactions. Considering the application of new environmental protection technologies represented by fast-charging energy storage and electric vehicles in the microgrid, wind turbines are added as the operators in this model, and electric vehicles are only used as large users, and electricity is not sold.
Because the charging load of electric vehicles is very random, in order to meet its charging needs, reduce the adverse impact of electric vehicle charging on the distribution network. Access to energy storage systems (ESS) in fast-charging stations can alleviate the adverse effects of uncertain charging loads. At the same time, the use of low-cost electricity during valleys instead of highpriced electricity during peaks is used to cut peaks and fill valleys. ESS acts as the depositor of this model.
The microgrid is composed of 6 wind turbines, 1 electric energy storage system ESS, and 3 electric vehicles [17]. The parameter settings are shown in table 2 to table 4. In the power market environment, the output of each distributed power supply of the virtual power plant is reasonably adjusted, and the optimization decision-making goal of the three major players is to maximize the total profit of the consortium. The objective function is:

A. BASIC PSO
The research focus of this paper is to solve the optimal bidding strategy of each market entity in the microgrid. In microgrid transactions, the game competition among the three major players can be regarded as a multi-objective optimization model. Among them, the constraint condition is to meet the electricity demand of each subject. The objective function is mainly as follows: all major entities obtain the maximum profit; ensure that there is no abandonment of electricity when conducting mutual transactions.
When solving multi-objective optimization problems, basic PSO has the advantages of fast convergence, high accuracy, strong stability, simplicity and generality, and easy implementation. It is highly capable of optimizing complex nonlinear problems and is better suited for solving complex multi-dimensional optimization problems. There is a certain degree of fit with this model, as shown in Fig.2.
But the global searchability of the PSO is strong at the beginning, but the local searchability is relatively weak. With iteration, its global search capability is relatively weakened, while its local search capability is relatively enhanced. This leads to uncertain solutions when solving microgrid transactions, and power abandonment may occur. In order to obtain more accurate and efficient results and make transaction data universal, after improving the PSO algorithm, an optimized particle swarm algorithm (OPSO) can be 9 obtained to solve the microgrid transaction problem under the blockchain.

B. OPTIMIZED PSO
Particle Swarm Optimization (PSO) is a swarm-based optimization tool. The system is initialized as a set of random solutions, and the optimal solution is searched through iteration [19]. The particle swarm optimization algorithm is an optimization algorithm based on the theory of swarm intelligence, which guides the optimization search through the swarm intelligence generated by the whole work and competition among particles in the swarm [20]. Compared with evolutionary algorithms, PSO retains the global search strategy based on population, but its speed-displacement model is simple to operate and avoids complicated genetic operations [21].
Although when solving multi-objective competition problems, particle swarm optimization is a good method. However, the traditional particle swarm algorithm is easy to fall into a local optimal solution, which leads to multiple parties in the game not getting the best profit point. The core of optimizing the particle swarm algorithm is to enhance the searchability and reduce the number of iterations. Therefore, the OPSO can obtain the best results more efficiently and accurately in microgrid transactions.
The specific optimization process is as follows: In a D-dimensional search space, suppose there are U particles in the group, where the position j x of the j-th particle is 12 [ , , , ] j j jD x x x , The position of each particle corresponds to a potential solution. The D-dimensional velocity j v of the j-th particle is 12 [ , , , ] j j jD v v v , and the current optimal position b j s of the j-th particle is 12 [ , , , ] j j jD s s s , the current optimal of the entire particle swarm The position b g s is 12 [ , , , ] g g gD s s s . After the k-th flight, the update speed of the j-th particle is: Updated location: Among them: k j v is the speed of the j-th particle after the k-th flight; 1  k j v is the speed of the j-th particle after the k+1 flight; k j x is the position of the j-th particle after the k-th flight; x is the position of the j-th particle after the k+1 flight; 12 cc is the learning factor;  is the inertia weight; 12 rr is a random number between 0 and 1. To optimized the particle swarm algorithm is to improve the learning factor. The linear dynamic adjustment method is used to calculate the learning factor 12 cc . Compared with the fixed value, the learning speed is accelerated 12 cc are: The specific implementation steps of the improved algorithm are shown in Fig.3.

C. TRADING PROCESS
The OPSO is used to solve the competitive game process in the microgrid market under blockchain technology as follows: 9 (1) Set the initial set of strategies for each game subject to participate in the competitive game. Each subject publishes information in the blockchain network during the time period t, and each subject can use the private key to view other information. For market subject k, n sub-particle swarm individuals are randomly generated within its quotation range. Each particle swarm individual represents a quotation strategy. The price strategy set of subject k in time period t can be expressed as: (2) Each market entity releases purchase and sales information to the blockchain network according to its own needs. The market entity k1 selects another market entity k2 as the transaction object by predicting its own controllable resource adjustment ability within the time period t and then uses the secret key to decrypt all the information of k2. After verifying its authenticity, determine whether the objective function value meets the conditions, if not, the subject k1 converts the transaction object or k2 provides a second offer and enters the loop. (3) Update the objective function value according to the game strategy. If the subject kl accepts the purchase/sale strategy of the subject k2, the two parties reach an agreement to generate a smart contract: .
Ctr sign sign Q t t t P (4) If the subject k2 meets its own needs, the position of the particles in each subgroup is updated. Calculate the local optimal pbest1, pbest2 of the particle, and the global optimal gbest1, gbest2 of each subgroup according to the method of a single objective function. Recombine the new particle swarm to get the maximum profit of this entity and the best quotation between the entities. (5) Until the maximum number of iterations is met, the number of iterations is ; if , go to step 2; otherwise, output the optimal result.

V. CASE ANALYSIS A. TRADING MODEL ANALYSIS
In order to verify the feasibility of the constructed game model, the microgrid market competition game simulation system is constructed in Python. The system selects 3 groups of MOs, 2 groups of DPs, and 3 groups of LUs as the main players in the market game. The subjects compete with each other according to their own goals and stipulate that the electricity purchase price of each game subject is replaced by the corresponding electricity sale price [22]. If the bidding strategy space of each type of market entity is set to be consistent, the bidding strategy range of each type of entity is described as: MOs: 0.5 ~ 1.1 yuan/(kWh); DPs: are 0.5 ~ 0.95 yuan/(kWh); LUs: are 0.5 ~ 1.05 yuan/(kWh).
The 24-hour electricity demand of each entity and the electricity sold by MOs and DPs can be obtained by reviewing the data [23]. The 24-hour electricity demand and supply of each market entity are shown in Fig.4 and Fig.5:

1) MOS ARE THE MOST PROFITABLE
From Fig.6, it can be seen that during the daytime (08:00-23:00) there is a large demand for electricity, and all market entities sell electricity at higher prices. Affected by competition from DPs, there is only a slight difference between the quotations of MOs to LUs and DPs. However, during the period from 00:00 to 07:00, MOs have become the main suppliers of electricity for LUs, and their quotations have been increased accordingly and better benefits have been obtained. Selecting the quotation at 12 noon for analysis, from table 5, it can be seen that the system demand reaches the maximum at this time, and some MOs that lack power will also purchase electricity from other entities. The purchase price from multi-power MOs is 1.05 yuan/(kWh), due to the limited supply capacity of the store, the store's quotation is 0.92 yuan/(kWh). On the premise of maximizing the profit of the operator, the quoted price of the MOs will always be slightly higher than that of the depository in order to obtain high profits.
From Fig.7, the period between 0:00-09:00 for each market entity is the low electricity consumption stage. The major entities have low demand, and each electricity seller will not sell electricity to MOs. DPs need to store power for sale at this time, so MOs can obtain certain benefits by selling power to DPs at this time. There is electricity demand between 11:00-23:00, and it peaks at 12:00 noon. At this time, the MOs is also the main power supplier for LUs.   The game data at 12:00 noon is still selected for analysis, as Fig.8 and table 6. When customizing the bidding strategy for LUs, the electricity price of MOs is set at 1.04 yuan/(kWh), and the price of DPs should not be higher than 1.04 yuan/(kWh). In order to maximize the profits of DPs, the pricing strategy of DPs at this time is also 1.04 yuan/(kWh), and they compete with MOs at the same time. During the period from 0:00-07:00, DPs belong to the charging period of energy storage batteries and purchase electricity from MOs at a lower price to sell during the daytime peak power consumption.

2) DPS ARE THE MOST PROFITABLE
DPs are in a charged state during the time period of 0:00-07:00, and only purchase electricity from MOs at low prices without selling electricity. From Fig.9, in the period from 08:00 to 24:00, it will compete with other market players and formulate the optimal bidding strategy. Among them, electricity is mainly sold to LUs to obtain benefits. DPs will sell electricity to each other according to the situation, and they will also sell electricity to MOs who lack electricity during the noon peak period.

B. SOURCE-STORAGE-LOAD COMPETITION MODEL
The status of each node in the blockchain is equal, and normal operation is jointly maintained through a consensus mechanism. Therefore, under blockchain architecture, the three real-time scheduling transaction models are shown in Fig.10. Through experimental simulation, input the parameters of wind turbines, energy storage systems, and electric vehicles, using an optimized particle swarm algorithm, in the game of small microgrids, output the output power of each subject, and the results are shown in Fig.11 to Fig.13.  In general, wind turbine power generation can be fully utilized, and the energy storage system and electric vehicles coordinate to ensure the stable operation of the microgrid and reduce operating costs. The interaction between source storage and load is in line with following the output of renewable energy, maximizing the use of renewable energy, and enhancing the load experience. The feasibility and effectiveness of the algorithm are verified.
For wind turbines: As the marginal cost of new wind energy is negligible, wind farms, as operators, usually guarantee that all electricity is sold at a low price. However, with the addition of energy storage systems, wind farms will participate more actively in competition. During the low electricity consumption period (0:00-08:00), generate less electricity, and generate more electricity during the peak electricity consumption period (09:00-24:00) to obtain more profits.
For ESS: wind power needs to be stored when power is low and sold during the day to complete. In the case of only considering peak shaving and valley filling, the energy storage system strategically selects storage during the double low period of night load and price. And strategically discharge during the double peak period of load and price to earn the profit difference between the peak and trough of price.
For electric vehicle users: mainly rely on the battery to participate in charging and discharging, mainly charging directly at night. Due to the limited capacity of the battery, it is also necessary to connect to the energy storage system to obtain sufficient power during the day.

C. ALGORITHM VERIFICATION
In order to verify the performance of the (OPSO) in the microgrid game model transaction, this paper uses the ant colony algorithm, genetic algorithm, and traditional particle swarm algorithm to optimize the objective function. Among them, the number of iterations is set to 200. Then take the maximum value of Microgrid operators profit (MOMAX), the maximum value of Distributed storage providers profit (DPMAX), and the minimum value of Large users cost (LUMIN) as the objective function [24]. The results are compared in table 7 below: