An Optimal Home Energy Management Paradigm With an Adaptive Neuro-Fuzzy Regulation

In the smart grid paradigm, residential consumers should participate actively in the energy exchange mechanisms by adjusting their consumption and generation. To this end, a proper home energy management system (HEMS), in addition to achieving a high level of comfort for the consumers, should handle the practical difficulties due to the uncertainty and technical limits. With this aim, in this paper, a new HEMS is proposed to carry out day-ahead management and real-time regulation. While an optimal scheduling solution based on some forecasted values of uncertain parameters is achieved for day ahead management, real-time regulation is accomplished by an adaptive neuro-fuzzy inference system, which can regulate the gaps between the forecasted and real values. Investigated case studies indicate that the proposed HEMS can find an optimal operating scenario with an acceptable success rate for real-time regulation.


adjust
Superscript to discern variables and parameters related to the real-time session

A. Aims and Motivation
The smart grid is an intelligent grid that features smart metering technologies, modern power converters, rapid communication infrastructure, automation, and consumer participation [1]. These technologies are helpful to fulfill the efficient and sustainable operation of power systems and to meet some of their long-term challenges [2,3]. However, in addition to the opportunities, the smart grid presents new challenges in the field of electrical sciences. Development of home area networks for the intelligent operation of residential end-users is one of the challenges. The energy consumption of the residential sector accounts for around 30-40% of the total energy use all over the world [4]. The consumption is expected to get higher shortly due to population growth and housing expansion along with the deployment of smart home appliances. Furthermore, to conform with the policies of the smart grid paradigm, the researchers tend to change the role of the end-user in the chain of the electric energy system from a passive consumer to an active market player [5]. To this purpose, end-users become prosumers and should participate more actively in energy exchange mechanisms by adjusting their consumption patterns and by managing their own available generation devices.

B. Literature Review and Background
Developing an optimal model of a home energy management system (HEMS) has recently received many attentions in the literature. It is evident from past studies that consumers are interested in saving money through time-of-use pricing and price signals [6]. In response to this attention, demand response (DR) became a real option. DR is an opportunity for consumers to play a crucial role in the operation of the electric grid by reducing or shifting their electricity usage during the required periods in response to time-based tariffs or other forms of financial incentives. In [3], real-time scheduling of residential appliances in the HEMS is discussed. The conditional value-at-risk is utilized to make a trade-off between the expected costs and the risk that the system faces the uncertainties of the local generations and other factors. In [7], a customer solely participates in the DR event by tracking a demand curtailment request and duration. In [8], an incentive-based consumption management system is aiming to achieve a trade-off between minimizing the payment and the waiting time for the operation of each household appliance based on the users' needs. In [9], a HEMS is modeled as an eventdriven binary linear programming problem. To consider the dynamics of the consumption for a household, the optimization process is executed each time when DR messages are received. In [10] and [11], a smart home is managed using an optimization method that considers dynamic prices. In [12], a household load profile under variable prices is investigated by considering the existence of smart appliances and real-time electricity prices.
In [13], a residential energy management model is proposed to schedule the local generations solely. In [14][15][16][17], small-scale renewable generations' management is considered alongside scheduling of home appliances in the HEMS problem. These papers study the energy scheduling of smart houses, which are equipped with a solar panel. In [18], an energy management system optimizes energy scheduling in a residential smart microgrid considering energy consumption cost and user's comfort preferences. In [19], a stochastic dynamic programming problem is formulated to optimize the power allocation among an electrical vehicle (EV) battery, home power demand, and the utility grid. The strategy, mainly focusing on EV, incorporates probability distributions of trip time and trip length. In this study, the uncertainties are described by scenarios generation. In [5], a stochastic model of a HEMS by considering uncertainties of EV availability and smallscale renewable energy generation is proposed. Different DR programs are simulated, and the results from the point of customer's cost and inhabitants' satisfaction are analyzed. In [20], a multi-agent control structure is proposed for coordinated management of energy and comfort in an integrated building and microgrid system. A deep-learning-based strategy for real-time management of energy storages (ESs) is adopted to mitigate the effects of uncertain parameters on the system operation. A distributed framework for HEMS based on cost minimization is proposed in [21]. In [22], a home management system is developed on a multi-agent system platform in which the communications and interactions among agents were implemented based on IoT principles. In [23], a management system based on the Internet of Things (IoT) is proposed to monitor and control devices remotely and generate online bill via a mobile application. In [24], ignoring the intermittent behavior of renewable generations, a metaheuristic based model is proposed to manage an IoT enabled smart home.
In [25], an intelligent fuzzy-based management system is presented to find the best energy-efficiency scenario in smart home applications. In this study, an associativeneural-network-based model combined with fuzzy rules to make an adaptive neuro-fuzzy inference system (ANFIS). It is shown that ANFIS can be a convenient solution for a home energy management problem.

