Economic Operation of Utility-Connected Microgrids in a Fast and Flexible Framework Considering Non-Dispatchable Energy Sources

: This paper introduces a modiﬁed consensus-based real-time optimization framework for utility-connected and islanded microgrids scheduling in normal conditions and under cyberattacks. The exchange of power with the utility is modeled, and the operation of the microgrid energy resources is optimized to minimize the total energy cost. This framework tracks both generation and load variations to decide optimal power generations and the exchange of power with the utility. A linear cost function is deﬁned for the utility where the rates are updated at every time interval. In addition, a realistic approach is taken to limit the power generation from renewable energy sources, including photovoltaics (PVs), wind turbines (WTs), and dispatchable distributed generators (DDGs). The maximum output power of DDGs is limited to their ramp rates. Besides this, a speciﬁc cloud-fog architecture is suggested to make the real-time operation and monitoring of the proposed method feasible for utility-connected and islanded microgrids. The cloud-fog-based framework is ﬂexible in applying demand response (DR) programs for more efﬁciency of the power operation. The algorithm’s performance is examined on the 14 bus IEEE network and is compared with optimal results. Three operating scenarios are considered to model the load as light and heavy, and after denial of service (DoS) attack to indicate the algorithm’s feasibility, robustness, and proﬁciency. In addition, the uncertainty of the system is analyzed using the unscented transformation (UT) method. The simulation results demonstrate a robust, rapid converging rate and the capability to track the load variations due to the probable responsive loads (considering DR programs) or natural alters of load demand.


Background and Motivation
Traditionally, electric power systems are controlled by centralized control units that require access to the entire operational information of units to manage the system [1,2]. The expansion of microgrids has introduced the inclusion of non-dispatchable units, inflexible/un-controllable loads, and a large amount of distributed energy recourses (DERs) along with intelligent participants on the demand side, which have made the centralized economic dispatch (ED) challenging. These challenges may include communication traffic, a computational burden in large-scale systems, a difficulty in real-time monitoring of the grid, preservation of the privacy of the units' information, cybersecurity issues and attacks, and low robustness in terms of communication or agent failures [3,4].
Distributed control methods are recommended as effective alternatives to centralized methods in order to overcome the abovementioned challenges [5]. Distributed algorithms Distributed control methods are recommended as effective alternatives to centralized methods in order to overcome the abovementioned challenges [5]. Distributed algorithms change the traditional form of centralized systems by eliminating the central control unit and sharing its duty with the existing agents in the system. These agents achieve the optimal solution by performing local computation and several data exchanges of their status. A schematic comparison of these two methods is drawn in Figure 1. In distributed systems, the computation burden of the controlling process, imposed on the central control unit in the centralized methods, is shared among all the computing agents situated in various regions. Moreover, in comparison with the centralized methods, the confidential data of controlled units/equipment is conserved in distributed schemes against a solitary unit that possesses the full power of commanding and controlling the whole system and the full knowledge of the status of the entire system components.
In a technical classification, distributed methods can be categorized into three main groups: (1) primal-dual Lagrangian-based approaches [6], (2) schemes based on the alternating-direction method of multipliers (ADMM) [7], and (3) averaging consensus-based methods [8]. The first two categories have a flexible form that can be employed in various optimization problems. Although these methods have some advantages, they are complicated and might not be appropriate for near-real-time operations, which need more energy for distributed calculation in nodes and communication through links. However, the average consensus-based algorithms have an appropriate structure that helps to lead to an optimal solution through a few basic calculations. The average consensus algorithm as a simple and super-fast distributed algorithm is introduced to change traditional systems' centralized control. This algorithm is utilized for computing distributed ED (DED) in the literature (see Section 1.2).
On the other hand, for a microgrid's real-time and distributed operation, we need to design an efficient cyber-physical architecture with fast performance. This should be able to monitor various load demands and the distributed operation feasibility for applying the proposed suggested distributed method in this paper. Meanwhile, we should be aware that cyber-physical systems are threatened by cyberattacks [9]. Thus, the suggested cyber-physical architectures should also be resistive against cyberattacks. The most popular attacks among attackers are denial of service (DoS) and distributed DoS (DDoS) attacks [10], which threaten the cyber-physical systems, and future microgrids are not an exception [11]. Thus, researchers need to consider it an effective type of attack. Cloud computing technology is strongly suggested to have a distributed monitoring architecture with fast response and local processors [12]. Cloud computing provides a distributed architecture with local computers and databases to analyze and store the local data [13].
On the other hand, for overcoming the effect of DoS/DDoS attacks, we need an architecture with a super-fast reaction to perform local and real-time analysis. To this end, fog computing allows us to have on-site computation with the lowest transmission delay. In distributed systems, the computation burden of the controlling process, imposed on the central control unit in the centralized methods, is shared among all the computing agents situated in various regions. Moreover, in comparison with the centralized methods, the confidential data of controlled units/equipment is conserved in distributed schemes against a solitary unit that possesses the full power of commanding and controlling the whole system and the full knowledge of the status of the entire system components.
In a technical classification, distributed methods can be categorized into three main groups: (1) primal-dual Lagrangian-based approaches [6], (2) schemes based on the alternating-direction method of multipliers (ADMM) [7], and (3) averaging consensusbased methods [8]. The first two categories have a flexible form that can be employed in various optimization problems. Although these methods have some advantages, they are complicated and might not be appropriate for near-real-time operations, which need more energy for distributed calculation in nodes and communication through links. However, the average consensus-based algorithms have an appropriate structure that helps to lead to an optimal solution through a few basic calculations. The average consensus algorithm as a simple and super-fast distributed algorithm is introduced to change traditional systems' centralized control. This algorithm is utilized for computing distributed ED (DED) in the literature (see Section 1.2).
On the other hand, for a microgrid's real-time and distributed operation, we need to design an efficient cyber-physical architecture with fast performance. This should be able to monitor various load demands and the distributed operation feasibility for applying the proposed suggested distributed method in this paper. Meanwhile, we should be aware that cyber-physical systems are threatened by cyberattacks [9]. Thus, the suggested cyber-physical architectures should also be resistive against cyberattacks. The most popular attacks among attackers are denial of service (DoS) and distributed DoS (DDoS) attacks [10], which threaten the cyber-physical systems, and future microgrids are not an exception [11]. Thus, researchers need to consider it an effective type of attack. Cloud computing technology is strongly suggested to have a distributed monitoring architecture with fast response and local processors [12]. Cloud computing provides a distributed architecture with local computers and databases to analyze and store the local data [13].
On the other hand, for overcoming the effect of DoS/DDoS attacks, we need an architecture with a super-fast reaction to perform local and real-time analysis. To this end, fog computing allows us to have on-site computation with the lowest transmission delay. Deploying a fog layer into the cloud-based structure is an advised solution for the desired system [14]. Cloud computing technology and its fog computing extension provide a proper environment with quick, flexible, and efficient functionality for distributed systems [15].

