## 3 The Structure of Proposed Algorithm
### EMS-DR Unit  
Creating a balance between supply and demand, dropping the courier and reducing the costs by shifting the power from the expensive periods to cheaper periods, are some of the most important effects of DR that the proposed EMS has been able to take advantage of. In this paper, it is assumed that the consumers and H-MGs have already agreed on a short/long-term contract with an energy provider or utility grid, therefore any discussion revolving around the contracts and intellectual property is beyond the scope of this paper. In addition, in this paper, it is not directly investigated the discussion  
Figure 2: Flowchart of the proposed algorithm. of DR schemes benefits for the consumer, however, it emphasizes the positive impact of DR programs on decreasing H-MG emissions and increasing H-MGs profits derived from following the DR schemes. Prior to explaining each DR program, it should be mentioned that each DR scheme including EMS-DRP, EMS-DRE, and EMS-DRPE are applied just for an H-MG throughout the paper. Demand response based on price (EMS-DRP), demand response based on emission (EMS-DRE), and demand response based on emission and price (EMS-DRPE) are used for H-MG#A, H-MG#B, and H-MG#C, respectively. The proposed EMS algorithm has used DR at three levels, described as follows:  
* **EMS-DRP:** At this level of EMS, cost reduction has been considered through the power demand shift. According to Figure 3, each H-MG must first respond to its power demand through embedded non-dispatchable units. If power demand is lower than power generation by non-dispatchable units and the power shortage is higher than the minimum CHP generation, then CHP goes into orbit. Two things happen in this case: (i) due to CHP, the power shortage will be lost and a surplus of power generation will occur; or (ii) the shortage of generation will continue to be less intense. If there is still a shortage of generation, ES will be discharged according to ES capacity and constraints. If there is still a problem with power dissipation in the ES, then according to the DR constraints, power shortages are shifted to other time when the cost of power is cheaper. It should be noted that the EMS only can shift a certain amount of power to future periods, otherwise it would have to generate a power loss through load energy market. In this case, the purchase priority is with other H-MG neighbors, followed by the retailer, due to the fact that H-MGs are able to accrue the profit whilst the lack of purchasing from retailers also reduces the pollution caused by traditional electricity generation systems. With these interpretations, if the CHP enters the circuit and the power generation surplus occurs, the EMS can charge ES according to the constraints of the issue, and respond to the DR whilst selling its excess power in load energy market, which is the priority of purchasing with Other H-MGs and then retailers. For this purpose, H-MGs should position their pre-bid price at a level that is lower than the retailers offer. Flowcharts of the electrical sections are shown in Figures 3a. As depicted in Figure 3b, DR is not considered in the thermal sector. In the thermal sector, in the first place, TSP and CHP have an obligation to respond to the thermal loads. If there is a lack of thermal power, since there is no emission constraint on H-MG #A, then GB enters the circuit and responds to loads. * **EMS-DRE:** In this section, EMS has focused its efforts on reducing pollution using DR. Emax is introduced as a pollution indicator. In more detail, let Y be a finite set of random variable regarding the pollution of a large-scale power network (Emission of Electricity Consumption in UK Power System [44]) with finite outcomes \(y_{1},y_{2},...,y_{n}\). Hence, Emax equals \(E[Y]\) representing the expected value of Y. As shown in Figure 4a, if the need for using CHP arises, it must first be checked that the amount of power shortage is higher than the minimum CHP generation power, and then check whether the CHP inflow circuit exceeds the Emax pollution index.