C. Contributions
Reviewing the literature shows that a considerable share of the studies has provided impressive models for smart HEMS. However, this appraisal indeed reveals the need for a model to tackle the deficiencies of previous studies. In sum, ignoring local generations, lack of efficient operating strategies against uncertain parameters, neglecting user comfort, considering homogeneous assumption for all appliances, controlling a limited number of devices and the complexity of the control systems are the main defects that can be listed for the researches mentioned above. The scope of this work is to propose a model of a HEMS which can address these issues as much as possible. Accordingly, the intended model is aiming to jointly schedule various household appliances, renewable energy resources, and bidirectional operation of ES under DR strategies. Besides, the model should be able to track the aggregator demand control request for pre-specified hours. It should contain a proper approach to handle the gap between forecast and real-time values for uncertainty parameters as well. Besides, the output of the model should be reliable from the point of optimality. Therefore, in this paper, a two-stage model of a HEMS is proposed. The first stage produces an optimal day-ahead scheduling solution based on the forecasted values of uncertain parameters. The second stage, whose core is an adaptive neuro-fuzzy inference system combined with an optimization-based training pattern, is able to regulate the gaps between the forecasted and real values. The key novel contributions of the method proposed in this paper can be summarized as follows: 1.
proposing a two-stage model of a HEMS considering optimal day-ahead management and an adaptive real-time correction mechanism to deal with the forecast errors, 2.
incorporating the uncertainties of the distributed renewable resources and loads, the aggregator demand control request, the dissatisfaction cost of inhabitants and the degradation cost of the battery into the scheduling of smart houses, 3. integrating an adaptive neuro-fuzzy inference system combined with an optimization-based training pattern into the HEMS to provide proper real-time operation under sudden changes of working conditions due to the presence of uncertain parameters.

D. Paper Organization
The remainder of the paper is organized as follows: in Section II, the outline of the proposed HEMS paradigm is presented. The mathematical formulation of the proposed method is presented in Section III. Section IV contains numerical studies and discussion. Section V presents the conclusions of the paper.

II. The outline of the proposed home energy management paradigm
In this section, the operating strategies and proposed energy management structure are presented and discussed.

A. Review on the operating strategy
Restructuring of the system, modern developments in distribution grids, and the energy market liberalization procedure resulted in new interaction policies between the system actors. Considering the definitions available in [26], three main groups of actors participate in the new operating paradigm of the energy system; the system operators (including the operators of the electricity markets, and the transmission and distribution systems), the aggregators (legal entities that hold contracts with system users for the provision of energy, DR and ancillary services), and the system users, i.e. producers, consumers and prosumers. To conform to the requirements of the policies for the new structure, the low voltage (LV) networks are expected to be updated. In this work, by adapting the mentioned actors for the LV networks, a schematic of the operational architecture is considered as in Fig. 1. Based on this figure, the grid domain is connected to the customer domain through the aggregator agents. Therefore, all traditional strategies of operation on both sides should be modified based on this new procedure. As observed from Fig 1, the essential components in the customer domain, which should be considered in the operating framework, are [27]:  aggregator agent: to establish a connection with the distribution, operation, market, and service provider domains of the grid  local generators: to generate electric energy that can be either used locally or injected into the grid  sensors and smart devices: to monitor and control the electrical appliances  energy storage systems: to add flexibility in managing electric resources  To model a new HEMS under the above-mentioned operational circumstances, the components and their mutual interactions should be incorporated in the proposed management framework. Therefore, the following assumptions are utilized in the simulations:  the entire necessary infrastructures including sensors, actuators, and communication links have already been implemented  based on the contract between the customer and aggregator, there will be specific energy demand setpoints predetermined by the aggregator which must be satisfied by the customer It worth mentioning that the method proposed in this paper is implemented in the case when there is a direct interaction between a prosumer and an aggregator. However, it can be adapted in the case when there is an absence of an aggregator, and a virtual aggregation environment (VAE) is adopted for allowing the exchange of information among the prosumers to provide a service to a Distribution System Operator (DSO). This strategy is adopted in the research project Distributed management logics and Devices for electricity savings in active users installations (DEMAND) and described in [28].