Literature Review and Research Gap
There are related studies in the literature to provide optimization schemes regarding various assumptions to ease the ED of microgrids. Some papers present distributed optimization schemes based on the average consensus algorithm as a simple distributed algorithm. To review some papers in this regard, an incremental cost consensus (ICC) algorithm is proposed in [16] for the ED to illustrate distributed control on a smart grid. Several related effects were addressed in this paper, such as the relationship between communication topology and convergence rate. A distributed multi-agent-based framework organized on a three-layer computing architecture for optimal ED in the microgrids is proposed in [17]. A fully distributed ED scheme is presented in [18] for microgrids using an average consensus algorithm. In [19], a distributed method for the ED of islanded microgrids is investigated, designed to be resilient under additive communication noise. Another DED method is suggested in [20] for microgrids considering random time delay.
A common point in these algorithms is that they have been developed for islanded microgrids, and all of their cost functions are assumed to be quadratic. Optimization in the case of quadratic functions seems to reach a global minimum. The conventional consensus DED utilized the first derivative of each DER to update the power productions. However, when the power exchange with a utility is also included in the ED of a microgrid, the cost function model is not quadratic and is considered mostly linear with sale and purchase prices based on a purchasing agreement between the utility and microgrid. Therefore, the first-order derivative cannot help to reach a consensus. This leaves the abovementioned studied consensus-based algorithms ineffective in the case of utilityconnected microgrids. The ramp rate constraints of power generation are not considered in these methods. Moreover, only the power mismatch is considered to reach the new consensus in these consensus algorithms. However, in a utility-connected microgrid, the rate price of the utility can change the optimal states, even if the mismatched power remains at zero.
Some studies try to address parts of the abovementioned challenges. In [21], the distributed ED problem is handled using the diffusion technique considering the linear exchange power of the microgrid with the utility. Furthermore, the uncertainty of loads and solar panels are considered. In [22], a distributed economic dispatch is solved based on the average consensus algorithm considering cyberattacks on the generators and the connection to the main grid. However, the exchanged power is assumed to be quadratic. The dynamic ED problem in [23] is solved using a distributed method considering a non-quadratic cost function and ramp rates for islanded microgrids.
So far, related studies are discussed from the utilized methodology and considered constraints. The other challenging issues in designing a feasible ED framework are the presentation of secure communication and computation infrastructure and modeling the uncertainty of the existing units with random nature. Cloud-based technologies and services are strongly suggested these days by researchers to provide an economical and fast cyber-physical infrastructure for distributed operation and the management of microgrids. In [24], a cloud-edge computing-based framework is presented to perform dynamic economic dispatches, operated on a local digital signal processor chip and a remote cloud computing platform. The dispatch process in the paper is split into two offline and online parts, i.e., offline calculation and real-time decision making. In [25], a centralized threelayer hierarchical control system is designed for controlling microgrids using the internet of things and cloud computing. The cloud-based system detects the islanding mode of the microgrid. A learning-based decision-making framework is proposed in [26] for the economic energy dispatch of an islanding microgrid based on the cloud-edge computing architecture. Cloud resources in these papers are utilized to address the ED decision sequences considering the stochastic nature of renewable generators and load demands. In [27], a microgrid-oriented edge computing architecture is proposed considering some parts such as data processing, security mechanisms, and network communication. An energy sharing cloud mechanism for a smart microgrid in the presence of renewable energy sources is suggested in [28]. The mechanism is an energy-sharing infrastructure consisting of many cloud users and one energy service provider.
All cyber-physical architecture may be threatened by cyberattacks. The future smart microgrids are not an exception and should be resistive against various attacks. As is mentioned, DoS/DDoS attacks, as the most popular type of attack among attackers, must be considered in future cyber-physical microgrid architectures. To overcome this issue in cloud-based architectures, we need superfast communication architecture to detect the malicious point and remove or protect it. Integrating a fog layer (as an extension of cloud computing with quicker functionality) into the cloud architecture is a wise solution suggested in the literature. In [14], a defense framework is suggested using a fog computingbased structure. Furthermore, a DoS detection architecture is also introduced in [29] for intelligent vehicular networks using fog computing.
Due to the unavoidable presence of uncertain sources of generation and energy consumption in microgrids, modeling the uncertainty is a vital issue. To model the uncertainty of stochastic units, i.e., wind turbines (WTs) and photovoltaics (PVs), many schemes are suggested in the literature. One of the most accurate methods in this regard is Monte Carlo [30]. However, it takes lots of time and computing energy to reach the model. Another powerful method with fast functionality and accurate responses is the unscented transformation (UT) introduced [31].
Moreover, it can accurately estimate the output of a nonlinear problem in fewer iterations [32]. A list of the reviewed papers is gathered in Table 1 analyzing the mentioned key features in papers compared to the proposed framework presented in this paper. In the following subsection, the contributions of the paper are comprehensively explained.

