## 4 BILSTM-Transformer Based Bems Model
### Microgrid Optimization Method Based on Cognitive Control
#### 4.5.1 User-Side Cognitive Control (UACC)  
On the user side of BEMS, the cognitive control system (UACC) leverages CPS's real-time monitoring and data transmission capabilities to achieve adaptive responses to data loss and communication instability in dynamic environments. CPS's sensor network collects real-time data on EV charging and discharging status, user demand, and communication channel quality, transmitting this information to UACC. This enables UACC to make adjustment decisions based on information gaps, even in unstable communication environments.  
Specifically, UACC quantifies the data gaps transmitted by CPS to balance information gaps, retransmission costs, and channel switching costs, thereby adaptively optimizing charging and discharging strategies. When CPS detects that the information gap \(H_{u}(k)\) exceeds a threshold, UACC can adjust the charging strategy through an instantaneous reward function to reduce scheduling errors caused by incomplete information. Additionally, the real-time data stream from CPS supports UACC in triggering channel switching or increasing the number of retransmissions when channel failures or high bit error rates occur, ensuring the integrity and reliability of information transmission.  
\[r_{u}(k)=\frac{1}{\beta H_{u}(k)+C_{r}N_{r}+C_{c}(a_{c}(k))} \tag{18}\]  
\(H_{u}(k)\) represents the information gap, reflecting the data uncertainty, \(\beta\) is the weight coefficient of the information gap, \(0\leq\beta<1\), \(C_{r}\) is the cost of each retransmission, \(N_{r}\) is the upper limit of the number of retransmissions, \(C_{c}(a_{c}(k))\) is the cost of switching the alternate channel, and \(a_{c}(k)\) indicates whether to switch the channel at time \(k\).  
In offline training, we will simulate the data loss scenario in a random masking manner to improve the robustness of the model in an uncertain environment. Specifically, the program dynamically generates a masking mask at each time step and applies it to the data to simulate packet loss and guarantee data uncertainty. When the UACC detects that the information gap \(H_{u}(k)\) exceeds the threshold, it will help the UCC agent adjust the charging and discharging strategy according to the information gap weight coefficient \(\beta\) and the communication retransmission cost \(C_{r}\). The program implementation of the UACC algorithm is shown in Algorithm 1.  
Firstly, when the current channel fails or the bit error rate \(\eta_{i}(k)\) is high, UACC triggers channel switching based on the switching cost \(C_{c}(a_{c}(k)\) in the immediate reward function and the long-term expected loss, so as to reduce the information gap caused by communication loss.  
Secondly, in the case of incomplete information, UACC will obtain key data by increasing the number of communication retransmissions \(N_{r}\) to improve data integrity. That is, when UACC detects a large information gap \(H_{u}(k)\), it will remind the BEMS to trigger a retransmission, so as to ensure that the required information can arrive in time. The number of retransmissions of the BEMS will also be limited by the cost \(C_{r}\) and the upper limit of the retransmission \(N_{r}\), which will help the BEMS find a balance between reliability and economics.  
Finally, in the case of information loss, the UACC will reduce the charging rate to ensure the smoothness of energy consumption. For example, if the charging rate uncharacteristically exceeds the current power dispatch requirements, the UACC may adjust the charging rate based on information gaps to prevent power waste from the BEMS or overload the grid.  
The Function (19) for quantifying the information gap is as follows:  
\[H_{u}(k)=\eta_{i}(k)\big{|}\delta_{i}^{t}(k-1)-\delta_{i}^{t}(k)\big{|} \tag{19}\]  
where: \(\big{|}\delta_{i}^{t}(k-1)-\delta_{i}^{t}(k)\big{|}\) represents the user's actual power demand in the \(k\) iteration, \(\eta_{i}(k)\) is the frame error rate (FER) of the current channel, which reflects the communication quality.