B. The proposed management framework
The implemented HEMS can optimize joint scheduling of various household appliances, renewable energy resources (RES), and energy storages under DR strategies. Besides, it should fulfill the aggregator requests and satisfy the practical requirements. To this end, the following twostage HEMS is proposed: Stage 1: By developing a Mixed Integer Linear Programming (MILP) model, the scheduling of different loads and storage of a typical prosumer are optimized in the day-ahead session. The objective function considers the initial consumption pattern of the user, the desired form of loads, contract tariffs, degradation cost of the battery, and discomfort cost of users. The constraints of critical, continuously controllable, and shiftable loads are also considered. The production of RES is regarded as the forecasted values in 24 hours. The output is the 24-hour schedule of each appliance, and charging and discharging of batteries.
Stage 2: An ANFIS based model manages the battery and controllable load level in the real-time session. To this end, at each desired period, the idle-charge-discharge states of the battery and the controllable loads will be updated due to the new values (actual) of RES production, or even customers' behavior. In other words, the fuzzy-based model is going to cover the uncertainties and keep the scheduled pattern (the day-ahead session) as much as the capability of the user's technologies allows. Fig. 2 depicts the schematic of the proposed two-stage management framework. As observed, both the stages mentioned above act sequentially. First, traditional day-ahead scheduling is solved. The realtime module, which includes an adaptive fuzzy controller, balances the differences between day-ahead forecast values and the actual real-time values in the operating phase. As observed, the proposed HEMS realizes both day-ahead planning and operating aspects of the system.

III. Optimal home energy management formulation
Considering the novel process of operation for the HEMS, a two-session framework is proposed to handle the energy management process. At an earlier session, day-ahead management is executed based on the forecasted load profile, Photovoltaic (PV) panels, and wind turbine (WT) generation profiles. The second session includes a fuzzy-logic based model that controls the loads and battery in the real-time mode to satisfy the energy amount scheduled during the dayahead management. The following subsections describe the structure and design of the model formulation.

A. Day-Ahead management
In general, an algorithm for HEMS solves the optimal operating schedule of distributed energy resources (DERs) and home appliances. In this study, wind micro-turbines, PV units, and batteries are considered as DERs. In order to find the modified consumption pattern of the customer, the household appliances are categorized into two groups; non-

Stage 1:Day-ahead Management
Optimal day-ahead management formulating and solving Save optimal schemes Generation and load forecasts Market price

Two stage HEMS
Save the parameters of fuzzy systems

Start End
Extract the obtained scheduling schemes of controllable loads and batteries Generating Learning Data

Stage 2:Real-time regulation
Training ANFIS based systems controllable (critical) loads, and controllable loads. The HEMS cannot schedule a non-controllable load such as a TV or lighting. Controllable loads include appliances of which the HEMS control the operation. Their operation characteristic classifies controllable loads into continuously controllable loads and shiftable loads. In this study, the HVAC, storage water heater, and must-run services are set as continuously controllable, shiftable, and critical loads, respectively. The HEMS manages the controllable (both continuously controllable and shiftable) appliances as well as the battery while considering the comfort level of the customer and the degradation cost of the battery. Besides, it should fulfill the requested load pattern determined by the operating agent.