Contributions
This paper proposes an algorithm based on a modified average consensus algorithm to be used but not limited to utility-connected microgrids in the presence of dispatchable and non-dispatchable power resources. Both sale and purchase prices of the utility can change at desired preset time intervals. The ramp rate constraints for DDGs are also considered to preserve their safety during sudden changes in load or generation like a cyberattack. In a sudden load drop or during a very light load, the algorithm can curtail a renewable source power if the ramp rate constraints or power limitations of units restrict the output power of other sources. Furthermore, under very heavy loads or after the situation caused by DoS/DDoS attacks, the amount of load demand that should be shed is defined to realize the ramp rate and power limitations. To this end, the effect of DoS/DDoS attacks as common types of cyberattacks are considered and examined in the proposed framework. The method is developed, and its convergence is proved by comparing its performance with the analytical approach under light load, heavy load, and DoS cyberattack. Thus, the method flexibility for systems with demand response (DR) programs is examined through several scenarios. Moreover, a cloud-fog-based architecture is specified to provide a flexible, fast, and private framework for operating microgrid systems. Then, the uncertainty of PVs and WTs are modeled using the UT method, and sensitivity analysis of the proposed algorithm is performed to evaluate the effects of the uncertainty on the overall cost of the system. Therefore, the main contributions of the paper are as follows: • A stochastic distributed ED method is proposed to optimize the cost function of both utility-connected and islanded microgrids. In this method, a new approach is defined to consider the utility price with a linear cost function when the generation units have quadratic cost functions. The presence of non-dispatchable generation units with a stochastic nature is considered in this paper. Furthermore, a new initialization method is presented to find the ED under load, power, and price changes. • A cloud-fog architecture is defined specifically for the proposed distributed method with the capability of fast data communication and local computation. This architecture has a fast response performance in various situations, proper for a secure, private, and real-time operation of utility-connected and islanded microgrids. Furthermore, DR programs can be effectively applied using the suggested architecture in a real-time manner by informing the real-time energy price of the load demands.

•
The proposed distributed framework can curtail power production when needed, especially when the load is small and renewable sources are lush. Besides, to maximize the benefit of the microgrid, a method is designed to optimally export/import power to/from the utility based on the suggested utility price and the incremental cost of generation units. Besides, the impact of denial attacks on the functionality of both the algorithm and the proposed cloud-fog-based architecture is also investigated.

•
The proposed method is evaluated through different scenarios, including (1) under heavy/light load and sudden changes to prove its flexibility in implementing the DR programs and load demand changes in response to the received electricity price signals.
(2) After DoS/DDoS attacks/normal conditions, estimate its flexibility against the denial attacks. Finally (3), after attack condition and revival of the attacked unit to show the resiliency of the proposed framework. Moreover, the performance of the proposed scheme is compared with a precise centralized scheme to evaluate its accuracy.
The rest of the paper is organized as follows: first, the model of a utility-connected microgrid is explained in Section 2. This section presents the cost function of each DG and utility. In Section 3, the overall cost function of the system and its limitations are formalized. The modified consensus-based algorithm is elaborated in Section 4. The distributed architecture of the proposed algorithm is explained in Section 5, and then the principle of the UT method is elaborated in Section 6. Finally, the simulation results under different conditions are presented in Section 7.

Model of Utility-Connected Microgrid
The utility connection allows for the import and export of the electric power of a microgrid with the upstream (utility), hence accomplishing a more realistic ED. In the ED, the electric power supplied by distributed generators (DGs) and utility should be balanced with the load demand to optimize the total cost of power generation.

Distributed Generators
The main elements used to define the cost of electrical energy generation are operating costs, facility construction, and ownership cost. In ED, the operating cost is the most important one, and it is dominated mainly by the fuel cost [33]. DGs mostly fall into dispatchable distributed generators (DDG) and non-dispatchable sources [34]. DDGs have controllable sources of energy to operate, including hydropower energy and thermal energy sources such as oil, coal and gas, diesel, biomass, and hydrogen. Non-dispatchable DGs include WTs and PVs with stochastic output generation due to uncertain inputs such as wind power and sunlight [35].

Dispatchable DGs
The total cost of DDGs is generally modeled as cubic or quadratic functions and can be piecewise linearized [36,37]. In this paper, the cost function of dispatchable DGs is modeled by a quadratic equation as follows [5]:

Non-Dispatchable DGs
Non-dispatchable DGs like PVs and WTs have low operational costs, primarily because of their free input energy source [38]. Therefore, these sources could play an essential role in the ED. However, uncertain renewable energy sources could result in more uncertainty in the microgrid operation. Therefore, from the economic point of view, the maximum output power of renewable sources is assumed to be injected into the grid unless the power mismatch cannot be realized without curtailing the power of these sources.

Utility
In the operation of utility-connected microgrids, unit scheduling and power dispatch among the power supplies in the system and transactions with the utility must be considered. The power exchange with the utility is deemed to be traded in by a constant sale and purchase price during a predefined time interval based on a power purchasing agreement between the utility and microgrid [17]. Therefore, the sales price and the purchase price of the utility must be updated to reflect this fact. The total transaction cost for trading power with the utility, C u , in a time interval is defined as follows: where positive P u means the microgrid collects power from the utility and the negative P u means the microgrid delivers power to the utility. Furthermore, the amount of this power is limited to a certain amount. Herein, both C s and C b are positive price rates that are defined by the utility in each time interval, as well as P u max and P u min . For example, during the night the P u min can be defined as zero, which means utility does not purchase power in that time interval. Note that, in the islanded mode P u max and P u min are adjusted to zero. Due to that, P u becomes zero. Consequently, there would be no power transaction with the upstream, and the microgrid becomes an islanded one.

Problem Formulization
The objective function and its constraints should be defined to calculate the optimal dispatch of microgrid resources.

Objective Function
When the microgrid is connected to the utility, the objective function is a cost function that should involve the technical performance of distributed power supplies, local availability of energy resources, and load demand of the microgrid. Moreover, the electricity transaction between the microgrid and utility should be considered; namely, the system of electricity purchase and sales [39]. Thus, the total cost function of a grid-connected microgrid is as follows: wherein: shows the sum of the cost functions of all DDGs and the term, C t u , shows the cost of power exchanged with the utility. Positive C t u means that microgrid imports power from the utility, negative C t u means the microgrid sells electricity to the utility.

Power Balance
The power generated by all DGs, including the dispatchable and non-dispatchable units combined with the exchange power with the utility, must satisfy the total load known as equality constraint, as follows [40]:

Generation Constraints
All power generations have maximum and minimum limits known as inequality constraints [41]. The inequality constraint for the utility side can be such that it accommodates both the import and the export of the power at each time interval. Therefore, these constraints can define the maximum power exchanged with utility at a specific time interval. The power limits for the DDGs and the utility can be formulated as follows: Equation (8) reflects that the power of renewable units can be curtailed if necessary. Since the renewable units' fuel cost is zero, the optimal point is where these units produce their maximum available power unless the equality constraints cannot be met.

