A Two-Stage Optimal Scheduling Model of Microgrid Based on Chance-Constrained Programming in Spot Markets

Aimed at the coordination control problem of each unit caused by microgrid participation in the spot market and considering the randomness of wind and solar output and the uncertainty of spot market prices, a day-ahead real-time two-stage optimal scheduling model for microgrid was established by using the chance-constrained programming theory. On this basis, an improved particle swarm optimization (PSO) algorithm based on stochastic simulation technology was used to solve the problem and the effect of demand side management and confidence level on scheduling results is discussed. The example results verified the correctness and effectiveness of the proposed model, which can provide a theoretical basis in terms of reasonably coordinating the output of each unit in the microgrid in the spot market.


Introduction
Compared with the medium-term and long-term market, the spot market of electric power mainly includes electricity commodity trading with finer time granularity within a few days before and during the day [1], which can form a time-of-use electricity price reflecting the real production cost of commodities and represent the actual situation of power supply and demand. Thus, it can improve the timeliness of power resource allocation and play a vital role in the power market system [2,3].
With the issuance of the Notice on Carrying out the Pilot Work of Power Spot Market Construction by the National Development and Reform Commission (NDRC), the construction of China's power spot market has officially started. As of June 2019, the first batch of eight power spot market pilot projects have all started their simulation test run, which indicates that the construction of a power spot market with Chinese characteristics has achieved positive results. Under the background of accelerating the construction of a power spot market, research on how to guide the reasonable decision-making of transaction participants can be conducive to not only achieving the maximum economic benefits of each subject, but also the entire market's efficient allocation of resources, which can ensure the orderly economic operation of the spot market and has important theoretical significance and practical value [4][5][6].
The spot market transaction mainly includes two kinds of participants: the power generation subject and power consumption subject. As the traditional thermal power units are more involved in the peak load regulation auxiliary service market [7], the research on spot market decision-making for power generation focuses on renewable energy producers. However, renewable energy generation has the characteristics of being intermittent and uncertain, which leads to the failure of renewable energy generation companies to formulate transaction declaration strategies by the declaration model of conventional power generation enterprises. In view of the above problems, the literature [8] assumes that wind power generation enterprises are the price receivers in the spot market, and fully considering the uncertainty of the wind turbine (WT) output and clearing price, the optimal output model of wind power plants by using stochastic simulation technology can be constructed. In [9], based on the study in [8], a penalty coefficient was introduced to punish the deviation between the real-time market output and daily market declared volume. Taking the maximum total profit as the objective function, a stochastic optimization model of wind power business in the two-stage market was constructed. This model fully considers multiple market stages and uncertain factors, further optimizing the declaration strategy of wind power suppliers.
In addition, with the progress of energy storage technology, a variety of energy storage system (ESS) has been continuously studied to reduce the renewable energy output's uncertainty. In [10,11], by virtue of the ESS's flexible charging and discharging function, the coupling between the ESS and the wind farm was realized. By establishing the linear affine function between the electricity market price, the wind power predicted output deviation, and the power of the ESS charge and discharge, the stochastic optimization declaration strategy of WT was proposed. In [12], considering both the randomness of the wind power output and the uncertainty of the spot price, a hybrid power plant's two-level stochastic optimization model consisting of wind power suppliers and electric vehicles was proposed, which could effectively reduce the bidding risk and increase the profit of the hybrid power plant.
In research on the spot market decision-making of consumers, the main goal is to minimize the purchase cost of the electricity sellers or large users. In [13], a short-term planning model was proposed to predict the load curve under the real-time electricity price, which fully considers the fluctuation of the user load and market electricity price. A day-ahead decision-making model of the electricity sellers based on stochastic optimization was established. In the study by [14], based on [13], the demand response of sensitive users was introduced, considering the uncertainty of the spot price and the consumer behavior of users. The day-ahead market quotation strategy model of selling by the electricity supplier was constructed. In [15,16], based on robust optimization and stochastic mixed integer programming, the optimal power purchase decision-making and quotation decision-making models for the day-ahead market of selling by the electricity supplier were proposed.
The above research on the optimization decision of the spot market only involves a single subject and fails to form the link between the generation side and the power consumption side. As the link between the power load and distributed energy in a microgrid integrates multiple system resources, it can simultaneously carry out energy supply and demand response, realizing the efficient allocation of power resources, so it has unique advantages in the process of participating in the spot market. In [17], aiming at maximizing the real-time market profit of the microgrid, each unit of the microgrid's bidding strategies were studied under two bidding mechanisms. However, the demand response of the microgrid load was not considered and the users' potential was not fully explored. In [18], the price response model of a sensitive load was introduced. Aimed at minimizing the expected net cost, a hybrid stochastic robust optimization model was proposed. However, the penalty cost caused by an unbalanced power in the real-time market of microgrid was ignored, and research on the two-stage market transaction of day-ahead and real-time is lacking. To sum up, there have been a few studies on microgrids participating in spot market transactions, and there are still some deficiencies.
Compared with the existing research, the innovation of this paper lies in the following points. First, in the microgrid spot market decision-making issues, it fully considers the fluctuation of the stochastic micro-source output and spot market electricity price and discusses the economics and reliability of simultaneous optimization on both sides of supply and demand. Second, it employs stochastic simulation technology to transform the optimal scheduling problem into a problem with maximum system revenue under a certain confidence level and also uses the particle swarm optimization (PSO) algorithm to solve it effectively. Third, it optimizes the microgrid scheduling by changing the objective function and constraining the confidence level to meet the decision-makers of different risk preferences.
The rest of this manuscript is organized as follows. In Section 2, considering the uncertain output of the WT and photovoltaic (PV), a microgrid energy management system integrating wind-PV-load-storage was established, which is the technical basis for building a two-stage scheduling model of a microgrid. Section 3 discusses the process of the microgrid participating in the spot market, according to the operation rules of China's power spot market and microgrid construction methods. On this basis, a microgrid two-stage optimal scheduling model based on the stochastic chance constraint was constructed, which takes the predicted power of renewable energy generation and the predicted price of the spot market as random variables. In Section 4, the stochastic simulation technology is used to deal with the random variables in the model, and then a PSO algorithm suitable for solving this model is proposed. Section 5 introduces the simulation results, verifies the validity of the model, and discusses the effect of demand side management and confidence level on scheduling results. Section 6 highlights the main conclusions of this paper.