A.1 OBJECTIVE FUNCTION
The proposed model is formulated as a MILP optimization problem with the following linear objective function: As described in (1), the objective function consists of energy cost, dissatisfaction cost of the user, and degradation cost of the battery. In (1), t V denotes the dissatisfaction cost caused by the deviation from the reference consumption and is defined by (2). Based on (2), if the customer changes his load pattern, his comfort decreases. By using (2), a cost is assigned to the decrease of comfort and defined as the dissatisfaction cost function.
if appliance is not continuously controllable if appliance is continuously controllable , 0, Based on (2), if the consumption of appliance i at time t changes from , , is applied to the prosumer. As it can be seen from (3), for continuously controllable appliances, , App it E is considered a variable between zero and the nominated consumption of that appliance, while for the appliances that are not continuously controllable, , App it E is regarded as a parameter equals to Nom i E . Since the differential dissatisfaction of a customer increases by getting distance from the controllable reference load, t V is considered as a convex function [29]. A higher value of the inelasticity parameter App i v indicates that the operation of the appliance i at the initial time (i.e., the most convenient time) is more important for the consumer. It means that devices with higher App i v have a higher priority for the customer, therefore changing the operation time of these appliances are more costly. It is noteworthy that the user's discomfort can be quantified based on some surveys and questionnaire from the customer. The discomfort factors can be different for each customer. Reference [29] studied the impact of varying discomfort factors on the cost and operation of the customer. In the current study, it is assumed that the values of discomfort factors and inelasticity parameters are available for the under-study customer.
It should be noted that , App it s at other times of the day.
The degradation cost of the battery results from the operation in discharge mode. The battery degradation cost can be given as follows: In (4), d C is battery cost in €/kWh that is considered as wear for discharge because of extra cycling of the battery and can be calculated by (5) is the capital cost of the battery in € and ET L is the battery lifetime throughput energy in kWh for the particular cycling regime.

A.2. DECISION VARIABLES
Decision variables include the energy that is discharged from the battery at time

A.3 SYSTEM AND BATTERY RELATED CONSTRAINTS
The explained optimization problem is solved by considering the following constraints.
Equation (6) shows that the demand containing the customer load (critical and controllable loads) and the charging requirements of the battery is either supplied through the grid or by the internal generation of wind and PV, or by the energy from the battery.
Critical Control B ch t denotes the amount of controllable load (continuously controllable and shiftable loads) participating in DR programs. Equation (7) guarantees that the battery cannot be charged and discharged simultaneously.
This work is licensed under a Creative Commons Attribution 4.0 License. For more information, see https://creativecommons.org/licenses/by/4.0/. This article has been accepted for publication in a future issue of this journal, but has not been fully edited. Content may change prior to final publication. The received power from the grid equals the surplus of wind and PV generations and injection of the battery as presented in (8).
Constraint (9) limits the injection from the grid based on the contract of the customer.
In order to encourage the customer to obey the desired consumption signal from the aggregator environment, the following constraints are considered. 2 2 , The goal of an aggregator or a system operator is to push the customer to change his consumption to . According to (10), the net use of the customer should be equal to the desired pattern dictated by the aggregator, and only a small deviation around the desired consumption is allowed.
According to (11), a 10% deviation from the desired consumption is considered. This value can be changed based on the flexibility of the customers. In other words, for customers with high capacity of the storage system, the deviation can be set lower than 10%.
Equation (12) Based on (12), the SOC (in p.u.) of the battery at time t is a function of the SOC at time 1 t  , the injected energy to the battery, and the injected energy back to the grid and house at time t. In the second and third terms of (12), the injected energy and discharged energy are divided by the battery capacity to keep the equation in p.u.
Inequality (13) limits the depth of discharge and guarantees that the battery is not overcharged. It should be noted that max SOC and min SOC can be set in a way that the battery can provide some reserve for the operation of the battery in the real-time. In other words, some part of the capacity of the battery can be reserved for operating in the real-time; hence, not all the capacity of the battery can be scheduled in the day-ahead session. On this basis, max 1 SOC  will be the reserved capacity of the battery in charging mode in the real-time, while min 0 SOC  is the reserved capacity of the battery in discharging state.
The charging and discharging rates of batteries are limited, as presented in (14) to (17).
Equation (14) defines the charging rate of the battery at time t . Similarly, (15) explains the discharging rate of the battery at time t . These two variables cannot exceed the maximum charging and discharging rate limits of the battery, as presented in (16) and (17), respectively. In the above formulation,

Control t E and
Critical t E represent the energy consumption of controllable loads (HVAC and storage water heater) and critical loads (must-run services), respectively.