Ramp Rate Constraints
In a realistic system, the power generation increment of a unit is limited by its ramp rate constraints in any interval [42]. This means that the power of a unit, including the utility, cannot instantly step up or step down. Hence, in the time interval of solving the algorithm, the amount of change in generated power can be limited as follows: where, R i and R u are the ramp rate limits of the i-th DDG and the utility in each interval (MW/interval), respectively.

Average Consensus Algorithms
The utilized average consensus-based algorithm is a discrete-time distributed scheme to achieve a consensus among several agents by exchanging data between so-called neighbors. In other words, every agent shares particular data as its consensus variable, which shows its status to the connected agents, considered its neighbors. In this exchange of information, the agent takes the consensus variables of the neighbors to update its status accordingly. This process continues until all participating agents achieve the same consensus variable [43]. In the standard average consensus-based method, one unit is chosen as a leader unit that receives the power mismatch and the neighbor consensus variables to realize the equality constraint. According to the average consensus algorithm, the information updating process of agent i is presented as follows: where x K i is the local information discovered by agent i at iteration K, and x K+1 i is the update of x K i at iteration K + 1. ∆P K is the power mismatch at iteration K. The information update process for the follower agents is performed by (11), and this process for the leader is executed by (12). The overall information exchange process can be modeled as: Different methods exist to determine d ij 's where each results in a different convergence rate. Since the average algorithm has a distributed structure, and since it is compatible with changes in communication network topology and is capable of providing a rapid convergence, the communication coefficients between the agents i and j can be obtained as follows:

The Proposed Method
It has been explained that the load demand must be provided by a combination of all power generation units to minimize the cost of the power generation. What is known from analytical ED is that the minimum cost is reached when the incremental cost of all dispatchable generators for all units that do not exceed the power generation constraints becomes equal. Therefore, it is proposed that the incremental cost of each dispatchable generator be selected as the consensus variable and is updated by the consensus-updating rule by the DDGs as agents. The consensus-updating rule is as follows: As is presented in (15), there is no leader in the proposed method. This means that the leader's duty is split among all the agents, and all generators adjust their incremental cost to compensate for power mismatch. In the proposed framework, the power mismatch is calculated and shared with all the agents as opposed to the fog layer architecture (more information is explained in Section 5). Furthermore, the microgrid Laplacian matrix is defined based on the communication links of the DDGs, excluding the utility, since the cost of the utility is considered linear. Therefore, for each DDG, the incremental cost can be calculated according to this information as follows: Therefore, the power of each generator in discrete form is obtained as follows: The power mismatch at each iteration can be calculated as follows: Unlike the islanded microgrids, the power balance equation of utility-connected microgrids, introduced in (5), cannot be satisfied by merely imposing (15). The reason is that the utility does not contribute to consensus variables. Therefore, utility power should be determined. The following assumption can determine this amount.

Power Import from the Utility
Suppose that the average incremental costs of generators are greater than the purchase price of utility at an operating point. In that case, importing power from the utility makes economic sense, i.e., the utility is cheaper. In this case, the utility power is increased until an optimal point was reached. Meanwhile, increasing utility power decreases the power generation of all dispatchable sources, which is aligned with the path to the optimal operating point. On the other hand, when the average incremental cost of generators is less than the purchase price of the utility, the utility power becomes more expensive, and power imported from the utility should be decreased. Thus, the amount of utility power is determined as follows: A visual explanation of these equations might better demonstrate the utility contribution and import and export decisions. Figure 2 shows the cost function of two generators and the purchase price of the utility. When the incremental costs of generator i(i = 1 or 2), λ i is higher than the purchasing power at the rate C b , i.e., in red parts, a reasonable action is to increase the power purchased from the utility, contributing to other generators and pushing the operating point toward the optimal point. If the incremental cost of generator i is less than C b , i.e., in blue areas where the generator's power rate is less expensive than the utility rate, decreasing the power of the utility makes the incremental costs of dispatchable generators increase and move to an optimal point. 1 or 2), is higher than the purchasing power at the rate , i.e., in red parts, a reasonable action is to increase the power purchased from the utility, contributing to other generators and pushing the operating point toward the optimal point. If the incremental cost of generator is less than , i.e., in blue areas where the generator's power rate is less expensive than the utility rate, decreasing the power of the utility makes the incremental costs of dispatchable generators increase and move to an optimal point.

Power Export to the Utility
According to the assumptions made earlier, the power from the utility can be negative as well. This means that when the microgrid can produce power at a lower cost than the utility, this power can be sold to the utility. The amount of electricity exported to the utility depends on the sale rate of the utility at that time and the incremental cost of the microgrid. It can be obtained as follows:

Triggering the Optimal Search Algorithm
The power mismatch triggers the algorithm to search for new optimal points in the conventional consensus algorithm. However, there might be a case where the power mismatch remains unchanged while the utility price is changed. This rate change might require a new optimal operating point. Therefore, an initial point is used to trigger the algorithm. At point A in Figure 3, the incremental cost of the generator is equal to the utility purchase price. Regardless of the constraints mentioned in (5)-(10), this point is mostly the optimal point at a heavy load.
Similarly, point B is the point at which the incremental cost of the generator is equal to the sale price of the utility and is mostly the optimal point in the light load, since the most probable point of optimal dispatch with considering power constraints is a point with the incremental cost around the utility purchase or sale price (point A or B). Therefore, appropriate initial points of optimal search through consensus are determined as follows:

Power Export to the Utility
According to the assumptions made earlier, the power from the utility can be negative as well. This means that when the microgrid can produce power at a lower cost than the utility, this power can be sold to the utility. The amount of electricity exported to the utility depends on the sale rate of the utility at that time and the incremental cost of the microgrid. It can be obtained as follows:

Triggering the Optimal Search Algorithm
The power mismatch triggers the algorithm to search for new optimal points in the conventional consensus algorithm. However, there might be a case where the power mismatch remains unchanged while the utility price is changed. This rate change might require a new optimal operating point. Therefore, an initial point is used to trigger the algorithm. At point A in Figure 3, the incremental cost of the generator is equal to the utility purchase price. Regardless of the constraints mentioned in (5)-(10), this point is mostly the optimal point at a heavy load.  shows the incremental cost when the power is being purchased from the utility (point A).
shows the incremental cost when the power is sold to the utility (point B). The power mismatch is used to select either of these operations. However, all DDGs inside the microgrid must match either of these rates (A or B) at the optimal point.
In each time interval, the power constraints and the ramp-rate constraints are considered as follows: , ≤ , ≤ , wherein: Similarly, point B is the point at which the incremental cost of the generator is equal to the sale price of the utility and is mostly the optimal point in the light load, since the most probable point of optimal dispatch with considering power constraints is a point with the incremental cost around the utility purchase or sale price (point A or B). Therefore, appropriate initial points of optimal search through consensus are determined as follows: In each time interval, the power constraints and the ramp-rate constraints are considered as follows: wherein: When the maximum power delivered to the utility becomes zero, P u,t min = 0, the utility does not purchase power in that period. Therefore, if, in any situation, the power of all dispatchable units, utility included, reaches their minimum (imposed by either their ramp rate limitations or absolute limitations), then the renewable power should be curtailed. Furthermore, if all dispatchable units provided their maximum power in that period, and the equality constraint is not realized, load shedding can happen. The diagram of the proposed algorithm is demonstrated in Figure 4.

Proposed Distributed Architecture
So far, we have the required formulations for the distributed scheduling of utilityconnected microgrids. The proposed method has the potential to be employed for the realtime operation of microgrids. However, it needs a specific infrastructure adapted to the presented distributed format. In this paper, a cloud-fog architecture is designed to comprise three layers: (1) physical layer, (2) fog layer (3) cloud layer.
The physical layer is the layer of components and equipment such as power lines, generation units, load demands, and controllers. Therefore agents (dispatchable generation units) are situated in this layer and performed based on the achieved consensus. These agents get the consensus by exchanging the consensus variables with their neighboring agents through communication links. The neighbors for each agent are determined randomly at the beginning of every ED time interval by the fog layer.

Proposed Distributed Architecture
So far, we have the required formulations for the distributed scheduling of utilityconnected microgrids. The proposed method has the potential to be employed for the real-time operation of microgrids. However, it needs a specific infrastructure adapted to the presented distributed format. In this paper, a cloud-fog architecture is designed to comprise three layers: (1) physical layer, (2) fog layer (3) cloud layer.
The physical layer is the layer of components and equipment such as power lines, generation units, load demands, and controllers. Therefore agents (dispatchable gener-ation units) are situated in this layer and performed based on the achieved consensus. These agents get the consensus by exchanging the consensus variables with their neighboring agents through communication links. The neighbors for each agent are determined randomly at the beginning of every ED time interval by the fog layer.
Fog computing provides a distributed environment for fast information communication by preparing a wide geographical distribution close to the end-users and local storage and computation units, facilitating the implementation of distributed methods [44]. Thus, we assume that the real-time computations of the proposed method are executed in a fog layer near the generators. This layer is also liable for specifying the topology of the communication links between neighbors. In other words, at the commencing iteration of each interval, the neighboring agents who have a connection with each other are specified. An excellent feature of the fog layer is its ability to present a service to protect the system against cyberattacks. Using fog defender devices as is presented in [14], the DoS attack is detected by this layer. This attack or its distributed peer called DDoS causes a device in the network to be unavailable or out of response using various methodologies [45]. In this paper, we assume that the system's topology after recognizing the DoS/DDoS attack on a unit is changed by the topology specification agent in the fog layer. The under-attack unit is isolated during future intervals until the unit is recovered and ready to work as it is intended to.
The fog layer also collects the real-time consumption data from consumers to compute the power mismatch. The obtained mismatch and the related data are given to the generation units and the utility for updating their real-time statuses. In addition, the achieved electricity price is broadcast to the load demands to change their demand in response to the current price of the power to have a more efficient system using the DR program.
The fog layer is equipped with local computation and storage devices to handle the abovementioned duties. However, these devices have short capacities and are unsuitable for long-term storage and heavy computations like schematization for future maintenance, planning, and prediction analysis. Therefore, we need to transfer the collected real-time data to the cloud layer. Cloud computing provides users with solid databases and computation units adequate for future maintenance, planning, and prediction analysis. The suggested architecture is demonstrated in Figure 5. for long-term storage and heavy computations like schematization for future maintenance, planning, and prediction analysis. Therefore, we need to transfer the collected realtime data to the cloud layer. Cloud computing provides users with solid databases and computation units adequate for future maintenance, planning, and prediction analysis. The suggested architecture is demonstrated in Figure 5.

Unscented Transformation (UT)
The UT method is one of the fast and accurate methods for uncertainty modeling in a nonlinear system. In this method, the uncertainty of a nonlinear problem in the form of = ( ) can be modeled wherein , and represent output and input vectors, respectively, and (. ) is a nonlinear function. Considering uncertain variables with the mean and standard deviation , the mean and covariance of the output vectors ( and ) can be estimated by projecting 2 + 1 sample points of . These sample points can be defined as follows:

Unscented Transformation (UT)
The UT method is one of the fast and accurate methods for uncertainty modeling in a nonlinear system. In this method, the uncertainty of a nonlinear problem in the form of Y = f (X) can be modeled wherein Y, and X represent output and input vectors, respectively, and f (.) is a nonlinear function. Considering m uncertain variables with the mean µ X and standard deviation P XX , the mean and covariance of the output vectors (µ Y and P YY ) can be estimated by projecting 2m + 1 sample points of X. These sample points can be defined as follows: where, W 0 is the first element of the weight coefficient vector, W. The rest of the elements of this vector are calculated such that their summation should be one. In other words, W l is calculated as follows: Finally, the expected mean and covariance matrix of the outputs of the stochastic samples is calculated using UT as follows [31]:

Uncertainty Modeling of WTs and PVs
Penetrating WTs and PVs in power systems has created a significant source of uncertainty in a system due to their stochastic nature. These uncertainties should be considered in a smart grid with high penetration of WTs and PVs. The effect of these uncertainties has been considered in the power mismatch, as stated in (18). The nonlinear relationship between the input wind speed and the output power of a WT can be formulated as follows [46]: where ρ, A, and C p are the air density, sweep area of wind rotor, and power coefficient of the wind turbine, respectively, which presented as a lumped coefficient k w . The nonlinear relationship between PV output power and irradiance and temperature can be considered as follows [47,48]: where, η, S, I, T are the efficiency of PV, Area of PV cells, irradiance, and ambient temperature.

