In the two-layer programming model proposed in this paper, the decision variables of the upper-level model are the capacity and power of the storage system and the objective is the lowest cost of the battery energy storage system. The decision variables in the lower-level model is the charging and discharging power of battery. With the increasing number of grid-connected microgrids, the power fluctuations on connection line will adversely affect the stability of the grid. So in the lower-level model, the objective is the lowest power fluctuation of the system connection line.

### 3.1 The upper-level model

The objective function is:

$$ {\displaystyle \begin{array}{l}\min {C}_A={c}_1{P}_{ES}+{c}_2{E}_{ES}+\\ {}\kern0.1em \lambda \sqrt{\frac{1}{N-1}\sum \limits_{i=1}^N\left({P}_{ES}(i)+{P}_L(i)-{P}_G(i)-{P}_{base}\right)}\end{array}} $$

(2)

In the above formula, *c*
_{1}is the unit power cost, for lithium batteries, lead acid and other battery energy storage, it is mainly the cost of power converter system (PCS); *c*
_{2} is the unit capacity costs, it is mainly the cost of the battery; *λ* is the penalty factor for the power fluctuation of the connection line; *P*
_{
ES
} is the power of energy storage in microgrid; *P*
_{
L
} is the power of load in microgrid; *P*
_{
G
}is the power of generation in microgrid; *P*
_{
base
}is the demand capacity reported to the grid by the microgrid corporation.

The constraints are shown as follows:

$$ {\displaystyle \begin{array}{l}{P}_{ES}\le {P}_M\kern3em {E}_{ES}\ge \frac{1}{SOC_M-{SOC}_0}\sum \limits_{k=1}^i{P}_{ES}(i)\varDelta t\\ {}{E}_{ES}\le {E}_M\kern2.5em {P}_{ES}\ge {P}_{ES}(i)\\ {}i=1,2,\dots N\end{array}} $$

(3)

Where *N* is the calculated number of time period, *P*
_{
M
} and *E*
_{
M
} are respectively the power and energy capacity of the storage system due to the installation site, grid power, etc., *SOC*
_{
M
} is the highest state of charge that allowed by the battery energy storage system, *SOC*
_{0} is the initial state of charge of the battery energy storage system.

### 3.2 The lower-level model

Two–part electricity price is implemented for the grid-connected microgrid. The first part is the fixed demand capacity cost which is calculated according to the demand power. And the second part is the electricity cost bought from the connection line. For the microgrid operator, the fluctuation variance of the connection line means extral tarrif. As a result, the inner objective function is given as below:

$$ {\displaystyle \begin{array}{l}\min f=\sum \limits_{i=1}^N\Big\{{P}_{ES}(i)+{P}_L(i)-{P}_G(i)-\\ {}\kern0em \frac{\sum \limits_{i=1}^N\left[{P}_L(i)-{P}_G(i)\right]}{N}\Big\}{}^2\end{array}} $$

(4)

The above equation shows the fluctuation variance of the power of the connection line within one day. *p*
_{
ES
}(*i*), *p*
_{
L
}(*i*), and*p*
_{
G
}(*i*)are respectively the energy storage power, load power and generation power in the microgrid system in the i time period.

The constraints are:

$$ \left\{\begin{array}{l}-{P}_{ES}\le {p}_{ES}(i)\le {P}_{ES}\\ {}{SOC}_m\le SOC(i)\le {SOC}_M\\ {}-{P}_{G0}\le {p}_{ES}(i)+{p}_L(i)-{p}_G(i)\le {P}_{L0}\end{array}\right. $$

(5)

Where *P*
_{
L0}, *P*
_{
G0} are respectively the maximum allowable consumption power and reverse power on the connect line. The first constrain is.

Here, *p*
_{
ES
} is maximum the charging and discharging power of the energy storage. Assuming that the energy storage system charging efficiency *η*
_{
c
} and discharge efficiency *η*
_{
d
} remains constant, set *α* as follows:

$$ \alpha =\left\{\begin{array}{l}{\eta}_c\kern0.5em {p}_{ES}>0\\ {}1/{\eta}_d\begin{array}{c}\end{array}{p}_{ES}\le 0\end{array}\right. $$

(6)

So we can get the equation:

$$ SOC(i)= SOC\left(i-1\right)+\frac{\alpha (i){P}_{ES}(i)\varDelta t}{E_{ES}} $$

(7)

where *Δt* is the calculating length of the time period.

### 3.3 Solving algorithm

It can be seen that the outer programming model is a quadratic problem and easy to solve. The inner programming mode is a mixed integer nonlinear programming (MINLP) problem, its objective function can be expressed as a form of univariate function and the dynamic programming method can be used to solve the problem. The dynamic programming method decomposes a multi-stage optimization problem into single-stage optimization problem by stage division, which is an effective algorithm to solve the problem. The dynamic programming model needs to determine the calculated decision quantity and state quantity. For each controllable resource, the power of each calculation period is the decision quantity in the dynamic programming model; for the energy storage, the SOC at the end of each calculation period is its state quantity, the initial state quantity and the end state quantity are known quantities.

The optimal strategy of the dynamic programming decision-making process has the property as follows: the rest of the decisions must be an optimal strategy when any of the state is taken as the initial level and initial state, regardless of the initial state and the initial decision. That is, if there is a N-level decision process with initial state of *x*(0), its optimal strategy is{*u*(0), *u*(1), …*u*(*N* − 1)}. Then, for a N-1-level decision process with initial state of *x*(1), the decision set {*u*(1), *u*(2), …*u*(*N* − 1)} must be the optimal strategy.

Figure 1 shows the state transfer diagram of the N-level decision process. An N-level decision process can be expressed as follows:

$$ {\displaystyle \begin{array}{l}x\left(k+1\right)=f\left[x(k),u(k),k\right]\\ {}x(0)={x}_0\end{array}} $$

(8)

The constraints in the above formula are:*x*(*k*) ∈ *X* ∈ *R*
^{n}, *u*(*k*) ∈ *Ω* ⊂ *R*
^{m}. At this point, the programming problem can be described as solving an optimal control (decision) series*u*
^{∗}(*k*) , *k* = 0 , 1 , … *N* − 1, which can make the performance indicator minimum.

The solving method of dynamic programming problem can be divided into two directions: forward and backward. The forward direction means starting from the initial layer and calculating gradually to the end; the backward direction means starting from the end layer and reversing the solution. In practical problems, the backward direction is more commonly used. The paper adopt the backward direction method.

At each stage, SOC at the end is considered as the state quantity, the power of the energy storage *p*
_{
ES
}(*i*) is the decision quantity, the variance of the power becomes the corresponding cost function, and eq. (7) is the dynamic equation.

The power decision set of energy storage can be established through the charging and discharging power limit and the line power limit. The state set of energy storage can be established through the SOC range of operation. Enumeration method is adopted to solve the single stage MINLP problem. According to the second constrain in eq. (5), the state set is composed of discrete value from *SOC*
_{
m
}to *SOC*
_{
M
}by a step of *ΔSOC*. The decision set can be calculated accordingly. The less*ΔSOC* value, the larger the decision set. As a result, it takes more time to calculate the optimal decision.

According to the steps above, the optimal charging and discharging process of energy storage system can be solved. Figure 2 shows the transition diagram of SOC’s stage state.

From the inner problem we can get the optimal charging and discharging process of the energy storage system. The solve of inner problem is transferred to the upper-level model to get the optimal configuration of energy storage system. And the configuration is transferred to the lower-lever model as constrains. The iteration process is illustrated in Fig. 3.