2.1 Microgrid energy management optimization model
The objective problem and constraint functions of the optimization model for energy management in the microgrid considering the two possible operation modes are formulated in this section. In the isolated mode, the microgrid objective is formulated to minimize the energy production cost (fuel cost), and the operation and maintenance costs within the microgrid. While operating in gridconnected mode, the microgrid can either send (sell) power to the main grid or receive (buy) from the main grid. During the periods receiving power from the main grid, the microgrid is expected to minimize the energy production cost, operation and maintenance cost and the expense of buying power from the main grid; while sending power to the main grid, the objective is to maximize the profit which is the energy selling revenue minus the energy production cost, and operation and maintenance cost.
This objective function is subjected to six decision variables: the charging/discharging power of the VRB, state of charge (SOC) of the VRB, charging/discharging power of the LiIon battery, SOC of the LiIon battery, the diesel generator power output, and the quantity of power exchange with the main grid.
2.2 Formulation of objective functions
The following are some of the information that should be specified in advance for a dayahead energy management in microgrids [16, 17]:

24hahead hourly load demand forecast

24hahead hourly wind power forecast

24hahead hourly PV power forecast

Grid price forecast, or prespecified grid price
The objective functions are formulated independently by considering three operational cases based on the microgrid operating mode and the power flow directions between the microgrid and the main grid. In case I, the objective function for the isolated mode of operation is considered. In case II, the microgrid is in gridconnected mode and it receives (buys) power from the main grid. While in case III, the microgrid is also in gridconnected mode but it sends (sells) power to the main grid.
2.2.1 Case I – isolated mode
In case I, the objective targets to minimize the energy production cost (fuel cost), and the operation and maintenance costs within the microgrid.
The objective function is given by:
$$ M i n{\displaystyle \sum_{t=1}^n\left\{\begin{array}{l}{\displaystyle \sum_{i=1}^m\left({F}_i\left({P}_i(t)\right).{\tau}_i(t)+ S{C}_i(t)\right)}+\\ {}{\displaystyle \sum_{i=1}^m{C}_{OM, i}(t){P}_i(t)}+{C}_{OM wind}(t){P}_{wind}(t)+\\ {}{C}_{OM pv}(t) Ppv(t)+{\displaystyle \sum_{j=1}^q{C}_{OM es, j}(t){P}_{es, j}(t)}\end{array}\right\}}\kern1.1em (1) $$
Where, n is the number of time steps for a scheduling day; m indicates the number of all types of dispatchable DGs; q is the number of all types of energy storage units within the microgrid; P
_{
i
}(t) is the power output of the i^{th} dispatchable DG at time t and F
_{
i
}(P
_{
i
}(t)) is the corresponding fuel cost function, and for a diesel generator it is defined as:
$$ {F}_i\left({P}_i(t)\right)={a}_i.{P}_i{(t)}^2+{b}_i.{P}_i(t)+ c $$
(2)
Where, a_{i}, b_{i} and c_{i} are the cost function parameters.
τ
_{
i
}(t) = 1, if the i^{th} dispatchable DG is in operation;
τ
_{
i
}(t) = 0, if the i^{th} dispatchable DG is OFF at time t;
SC
_{
i
}(t) is the start up cost function of each dispatchable DG and is given by:
$$ S{C}_i(t)= s{c}_i, if{\tau}_i(t)  {\tau}_i\left( t1\right) = 1 $$
otherwise
Where, sc
_{
i
} is the start up cost of dispatchable DG i.
c
_{
OM,i
}(t) is the operation and maintenance cost of the i^{th} dispatchable DG at time t; c
_{
OMwind
}(t) is the operation and maintenance cost of the wind power generation system at time t; P
_{
wind
}(t) is the forecasted wind generation at time t; c
_{
OMpv
}(t) is the operation and maintenance cost of the PV system at time t; P
_{
pv
}(t) is the forecasted PV generation at time t; C
_{
OMes,j
}(t) is the operation and maintenance cost of the j^{th} energy storage unit at time t; P
_{
es,j
}(t) is the j^{th} energy storage charging/discharging power at time t.
2.2.2 Case II – Nonisolated mode  buying power from main grid
In this case, the objective aims in minimizing the energy production cost, the operation and maintenance costs and the expenses of energy purchasing from the main grid.
The objective function is:
$$ M i n{\displaystyle \sum_{t=1}^n\left\{\begin{array}{l}{c}_{grid buy}(t){P}_{grid}(t)+{\displaystyle \sum_{i=1}^m\left({F}_i\left({P}_i(t)\right).{\tau}_i(t)+ S{C}_i(t)\right)}+\\ {}{\displaystyle \sum_{i=1}^m{C}_{OM, i}(t){P}_i(t)}+{C}_{OM wind}(t){P}_{wind}(t)+\\ {}{C}_{OM pv}(t) Ppv(t)+{\displaystyle \sum_{j=1}^q{C}_{OM es, j}(t){P}_{es, j}(t)}\end{array}\right\}}\kern1.1em (3) $$
Where, c
_{
gridbuy
}(t) is the electricity buying price from the main grid at time t; P
_{
grid
}(t) is the power purchased from the main grid at time t, P
_{
grid
}(t) > 0.
2.2.3 Case III  Nonisolated mode  selling power to main grid
Here, the objective aims in maximizing the profit which is the energy selling revenue minus the energy production cost and the operation and maintenance costs within the microgrid.
The objective function becomes
$$ M a x{\displaystyle \sum_{t=1}^n\left\{\begin{array}{l}{c}_{grid sell}(t){P}_{grid}(t)\\ {}\left\{\begin{array}{l}{\displaystyle \sum_{i=1}^m\left({F}_i\left({P}_i(t)\right).{\tau}_i(t)+ S{C}_i(t)\right)}+{\displaystyle \sum_{i=1}^m{C}_{OM, i}(t){P}_i(t)}+\\ {}{C}_{OM wind}(t){P}_{wind}(t)+{C}_{OM pv}(t) Ppv(t)+\\ {}{\displaystyle \sum_{j=1}^q{C}_{OM es, j}(t){P}_{es, j}(t)}\end{array}\right\}\end{array}\right\}}\kern0.1em (4) $$
Where, c
_{
gridsell
}(t) is the electricity selling price to the main grid at time t; P
_{
grid
}(t) is the power sold to the main grid at time t, P
_{
grid
}(t) < 0.
2.3 Formulation of constraint functions
The objective functions formulated above are subjected to the following constraints; comprising ESS units’ capacity and operational limits, dispatchable DGs’ power limit, grid power transfer limits, and all other technical requirements in the microgrid:
2.3.1 Power output of the i^{th} dispatchable DG at time t
$$ {P}_i^{\min }(t)\le {P}_i(t)\le {P}_i^{\max }(t) $$
(5)
2.3.2 Grid power exchange limits
$$ {P}_{grid}^{\min }(t)\le {P}_{grid}(t)\le {P}_{grid}^{\max }(t) $$
(6)
The grid power exchange minimum (\( {P}_{grid}^{\min }(t) \)) and maximum (\( {P}_{grid}^{\max }(t) \)) limits can be set as a large amount or the capacity of the transformer linking the microgrid and the main grid.
2.3.3 Demandsupply balance
$$ \begin{array}{l}{\displaystyle \sum_{i=1}^m{P}_i}(t)+{\displaystyle \sum_{i= j}^q{P}_{es, j}}(t)=\\ {}{P}_{load}(t){P}_{wind}(t){P}_{pv}(t){P}_{grid}(t)\end{array} $$
(7)
where P
_{
load
}(t) denotes the forecasted load demands at time t.
2.3.4 ESS units charging/discharging power limits
$$ {P}_{es, j}^{\min }(t)\le {P}_{es, j}(t)\le {P}_{es, j}^{\max }(t) $$
(8)
P
_{
es,j
}(t) > 0, the i^{th} energy storage is discharging;
P
_{
es,j
}(t) < 0, the i^{th} energy storage is charging;
P
_{
es,j
}(t) = 0, the i^{th} energy storage is inactive.
2.3.5 ESS units dynamic operation performance
$$ SO{C}_{es, j}\left( t+1\right)= SO{C}_{es, j}(t)\frac{\eta_{es, j}(t){P}_{es, j}(t)}{C_{es, j}} $$
(9)
$$ SO{C}_{es, j}^{\min}\le SO{C}_{es, j}\left( t+1\right)\le SO{C}_{es, j}^{\max } $$
Where, η
_{
es,j
}(t) is the i^{th} energy storage unit charging or discharging efficiency at time t; C
_{
es,j
} denotes the rated storage capacity of j^{th} energy storage unit.
Thus, the decision variables that need to be determined are the ESUs’ charging/discharging power P
_{
es,j
}(t) and their state of charges SOC
_{
es,j
}(t) (for i =1, 2, …, q); the power output of dispatchable DGs P
_{
i
}(t),and the quantity of power exchange with the main grid P
_{
grid
}(t) at time t.