Simulation and Discussion
To model a utility-connected microgrid in the presence of non-dispatchable renewable energy sources, a standard and balanced 14-bus IEEE system has been selected and modified as considered in [49]. The modified system is shown in Figure 6. Three DDGs and the utility are dispatchable sources in this microgrid, and PVs and WTs are non-dispatchable. However, the power of non-dispatchable units can be curtailed if necessary. The algorithm is expected to reach the optimal solution regardless of the loading conditions, generation portfolio, and DoS/DDoS cyberattacks. In each of these conditions, the utility exchange power at a preset rate for sale and purchase, and the ramp rate of all dispatchable units is considered. In these conditions, the algorithm calculated the amount of load that should be shed (such as the situations like DoS/DDoS attack) and the amount of renewable power that should be curtailed (in a light load or a sudden load drop).
patchable. However, the power of non-dispatchable units can be curtailed if necessary. The algorithm is expected to reach the optimal solution regardless of the loading conditions, generation portfolio, and DoS/DDoS cyberattacks. In each of these conditions, the utility exchange power at a preset rate for sale and purchase, and the ramp rate of all dispatchable units is considered. In these conditions, the algorithm calculated the amount of load that should be shed (such as the situations like DoS/DDoS attack) and the amount of renewable power that should be curtailed (in a light load or a sudden load drop).   In time interval 3, the maximum and minimum power exchanged with the utility is considered zero to show the algorithm's capability for the islanded mode.

•
The net load demand, P t D − ∑ m i P non−disp,t i , for time interval 9 is considered the same as time interval 10 to evaluate the initializing method's performance.

•
In time interval 15, the sudden drop of the load is assumed to examine the algorithm under sudden load changes and prove the flexibility of the method for any future probable DR programs.
The cost coefficients of power generation in each DDG and their power limits are provided in Table 2. Moreover, the ramp rates of power generation units considered for three scenarios are listed in Table 2. The cost coefficients of generators are considered constant. The hourly utility power prices are provided in Table 3. These prices are generated randomly in the limitation of the changes in real markets and are based on the generation power prices. Herein, the maximum and minimum allowable powers exchanged with the upstream are also defined hourly. Based on the presented cloud-fog architecture, the communication topology of the neighbors before starting each interval is randomly determined by the fog layer. The total load demand is calculated according to the data assembled from consumers. To avoid DoS/DDoS attack damages, we assume that the fog layer defenders detect the DoS/DDoS attack. After the detection and isolation process, the fog layer determines the new topology for communication links.

Operation under Light Load
In a light load condition of the proposed microgrid, the maximum power demand can reach 225 MW. In this study, this load is expected to change at intervals of 1 h. However, DED can be performed within smaller time intervals using the proposed fast and flexible framework. The hourly load distribution is shown in Figure 7. The power generation portfolio contains a sector of non-dispatchable resources, which is shown in Figure 8. As Figure 8 shows, power generation is from two sources: solar and wind. The extent of wind power generation in the intervals varies from 15 MW to 30 MW. The photovoltaic power generation is available during the daytime and can reach a maximum of 30 MW. These are the power ratings of the renewable energy sources considered in the modified IEEE power system and can be curtailed if necessary. These are the power ratings of the renewable energy sources considered in the modified IEEE power system and can be curtailed if necessary. The results from the proposed consensus-based approach are compared with the precise centralized method of the interior point method [50]. Figure 9 compares running these two optimization techniques for a light-load day. The figure demonstrates that the consensus-based approach reaches the ED quickly (solving all intervals within 0.0390 s compared to 1.8737 s for analytical methods). Each time interval in Figure 9 consists of 50 iterations. It is seen that most intervals take fewer than 10 iterations (equivalent to 0.2 on time axes) to reach the ED.   The results from the proposed consensus-based approach are compared with the precise centralized method of the interior point method [50]. Figure 9 compares running these two optimization techniques for a light-load day. The figure demonstrates that the consensus-based approach reaches the ED quickly (solving all intervals within 0.0390 s compared to 1.8737 s for analytical methods). Each time interval in Figure 9 consists of 50 iterations. It is seen that most intervals take fewer than 10 iterations (equivalent to 0.2 on time axes) to reach the ED.   Since several DDGs are involved in this power generation, their economic contributions are shown in Figure 10. In interval three, the amount of power exchanged with utility is purposely considered zero, and the algorithm can find the optimum points even for an islanded microgrid. During intervals 9 and 10, the net load demand ( − ∑ − , ) is considered the same, but the price rate of the utility changes.    Since several DDGs are involved in this power generation, their economic contributions are shown in Figure 10. In interval three, the amount of power exchanged with utility is purposely considered zero, and the algorithm can find the optimum points even for an islanded microgrid. During intervals 9 and 10, the net load demand ( − ∑ − , ) is considered the same, but the price rate of the utility changes. Since several DDGs are involved in this power generation, their economic contributions are shown in Figure 10. In interval three, the amount of power exchanged with utility is purposely considered zero, and the algorithm can find the optimum points even for an islanded microgrid. During intervals 9 and 10, the net load demand (P t D − ∑ m i P non−disp,t i ) is considered the same, but the price rate of the utility changes. The initialization method helps the algorithm find the optimal points in this condition. Furthermore, in interval 15, the load suddenly dropped, and this result is also verified by the standard ED, also shown in Figure 10. It should be noted that the contribution of the photovoltaic and the wind, combined, made a significant contribution to lowering the cost of energy production in the microgrid. The power mismatch in Error! Reference source not found.) is calculated such that the renewable sources are considered with their maximum available power, as demonstrated in Figure 11. Since, during the first three intervals, the power of all sources is at their minimum and the utility in this time does not purchase much power, the calculated power mismatch, by Error! Reference source not The initialization method helps the algorithm find the optimal points in this condition. Furthermore, in interval 15, the load suddenly dropped, and this result is also verified by the standard ED, also shown in Figure 10. It should be noted that the contribution of the photovoltaic and the wind, combined, made a significant contribution to lowering the cost of energy production in the microgrid. The power mismatch in (36) is calculated such that the renewable sources are considered with their maximum available power, as demonstrated in Figure 11. Since, during the first three intervals, the power of all sources is at their minimum and the utility in this time does not purchase much power, the calculated power mismatch, by (36), is greater than zero, and this amount is the extent of renewable power that should be curtailed. The initialization method helps the algorithm find the optimal points in this condition. Furthermore, in interval 15, the load suddenly dropped, and this result is also verified by the standard ED, also shown in Figure 10. It should be noted that the contribution of the photovoltaic and the wind, combined, made a significant contribution to lowering the cost of energy production in the microgrid. The power mismatch in Error! Reference source not found.) is calculated such that the renewable sources are considered with their maximum available power, as demonstrated in Figure 11. Since, during the first three intervals, the power of all sources is at their minimum and the utility in this time does not purchase much power, the calculated power mismatch, by Error! Reference source not found.), is greater than zero, and this amount is the extent of renewable power that should be curtailed. Figure 11. The incremental cost of generators and utility rates.