Microgrid System Model Considering Randomness of Power Supply Output
A microgrid is a small-scale power generation and distribution system including distributed power supply, user load, ESS, and control system. Each unit is connected to each other by power electronic devices and is connected to the public power grid through static switches, which can realize a flexible conversion between the grid connection and grid disconnection. The structure of the microgrid system is shown in Figure 1. maximum system revenue under a certain confidence level and also uses the particle swarm optimization (PSO)algorithm to solve it effectively. Third, it optimizes the microgrid scheduling by changing the objective function and constraining the confidence level to meet the decision-makers of different risk preferences.
The rest of this manuscript is organized as follows. In Section 2, considering the uncertain output of the WT and photovoltaic (PV), a microgrid energy management system integrating wind-PV-load-storage was established, which is the technical basis for building a two-stage scheduling model of a microgrid. Section 3 discusses the process of the microgrid participating in the spot market, according to the operation rules of China's power spot market and microgrid construction methods. On this basis, a microgrid two-stage optimal scheduling model based on the stochastic chance constraint was constructed, which takes the predicted power of renewable energy generation and the predicted price of the spot market as random variables. In Section 4, the stochastic simulation technology is used to deal with the random variables in the model, and then a PSO algorithm suitable for solving this model is proposed. Section 5 introduces the simulation results, verifies the validity of the model, and discusses the effect of demand side management and confidence level on scheduling results. Section 6 highlights the main conclusions of this paper.

Microgrid System Model Considering Randomness of Power Supply Output
A microgrid is a small-scale power generation and distribution system including distributed power supply, user load, ESS, and control system. Each unit is connected to each other by power electronic devices and is connected to the public power grid through static switches, which can realize a flexible conversion between the grid connection and grid disconnection. The structure of the microgrid system is shown in Figure 1.
As shown in Figure 1, random micro-sources with uncertain output such as WT and photo voltaic are given priority to generate power to provide the electrical energy required for internal loads. ESS improves the power supply rejection ratio of random micro-sources through low storage and high-discharge, and controllable micro-sources such as diesel generator (DG) provide backup services when random micro-sources and the ESS cannot meet the load demand.

Stochastic Output Model of Wind Turbine
Due to the influence of wind energy quality in different periods, the output power of WT often tends to be intermittent. Through statistical research on a large number of measured data, the two-parameter Weibull distribution is widely used to fit the distribution of the actual wind speed [19], and the probability density function can be expressed as: As shown in Figure 1, random micro-sources with uncertain output such as WT and photo voltaic are given priority to generate power to provide the electrical energy required for internal loads. ESS improves the power supply rejection ratio of random micro-sources through low storage and high-discharge, and controllable micro-sources such as diesel generator (DG) provide backup services when random micro-sources and the ESS cannot meet the load demand.

Stochastic Output Model of Wind Turbine
Due to the influence of wind energy quality in different periods, the output power of WT often tends to be intermittent. Through statistical research on a large number of measured data, the two-parameter Weibull distribution is widely used to fit the distribution of the actual wind speed [19], and the probability density function can be expressed as: where v is the wind speed; k is a shape parameter, and its value fluctuates in the range of 1.8-2.8 according to the specific situation of wind energy resources; and c is a scale parameter reflecting the average wind speed during the period. The relationship between the output power of the WT and the actual wind speed v is as follows: where P WT is the output power of the WT; v ci and v co represent the cut-in wind speed and the cut-out wind speed, respectively; p r is the rated power of the WT; and v r is the rated wind speed.

Stochastic Output Model of Photovoltaic
Like wind energy resources, the light and temperature received by PVs will constantly change due to the characteristics of solar radiation distribution, which results in the great uncertainty regarding its output. Related research shows that the solar illumination intensity approximately obeys a Beta distribution in a certain period of time [20]. Its probability density function can be expressed as: where l is the solar irradiance; a l and b l are the shape parameters; and l max is the maximum irradiance. The relationship between the output power of the PV unit and the solar irradiance can be expressed as: where P PV is the output power of PV units; S PV is the total area of PV units; and η is the photoelectric conversion efficiency of PV units.

