## 5 System Overview
### TWIN-ADAPT: Continuous Learning for Digital Twin-Enabled Online Anomaly Classification
If this accuracy is below the warning level of the new concept, it indicates that the model is outdated and needs retraining with the new concept samples. The learner will be updated again on the samples in the adaptive window, and the system will transition to the normal state, emptying the adaptive window (lines: 35 to 41 of Algorithm 1). Similarly, the model will also continue collecting samples for updating if the size of the adaptive window has reached its maximum limit to ensure that the real-time constraints of memory and processing speed are met. Figure 9: Continuous learning process for Algorithm 1. The “current” sliding observation window (_ObsWin_) captures the performance metric for short concepts, and the previous observation window (_PrevObsWin_) captures the feature–target relationship over the last complete window. In comparing accuracy between two windows, the adaptive window (_AdaptWin_) starts to collect data samples as new concept samples for potential drift adaptation. ```
1:Input:DataFlow: stream of sensor data from CPS component
2:\(\gamma\): threshold for initiating detection, \(\delta\): threshold for adaptation
3:\(winSize\): size of the observation window (for detection), \(winSizeMax\): maximum size of the data collection window (for adaptation)
4:Model: pre-trained model on historical data
5:Output: New average accuracy of Model
6:functionAdaptModel(\(DataFlow\), Model, \(\gamma,\delta,winSize,winSizeMax\))
7:\(AdaptWin\leftarrow\emptyset\)
8:\(CurrentState\gets 0\)\(\triangleright\) 0: Normal, 1: Warning, 2: Adaptation
9:forbatch = \(b\) samples \(\in DataFlow\)do
10:\(ObsWin\leftarrow\) last \(winSize\) samples from batch \(b\)
11:\(PrevObsWin\gets b-winSize\) samples
12:\(CurrAcc\leftarrow\) calculate\(\_\)accuracy(\(ObsWin\))
13:\(PrevAcc\leftarrow\) calculate\(\_\)accuracy(\(PrevObsWin\))
14:if\(CurrentState==0\) and \(CurrAcc<\gamma\times PrevAcc\)then
15:\(AdaptWin\gets AdapWin\cup\{batch\}\)
16:\(CurrentState\gets 1\)\(\triangleright\) Enter warning state
17:endif
18:if\(CurrentState==1\)then
19:\(AdaptWinLen\leftarrow|AdaptWin|\)
20:if\(CurrAcc<\delta\times PrevAcc\)then
21:\(\triangleright\) Accuracy of current observation window drops to drift level
22:\(CurrentState\gets 2\)
23:\(f\leftarrow CurrAcc\)\(\triangleright\) Record accuracy at drift starting point
24:\(Model\leftarrow\) UpdateModel(\(Model\), \(AdaptWin\))
25:\(\triangleright\) Update the model by retraining it on new concept samples
26:elseif\(CurrAcc>\gamma\times PrevAcc\) or \(AdaptWinLen==winSizeMax\)then
27:\(\triangleright\) False alarm, accuracy increased back or max window size reached
28:\(AdaptWin\leftarrow\emptyset\)
29:\(CurrentState\gets 0\)
30:else
31:\(AdaptWin\gets AdapWin\cup\{batch\}\)
32:endif
33:endif
34:if\(CurrentState==2\)then\(\triangleright\) Within drift state
35:\(AdaptWinLen\leftarrow|AdaptWin|\)
36:if\(AdaptWinLen\geq winSizeMax\)then
37:\(\triangleright\) If sufficient new concept samples are collected
38:\(NewConceptAcc\leftarrow\) calculate\(\_\)accuracy(\(ObsWin[f:f+winSize]\))
39:\(\triangleright\) Calculate accuracy on new concept window starting from index \(f\)
40:\(CurrAcc<\delta\times NewConceptAcc\) OR \(AdaptWinLen==winSizeMax\)then
41:\(\triangleright\) When new concept accuracy drops to the warning level or sufficient
42:new concept samples are collected
43:\(Model\leftarrow\) UpdateModel(\(Model\), \(AdaptWin\))
44:\(\triangleright\) Retrain the classifier on all the newly collected samples
45:\(AdaptWin\leftarrow\emptyset\)\(\triangleright\) Empty new concept window
46:\(CurrentState\gets 0\)\(\triangleright\) Change to a normal state
47:else
48:\(AdaptWin\gets AdapWin\cup\{batch\}\)\(\triangleright\) Keep collecting new samples
49:endif
50:endif
51:endif
52:endfor
53:return New Average Accuracy of Model
54:endfunction
```  
**Algorithm 1** Continuous Learning Model using Adaptive and Sliding WindowsTo ensure the model's effectiveness, a hyperparameter optimization function is implemented.