Operation in Heavy Load
The heavy load condition in the microgrid is shown in Figure 7. The peak power of 371 MW can be met by the total power generation capabilities within the microgrid. Therefore, it is expected that under the normal operating condition, when there is no cyberattack, a portion of the power would be provided by the utility until it makes economic sense. The consensus-based approach is examined to respond to this operating condition, and its performance is compared with the precise centralized method of an interior point method. Figure 11. The incremental cost of generators and utility rates.

Operation in Heavy Load
The heavy load condition in the microgrid is shown in Figure 7. The peak power of 371 MW can be met by the total power generation capabilities within the microgrid. Therefore, it is expected that under the normal operating condition, when there is no cyberattack, a portion of the power would be provided by the utility until it makes economic sense. The consensus-based approach is examined to respond to this operating condition, and its performance is compared with the precise centralized method of an interior point method. Figure 12 shows the overall cost for the total power generated at any time interval from the proposed algorithm and the standard ED. As the figure shows, the results of the power generation optimization converge in a couple of iterations (fewer than 10 iterations and solving all intervals within 0.0360 s compared to 1.8628 s for analytical methods). The power generation by each DDG and the utility exchange are shown in Figure 13.  Figure 12 shows the overall cost for the total power generated at any time interval from the proposed algorithm and the standard ED. As the figure shows, the results of the power generation optimization converge in a couple of iterations (fewer than 10 iterations and solving all intervals within 0.0360 s compared to 1.8628 s for analytical methods). The power generation by each DDG and the utility exchange are shown in Figure 13.    The load of the microgrid is high, especially during the daytime, and since the price of utility power is high, buying an amount of power that is less than its maximum from the utility makes economic sense. This decision is based on the incremental cost of generation from the power source compared to the utility rates. Looking at Figure 13, the power is imported from the utility from all intervals other than time interval 15. In time interval 15, the load suddenly dropped; therefore, the surplus of production is exported to the utility. However, this drop of load causes the ramp rate limitation and the power limitations of units to meet, and as a result, in time intervals 16 and 17, a small amount of load should be shed, as shown in Figure 14. In time interval 17, the primary reason that causes load shedding is the ramp rate limit of the utility. The load of the microgrid is high, especially during the daytime, and since the price of utility power is high, buying an amount of power that is less than its maximum from the utility makes economic sense. This decision is based on the incremental cost of generation from the power source compared to the utility rates. Looking at Figure 13, the power is imported from the utility from all intervals other than time interval 15. In time interval 15, the load suddenly dropped; therefore, the surplus of production is exported to the utility. However, this drop of load causes the ramp rate limitation and the power limitations of units to meet, and as a result, in time intervals 16 and 17, a small amount of load should be shed, as shown in Figure 14. In time interval 17, the primary reason that causes load shedding is the ramp rate limit of the utility. As mentioned earlier, there may be cases in which the power mismatch remains zero, but the rates from the utility might have changed. This needs to be picked by the algorithm, and proper decisions would be made to optimize the cost. This is purposely scheduled on intervals 9 and 10 with a zero power mismatch. This means that the value for − ∑ − , in these intervals is the same as if the generation units have no change. However, the proposed algorithm optimizes the generation output powers since the utility rates change. Furthermore, during time interval 3, the microgrid becomes islanded, and the algorithm operates properly.

Optimization during Denial of Service Attack
Cyberattacks on a power system may make the units irresponsive to the consensus shared and requests made. In this scenario, it is assumed that the DDG2 is isolated from the system due to a DoS/DDoS attack in the time interval 11 (it can happen during an interval). The fog layer would change the system's topology and wait for the DDG2 recovery. After five intervals, the cyberattack is removed, and the unit rejoins the microgrid As mentioned earlier, there may be cases in which the power mismatch remains zero, but the rates from the utility might have changed. This needs to be picked by the algorithm, and proper decisions would be made to optimize the cost. This is purposely scheduled on intervals 9 and 10 with a zero power mismatch. This means that the value for P t D − ∑ m i P non−disp,t i in these intervals is the same as if the generation units have no change. However, the proposed algorithm optimizes the generation output powers since the utility rates change. Furthermore, during time interval 3, the microgrid becomes islanded, and the algorithm operates properly. but the rates from the utility might have changed. This needs to be picked by the algorithm, and proper decisions would be made to optimize the cost. This is purposely scheduled on intervals 9 and 10 with a zero power mismatch. This means that the value for − ∑ − , in these intervals is the same as if the generation units have no change. However, the proposed algorithm optimizes the generation output powers since the utility rates change. Furthermore, during time interval 3, the microgrid becomes islanded, and the algorithm operates properly.

Optimization during Denial of Service Attack
Cyberattacks on a power system may make the units irresponsive to the consensus shared and requests made. In this scenario, it is assumed that the DDG2 is isolated from the system due to a DoS/DDoS attack in the time interval 11 (it can happen during an interval). The fog layer would change the system's topology and wait for the DDG2 recovery. After five intervals, the cyberattack is removed, and the unit rejoins the microgrid and responds to the requests.
The communication links between the DDGs before the DoS/DDoS attack areas and during the attack are shown in Figure 15. While under attack, the power generation from DDG 2 went down to zero. A consensus is reached among the rest of the units and the utility. This way, the attacked system cannot pull the entire dispatch down. However, the consensus point developed in this paper reached the optimal point in limited communication developed by the elements. Figure 16 shows how the consensus is reached, and the power generation is set while generation unit 2 is under DoS/DDoS attack. The utility is used to import power, and once the system is restored, the power is sold back to the utility.