A.4. MODELING THE HOUSEHOLD APPLIANCES
HVAC provides the indoor temperature in the desired temperature band. Equation (18) represents the relation between the indoor temperature and the electrical consumption of the HVAC. Here, in i  is the initial indoor temperature which is assumed to be equal to the desired temperature, in des  . In (19), it is expressed that the permissible variation for indoor temperature is limited to 1 degree relative to the desired temperature. Besides, the corresponding maximum and minimum values of the HVAC's load consumption and the load shedding are stated in (20) and (21) , 0 l HV HVAC curta t A storage water heater is an appliance that stores the heat in the water tank. The load and energy limitations of the storage water heater are represented in (22) and (23), respectively. The load curtailment constraint related to the water heater is described in (24 According to (19) the must-run-services are critical loads include the loads that should be provided quickly, and they should not be interrupted. ,, mrs rt mrs pred tt

B. ANFIS Regulation
In the proposed HEMS, a real-time adjustment module is integrated. This module is operated by an adaptive neurofuzzy inference system (ANFIS) method. In this sub-section, a detailed description of this method is provided.

B.1. PROPOSED ANFIS-BASED REGULATION FRAMEWORK FOR THE REAL-TIME ADJUSTMENT
As mentioned earlier, the fuzzy-based model is going to cover the uncertainties and preserve the operational planning of the day-ahead session. To this end, the proposed model should receive and analyze the information of the error values as inputs to make the proper decisions for the control variables as outputs. Considering the flexibility and fast interaction, batteries and continuously controllable loads are control variables, which are regarded as adjustment lever of the real-time session. Given that the investigated home has one controllable load and one battery, a fuzzy model shown in Fig. 3 is proposed for the real-time regulation. As observed, this system consists of three inputs and two outputs. The inputs are the errors of forecasted signals, which are related to the load profile and renewable-based generation outputs. The outputs of the proposed model consisting of the following items; battery interaction and controllable load interaction. For battery interaction, the battery can be charged (discharged) under the condition that there exists a surplus (shortage) of power after considering the errors signals. For the controllable load interaction, the load can increase (decrease) the consumption level continuously for the situation of surplus (shortage) of power. Considering all, the intended fuzzy-based model is a multiple-input and multipleoutput (MIMO) fuzzy controller. In this study, for simplicity, the controller related to each independent output is designed individually. By applying this strategy, the model turns to a multiple-input and single-output fuzzy controller. Therefore, as observed in Fig. 3, the proposed fuzzy model for the investigated home consists of two separate fuzzy systems. Each system is associated with one output. It is worth mentioning that the proposed framework can be simply extended for the prosumers with more than two outputs.