Controllable Power Supply Output Model
Distributed power generation with a controllable output is generally composed of small power generation equipment such as DG and a gas engine generator, and has the advantages of a fast start-stop speed (only 15 s) and stability. It often provides backup assistance for microgrid systems when the new energy unit and the ESS cannot meet the load demand. Therefore, its power generation is jointly determined by the user power load, the output of the new energy unit, and the ESS discharge power. By using linear function to simplify the operation cost of the controllable micro-sources and ignoring its climb and start-stop constraints [21], the following cost functions and constraints can be established: 0 ≤ Q c,n,s,τ ≤ Q R c,n,s − Q R c,n,s−1 0 ≤ Q c,n,1,τ ≤ Q R c,n,1 where is the number of all-day spot market hours; S is the total number of segments of piecewise linear operation cost; C c is the operating cost of the controllable unit; N is the total number of controllable units; C A c,n,s is the average power generation cost of unit n at the segmentation of s; Q c,n,s,τ is the power generation capacity of unit n at the segmentation of s in the time period of τ; and Q R c,n,s is the rated power generation capacity of unit n at the segmentation of s.

Energy Storage System Model
ESS can realize the energy transfer of a microgrid system through ESS, and release and optimize the microgrid power output curve to adapt to the price change in the spot market and ensure the economic operation of the microgrid. Under the current energy storage technology conditions, the microgrid ESS still mainly uses battery energy storage. The state of charge can be expressed as: where SOC τ is the state of charge of battery energy storage in the time period of τ; Q b,τ is the charge and discharge amount of the ESS in the time period of τ (Q b,τ is positive for battery discharge and negative for battery charge); η in and η out are the charge and discharge efficiencies respectively; and W b is the battery energy storage capacity. The charging and discharging constraints and power constraints of ESS [22] can be expressed as: where SOC min is the lower limit of the state of charge, preferably 0.2; and SOC max is the upper limit of the state of charge, preferably 0.9. The operating cost of the ESS can be expressed as follows: where C b is the depreciation loss cost generated by battery energy storage's charging and discharging and µ b is the battery loss coefficient.

User Demand Response Model
In order to avoid the uncertain risks brought by the spot market and realize the optimal allocation of power resources, a microgrid can use demand side management to encourage electricity users to change their electricity consumption behaviors in order to optimize the load curve. Among them, the TOU (time-of-use) price is one of the most effective ways to guide users to participate in load regulation. According to micro-economic principles, the relationship between electricity consumption and electricity price can be described by the elasticity of power price [23]. The transfer of electricity consumption by users due to participation in the TOU demand response can be expressed as: where Q 0 τ , Q τ , P 0 τ , and P τ are respectively the user's electricity consumption and electricity price in the time period of τ before and after the demand response; P 0 ς and P ς are the user's electricity price at the hour of ς before and after the demand response, respectively; e ττ and e ςτ are the self-elasticity and the cross-elasticity, respectively; and ∆Q τ and ∆P τ are the change of electricity consumption and electricity price in the time period of τ, respectively. The cost of implementing demand side management for a microgrid can be expressed as:

The Mechanism of a Microgrid's Participation in Spot Market Trading
Based on the research on the operation scheme and the simulated trial operation of power spot markets in Guangdong, Zhejiang, and the Western Economic Zone of Inner Mongolia, China's power spot market operation pilot generally adopts the way of "day-ahead + real-time" for settlement. The day-ahead market starts on the day before the actual delivery of electricity, and power generation enterprises and power users sequentially declare the electricity price curve and the electricity demand curve. The electric power control center shall conduct Market clearing and settle according to the clearing results. The real-time market is the balance of forecasting errors in the day-ahead market, which is carried out before the actual delivery of electricity. The declaration of the market participants is similar to that of the day-ahead market. Finally, the power control center carries out real-time market clearing and settles the deviation power according to the real-time clearing price.
According to the Proposed Measures for Grid-connected Microgrid Construction issued by the State Energy Administration of China, a microgrid can be used as a power selling company with the distribution grid management right to participate in the spot market transactions of electric power. Therefore, the research object is the grid-connected microgrid that participates in the spot market transaction as the seller of electricity.
As the main seller of electricity, the microgrid participates in the spot market transaction including two settlement stages: day-ahead market and real-time market. The day-ahead market will be launched one day in advance, the microgrid will only declare the electricity, not price, and settle the electricity consumption curve according to the clearing result. Based on the actual operation results of the trading day, the real-time market will settle the deviation between the actual electricity consumption and the bid winning curve of the day-ahead market, according to the real-time price.
In addition, the State Energy Administration issued "the regulatory measures for the full purchase of renewable energy power by power grid companies", which requires power grid companies to purchase all the on-grid power of renewable energy power generation enterprises except for the market trading power, which means that the extra power of new energy used for spontaneous self-use can be sold to power grid companies.
Therefore, the energy interaction between the microgrid and power grid involved in spot market transactions mainly includes the daily market declared power, real-time market deviation power, and on-grid power. At the same time, the microgrid will generate operating costs and electricity sales revenue, which will affect the total revenue. The settlement process of the microgrid participating in spot market transactions is shown in Figure 2. It is necessary to fully consider the uncertainty of the spot market price and the randomness of the power supply output, and optimize and schedule various adjustable resources in the microgrid to maximize the benefits of participating in the spot market.
power, and on-grid power. At the same time, the microgrid will generate operating costs and electricity sales revenue, which will affect the total revenue. The settlement process of the microgrid participating in spot market transactions is shown in Figure 2. It is necessary to fully consider the uncertainty of the spot market price and the randomness of the power supply output, and optimize and schedule various adjustable resources in the microgrid to maximize the benefits of participating in the spot market.

