mlst_network-ea1.h
//Here is a feedback loop round of the MLST algorithm
Here is a feedback loop round of the MLST algorithm
Here is a feedback loop round of the MLST algorithm
static void mlst_recalculate(){
#ifdef ROOT
	own_mlst_public_variable.distance_to_root = 0;
	own_mlst_public_variable.parent_id = 0xffff;
	own_mlst_public_variable.children_count = 0xff;
#else 
	uint8_t children_count = 0;
	uint8_t distance_to_root = 0xff;
	uint8_t number_of_potential_parents = 0;
	struct Nbr* best_parent = 0;
	struct mlst_public_variable* best_parent_pv = 0;
	struct Nbr* n = pvn_getNbrs(&mlst_pvn);
	for(; n!=0; n=pvn_getNextNbr(n)){
		struct mlst_public_variable* n_pv = (struct mlst_public_variable*)(n->public_var);
		if(n_pv->parent_id == 0){
			mlst_stay_active_for_next_n_periods = IF_CHANGE_STAY_ACTIVE_FOR_N_PERIODS;
			children_count++;
			continue; 
		} else {
			if( n_pv->parent_id == (RIME_ID) || n_pv->energy_state == 0 ){ 
				children_count++;
				continue;
			} else { 
				if(n_pv->distance_to_root+1 < distance_to_root){
					distance_to_root = n_pv->distance_to_root+1;
					number_of_potential_parents = 1;
					best_parent = n;
					best_parent_pv = n_pv;
				} else if(n_pv->distance_to_root+1 == distance_to_root){ 
					if(best_parent_pv->energy_state > n_pv->energy_state){
						number_of_potential_parents = 1;
						best_parent = n;
						best_parent_pv = n_pv;
					} else if(best_parent_pv->energy_state > n_pv->energy_state){ 
						if(best_parent_pv->children_count < n_pv->children_count){
							number_of_potential_parents = 1;
							best_parent = n;
							best_parent_pv = n_pv;
						} else if(best_parent_pv->children_count == n_pv->children_count){
							number_of_potential_parents++;
							if(best_parent->id > n->id){ 
								best_parent = n;
								best_parent_pv = n_pv;
							}
						}
					}
				}
			}
		}
	}
	if(best_parent!=0){
		if(number_of_potential_parents>1 && random_rand()<0.5*RANDOM_RAND_MAX){
#ifdef DEBUG
			printf("CANNOT DECIDE\n");
#endif
			own_mlst_public_variable.parent_id = 0;
			own_mlst_public_variable.distance_to_root = 0xff;
			own_mlst_public_variable.children_count = children_count;
		} else {
			if(own_mlst_public_variable.parent_id==0 ||
					own_mlst_public_variable.parent_id!=best_parent->id ||
					own_mlst_public_variable.distance_to_root!=distance_to_root ||
					own_mlst_public_variable.children_count != children_count
			  ){
				mlst_stay_active_for_next_n_periods = IF_CHANGE_STAY_ACTIVE_FOR_N_PERIODS;
				divide_period_time_by = 3;
			}
			own_mlst_public_variable.parent_id = best_parent->id;
			own_mlst_public_variable.distance_to_root = distance_to_root;
			own_mlst_public_variable.children_count = children_count;
			mlst_parent = best_parent;
		}
	} else {
		own_mlst_public_variable.parent_id = 0;
		own_mlst_public_variable.distance_to_root = 0xff;
		own_mlst_public_variable.children_count = children_count;
	}
#endif
}
static void mlst_recalculate ( ) { #ifdef ROOT own_mlst_public_variable . distance_to_root = 0 ; own_mlst_public_variable . parent_id = 0xffff ; own_mlst_public_variable . children_count = 0xff ; #else uint8_t children_count = 0 ; uint8_t distance_to_root = 0xff ; uint8_t number_of_potential_parents = 0 ; struct Nbr * best_parent = 0 ; struct mlst_public_variable * best_parent_pv = 0 ; struct Nbr * n = pvn_getNbrs ( & mlst_pvn ) ; for ( ; n != 0 ; n = pvn_getNextNbr ( n ) ) { struct mlst_public_variable * n_pv = ( struct mlst_public_variable * ) ( n -> public_var ) ; if ( n_pv -> parent_id == 0 ) { mlst_stay_active_for_next_n_periods = IF_CHANGE_STAY_ACTIVE_FOR_N_PERIODS ; children_count ++ ; continue ; } else { if ( n_pv -> parent_id == ( RIME_ID ) || n_pv -> energy_state == 0 ) { children_count ++ ; continue ; } else { if ( n_pv -> distance_to_root + 1 < distance_to_root ) { distance_to_root = n_pv -> distance_to_root + 1 ; number_of_potential_parents = 1 ; best_parent = n ; best_parent_pv = n_pv ; } else if ( n_pv -> distance_to_root + 1 == distance_to_root ) { if ( best_parent_pv -> energy_state > n_pv -> energy_state ) { number_of_potential_parents = 1 ; best_parent = n ; best_parent_pv = n_pv ; } else if ( best_parent_pv -> energy_state > n_pv -> energy_state ) { if ( best_parent_pv -> children_count < n_pv -> children_count ) { number_of_potential_parents = 1 ; best_parent = n ; best_parent_pv = n_pv ; } else if ( best_parent_pv -> children_count == n_pv -> children_count ) { number_of_potential_parents ++ ; if ( best_parent -> id > n -> id ) { best_parent = n ; best_parent_pv = n_pv ; } } } } } } } if ( best_parent != 0 ) { if ( number_of_potential_parents > 1 && random_rand ( ) < 0.5 * RANDOM_RAND_MAX ) { #ifdef DEBUG printf ( " \n " ) ; #endif own_mlst_public_variable . parent_id = 0 ; own_mlst_public_variable . distance_to_root = 0xff ; own_mlst_public_variable . children_count = children_count ; } else { if ( own_mlst_public_variable . parent_id == 0 || own_mlst_public_variable . parent_id != best_parent -> id || own_mlst_public_variable . distance_to_root != distance_to_root || own_mlst_public_variable . children_count != children_count ) { mlst_stay_active_for_next_n_periods = IF_CHANGE_STAY_ACTIVE_FOR_N_PERIODS ; divide_period_time_by = 3 ; } own_mlst_public_variable . parent_id = best_parent -> id ; own_mlst_public_variable . distance_to_root = distance_to_root ; own_mlst_public_variable . children_count = children_count ; mlst_parent = best_parent ; } } else { own_mlst_public_variable . parent_id = 0 ; own_mlst_public_variable . distance_to_root = 0xff ; own_mlst_public_variable . children_count = children_count ; } #endif }
Here is a feedback loop round of the MLST algorithm
Here is a feedback loop round of the MLST algorithm
//Iterating neighbors //Neighbor has undefined state; //is defined child //potential parent //closer than current best parent //compare energy //more children than current best parent //has same values as current best parent //If multiple choices, choose parent with lowest id //set state //if parent is not unique, you may want to wait one round //stay undefined //check if something has changed and you should stay online for some rounds //set new state //undefined