B.2. ANFIS-FCM MODEL DEVELOPMENT
In order to develop a simple fuzzy system, an expert who is familiar with the target system decides the rules. In such systems, the membership functions (MFs) assigned to each input variable are chosen empirically, that is, by plotting the data sets and examining them visually, or only by trial and error. For data sets with more than three inputs, visualization techniques are not very useful, and most of the time, it must rely on trial and error. Besides, generally, it becomes complicated to describe the rules manually to reach the precision needed, when the number of rules gets higher. In these situations, the technique of automatic model identification is used. The obtained models are often realized using a learning set of input-output pairs. In this study, the adaptive neuro-fuzzy inference system based on fuzzy cmeans clustering algorithm (ANFIS-FCM) is utilized to build the model for the estimation of suitable interaction in the real-time framework. An in-depth analysis and mathematical basis of ANFIS and FCM can be found in several literature studies, including [30,31]. To this end, in this study, solely the outlook of utilized procedure is presented.
The traditional procedure to develop each ANFIS system involves two steps: input screening and model selection. For the input screening and model selection, the learning data is split into three sets (training, validation, and test). The training data set is used for model training, the validation data set for parameters optimizing, and the test set is used to test the accuracy of the developed model. In addition to the mentioned steps, inspired by the procedure presented in [32] to predict chaotic series using ANFIS, an initial fuzzy system (FIS) as a starting point to train ANFIS system is integrated with the proposed methodology. Considering this, the twostep development procedure is modified as following steps; building initial fuzzy interface system (FIS), preprocessing data to construct the sets arrays, and training ANFIS system considering the initial FIS, training, and validation sets. In order to obtain the initial FIS, the k-fold cross-validation method is embedded in the training process of ANFIS systems development. This technique involves randomly dividing the data into k folds or groups. The first fold is kept for testing, and the model is trained on k-1 folds. The process is repeated k times and each time different fold is used for validation. By using this method, every training data get to be tested exactly once and is used in training k-1 times, and the bias is reduced. Besides, the k-fold cross-validation method is simultaneously used for accurate performance assessment since it has been found superior to other methods (e.g., holdout, bootstrap and leave-one-out cross-validation methods) in determining the generalization error in model selection problems [33]. In this study, the output of the 7-fold cross-validation method is used as the initial FIS.
In Fig. 4, the procedure of ANFIS-FCM system development is presented. As observed, first, the entire learning data is divided into training and testing data. In this stage, it is assumed that the learning data has already been provided. However, the procedure for preparing this data will be presented in the next sub-section. Second, the training data is randomly assigned to seven equal-sized folds. The first fold is kept for testing, and the remaining six different folds are used as the training set. The process is repeated seven times, and each time, a different group of data is used for testing. Third, the ANFIS with the averaged parameters of obtained models is selected as the initial fuzzy system to be considered in the next pace. At the final step, the initial FIS is trained, validated, and tested based on the full original learning data. The obtained fuzzy system selected as ANFIS system and can be used for real-time adjustment. This procedure is repeated to yield all fuzzy systems corresponding to the outputs. The generation procedure of learning data is presented in Fig. 5. As observed, this approach includes three steps as follows: Step 1: different states of errors related to each uncertain parameter is generated. For this purpose, the scenario generation procedure of [34] is adopted. The method is based on the discretization of the probability distribution of the parameter errors and roulette wheel mechanism. By using this method, firstly 1000 daily (24-hour) scenarios for WT, PV, and demand are generated. Then, these daily scenarios are compared with the corresponding forecasted values to obtain 1000 daily (24-hour) error vectors.
Step2: for the same daily error vector of WT, PV, and demand, the corresponding elements (same hour) are matched, merged, and stored to make a list of combinations.
Step3: to achieve optimal reactions against the unbalance resulted from the forecast errors, the solutions of an optimization problem are utilized to provide learning data for training the intended ANFIS systems. To this end, the obtained combinations from step 2 are adapted to the optimization problem described in the next sub-section. Solving individual problems determines the optimal reactions of the battery and controllable load. Merging these reactions (outputs) alongside the error values (inputs) provide the final learning data. Step 1 Step 2 Step 3 Solving the optimization problem and providing data learning

B.4. THE EMBEDDED OPTIMIZATION PROBLEM
In this section, the formulation of the optimization problem embedded in the process of learning data generation is presented and described. Considering the amount of unbalance ɛ, which is calculated by (26), the system deals with three states:  0   : There is no need for any adjustment action.
 0   : There is surplus power in the system. The difference ɛ will be treated as a virtual source, which should be appropriately dispatched between the control variables (outputs).  0   : There is a shortage of power in the system. The difference ɛ will be treated as a virtual load, which should be adequately supplied by the control variables. (26) WT error PV error Load error     As control variables, the free capacity of batteries and controllable loads can be operated as loads. On the other side, the occupied capacity of batteries and controllable loads can be adjusted to handle the shortage of power. Considering these roles of the elements, accordingly, there is several generation units and loads which should be optimally managed to control the amount of imbalance  . The advantages of this optimal management should be in line with the optimization problem of the previous stage. Hence, a similar optimization framework is adapted for the problem. This means that the objective functions of dissatisfaction cost of inhabitants and the degradation cost of the battery should be minimized subjected to the constraints of controllable loads and batteries. To this end, the following optimization problem is solved: ,max 0 charge charge rr  (29) ,max 0 discharge discharge rr  As observed, the optimal solution of the problem should minimize the function adjust Cost (27). Constraint (28) guarantees the control variables can satisfy the actual amount of unbalance. The batteries charging, discharging, and SOC are restricted by the maximum limits as described by (29)- (32). Other equations are the same as the day-ahead management section. The superscript "adjust" demonstrates the variables of the real-time session. It is important to note that only one of the operational modes for batteries (charging or discharging) and controllable loads (increase or decrease of the load level) can occur during a specific adjustment.