Market Clearing Price Forecast
Since the market clearing electricity price can be expressed in the form of an equal time series, the autoregressive AR (p) model is introduced to predict the spot market price. Based on the foreign electricity market transaction data and related scholars' research as well as the fact that the information criterion function can be used to select the order of the AR (p) model as 1, the clearing electricity price predicted by the day-ahead market can be expressed as: where , DA c P τ and

Microgrid Day-Ahead Scheduling Model Based on Chance-Constrained Programming
In order to participate in spot market transactions, microgrid needs to consider many uncertain factors to achieve economic scheduling. As an important branch of stochastic programming theory, chance constrained programming allows the optimal scheduling results not to meet the constraints to some extent, but requires the probability of the constraints to be established not less than a certain level of confidence. Thus, we can deal with the random variables in microgrid scheduling by softening the constraints, which can be expressed as follows:

Market Clearing Price Forecast
Since the market clearing electricity price can be expressed in the form of an equal time series, the autoregressive AR (p) model is introduced to predict the spot market price. Based on the foreign electricity market transaction data and related scholars' research as well as the fact that the information criterion function can be used to select the order of the AR (p) model as 1, the clearing electricity price predicted by the day-ahead market can be expressed as: where P DA c,τ and P DA,D−1 c,τ are the market clearing electricity prices in the time period of τ in the day-ahead market in the current day and the previous day, respectively; C(τ) is the average value of the market clearing electricity price in the time period of τ in the day-ahead market; φ 1 is the autoregressive coefficient; and ε τ is the white noise sequence with a mean of 0 and a variance of σ 2 .

Microgrid Day-Ahead Scheduling Model Based on Chance-Constrained Programming
In order to participate in spot market transactions, microgrid needs to consider many uncertain factors to achieve economic scheduling. As an important branch of stochastic programming theory, chance constrained programming allows the optimal scheduling results not to meet the constraints to some extent, but requires the probability of the constraints to be established not less than a certain level of confidence. Thus, we can deal with the random variables in microgrid scheduling by softening the constraints, which can be expressed as follows: where x and ε are the decision variables and the random variables, respectively; f (x, ε) is the objective function; g m (x, ε) is the random chance constraint; g n (x, ε) ≤ 0 is the conventional constraint; α and β are the confidence levels; and f is the expected value of the objective function, that is, the minimum value of the objective function when the probability is not lower than α.
As for the microgrid, the day-ahead optimal scheduling embodies the maximum comprehensive expected income. In other words, the output of each system is optimized to form the optimal power demand curve for each period by accurately forecasting the load and the day-ahead clearing price in order to achieve the maximum profit in the day-ahead market competition. Therefore, the following chance-constrained programming model can be established. The superscript DA of all symbols in the first stage optimization model represents the day-ahead market stage and the superscript RT of all symbols in the second stage optimization model represents the real-time market stage.
(1) The objective function where R DA s is the market clearing income of the microgrid day-ahead market; P s,τ Q DA τ represents the revenue from the electricity sale of the microgrid in the τ period (the object of electricity sale is the users); P DA c,τ Q DA r,τ represents the power purchase expenditure of the microgrid in the day-ahead market in the τ period; P on Q DA on,τ represents the proceeds from the sale of the remaining electricity of the microgrid to the grid company in the τ period (the object of sale is the grid company); P s,τ and Q DA τ are the users' electricity price and the predicted response electricity in the time period of τ, respectively; P DA c,τ and Q DA r,τ are the market clearing electricity price and of the day-ahead market and the microgrid day-ahead declared power in the time period of τ, respectively (Q DA r,τ is the electricity purchase demand declared by the microgrid in the day-ahead market of the day before the trading day); and P on and Q DA on,τ are the microgrid on-grid electricity price and the predicted on-grid power in the time period of τ, respectively.
The microgrid electric quantity balance constraint is: where P DA WT,τ and P DA PV,τ are the day-ahead predicted power of WT and PV units in the time period of τ, respectively; and 24 is the unit time period of the spot market, which is used to approximately convert power into electricity.
Due to factors such as the stochastic output of WT and PV and the uncertainty of the spot price, the microgrid cannot guarantee that the declared electricity quantity is completely consistent with the required electricity quantity in the day-ahead forecast. However, a certain confidence level must be satisfied. Therefore, the power balance constraint can be converted into a chance constraint: where σ is the allowable deviation of unbalanced power.

2.
The microgrid interactive power constraint (Internet access and power purchase cannot be carried out simultaneously) is: 3.
The stochastic micro-source output constraint is:  (24) where P R WT and P R PV are the rated power of the WT and PV unit, respectively. 4.
The battery energy storage state and charge/discharge rate constraint are Equations (8) and (9). 6.
The demand response power balance constraint is:

The Real-Time Market Microgrid Scheduling Model
In the real-time market stage, penalty fees will be charged for the deviation between the actual purchased electricity in the real-time market and the declared electricity in the day-ahead market in order to encourage users to reasonably declare the day-ahead demand curve and prevent them from making profits by exploiting loopholes in the market rules. Therefore, in the process of participating in the real-time market, microgrids should consider the penalty fee of deviation power in addition to forecasting the electricity prices in order to achieve optimal scheduling. The optimal scheduling model (where the real-time electricity price forecasting methods and the conventional constraints are consistent with the day-ahead scheduling, and will not be described again) can be expressed as: where R RT s is the real-time market income based on the day-ahead market's income; C RT p is the deviation electricity penalty fee; Q RT τ is the actual power consumption after the user participates in the demand response in the time period of τ; Q RT on,τ is the on-grid energy of the microgrid in the time period of τ; Q RT a,τ is the actual power purchased by the microgrid in the time period of τ; P RT c,τ is the real-time market's electricity price in the time period of τ; and P p is the unit electricity penalty fee.

The Solving of Model
In the first stage, two kinds of uncertain factors (i.e., clearing price and output of WT in the day-ahead market) are dealt with by using the prediction model and stochastic simulation method. Then, the PSO algorithm was used to find the optimal solution of each unit's output and the declared electricity quantity in the day-ahead market, and the result was input to the next stage.
The second stage uses the same method to deal with the real-time market price and the output of the random power supply of the microgrid. Taking the optimization result of the first stage and the day-ahead market clearing price as the known quantity, we introduced the penalty cost of the electricity quantity's deviation. Aimed at the maximum revenue, the PSO was used to obtain the optimal scheduling results of the microgrid in the real-time market. The specific solution methods of the first stage or the second stage are as follows:

Stochastic Simulation
In view of the existence of chance constraints in the established model, stochastic simulation technology was introduced to process variables, and the steps are as follows: (1) Solve the objective function. (1) Randomly generate mutually independent variables ε k (k = 1, 2, . . . , n), according to the probability distribution of random variables in the objective function; (2) calculate a function value f k = f (x, ε k ); and (3) set n = αn, and the n th maximum in f 1 , f 2 , . . . , f n is the target value according to the law of large numbers. (2) Test the chance constraint. (1) Set n = 0; (2) randomly generate a variable ε, according to the probability distribution of random variables in constraint conditions; (3) calculate g(x, ε), and n = n + 1, if it is less than or equal to 0; (4) after repeating steps (2) and (3) for n times, the constraint holds if n/n ≥ β.

Particle Swarm Optimization Algorithm
After dealing with random variables, the PSO based on the Monte Carlo simulation was used to solve the model. The specific process is as follows: (1) Initialize data input. Input the deterministic values such as the controllable micro-source, battery cost parameters and load demand as well as confidence level and algorithm control parameters. (2) Population initialization. With the output of WT and PV and the spot electricity price as random variables, random variables are generated from probability distribution to form an initial population in which all particles satisfy the constraint under the constraint condition test and by using the above random simulation technology, and the population position and velocity are initialized. (3) The population target value is obtained as the fitness, and the individual extremum and global extremum are judged based on the fitness. (4) Update the particle position and velocity. Iterative calculation is carried out to generate a new generation of particle swarms. The position and velocity are updated as follows: where Ψ is the inertia weight; v i ρ and L i ρ are the velocity and position of the ith iteration of particle ρ, respectively; l 1 and l 2 are learning factors; r 1 and r 2 are random numbers between [0, 1]; and P i ρOT and g i ρOT are the individual extremum of the ith iteration of particle ρ and the global extremum of the population, respectively. (5) Repeat steps (2)-(4) to search for the optimal solution until the maximum iteration numb is reached. Output that optimal solution and the corresponding particle position.
Compared with deterministic programming problem solving, the solution method used in this paper first deals with the uncertain factors in the model. On this basis, the PSO algorithm, which can find the optimal solution in a certain range, was used to solve the model. The flow chart (Figure 3) of the PSO algorithm combined with stochastic simulation technology can be shown as follows.
(5) Repeat steps (2)-(4) to search for the optimal solution until the maximum iteration numb is reached. Output that optimal solution and the corresponding particle position.
Compared with deterministic programming problem solving, the solution method used in this paper first deals with the uncertain factors in the model. On this basis, the PSO algorithm, which can find the optimal solution in a certain range, was used to solve the model. The flow chart ( Figure  3) of the PSO algorithm combined with stochastic simulation technology can be shown as follows.

Convergence Test
In this paper, the PSO was improved by stochastic simulation technology to solve the two-stage optimal scheduling model of a microgrid based on chance-constrained programming. The convergence curve of the PSO can be shown in Figure 4.

Convergence Test
In this paper, the PSO was improved by stochastic simulation technology to solve the two-stage optimal scheduling model of a microgrid based on chance-constrained programming. The convergence curve of the PSO can be shown in Figure 4.

Basic Data
A microgrid that integrated with WT, PV, DG, and ESS, was selected to simulate its

