## 4 Platform Design
### ToU Ranges Using GRU Model Predictions and K-Mean Clustering
#### 4.1.2 Definition of ToU Ranges Using On-Chain Clustering  
What is proposed, is that the energy ToU ranges and tariffs are defined ahead of each trading round; the tariffs are then defined accordingly in the smart contract arbitrating the interactions between the provider and the set of consumer clients. The novelty is that the clustering is implemented and performed on a smart contract. Interestingly, smart contracts are seen not to be suitable for implementing heavy computational algorithms due to code execution cost, which is abstracted in terms of gas. However, implementing a clustering algorithm to define ToU ranges on a smart contract is interesting in the sense that this would permit further decentralization, where ToU ranges are defined by a smart independent entity, ensuring their integrity and transparency. For this purpose, the K-mean clustering algorithm was implemented with K = 2 for two clusters, which are the electricity demand peak and off-peak, on a Solidity smart contract. It was compiled and tested on a Remix IDE platform to define ToU ranges on both predicted and real electricity consumption, for each round. The smart contract execution cost was also assessed and benchmarked to the execution cost of other decentralized applications' smart contracts presented in other published works. The aim is to assess if performing K-mean clustering on a smart contract is realistic in terms of gas consumption. The K-mean algorithm flowchart is illustrated in Figure 6.  
So, the clustering is performed on a set of points within coordinates (x,y), where x designates the time of the day, ranging from 00:00 h to 23:00 h and y is the mean average of the electricity consumed on a particular hour of the day, during 100 days. For each hour of the day, the corresponding y value is computed from both predicted and real consumption data for each trading round according to the pseudocode in Algorithm 1. Once the arrays X and Y are both defined, the clustering is performed according to pseudocodes in Algorithms 2 and 3. Figure 5: Real energy consumption versus predicted energy consumption for the 50 th trading round. ```
1:\(Sum\gets 0\)
2:size\(\gets 0\)
3:for\(i\gets 0\) to 24 do
4:for\(j\gets i\) to 100 do
5:\(Sum\gets dataset[j]\)
6:\(j\gets j+24\)
7:\(size\gets size+1\)
8:endfor
9:\(y[i]\gets sum/size\)
10:endfor
```  
**Algorithm 1** Hourly mean average electricity consumption for one trading round  
The functions in Algorithms 2 and 3 are internal and can only be called in the main function, which is made public. In Algorithm 2, peaks and off-peak clusters are set for each hour of the day, according to the distance of the points from the centroids, passed as parameters. The function of Algorithm 3 calculates the new centroid for the next iteration. ```
1:for\(i\gets 0\) to \(size(X)\)do
2:\(d1\leftarrow\sqrt{X[i]-C1.x^{2}+Y[i]-C1.y^{2}}\)
3:\(d2\leftarrow\sqrt{X[i]-C2.x^{2}+Y[i]-C2.y^{2}}\)
4:if\(d1\leq d2\)then
5: clusters.push(1)
6:else
7: clusters.push(2)
8:endif
9:endfor
10:return clusters
```  
**Algorithm 2** SetCluster(centroid C1,centroid C2)  
To compute the clusters, X and Y arrays are defined in the smart contract as well as the two initial centroids. The initial off-peak cluster centroid is set to be the point of the set with the lowest consumption, whereas the initial peak cluster centroid is set to be the point in the dataset with the highest electricity consumption. The main function defined in Algorithm 4 invokes the functions of Algorithms 2 and 3 through a loop until convergence  
Figure 6: The K-mean algorithm flowchart. according to the flowchart in Figure 6. A decentralized P2P purchase and rental application based on blockchain has been presented in [65]. This has served to make a comparison with the implemented K-mean clustering smart contract, both deployment and invoking gas consumption.