-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_consensus.c
68 lines (59 loc) · 2.13 KB
/
app_consensus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/***************************************************************************//**
* @file app_consensus.c
* @brief Implementation file of the Average Consensus algorithm.
* @author Georgios Apostolakis
******************************************************************************/
#include "app_consensus.h"
#include "app_log.h"
#include "app_tools.h"
///These weights are used by the algorithm to update the current board's state.
static float weights[NUM_OF_BOARDS];
/** Initializes the weights array, required for the update of this board's state.
*
* @date 01/02/2023
*/
static void initialize_weights(){
int deg[NUM_OF_BOARDS]; //the degree of each node of the graph
int gr_deg = 0; //the degree of the graph
for(int i=0;i<NUM_OF_BOARDS;i++){
deg[i] = 0;
for(int j=0;j<NUM_OF_BOARDS;j++){
if(graph[i][j] && i!=j)
deg[i]++;
}
if(deg[i]>gr_deg)
gr_deg = deg[i];
}
for(int j=0;j<NUM_OF_BOARDS;j++){
if(!graph[BOARD_ID][j])
weights[j] = 0;
else if(j==BOARD_ID)
weights[j] = 1.0 - deg[BOARD_ID]/(1.0*(gr_deg+1));
else
weights[j] = 1.0/(gr_deg+1);
}
}
/*******************************************************************************
* It initializes the consensus setup.
******************************************************************************/
void initialize_consensus_setup(){
if(temperature<MIN_TEMPERATURE){
app_log_error("Error. Temperature not yet measured. Consensus setup cannot be initialized.\n");
return;
}
initialize_weights();
consensus_iters = 0;
consensus_states[BOARD_ID] = temperature;
//The states of the other boards do not need initialization.
//They will be set when a message from those boards will be received.
}
/*******************************************************************************
* It updates the state of this board.
******************************************************************************/
void update_consensus_state(){
float next_state = 0;
for(int i=0;i<NUM_OF_BOARDS;i++)
next_state += weights[i]*consensus_states[i];
consensus_states[BOARD_ID] = next_state;
consensus_iters++;
}