Basic Data
A microgrid that integrated with WT, PV, DG, and ESS, was selected to simulate its participation in the spot market trading process to verify the rationality of the proposed model. The total output power of the DG in the microgrid was 1 MW, and the operation cost parameters of units are shown in Table 1. Based on the power curve of a typical load day, the user load was converted into electricity consumption in each period, as shown in Table 2. The other parameters used in the empirical analysis are shown in Table 3. It was assumed that all purchased power generated by the microgrid in the spot transaction process was all from the spot market. The actual data of a certain day in the Pennsylvania-New Jersey-Maryland (PJM) electric power market were used to forecast the day-ahead clearing price and the real-time price, and was taken as 24. By using the autoregressive model in Section 3.2.1, we could obtain the day-ahead market price and real-time market price, which are shown in Figure 5. Considering that the granularity of the day-ahead market clearing in China is 15 min, it was assumed that the forecast electricity price is the arithmetic average of four 15-min clearing rates per hour.
It was assumed that all purchased power generated by the microgrid in the spot transaction process was all from the spot market. The actual data of a certain day in the Pennsylvania-New Jersey-Maryland (PJM) electric power market were used to forecast the day-ahead clearing price and the real-time price, and ϖ was taken as 24. By using the autoregressive model in Section 3.2.1, we could obtain the day-ahead market price and real-time market price, which are shown in Figure  5. Considering that the granularity of the day-ahead market clearing in China is 15 min, it was assumed that the forecast electricity price is the arithmetic average of four 15-min clearing rates per hour.

Scheduling Operation Analysis
We set the confidence level of the objective function and the constrain condition as 0.9. While the particle swarm size was 80, the learning factor was 2, and the inertia weight was 0.8. The maximum number of iterations was 200. First, the demand response model was eliminated to optimize the microgrid, and the first-stage scheduling model was run for a comparison with the forecast electricity price of the day-ahead market ( Figure 6, ME is the interactive power between the microgrid and main network).

Scheduling Operation Analysis
We set the confidence level of the objective function and the constrain condition as 0.9. While the particle swarm size was 80, the learning factor was 2, and the inertia weight was 0.8. The maximum number of iterations was 200. First, the demand response model was eliminated to optimize the microgrid, and the first-stage scheduling model was run for a comparison with the forecast electricity price of the day-ahead market ( Figure 6, ME is the interactive power between the microgrid and main network). As can be seen from Figure 6, during the period when the forecast electricity price level is low, the microgrid tends to declare more electricity to meet its own electricity demand and continuously charge the ESS. With the increase in electricity price, the DG output increases and gradually replaces As can be seen from Figure 6, during the period when the forecast electricity price level is low, the microgrid tends to declare more electricity to meet its own electricity demand and continuously charge the ESS. With the increase in electricity price, the DG output increases and gradually replaces the spot market electricity to fill the shortage of the new energy electricity supply capacity. When operating to a higher electricity price level, the ESS starts to discharge and provides backup assistance jointly with DG. At this time, the microgrid will basically no longer purchase electricity from the spot market, but will instead put the remaining electricity on the grid. During the full load day, the total electricity declared in the day-ahead stage was 3934.21 kWh, which accounted for 11.97% of the forecast power on that day. The maximum electricity of DG per unit period was 392.56 kW. Compared with the total output electricity of 1 MW, there was still a large margin to bear the random fluctuation of the spot market electricity price. It can be seen that through the first stage of market optimization scheduling, the microgrid has high reliability while being economically optimal.
By continuing to run the second-stage scheduling model, the calculation results are shown in Figure 7. Additionally, a comparison of the two-stage scheduling results can be obtained, which is shown in Figure 8. As can be seen from Figures 7 and 8, in the real-time market scheduling stage, the operation plans of ESS and DG experienced severe fluctuations in some time periods, while the planned purchase curve of the spot market (two stage power purchase decision optimization curve, i.e., the part of the ME curve > 0 in Figure 8) fluctuated slightly. Its maximum fluctuating power per unit period was only 8.63 kWh. This is because in the second stage, when the predicted electricity price and the stochastic micro-source predict the output change, the microgrid comprehensively considers the operation cost of each unit and the deviation power penalty fee and chooses to actively adjust the ESS and DG unit with relatively small variable costs to adapt to the changes of uncertain factors and realize the maximum economic benefit of microgrid.