IV. Numerical simulation
The test system is a HEMS includes a 2 kW wind microturbine and a 2kW PV unit. A 3 kWh battery is considered that can store between 0.48 and 2.4 kWh in the day-ahead. Also, its maximum charging and discharging rates are 400W. Besides, the charging and discharging efficiencies of the battery are equal to 90%. Regarding the electrical loads, the maximum load capacity of the HVAC in each time slot is equivalent to 5.525 kW. The daily energy capacity of the storage water heater is equal to 10.46 kWh (180lt), which has a 2 kW heating element. The desired temperature of the building is assumed to equal 23 °C , and the dead-band of the HVAC is 1°C. Furthermore, the thermal resistance of the building shell and thermal conductance are equal to 18°C/kW and 0.525kWh/°C, respectively. In this study, the dissatisfaction factor of space heating and water heating are considered equivalent to 0.5 and 0.2 €/kWh, respectively. The battery cost is assumed to be 1.2 €/kWh. The forecasted values for WT and PV generations and the hourly market price are presented in Table 1. Table 2 2 shows the specifications and parameters of the ANFIS-FCM model.
All the optimization problems in this study were solved by using the GAMS 25.0.3 software. The fuzzy logic toolbox provided in MATLAB R2014a was used to develop the fuzzy models. A Laptop with 2.2 GHz Intel Core i3 processor was used to implement all programs.

A. Day-Ahead session
In this stage of HEMS, considering the forecasted values for the wind, PV and loads, day-ahead scheduling with the introduced objective function was executed. The aggregator requests to control the consumptions were reflected in the model as well. The impact of the proposed model from the grid viewpoint is shown in Fig. 6. In order to provide a better comparison, as observed from the figure, alongside the obtained load profile of the proposed model (when applying the aggregator request), the figure includes the profile obtained after solely considering local generations (without applying the aggregator request) and the base consumption profile of customer (without applying the aggregator request and local generation). Besides, demand set points requested by the aggregator are depicted in Fig. 6. By comparing the consumptions in Fig. 6 it can be observed that without applying the aggregator request, the consumption in hours in the range between hour 5 and hour 15 equals to zero. This means that the proposed HEMS can self-sufficiently manage the needs of the system by the modified values of loads and local generations. When applying the aggregator request, for the period 5-11, in which there are compulsory consumption requests, the proposed HEMS can effectively satisfy the energy demand set points. Besides, it is observed from Fig. 6 that the proposed HEMS shows successful performance at hour 22 and hour 24. In these hours, a compulsory reduction of consumption is imposed on the system. Considering the case studies shows that the system has a high degree of flexibility to handle different situations successfully. Fig. 7 depicts the consumption pattern of the smart home appliances corresponding to each case study mentioned in Fig. 6. As it is clear from the figure, controlling loads plays an active role in the proposed HEMS.  The calculated objective functions are compared in Fig. 8. As observed, the requests of aggregator impose an extra cost on the HEMS. This surplus expense can be then considered to adjust the financial interactions between the aggregator and the customer. However, this issue is out of the scope of current work. The separate cost terms are distinguished from each other in Fig. 8. The largest share of the cost is related to the customer dissatisfaction cost, and the lowest is the cost of the battery.
The details of the operation of the battery for the case study with the aggregator request are indicated in Fig. 9. According to this figure, by employing the proposed HEMS, the operation of the battery is radically changed. In fact, due to its high cost, the battery is less used in the management plans of proposed HEMS.
As observed, because of the high cost of battery operation, HEMS prefers to give priority to controllable loads to provide the scheduling scheme. Therefore, HVAC as a continuously controllable load is always one of the main tools of HEMS to run the management programs. HVAC energy consumption is adjusted according to the temperature changes in the building. Fig. 10 presents the inside temperature of building for the case study with the aggregator request. This figure also represents the forecasted outside temperatures in different hours. It should be reminded that the desired temperature set point in different hours is equal to 23°C, while the dead-band of the HVAC is 1°C. As observed, although the HEMS decreases the set point of the HVAC system during some hours, it increases the temperature from 23°C to 24 °C, as well. This helps the system to keep the room temperature in the range of comfortable levels while it optimizes energy consumption.

