unsigned short int search::next_action( wall &maze_memorizer, route &route_memorizer, unsigned short int position[], unsigned short int direction ) { unsigned short int next_action; if ( mode == REVERSE && virtual_route.get_the_direction_to_solve( position, direction ) == DUMMY ) { mode = NORMAL; virtual_route.has_potential_map = 0; virtual_route.reset_the_map_to_solve(); } if ( mode == NORMAL ) { next_action = _normal.next_action( maze_memorizer, route_memorizer, position, direction, virtual_walls ); if ( _normal.is_done ) { mode = REVERSE; _normal.is_done = 0; } } if ( mode == REVERSE ) { if ( !virtual_route.has_potential_map ) { mode = NORMAL; map_potentials( maze_memorizer, route_memorizer, position, direction ); mode = REVERSE; } next_action = virtual_route.get_the_direction_to_solve( position, direction ); } return next_action; }
void search::map_potentials( wall &maze_memorizer, route &route_memorizer, unsigned short int position[], unsigned short int direction ) { unsigned short int next_action; virtual_position[0] = aimed_position[0] = position[0]; virtual_position[1] = aimed_position[1] = position[1]; virtual_direction = aimed_direction = direction; route_memorizer.update_status( aimed_position, aimed_direction, GO_BACKWARD ); while( !_reverse.is_done && !(aimed_position[0] == 15 && aimed_position[1] == 0 && aimed_direction == DOWN) ) { next_action = _reverse.next_action( maze_memorizer, route_memorizer, aimed_position, aimed_direction, virtual_walls ); route_memorizer.update_status( aimed_position, aimed_direction, next_action ); } _reverse.is_done = 0; while ( !virtual_route.has_potential_map ) { if ( mode == NORMAL ) { _normal.map_potentials( maze_memorizer, virtual_route, virtual_position, virtual_direction, position, direction, aimed_position, aimed_direction ); if ( _normal.is_done ) { mode = REVERSE; _normal.is_done = 0; } } else if ( mode == REVERSE ) { _reverse.map_potentials( maze_memorizer, virtual_route, virtual_position, virtual_direction, position, direction, aimed_position, aimed_direction ); if ( _reverse.is_done ) { mode = NORMAL; _reverse.is_done = 0; } } } }
void pmaint::pmaint_lookup (bigint key, dhash_stat err, vec<chord_node> sl, route r) { if (err) { warning << host_node->my_ID () << " lookup failed. key " << key << ", err " << err << "\n"; pmaint_next (); //XXX delay? return; } assert (r.size () >= 2); assert (sl.size () >= 1); chordID succ = r.pop_back ()->id (); chordID pred = r.pop_back ()->id (); assert (succ == sl[0].x); if (dhblock_chash::num_efrags () > sl.size ()) { warning << "not enough successors: " << sl.size () << " vs " << dhblock_chash::num_efrags () << "\n"; //try again later active_cb = delaycb (PRTTMLONG, wrap (this, &pmaint::pmaint_next)); return; } if ((sl.size () > dhblock_chash::num_efrags() && dhblock_chash::num_efrags () > 1 && betweenbothincl (sl[0].x, sl[sl.size () - 1].x, host_node->my_ID ())) || (dhblock_chash::num_efrags () == 1 && sl[0].x == host_node->my_ID ()) //above is a special case since between always returns true // when the first two arguments are equal ) { trace << host_node->my_ID () << " PMAINT: we are a replica for " << key << " " << sl[0].x << " -- " << sl[dhblock_chash::num_efrags () - 1] << "\n"; //case I: we are a replica of the key. //i.e. in the successor list. Do nothing. //next time we'll do a lookup with the next key active_cb = delaycb (PRTTMTINY, wrap (this, &pmaint::pmaint_next)); } else { //case II: this key doesn't belong to us. Offer it to another node pmaint_searching = false; trace << host_node->my_ID () << ": offering " << key << "\n"; pmaint_offer (key, sl[0]); } }
void vnode_impl::find_successor_cb (chordID x, cbroute_t cb, vec<chord_node> s, route search_path, chordstat status) { if (status != CHORD_OK) { warnx << "find_successor_cb: find successor of " << x << " failed: " << status << "\n"; } else { nhops += search_path.size (); if (search_path.size () > nmaxhops) nmaxhops = search_path.size (); } cb (s, search_path, status); }
unsigned short int solve::next_action( wall &maze_memorizer, route &route_memorizer, unsigned short int position[], unsigned short int &direction ) { if ( !route_memorizer.has_potential_map ) { map_potentials( maze_memorizer, route_memorizer, position, direction ); } if ( (position[0] == GOAL_I_MIN || position[0] == GOAL_I_MAX) && (position[1] == GOAL_J_MIN || position[1] == GOAL_J_MAX) ) { if ( mode == NORMAL ) { mode = REVERSE; return GO_BACKWARD; } } if ( mode == NORMAL ) { route_memorizer.put_a_sign_to_reverse( position, direction ); return route_memorizer.get_the_direction_to_solve( position, direction ); } else if ( mode == REVERSE ) { return route_memorizer.get_the_direction_to_reverse( position, direction ); } return DUMMY; }
void reverse::map_potentials( wall &maze_memorizer, route &route_memorizer, unsigned short int position[], unsigned short int &direction, unsigned short int starting_position[], unsigned short int starting_direction, unsigned short int aimed_position[], unsigned short int aimed_direction ) { unsigned short int next_action = DUMMY; unsigned short int potential_to_set = 0; if ( position[0] == starting_position[0] && position[1] == starting_position[1] ) { if ( direction == starting_direction ) { route_memorizer.has_potential_map = 1; route_memorizer.solve_the_maze( maze_memorizer, starting_position, starting_direction, aimed_position, aimed_direction ); route_memorizer.reset(); is_done = 1; } else { if ( maze_memorizer.safe_to_go( position, direction, LEFT ) ) { next_action = GO_LEFTWARD; } else if ( maze_memorizer.safe_to_go( position, direction, UP ) ) { next_action = GO_FORWARD; } else if ( maze_memorizer.safe_to_go( position, direction, RIGHT ) ) { next_action = GO_RIGHTWARD; } route_memorizer.update_status( position, direction, next_action ); if ( direction == starting_direction ) { potential_to_set = route_memorizer.s; } else { potential_to_set = route_memorizer.t; } route_memorizer.put_a_sign_to_reverse( position, direction ); route_memorizer.set_potential( position, potential_to_set ); route_memorizer.update_visited_places( position ); is_done = 1; } } else { if ( route_memorizer.signed_crossroad( position ) ) { route_memorizer.decrement_the_count_of_facing_unvisited_blocks( position ); virtual_position[0] = position[0]; virtual_position[1] = position[1]; virtual_direction = direction; route_memorizer.update_status( virtual_position, virtual_direction, GO_LEFTWARD ); if ( maze_memorizer.safe_to_go( position, direction, LEFT ) && !(virtual_position[0] == starting_position[0] && virtual_position[1] == starting_position[1]) ) { if ( route_memorizer.signed_crossroad( position ) ) { next_action = GO_LEFTWARD; potential_to_set = route_memorizer.get_potential( position ) + route_memorizer.s; } else if ( route_memorizer.visited( position, direction, LEFT ) ) { if ( route_memorizer.get_potential( position, direction, LEFT ) > route_memorizer.get_potential( position ) + route_memorizer.s ) { next_action = GO_LEFTWARD; if ( maze_memorizer.safe_to_go( position, direction, RIGHT ) ) { if ( route_memorizer.get_potential( position ) > route_memorizer.get_potential( position, direction, RIGHT ) ) { potential_to_set = route_memorizer.get_potential( position ) + route_memorizer.s; } else { potential_to_set = route_memorizer.get_potential( position ) + route_memorizer.t; } } else { potential_to_set = route_memorizer.get_potential( position ) + route_memorizer.t; } } } else { next_action = GO_LEFTWARD; if ( maze_memorizer.safe_to_go( position, direction, RIGHT ) ) { if ( route_memorizer.get_potential( position ) > route_memorizer.get_potential( position, direction, RIGHT ) ) { potential_to_set = route_memorizer.get_potential( position ) + route_memorizer.s; } else { potential_to_set = route_memorizer.get_potential( position ) + route_memorizer.t; } } else { potential_to_set = route_memorizer.get_potential( position ) + route_memorizer.t; } } } if ( next_action == DUMMY ) { next_action = GO_FORWARD; potential_to_set = route_memorizer.get_potential( position ) + route_memorizer.t; } route_memorizer.update_status( position, direction, next_action ); route_memorizer.put_a_sign_to_reverse( position, direction ); route_memorizer.set_potential( position, potential_to_set ); route_memorizer.update_visited_places( position ); is_done = 1; } else { next_action = route_memorizer.get_the_direction_to_reverse( position, direction ); route_memorizer.remove_a_sign_to_reverse( position ); route_memorizer.update_status( position, direction, next_action ); } } }
unsigned short int reverse::next_action ( wall &maze_memorizer, route &route_memorizer, unsigned short int position[], unsigned short int direction, wall &virtual_walls ) { unsigned short int succeeded_virtual_foolish_solution; if ( route_memorizer.signed_crossroad( position ) ) { virtual_walls.update( position, direction, DOWN, UNSAFE ); if ( maze_memorizer.safe_to_go( position, direction, LEFT ) && virtual_walls.safe_to_go( position, direction, LEFT ) ) { route_memorizer.decrement_the_count_of_facing_unvisited_blocks( position ); virtual_position[0] = position[0]; virtual_position[1] = position[1]; virtual_direction = direction; route_memorizer.update_status( virtual_position, virtual_direction, GO_LEFTWARD ); if ( maze_memorizer.need_to_scan( virtual_position ) ) { succeeded_virtual_foolish_solution = route_memorizer.virtual_foolish_solution( maze_memorizer, virtual_position, virtual_direction, position ); route_memorizer.update_status( virtual_position, virtual_direction, TURN_L_90 ); if ( virtual_direction == direction && succeeded_virtual_foolish_solution ) { virtual_walls.update( virtual_position, virtual_direction, LEFT, UNSAFE ); } else { is_done = 1; return GO_LEFTWARD; } } else { route_memorizer.update_visited_places( virtual_position ); route_memorizer.update_for_virtual_walls( virtual_position, virtual_direction, maze_memorizer, virtual_walls ); virtual_walls.update( position, direction, LEFT, UNSAFE ); } } if ( route_memorizer.signed_crossroad( position ) && maze_memorizer.safe_to_go( position, direction, UP ) && virtual_walls.safe_to_go( position, direction, UP ) ) { route_memorizer.decrement_the_count_of_facing_unvisited_blocks( position ); virtual_position[0] = position[0]; virtual_position[1] = position[1]; virtual_direction = direction; route_memorizer.update_status( virtual_position, virtual_direction, GO_FORWARD ); if ( maze_memorizer.need_to_scan( virtual_position ) ) { succeeded_virtual_foolish_solution = route_memorizer.virtual_foolish_solution( maze_memorizer, virtual_position, virtual_direction, position ); route_memorizer.update_status( virtual_position, virtual_direction, TURN ); if ( virtual_direction == direction && succeeded_virtual_foolish_solution ) { virtual_walls.update( virtual_position, virtual_direction, UP, UNSAFE ); } else { is_done = 1; return GO_FORWARD; } } else { route_memorizer.update_visited_places( virtual_position ); route_memorizer.update_for_virtual_walls( virtual_position, virtual_direction, maze_memorizer, virtual_walls ); virtual_walls.update( position, direction, UP, UNSAFE ); } } if ( route_memorizer.signed_crossroad( position ) && maze_memorizer.safe_to_go( position, direction, RIGHT ) && virtual_walls.safe_to_go( position, direction, RIGHT ) ) { route_memorizer.decrement_the_count_of_facing_unvisited_blocks( position ); virtual_position[0] = position[0]; virtual_position[1] = position[1]; virtual_direction = direction; route_memorizer.update_status( virtual_position, virtual_direction, GO_RIGHTWARD ); if ( maze_memorizer.need_to_scan( virtual_position ) ) { succeeded_virtual_foolish_solution = route_memorizer.virtual_foolish_solution( maze_memorizer, virtual_position, virtual_direction, position ); route_memorizer.update_status( virtual_position, virtual_direction, TURN_R_90 ); if ( virtual_direction == direction && succeeded_virtual_foolish_solution ) { virtual_walls.update( virtual_position, virtual_direction, RIGHT, UNSAFE ); } else { is_done = 1; return GO_RIGHTWARD; } } else { route_memorizer.update_visited_places( virtual_position ); route_memorizer.update_for_virtual_walls( virtual_position, virtual_direction, maze_memorizer, virtual_walls ); virtual_walls.update( position, direction, RIGHT, UNSAFE ); } } } if ( maze_memorizer.safe_to_go( position, direction, LEFT ) && virtual_walls.safe_to_go( position, direction, LEFT ) ) { return GO_LEFTWARD; } else if ( maze_memorizer.safe_to_go( position, direction, UP ) && virtual_walls.safe_to_go( position, direction, UP ) ) { return GO_FORWARD; } else if ( maze_memorizer.safe_to_go( position, direction, RIGHT ) && virtual_walls.safe_to_go( position, direction, RIGHT ) ) { return GO_RIGHTWARD; } return DUMMY; }
void normal::map_potentials( wall &maze_memorizer, route &route_memorizer, unsigned short int position[], unsigned short int &direction, unsigned short int starting_position[], unsigned short int starting_direction, unsigned short int aimed_position[], unsigned short int aimed_direction ) { unsigned short int next_action = DUMMY; unsigned short int the_count_of_facing_unvisited_blocks = 0; //precisely wrong name unsigned short int potential_to_set = 0; virtual_position[0] = position[0]; virtual_position[1] = position[1]; virtual_direction = direction; route_memorizer.update_status( virtual_position, virtual_direction, GO_LEFTWARD ); if ( maze_memorizer.safe_to_go( position, direction, LEFT ) && !(virtual_position[0] == starting_position[0] && virtual_position[1] == starting_position[1]) ) { if ( route_memorizer.visited( position, direction, LEFT ) ) { if ( route_memorizer.get_potential( position, direction, LEFT ) > route_memorizer.get_potential( position ) + route_memorizer.t ) { the_count_of_facing_unvisited_blocks++; next_action = GO_LEFTWARD; potential_to_set = route_memorizer.get_potential( position ) + route_memorizer.t; } } else { the_count_of_facing_unvisited_blocks++; next_action = GO_LEFTWARD; potential_to_set = route_memorizer.get_potential( position ) + route_memorizer.t; } } virtual_position[0] = position[0]; virtual_position[1] = position[1]; virtual_direction = direction; route_memorizer.update_status( virtual_position, virtual_direction, GO_FORWARD ); if ( maze_memorizer.safe_to_go( position, direction, UP ) && !(virtual_position[0] == starting_position[0] && virtual_position[1] == starting_position[1]) ) { if ( route_memorizer.visited( position, direction, UP ) ) { if ( route_memorizer.get_potential( position, direction, UP ) > route_memorizer.get_potential( position ) + route_memorizer.s ) { the_count_of_facing_unvisited_blocks++; if ( next_action == DUMMY ) { next_action = GO_FORWARD; potential_to_set = route_memorizer.get_potential( position ) + route_memorizer.s; } } } else { the_count_of_facing_unvisited_blocks++; if ( next_action == DUMMY ) { next_action = GO_FORWARD; potential_to_set = route_memorizer.get_potential( position ) + route_memorizer.s; } } } virtual_position[0] = position[0]; virtual_position[1] = position[1]; virtual_direction = direction; route_memorizer.update_status( virtual_position, virtual_direction, GO_RIGHTWARD ); if ( maze_memorizer.safe_to_go( position, direction, RIGHT ) && !(virtual_position[0] == starting_position[0] && virtual_position[1] == starting_position[1]) ) { if ( route_memorizer.visited( position, direction, RIGHT ) ) { if ( route_memorizer.get_potential( position, direction, RIGHT ) > route_memorizer.get_potential( position ) + route_memorizer.t ) { the_count_of_facing_unvisited_blocks++; if ( next_action == DUMMY ) { next_action = GO_RIGHTWARD; potential_to_set = route_memorizer.get_potential( position ) + route_memorizer.t; } } } else { the_count_of_facing_unvisited_blocks++; if ( next_action == DUMMY ) { next_action = GO_RIGHTWARD; potential_to_set = route_memorizer.get_potential( position ) + route_memorizer.t; } } } if ( next_action != DUMMY ) { route_memorizer.set_the_count_of_facing_unvisited_blocks( position, the_count_of_facing_unvisited_blocks - 1 ); route_memorizer.update_status( position, direction, next_action ); route_memorizer.put_a_sign_to_reverse( position, direction ); route_memorizer.update_visited_places( position ); route_memorizer.set_potential( position, potential_to_set ); } else { if ( position[0] == starting_position[0] && position[1] == starting_position[1] ) { route_memorizer.update_status( position, direction, GO_BACKWARD ); route_memorizer.put_a_sign_to_reverse( position, direction ); route_memorizer.update_visited_places( position ); route_memorizer.set_potential( position, route_memorizer.t ); } else { route_memorizer.remove_a_sign_to_reverse( position ); route_memorizer.update_status( position, direction, GO_BACKWARD ); is_done = 1; } } }
unsigned short int normal::next_action ( wall &maze_memorizer, route &route_memorizer, unsigned short int position[], unsigned short int direction, wall &virtual_walls ) { unsigned short int the_count_of_safe_to_go_blocks; unsigned short int succeeded_virtual_foolish_solution; if ( maze_memorizer.need_to_scan( position ) ) { return SCAN; } else { route_memorizer.update_for_virtual_walls( position, direction, maze_memorizer, virtual_walls ); the_count_of_safe_to_go_blocks = maze_memorizer.the_count_of_safe_to_go_blocks(position,direction) - virtual_walls.the_count_of_unsafe_to_go_blocks(position,direction); if ( the_count_of_safe_to_go_blocks ) { if ( maze_memorizer.safe_to_go( position, direction, LEFT ) && virtual_walls.safe_to_go( position, direction, LEFT ) ) { virtual_position[0] = position[0]; virtual_position[1] = position[1]; virtual_direction = direction; route_memorizer.update_status( virtual_position, virtual_direction, GO_LEFTWARD ); if ( maze_memorizer.need_to_scan( virtual_position ) ) { if ( the_count_of_safe_to_go_blocks > 1 ) { succeeded_virtual_foolish_solution = route_memorizer.virtual_foolish_solution( maze_memorizer, virtual_position, virtual_direction, position ); route_memorizer.update_status( virtual_position, virtual_direction, TURN_L_90 ); if ( virtual_direction == direction && succeeded_virtual_foolish_solution ) { the_count_of_safe_to_go_blocks--; virtual_walls.update( virtual_position, virtual_direction, LEFT, UNSAFE ); } else { route_memorizer.set_the_count_of_facing_unvisited_blocks( position, the_count_of_safe_to_go_blocks - 1 ); return GO_LEFTWARD; } } else { route_memorizer.set_the_count_of_facing_unvisited_blocks( position, the_count_of_safe_to_go_blocks - 1 ); return GO_LEFTWARD; } } else { the_count_of_safe_to_go_blocks--; route_memorizer.update_visited_places( virtual_position ); route_memorizer.update_for_virtual_walls( position, direction, maze_memorizer, virtual_walls ); virtual_walls.update( position, direction, LEFT, UNSAFE ); } } if ( maze_memorizer.safe_to_go( position, direction, UP ) && virtual_walls.safe_to_go( position, direction, UP ) ) { virtual_position[0] = position[0]; virtual_position[1] = position[1]; virtual_direction = direction; route_memorizer.update_status( virtual_position, virtual_direction, GO_FORWARD ); if ( maze_memorizer.need_to_scan( virtual_position ) ) { if ( the_count_of_safe_to_go_blocks > 1 ) { succeeded_virtual_foolish_solution = route_memorizer.virtual_foolish_solution( maze_memorizer, virtual_position, virtual_direction, position ); route_memorizer.update_status( virtual_position, virtual_direction, TURN ); if ( virtual_direction == direction && succeeded_virtual_foolish_solution ) { the_count_of_safe_to_go_blocks--; virtual_walls.update( virtual_position, virtual_direction, UP, UNSAFE ); } else { route_memorizer.set_the_count_of_facing_unvisited_blocks( position, the_count_of_safe_to_go_blocks - 1 ); return GO_FORWARD; } } else { route_memorizer.set_the_count_of_facing_unvisited_blocks( position, the_count_of_safe_to_go_blocks - 1 ); return GO_FORWARD; } } else { the_count_of_safe_to_go_blocks--; route_memorizer.update_visited_places( virtual_position ); route_memorizer.update_for_virtual_walls( position, direction, maze_memorizer, virtual_walls ); virtual_walls.update( position, direction, UP, UNSAFE ); } } if ( maze_memorizer.safe_to_go( position, direction, RIGHT ) && virtual_walls.safe_to_go( position, direction, RIGHT ) ) { virtual_position[0] = position[0]; virtual_position[1] = position[1]; virtual_direction = direction; route_memorizer.update_status( virtual_position, virtual_direction, GO_RIGHTWARD ); if ( maze_memorizer.need_to_scan( virtual_position ) ) { if ( the_count_of_safe_to_go_blocks > 1 ) { succeeded_virtual_foolish_solution = route_memorizer.virtual_foolish_solution( maze_memorizer, virtual_position, virtual_direction, position ); route_memorizer.update_status( virtual_position, virtual_direction, TURN_R_90 ); if ( virtual_direction == direction && succeeded_virtual_foolish_solution ) { the_count_of_safe_to_go_blocks--; virtual_walls.update( virtual_position, virtual_direction, RIGHT, UNSAFE ); } else { route_memorizer.set_the_count_of_facing_unvisited_blocks( position, the_count_of_safe_to_go_blocks - 1 ); return GO_RIGHTWARD; } } else { route_memorizer.set_the_count_of_facing_unvisited_blocks( position, the_count_of_safe_to_go_blocks - 1 ); return GO_RIGHTWARD; } } else { the_count_of_safe_to_go_blocks--; route_memorizer.update_visited_places( virtual_position ); route_memorizer.update_for_virtual_walls( position, direction, maze_memorizer, virtual_walls ); virtual_walls.update( position, direction, RIGHT, UNSAFE ); } } } is_done = 1; return DUMMY; } }