The Impact Analysis of Demand Side Management
In order to further improve the optimal allocation of power resources, the demand side management was introduced to solve the model with the advantage that microgrid users can easily adjust load. Suppose that the microgrid uses the time-of-use electricity price to guide users to transfer load. The electricity price elasticity matrix adopts the elasticity matrix of large industrial users in this paper [24][25][26]. With the current sales price as the normal period electricity price, γ is defined as the floating proportion of the sales price. While the peak electricity price rises by 1 + γ on this basis, the valley electricity price is lowered by 1 − 1.2γ. The time division is shown in Table 4. Let γ float between [0, 0.8] in each step of 0.05 to obtain the microgrid real-time market income change as shown in Table 5. The results in Table 4 show that when the microgrid participates in spot market transactions, the implementation of demand side management can significantly improve its own economic benefits. With the increase of peak-to-valley price difference, the income shows a trend of increasing first and then decreasing. When the floating ratio was 0.55, the profit reached its highest. By analyzing the cost of each factor under different electricity prices, it can be found that after the implementation of time-of-use electricity price, the user reduces the electricity bill expenditure by transferring more flexible loads. The load transfer also promotes the coupling of the power consumption curve and renewable energy output, changes the co-directional distribution of the microgrid power declaration curve and the spot market electricity price, then reduces the reserve cost of the microgrid during peak hours and reduces the average spending on purchasing electricity. However, with the increasing peak-to-valley electricity price ratio, the sensitivity of users to electricity price decreases until it has been "saturated". Since then, the benefit increment of the microgrid due to the user demand response starts to be lower than the electricity bills saved by users, and the revenue declines. By setting the electricity price floating ratio to 0.55, the comparison of the day-ahead scheduling results before and after demand side management implementation of the microgrid, and the comparison of the two-stage scheduling results after the implementation of demand side management can be shown in Figure 9, Table 6 and Figure 10, respectively. cost of the microgrid during peak hours and reduces the average spending on purchasing electricity. However, with the increasing peak-to-valley electricity price ratio, the sensitivity of users to electricity price decreases until it has been "saturated". Since then, the benefit increment of the microgrid due to the user demand response starts to be lower than the electricity bills saved by users, and the revenue declines. By setting the electricity price floating ratio to 0.55, the comparison of the day-ahead scheduling results before and after demand side management implementation of the microgrid, and the comparison of the two-stage scheduling results after the implementation of demand side management can be shown in Figure 9, Table 6 and Figure 10, respectively.     Figure 10. The comparison of the two-stage optimal scheduling results (demand side management).
As shown in Figure 9 and Table 6, after demand side management is implemented in the microgrid, the user load curve and the microgrid scheduling unit output curve both tend to be flat, compared with before. The specific changes are as follows: The maximum electricity consumption per unit period of the user was reduced by 319.51 kWh, the minimum electricity consumption increased to 289.68 kWh, and the maximum declared electricity per unit period was reduced from 635.32 kWh to 582.89 kWh. While the maximum output power of the DG decreased from 547.41 kW to 387.56 kW, the maximum charging capacity of the ESS per unit period decreased from 422.79 kWh to 218.55 kWh, and the maximum discharge decreased from 378.69 kWh to 139.35 kWh. This is because under the stimulation of demand side management, the users in the microgrid can reduce the maximum electricity consumption and increase the minimum electricity consumption by changing the electricity demand in the peak and valley period to realize the peak load shifting. The change in load demand makes the output of DG and charge and discharge behavior of EES change correspondingly.
Additionally, by comparing Figure 10 with Figure 8, the adjustments in the real-time scheduling phase of the microgrid have decreased compared with the previous ones, and the ability of the microgrid to participate in the spot market to respond to changes in uncertain factors has increased after the implementation of demand side management.

Confidence Level Impact Analysis
The influence of confidence level on the microgrid's two-stage optimal scheduling was analyzed. Set α and β change between 0.8-1, respectively, and run the microgrid scheduling model and real-time market microgrid scheduling model in Section 3, respectively. Under different confidence combinations, we can obtain the microgrid revenue curve of the day-ahead market and real-time market, as shown in Figures 11 and 12. It can be seen from Figures 11 and 12 that when the confidence level of objective function and constraint condition is low (the red part in the figure), As shown in Figure 9 and Table 6, after demand side management is implemented in the microgrid, the user load curve and the microgrid scheduling unit output curve both tend to be flat, compared with before. The specific changes are as follows: The maximum electricity consumption per unit period of the user was reduced by 319.51 kWh, the minimum electricity consumption increased to 289.68 kWh, and the maximum declared electricity per unit period was reduced from 635.32 kWh to 582.89 kWh. While the maximum output power of the DG decreased from 547.41 kW to 387.56 kW, the maximum charging capacity of the ESS per unit period decreased from 422.79 kWh to 218.55 kWh, and the maximum discharge decreased from 378.69 kWh to 139.35 kWh. This is because under the stimulation of demand side management, the users in the microgrid can reduce the maximum electricity consumption and increase the minimum electricity consumption by changing the electricity demand in the peak and valley period to realize the peak load shifting. The change in load demand makes the output of DG and charge and discharge behavior of EES change correspondingly.
Additionally, by comparing Figure 10 with Figure 8, the adjustments in the real-time scheduling phase of the microgrid have decreased compared with the previous ones, and the ability of the microgrid to participate in the spot market to respond to changes in uncertain factors has increased after the implementation of demand side management.