B. Real-Time session
This stage of the HEMS is executed to correct the shortage or surplus of energy in the system, which resulted from the forecast errors. In the proposed HEMS, the ANFIS-FCM model is responsible for acting appropriately against these imbalances. The results of these modules are presented in this section. Fig. 11 shows two samples of surface views obtained for the performance of the system in controlling outputs against PV and WT errors. As observed in the figure, the developed ANFIS prioritizes the battery charge against the errors that result in the surplus power, and in case of lack of power, a combination of load-reduction and batterydischarge strategies is adopted. This strategy seems to be reasonable by considering the degradation and dissatisfaction costs. These reactions are precisely in line with the concepts included in the optimization problems and imply that the model training has been implemented correctly. obtained by the optimization for data sets of each output is shown in Fig. 12. These figures graphically illustrate the analysis of the proposed fuzzy model for all three types of data, including training, validation, and testing sets. In these figures, for useful analysis of each set, due to the high density of data, a close-up representation of the diagram is embedded in the figures. As shown in these figures, the results of the ANFIS-FCM model in comparison with actual data provide excellent precision and the output of the ANFIS-FCM model track well the target values for all data sets. Numerical analysis of the ANFIS-FCM model for predicting the regulation interactions is provided in Table 3. In this table, in addition to the mean error value, root-mean-square error (RMSE) and correlation coefficient (R) are two conventional criteria considered to assess the efficiency of the models. RMSE is routinely used as a criterion to show the discrepancy between the target and output values of the model. R is widely used to determine whether these values for the model are associated. These performance indices for all data sets are presented in Table 3. Mean error values are near zero, and RMSE values are lesser than 20 W. As observed, the results indicate the high performance of the ANFIS-FCM model and show that the model can be used successfully to estimate the proper interactions. The last column in Table 3 provides the values of the correlation coefficient for each set. In all sets, because of the proximity of targets and outputs of the model, the calculated amounts are close to the ideal value. As observed in Fig. 13, the regression of the relationship between the targets and output of the model is very close to the identity function. In fact, this figure graphically approves the low error of the model for the test set. The density distribution of error magnitudes for the test set is depicted in Fig. 14. As observed, approximately 91 % of the estimated outputs in the testing set have negligible maximum error equal to 5 W. Among the remaining 10% interactions, the half has an error lower than 20 W and only 2.5% of the data leads to an error higher than 50 W. Observing a maximum of 400 W of error magnitude in the samples and considering 10 % allowable amount of tolerance in the real-time mode, it can be concluded that the model has a successful adjustment in 95% of the states. In other words, it can be declared that the real-time controller yields a 95% success rate of the performance. It should be noted that only 2.5% of the data leads to an error higher than 50 W. In Fig. 14, in addition to the error density information, the characteristics of the normal distribution, which is fitted for each error density, are presented.

C. Comparative study
Since the proposed two-step method had an approximately similar data-driven strategy to a stochastic procedure confrontation uncertainties, the scenario-based algorithm of [5] was chosen to compare and evaluate the HEMS performance. To this end, the 1000 scenarios generated for data training were utilized to run the simulations. The method of [5] was adapted to investigate the case study by applying the aggregator requests, and the final expected objective function was calculated.
In order to calculate the total cost for the proposed HEMS, the cost of the worst state (the state with the biggest error) from the data provided to train ANFIS was considered as the cost of the real-time session.  Fig. 15 presents a comparison of the estimated total cost for both methods. As observed, even if the calculated objective functions are close to each other, the proposed system can outperform the technique of [5] from the optimal behavior viewpoint.

V. Conclusion and future works
This paper proposed a two-stage model of HEMS by considering the uncertainties of load and small-scale renewable energy generation. The first stage included a modified deterministic day-ahead scheduling problem. In this stage, the aggregator requests to control the consumption were fulfilled. Besides, the satisfaction characteristic of the customer was modeled and alongside with the battery degradation cost was considered in the problem. The second stage added a real-time regulation block to the proposed HEMS. This stage utilized an adaptive fuzzy logic controller to adjust the output power of the battery and controllable loads to handle the gap resulted from forecast errors. An embedded optimization problem applied to train the controller to maintain the optimality of the solution. The simulation results clearly show that the novel added block yielded up to 95% success rate to handle the deviations. Moreover, it is observed; by considering the realtime regulation algorithm, the proposed HEMS can optimize the operating schemes close to the ideal deterministic results, even when there is a considerable gap between the forecast values and the real ones.
However, as mentioned, the proposed model does not have a complete success rate and requires new strategies to improve the rate as much as possible. Therefore, exploring and The proposed HEMS