Uncertainty Analysis
The system with the heavy load (second scenario) is considered the sample case for uncertainty analysis. In the presence of the WT and PV, this system is solved using the proposed algorithm and modeling the uncertainty utilizing the UT method. The aim is to evaluate the effect of the uncertainty on the overall cost. The parameters of the WT and PV are shown in Table 4, and wind speeds, irradiance, and temperature are UT inputs. the nominal values of the wind speed ( ) and irradiance ( ) are considered as 11 m/s and 1000 W/m 2 respectively. The normalized mean of the wind speed and irradiance over all time intervals are shown in Figure 17. Herein, the variance of the wind speed is considered to be 3.147 m/s, and the variance of the irradiance is considered 20% of its mean at each time interval. The deterministic overall cost of the system is 11,459 $ in this scenario. The

Uncertainty Analysis
The system with the heavy load (second scenario) is considered the sample case for uncertainty analysis. In the presence of the WT and PV, this system is solved using the proposed algorithm and modeling the uncertainty utilizing the UT method. The aim is to evaluate the effect of the uncertainty on the overall cost. The parameters of the WT and PV are shown in Table 4, and wind speeds, irradiance, and temperature are UT inputs. the nominal values of the wind speed (V r ) and irradiance (I r ) are considered as 11 m/s and 1000 W/m 2 respectively. The normalized mean of the wind speed and irradiance over all time intervals are shown in Figure 17. Herein, the variance of the wind speed is considered to be 3.147 m/s, and the variance of the irradiance is considered 20% of its mean at each time interval. The deterministic overall cost of the system is 11,459 $ in this scenario. The sensitivity analysis result of the stochastic variables on the system cost is shown in Figure 18 for 60% of fluctuation in standard deviations. In this case, the cost of the system only changes by less than 0.13% (16 $).

Uncertainty Analysis
The system with the heavy load (second scenario) is considered the sample case for uncertainty analysis. In the presence of the WT and PV, this system is solved using the proposed algorithm and modeling the uncertainty utilizing the UT method. The aim is to evaluate the effect of the uncertainty on the overall cost. The parameters of the WT and PV are shown in Table 4, and wind speeds, irradiance, and temperature are UT inputs. the nominal values of the wind speed ( ) and irradiance ( ) are considered as 11 m/s and 1000 W/m 2 respectively. The normalized mean of the wind speed and irradiance over all time intervals are shown in Figure 17. Herein, the variance of the wind speed is considered to be 3.147 m/s, and the variance of the irradiance is considered 20% of its mean at each time interval. The deterministic overall cost of the system is 11,459 $ in this scenario. The sensitivity analysis result of the stochastic variables on the system cost is shown in Figure  18 for 60% of fluctuation in standard deviations. In this case, the cost of the system only changes by less than 0.13% (16 $).

Conclusions
This paper introduced a real-time distributed consensus-based algorithm for utilityconnected and islanded microgrids in the presence of dispatchable and non-dispatchable power plants. The utility charge rates change in a specified time interval. The algorithm considered the ramp rate constraints on power generation increment and the realistic utility cost function to achieve a real-time algorithm. The proposed method also defined a secure cloud-fog architecture, which enabled the DR program. The proposed secure cloud-fog layer makes it possible to have local computations in the lower layer (here, the fog layer) and heavy computations in the higher level (cloud layer). The algorithm's functionality, extent, and robustness were evaluated under three scenarios, including light and heavy loads and during a DoS/DDoS attack. The results demonstrated that regardless of

Conclusions
This paper introduced a real-time distributed consensus-based algorithm for utilityconnected and islanded microgrids in the presence of dispatchable and non-dispatchable power plants. The utility charge rates change in a specified time interval. The algorithm considered the ramp rate constraints on power generation increment and the realistic utility cost function to achieve a real-time algorithm. The proposed method also defined a secure cloud-fog architecture, which enabled the DR program. The proposed secure cloud-fog layer makes it possible to have local computations in the lower layer (here, the fog layer) and heavy computations in the higher level (cloud layer). The algorithm's functionality, extent, and robustness were evaluated under three scenarios, including light and heavy loads and during a DoS/DDoS attack. The results demonstrated that regardless of the communication links or the responsiveness of a particular power generation unit, the economic dispatch could reach a consensus in a few iterations that proves the algorithm's fast response and flexibility in different conditions. The simulation results advocate the high security and recovery capability of the proposed distributed cloud-fog framework and its flexibility to be operated adequately in demand changes in response to the electricity price alterations that facilitate future EDs considering DR programs. In this paper, network constraints are neglected due to simplification, which is suggested to be considered in future studies. It is suggested that real microgrids are considered in future studies. The set of the time interval Parameters: Cost function coefficients of i-th dispatchable DG C s (C t s ) Utility sale price at interval t C b (C t b ) Utility purchase price at interval t P i max , P i min Maximum and minimum power generation of i-th dispatchable DG P u max , P u min Maximum and minimum power generation of utility P u,t max , P u,t min Maximum and minimum power generation of utility at time interval t R u The maximum ramp rate of utility R i The maximum ramp rate of i-th DDG (w/s) d ij Communication coefficient between agent i and j N i Indices of agents that communicate with agent i D Stochastic matrix of microgrid graph L Laplacian matrix of microgrid graph Cut-in, cut-out, and rated wind speed of the wind turbine v Wind speed Variables The output power of i-th DDG at the k-th iteration of interval t P non−disp,t i The output power of i-th non-dispatchable DG at interval t P i,t max , P i,t min Maximum and minimum power generation of i-th DDG at the k-th iteration of interval t P u,t max , P u,t min Maximum and minimum Power generation of utility at the k-th iteration of interval t ∆P t,k Power mismatch at the k-th iteration of interval t C u (C t u ) Utility cost (at interval t) C t T The total cost of the microgrid at interval t P u P t u Utility power (at interval t) P t u,k Utility power at the k-th iteration of interval t P t D Demand at interval t λ k i The incremental cost of i-th Dispatchable DG at iteration k λ k The average of incremental costs at iteration k λ Lagrange coefficient for equality constraint