Confidence Level Impact Analysis
The influence of confidence level on the microgrid's two-stage optimal scheduling was analyzed. Set α and β change between 0.8-1, respectively, and run the microgrid scheduling model and real-time market microgrid scheduling model in Section 3, respectively. Under different confidence combinations, we can obtain the microgrid revenue curve of the day-ahead market and real-time market, as shown in Figures 11 and 12. It can be seen from Figures 11 and 12 that when the confidence level of objective function and constraint condition is low (the red part in the figure), the economic benefit of the microgrid is better. With the continuous increase of the objective function and constraint condition's confidence level, the economic benefit shows a downward trend. When the confidence level is high (the purple part in the figure), the economic benefit of the microgrid is worse. With the improvement of the confidence level, the requirements for the establishment of the objective function and constraints expressed in probability form are gradually strict, and the power balance constraint range is reduced. It is not conducive to the calculation of better optimization results, but conducive to improving the reliability of microgrid operation. the economic benefit of the microgrid is better. With the continuous increase of the objective function and constraint condition's confidence level, the economic benefit shows a downward trend. When the confidence level is high (the purple part in the figure), the economic benefit of the microgrid is worse. With the improvement of the confidence level, the requirements for the establishment of the objective function and constraints expressed in probability form are gradually strict, and the power balance constraint range is reduced. It is not conducive to the calculation of better optimization results, but conducive to improving the reliability of microgrid operation.  In addition, compared with Figures 11 and 12, it can be seen that with the improvement in confidence level, the economic benefit of the microgrid's day-ahead market scheduling declines faster than real-time market scheduling's. This is because there are relatively more uncertainty factors faced by the day-ahead market, and the economic cost of improving microgrid reliability is greater than that of the real-time market.
Therefore, in the two-stage scheduling optimization process of the microgrid, the improvement the economic benefit of the microgrid is better. With the continuous increase of the objective function and constraint condition's confidence level, the economic benefit shows a downward trend. When the confidence level is high (the purple part in the figure), the economic benefit of the microgrid is worse. With the improvement of the confidence level, the requirements for the establishment of the objective function and constraints expressed in probability form are gradually strict, and the power balance constraint range is reduced. It is not conducive to the calculation of better optimization results, but conducive to improving the reliability of microgrid operation.  In addition, compared with Figures 11 and 12, it can be seen that with the improvement in confidence level, the economic benefit of the microgrid's day-ahead market scheduling declines faster than real-time market scheduling's. This is because there are relatively more uncertainty factors faced by the day-ahead market, and the economic cost of improving microgrid reliability is greater than that of the real-time market.
Therefore, in the two-stage scheduling optimization process of the microgrid, the improvement in the confidence level will make the test of objective function and constraints stricter, enhancing In addition, compared with Figures 11 and 12, it can be seen that with the improvement in confidence level, the economic benefit of the microgrid's day-ahead market scheduling declines faster than real-time market scheduling's. This is because there are relatively more uncertainty factors faced by the day-ahead market, and the economic cost of improving microgrid reliability is greater than that of the real-time market.
Therefore, in the two-stage scheduling optimization process of the microgrid, the improvement in the confidence level will make the test of objective function and constraints stricter, enhancing the reliability of microgrid operation. At the same time, however, it will lead to a certain degree of decline in the economic benefits of the microgrid, and the economic benefits decline faster in the current market stage due to greater uncertainty. Therefore, the decision maker can choose the confidence level considering the risk aversion degree, and adjust the balance between the microgrid economy and reliability, thus achieving optimal scheduling.

Conclusions and Policy Implications
According to the basic structure of a microgrid, a microgrid system model considering the stochastic output of WT and PV was established in this paper. On this basis, considering various uncertain factors comprehensively and with the aim of maximizing the benefits of a microgrid system at a certain confidence level, a two-stage scheduling model based on chance-constrained programming was put forward, combined with the characteristics of the spot market. The purpose of this paper was to make a two-stage optimal scheduling plan for microgrids participating in the electricity spot market by optimizing the power supply side and load side simultaneously, and improving the economic benefits of the microgrid system. The following conclusions can be drawn: (1) The microgrid can effectively avoid potential risks by using the model proposed in this paper to make spot market decisions. Through two-stage optimal scheduling, it can deal with the fluctuation of WT and PV output and electricity price by adjusting system resources, which can both achieve higher reliability and pursue maximum benefits. (2) The demand side management should be introduced at the time of optimizing the supply-side resources. Thus, the two-stage economic benefits of the microgrid can be improved, the reserve capacity and declared power during the peak period of day-ahead market can be lowered, the output adjustment of the real-time market to each unit can be reduced, and then the microgrid's ability to resist risks can be significantly improved. (3) The confidence level will have a certain impact on the results of the two-stage scheduling of the microgrid. While risk-prone people running the model with a low confidence level can obtain better economic benefits, risk-averse people running the model with a high confidence level can be conducive to ensuring the safety and stability of the microgrid. Charge and discharge capacity of ESS in τ period (kWh) η in , η out Charge and discharge efficiency respectively W b Battery capacity (MWh) SOC min Lower limit of charged state SOC max Upper limit of charged state u b Battery loss factor Q 0 τ User power consumption in the τ period before demand response (kWh) Q τ User power consumption in the τ period after demand response (kWh) P 0 τ User electricity price in the τ period before demand response ($/kWh) P τ User electricity price in the τ period after demand response ($/kWh) P 0 ς User electricity price in the ς period before demand response ($/kWh) P ς User electricity price in the ς period after demand response ($/kWh) e ττ Self-elasticity e ςτ Cross elasticity ∆Q τ Change of user power consumption in the τ period (kWh) ∆P τ Change of electricity price of users in the τ period ($/kWh)

C(τ)
Average value of market clearing electricity price in the time period of τ in the day-ahead market ($/kWh) φ 1 Autoregressive coefficient ε τ White noise sequence σ Allowable deviation of unbalanced power P R WT Rated power of WT (MW) P R PV Rated power of PV unit (MW) P p Unit electricity penalty fee ($/kWh) ψ Inertia weight v i ρ Velocity of the ith iteration of particle ρ L i ρ Position of the ith iteration of particle ρ l 1 , l 2 Learning factors r 1 , r 2 Random numbers between [0, 1] P i ρOT Individual extremum of the ith iteration of particle ρ g i ρOT Global extremum of the population