void shortest_path(int coord_x, int coord_y, int tax, int x, int y) { if ((graph[coord_x][coord_y] == 1 | graph[coord_x][coord_y] == 3) && tax < coast[coord_x][coord_y]) { coast[coord_x][coord_y] = tax; if ((coord_x != x || coord_y != y) && graph[coord_x][coord_y + 1] != -1) shortest_path(coord_x, coord_y + 1, tax+1, coord_x, coord_y); if ((coord_x + 1 != x || coord_y != y) && graph[coord_x + 1][coord_y] != -1) shortest_path(coord_x + 1, coord_y, tax+1, coord_x, coord_y); if ((coord_x != x || coord_y - 1 != y) && graph[coord_x][coord_y - 1] != -1) shortest_path(coord_x, coord_y-1, tax+1, coord_x, coord_y); if ((coord_x - 1 != x || coord_y != y) && graph[coord_x - 1][coord_y] != -1) shortest_path(coord_x - 1, coord_y, tax+1, coord_x, coord_y); } if (graph[coord_x][coord_y] == 0) { if (tax < coast[coord_x][coord_y]) coast[coord_x][coord_y] = tax; if (coast[coord_x][coord_y] < shortest) shortest = coast[coord_x][coord_y]; } }
bool Hex<T>::is_winning_move(player_t player) { //check for winning move only after getting atleast size_ moves if (nMove_ < size_) { return false; } if (player == 1) { return shortest_path(pos_virtual_start_blue(), pos_virtual_end_blue(), player); } else { return shortest_path(pos_virtual_start_red(), pos_virtual_end_red(), player); } }
InternalRouteResult TripPlugin::ComputeRoute(const std::vector<PhantomNode> &snapped_phantoms, const api::TripParameters ¶meters, const std::vector<NodeID> &trip) { InternalRouteResult min_route; // given he final trip, compute total duration and return the route and location permutation PhantomNodes viapoint; const auto start = std::begin(trip); const auto end = std::end(trip); // computes a roundtrip from the nodes in trip for (auto it = start; it != end; ++it) { const auto from_node = *it; // if from_node is the last node, compute the route from the last to the first location const auto to_node = std::next(it) != end ? *std::next(it) : *start; viapoint = PhantomNodes{snapped_phantoms[from_node], snapped_phantoms[to_node]}; min_route.segment_end_coordinates.emplace_back(viapoint); } BOOST_ASSERT(min_route.segment_end_coordinates.size() == trip.size()); shortest_path(min_route.segment_end_coordinates, parameters.continue_straight, min_route); BOOST_ASSERT_MSG(min_route.shortest_path_length < INVALID_EDGE_WEIGHT, "unroutable route"); return min_route; }
void mouse_callback( int button, int state, int x, int y ){ if( state == 0 ){ for( int i = 0; i < nodes_len; i++ ){ if( return_dist_sqr( x, y, nodes[i].x, nodes[i].y ) <= nodes[i].radius*nodes[i].radius ){ if( button_press_count == 0 ){ nodes[i].is_start = true; button_press_count += 1; start = nodes[i]; }else if( button_press_count == 1 ){ nodes[i].is_end = true; button_press_count += 1; end = nodes[i]; shortest_path( nodes, nodes_len, start, end ); }else if( button_press_count == 2 ){ button_press_count = 0; for( int x = 0; x < nodes_len; x++ ){ nodes[x].is_end = false; nodes[x].is_start = false; } } } } } }
int read_graph() { int i, j, n, m, coord_x, coord_y; scanf("%d %d", &n, &m); shortest = n*m; for (i=0; i<=n+1; i++) for (j=0; j<=m+1; j++) { graph[i][j] = -1; coast[i][j] = n*m; } for (i=1; i<=n; i++) for (j=1; j<=m; j++) { scanf("%d", &graph[i][j]); if (graph[i][j] == 3) { coord_x = i; coord_y = j; } } shortest_path(coord_x, coord_y, 0, 0, 0); printf("%d\n", shortest); return 0; }
/** Calculates the shortest path from one version to another. * The shortest path is defined as the path with the smallest combined * size, not the length of the path. * @param handle the context handle * @param deltas the list of alpm_delta_t * objects that a file has * @param to the file to start the search at * @param path the pointer to a list location where alpm_delta_t * objects that * have the smallest size are placed. NULL is set if there is no path * possible with the files available. * @return the size of the path stored, or LONG_MAX if path is unfindable */ off_t _alpm_shortest_delta_path(alpm_handle_t *handle, alpm_list_t *deltas, const char *to, alpm_list_t **path) { alpm_list_t *bestpath = NULL; alpm_list_t *vertices; off_t bestsize = LONG_MAX; if(deltas == NULL) { *path = NULL; return bestsize; } _alpm_log(handle, ALPM_LOG_DEBUG, "started delta shortest-path search for '%s'\n", to); vertices = graph_init(deltas, 0); graph_init_size(handle, vertices); dijkstra(vertices); bestsize = shortest_path(vertices, to, &bestpath); _alpm_log(handle, ALPM_LOG_DEBUG, "delta shortest-path search complete : '%jd'\n", (intmax_t)bestsize); alpm_list_free_inner(vertices, _alpm_graph_free); alpm_list_free(vertices); *path = bestpath; return bestsize; }
Sp_based::Sp_based(const graph& G) : og(G), g(G), inQ(G, 0), tt(G), on_compo(G),coms(G), n_stub(G, 0), dd(G, 0), d_stub(G), avgd(G,0), maxd(G,0), nbet(G,0), ebet(g,0) { id2compo(); shrink(); shortest_path(); }
int calculated_weight(int values[], int rows, int columns) { int collection[rows][columns]; populate_matrix(rows, columns, collection, values); int result = shortest_path(rows, columns, collection); return result; }
void Topology::createLinks() { // Find maximum switchID SwitchID max_switch_id = 0; for (int i=0; i<m_links_src_vector.size(); i++) { max_switch_id = max(max_switch_id, m_links_src_vector[i]); max_switch_id = max(max_switch_id, m_links_dest_vector[i]); } // Initialize weight vector Matrix topology_weights; Matrix topology_bw_multis; int num_switches = max_switch_id+1; topology_weights.setSize(num_switches); topology_bw_multis.setSize(num_switches); for(int i=0; i<topology_weights.size(); i++) { topology_weights[i].setSize(num_switches); topology_bw_multis[i].setSize(num_switches); for(int j=0; j<topology_weights[i].size(); j++) { topology_weights[i][j] = INFINITE_LATENCY; topology_bw_multis[i][j] = -1; // initialize to an invalid value } } // Set identity weights to zero for(int i=0; i<topology_weights.size(); i++) { topology_weights[i][i] = 0; } // Fill in the topology weights and bandwidth multipliers for (int i=0; i<m_links_src_vector.size(); i++) { topology_weights[m_links_src_vector[i]][m_links_dest_vector[i]] = m_links_latency_vector[i]; topology_bw_multis[m_links_src_vector[i]][m_links_dest_vector[i]] = m_bw_multiplier_vector[i]; } // Walk topology and hookup the links Matrix dist = shortest_path(topology_weights); for(int i=0; i<topology_weights.size(); i++) { for(int j=0; j<topology_weights[i].size(); j++) { int weight = topology_weights[i][j]; if (weight > 0 && weight != INFINITE_LATENCY) { int bw_multiplier = topology_bw_multis[i][j]; assert(bw_multiplier > 0); NetDest destination_set = shortest_path_to_node(i, j, topology_weights, dist, m_nodes); makeLink(i, j, destination_set, weight, bw_multiplier); } } } }
dessert_per_result_t refresh_rt(void *data, struct timeval *scheduled, struct timeval *interval) { pthread_rwlock_wrlock(&pp_rwlock); all_nodes_t *node = all_nodes_head; while (node) { if (node->entry_age-- == 0) { all_nodes_t* el_to_delete = node; HASH_DEL(all_nodes_head, el_to_delete); free(el_to_delete); } dessert_info("RT ENTRY " MAC " | Seqnr = %d | EntryAge = %d", node->addr[0], node->addr[1], node->addr[2], node->addr[3], node->addr[4], node->addr[5], node->seq_nr, node->entry_age); node = node->hh.next; } /* DIJKSTRA */ if (all_nodes_head) { // add self into RT node = malloc(sizeof(all_nodes_t)); memcpy(node->addr, dessert_l25_defsrc, ETH_ALEN); node->entry_age = RT_ENTRY_AGE; node->neighbors = dir_neighbors_head; HASH_ADD_KEYPTR(hh, all_nodes_head, node->addr, ETH_ALEN, node); // calculate shortest paths from self to all other in RT shortest_path(dessert_l25_defsrc); // delete self from RT HASH_DEL(all_nodes_head, node); free(node); } pthread_rwlock_unlock(&pp_rwlock); return 0; }
int main(int argc, char** argv) { // parse command line options Options options = get_options(argc, argv); // build graph from data file Graph* graph = read_graph(options.filename); // validate vertex ids now that we know graph size validate_vertex_ids(options, graph->n); // branch to relevant function depending on execution mode switch (options.part) { case PRINT_DFS: print_dfs(graph, options.source); break; case PRINT_BFS: print_bfs(graph, options.source); break; case DETAILED_PATH: detailed_path(graph, options.source, options.dest); break; case ALL_PATHS: all_paths(graph, options.source, options.dest); break; case SHORTEST_PATH: shortest_path(graph, options.source, options.dest); break; } // clean up free_graph(graph); // done! exit(EXIT_SUCCESS); }
void main() { shortest_path(0, cost, distance, NODES, found); print_path(0,3); print_distance(); }
void track_server_code (){ RegisterAs("TRACKSERVER"); // Server data sw_info sw_infos [NUM_SWITCHES]; int sensor_info [NUM_SENSORS]; // time when the sensor is hit int broken_sensors[NUM_SENSORS] = {0}; // Server's track data track_node track[TRACK_MAX]; init_track(track); init_broken_sensors(broken_sensors); track_node *src, *dst; // Received stuff track_receive rcv_buff; int sender_tid; int request_type; int sensor_index = 0; //Reply buffs int reply_dist = 0; int reply_index = 0; struct track_loc reply_loc; char reply_buff; int reply_buff_loc_dist [2]; struct train_server_request tsr; int train_server_id; while((train_server_id = WhoIs("TRAINSERVER")) == -1); int track_to_train_courier_tid = Create(PR_COURIER, track_to_train_courier_code); int track_to_train_courier_ready = 0; Queue train_serverQ; struct train_server_request allocated_buff[TRACK_TO_TRAIN_QUEUE_SIZE]; Queue_init (&train_serverQ, allocated_buff, TRACK_TO_TRAIN_QUEUE_SIZE, sizeof(struct train_server_request)); //things for shortest path request track_node* path[TRACK_MAX] = {0}; int dist[TRACK_MAX] = {0}; int shortest_dist = 0; routing_command commands[TRACK_MAX]; init_routing_commands(commands); // switch_command sw_commands[NUM_MAX_SWITCH_ROUTE_COMMANDS]; // memset(sw_commands, 0, NUM_MAX_SWITCH_ROUTE_COMMANDS * sizeof(switch_command)); //things for changing switches when train goes //within a specified route Queue switch_queues[NUM_TRAINS]; Queue *Q; switch_command switch_queue_buffers[NUM_TRAINS][NUM_MAX_SWITCH_ROUTE_COMMANDS]; memset(switch_queue_buffers, 0, NUM_TRAINS * NUM_MAX_SWITCH_ROUTE_COMMANDS * sizeof(switch_command)); int trnum; for (trnum = 0; trnum < NUM_TRAINS; trnum++) { Queue_init(&switch_queues[trnum], switch_queue_buffers[trnum], NUM_MAX_SWITCH_ROUTE_COMMANDS, sizeof(switch_command)); } // //Reservation data structures track_reservation track_reservations[TRACK_MAX]; train_reservation_info tr_reservation_infos[NUM_TRAINS]; memset(track_reservations, 0, TRACK_MAX * sizeof(track_reservation)); memset(tr_reservation_infos, -1, NUM_TRAINS * sizeof(train_reservation_info)); // Queue isReservableQueue; isReservableQueueItem isReservableQueueItems[NUM_TRAINS]; Queue_init(&isReservableQueue, isReservableQueueItems, NUM_TRAINS, sizeof(isReservableQueueItem)); isReservableQueueItem irqi; int reserve_ret = -1; int ret; while(1){ Receive(&sender_tid, &rcv_buff, sizeof(track_receive)); request_type = rcv_buff.type; switch (request_type){ case TRACK_TYPE_COURIER_RTS: track_to_train_courier_ready = 1; break; case TRACK_TYPE_SENSOR_INFO: sensor_index = (rcv_buff.data.track_data.data2 - 'A')*16+rcv_buff.data.track_data.data1-1; //if (sensor_index == 56) {HardDebug("D9 is hit. Hit time:%d. Rcv Time:%d", rcv_buff.data.track_data.time, Time());} sensor_info[sensor_index] = rcv_buff.data.track_data.time; Reply(sender_tid, &reply_buff, sizeof(char)); tsr.type = TRAIN_TYPE_SENSOR_UPDATE; memcpy(&tsr.data, &rcv_buff.data.track_data, sizeof(track_info)); if (!broken_sensors[sensor_index]) { //Send(train_server_id, &tsr, sizeof(struct train_server_request), &reply_buff, sizeof(char)); ret = Queue_push(&train_serverQ, &tsr); Warning(ret == 0, "TRACKSERVER: Queue_push failed"); } break; case TRACK_TYPE_SWITCH_INFO: (sw_infos[(int)rcv_buff.data.track_data.data1]).swdir = rcv_buff.data.track_data.data2; (sw_infos[(int)rcv_buff.data.track_data.data1]).time = rcv_buff.data.track_data.time; Reply(sender_tid, &reply_buff, sizeof(char)); tsr.type = TRAIN_TYPE_SWITCH_UPDATE; memcpy(&tsr.data, &rcv_buff.data.track_data, sizeof(track_info)); //Send(train_server_id, &tsr, sizeof(struct train_server_request), &reply_buff, sizeof(char)); ret = Queue_push(&train_serverQ, &tsr); Warning(ret == 0, "TRACKSERVER: Queue_push failed"); break; case TRACK_TYPE_DIST: if((int)rcv_buff.data.track_data.data1 >= TRACK_MAX || (int)rcv_buff.data.track_data.data2 >= TRACK_MAX) { reply_dist = 0; } else { src = &track[(int)rcv_buff.data.track_data.data1]; dst = &track[(int)rcv_buff.data.track_data.data2]; reply_dist = get_dist_between_nodes(src, dst, sw_infos); } Reply(sender_tid, &reply_dist, sizeof(int)); break; case TRACK_TYPE_NEXT_LOC: //get current loc_index and find next node __get_next_node((int)rcv_buff.data.track_data.data1, track, sw_infos, &reply_loc.index, reply_loc.name); Reply(sender_tid, &reply_loc, sizeof(struct track_loc)); break; case TRACK_TYPE_NEXT_SENSOR: reply_index = get_next_sensor_index((int)rcv_buff.data.track_request_data.data1, track, sw_infos, broken_sensors, &tsr.data.spd.spd_val); Assert(reply_index == tsr.data.spd.spd_val.next_expected_sensor_index, "reply_index != result->next_expected_sensor_index"); //Debug("TRACKSERVER: Got next expected sensor %d, after that %d", tsr.data.spd.spd_val.next_expected_sensor_index, tsr.data.spd.spd_val.sensor_after_expected_sensor); Reply(sender_tid, &reply_index, sizeof(int)); tsr.type = TRAIN_TYPE_SENSOR_PREDICTION; tsr.data.spd.trnum = (int)rcv_buff.data.track_request_data.data2; if (rcv_buff.data.track_request_data.data3) { //Send(train_server_id, &tsr, sizeof(train_server_request), &reply_buff, sizeof(char)); ret = Queue_push(&train_serverQ, &tsr); Warning(ret == 0, "TRACKSERVER: Queue_push failed"); } break; case TRACK_TYPE_LOC_NAME: Reply(sender_tid, (char*)track[(int)rcv_buff.data.track_data.data1].name, 6 * sizeof(char)); break; case TRACK_TYPE_SENSOR_REVERSE: reply_index = get_reverse_sensor((int)rcv_buff.data.track_request_data.data1, track, sw_infos, broken_sensors, &tsr.data.spd.spd_val); Reply(sender_tid, &reply_index, sizeof(int)); tsr.data.spd.trnum = (int)rcv_buff.data.track_request_data.data2; if (rcv_buff.data.track_request_data.data3) { //Send(train_server_id, &tsr, sizeof(train_server_request), &reply_buff, sizeof(char)); ret = Queue_push(&train_serverQ, &tsr); Warning(ret == 0, "TRACKSERVER: Queue_push failed"); } break; case TRACK_TYPE_UPDATE_LOC_AND_DISTANCE: translate_sensor_loc(rcv_buff.data.track_request_data.data1, rcv_buff.data.track_request_data.data2, track, sw_infos, reply_buff_loc_dist); Reply(sender_tid, reply_buff_loc_dist, 2*sizeof(int)); break; case TRACK_TYPE_FIND_ROUTE: Warning(((0 <= rcv_buff.data.track_request_data.data2 && rcv_buff.data.track_request_data.data2 < TRACK_MAX) && (0 <= rcv_buff.data.track_request_data.data3 && rcv_buff.data.track_request_data.data3 < TRACK_MAX)), "TRACKSERVER: TRACK_TYPE_FIND_ROUTE - Source(%d) or destination(%d) is invalid", rcv_buff.data.track_request_data.data2, rcv_buff.data.track_request_data.data3); //Debug("FINDING ROUTE:trnum:%d ~ src:%s ~ dest:%s ~ offset:%d",rcv_buff.data.track_request_data.data1, track[rcv_buff.data.track_request_data.data2].name, track[rcv_buff.data.track_request_data.data3].name, rcv_buff.data.track_request_data.data4); shortest_dist = shortest_path((int)rcv_buff.data.track_request_data.data2, (int)rcv_buff.data.track_request_data.data3, track, path, dist, track_reservations); if (shortest_dist == __INFINITY) { shortest_path((int)rcv_buff.data.track_request_data.data2, (int)rcv_buff.data.track_request_data.data3, track, path, dist, 0); } generate_commands(path, track, commands); print_commands(commands); pull_out_switch_orders(commands, tsr.data.path_commands.sw_commands, &switch_queues[rcv_buff.data.track_request_data.data1]); pull_out_reverse_orders(commands, tsr.data.path_commands.rv_commands); tsr.type = TRAIN_TYPE_ROUTE_SWITCH_ORDERS; Reply(sender_tid, &tsr, sizeof(train_server_request)); break; case TRACK_TYPE_RESERVATION_INIT: reserve_ret = reserve_inital_piece (track_reservations, track, tr_reservation_infos, sw_infos, &switch_queues[rcv_buff.data.track_request_data.data1], rcv_buff.data.track_request_data.data1, rcv_buff.data.track_request_data.data2, rcv_buff.data.track_request_data.data3, rcv_buff.data.track_request_data.data4); Debug("TRACKSERVER: Received reservation initialization request for train %d", rcv_buff.data.track_request_data.data1); tsr.type = TRAIN_TYPE_RESERVATION_SUCCESS; Reply(sender_tid, &tsr, sizeof(train_server_request)); break; case TRACK_TYPE_RESERVATION: Q = &switch_queues[rcv_buff.data.track_request_data.data1]; reserve_ret = reserve_track(track_reservations, track, tr_reservation_infos, sw_infos, Q, rcv_buff.data.track_request_data.data1, rcv_buff.data.track_request_data.data2); ret = release_track(track_reservations, track, tr_reservation_infos, sw_infos, rcv_buff.data.track_request_data.data1, rcv_buff.data.track_request_data.data3); Assert (ret != -1, "TRACKSERVER: release failed: ret:%d", ret); tsr.type = (reserve_ret == -1) ? TRAIN_TYPE_RESERVATION_FAILURE : TRAIN_TYPE_RESERVATION_SUCCESS; Reply(sender_tid, &tsr, sizeof(train_server_request)); if (/*rcv_buff.data.track_request_data.data2 < 0 ||*/ rcv_buff.data.track_request_data.data3 > 0) { check_isReservableRequests(&isReservableQueue, &train_serverQ, track, sw_infos, track_reservations, tr_reservation_infos, train_server_id); } print_total_reserved_dist(track_reservations, tr_reservation_infos, rcv_buff.data.track_request_data.data1); break; case TRACK_TYPE_RESERVATION_REVERSE: reserve_ret = reverse_reservation (track, tr_reservation_infos, sw_infos, rcv_buff.data.track_request_data.data1); tsr.type = TRAIN_TYPE_RESERVATION_SUCCESS; Reply(sender_tid, &tsr, sizeof(train_server_request)); break; case TRACK_TYPE_RESERVATION_SENSOR_UPDATE: reserve_ret = reservation_sensor_update(track_reservations, track, tr_reservation_infos, sw_infos, &switch_queues[rcv_buff.data.track_request_data.data1], rcv_buff.data.track_request_data.data1, rcv_buff.data.track_request_data.data2, rcv_buff.data.track_request_data.data3, rcv_buff.data.track_request_data.data4, rcv_buff.data.track_request_data.data5); tsr.type = (reserve_ret == -1) ? TRAIN_TYPE_RESERVATION_FAILURE : TRAIN_TYPE_RESERVATION_SUCCESS; Reply(sender_tid, &tsr, sizeof(train_server_request)); //check_isReservableRequests(&isReservableQueue, &train_serverQ, track, sw_infos, track_reservations, tr_reservation_infos, train_server_id); break; case TRACK_TYPE_RESERVATION_STOP_UPDATE: reserve_ret = reservation_stop_update ( track_reservations, track, tr_reservation_infos, sw_infos, &switch_queues[rcv_buff.data.track_request_data.data1], rcv_buff.data.track_request_data.data1, rcv_buff.data.track_request_data.data2, rcv_buff.data.track_request_data.data3, rcv_buff.data.track_request_data.data4, rcv_buff.data.track_request_data.data5); tsr.type = /*(reserve_ret == -1) ? TRAIN_TYPE_RESERVATION_FAILURE :*/ TRAIN_TYPE_RESERVATION_SUCCESS; Reply(sender_tid, &tsr, sizeof(train_server_request)); break; case TRACK_TYPE_IS_RESERVABLE: tsr.type = TRAIN_TYPE_REQ_IN_PROGRESS; Reply(sender_tid, &tsr, sizeof(train_server_request)); irqi.trnum = rcv_buff.data.track_request_data.data1; irqi.mm = rcv_buff.data.track_request_data.data2; ret = Queue_push(&isReservableQueue, &irqi); Warning(ret == 0, "TRACKSERVER: Queue_push failed"); break; case TRACK_TYPE_PRINT_RESERVATIONS: Reply(sender_tid, &reply_buff, sizeof(char)); print_reservations(track_reservations, track, tr_reservation_infos); break; default: Reply(sender_tid, &reply_buff, sizeof(char)); Warning(0,"Unknown receive in track_server_code: type=%d from %d", request_type, sender_tid); break; } if (track_to_train_courier_ready && !Queue_pop(&train_serverQ, &tsr)){ Reply(track_to_train_courier_tid, &tsr, sizeof(train_server_request)); track_to_train_courier_ready = 0; } } Exit(); Warning(0,"Unexpected return from Exit() at track_server_code\n\r"); }
static int lash_core(lash_t * p_lash) { osm_log_t *p_log = &p_lash->p_osm->log; unsigned num_switches = p_lash->num_switches; switch_t **switches = p_lash->switches; unsigned lanes_needed = 1; unsigned int i, j, k, dest_switch = 0; reachable_dest_t *dests, *idest; int cycle_found = 0; unsigned v_lane; int stop = 0, output_link, i_next_switch; int output_link2, i_next_switch2; int cycle_found2 = 0; int status = -1; int *switch_bitmap = NULL; /* Bitmap to check if we have processed this pair */ unsigned start_vl = p_lash->p_osm->subn.opt.lash_start_vl; OSM_LOG_ENTER(p_log); if (p_lash->p_osm->subn.opt.do_mesh_analysis && osm_do_mesh_analysis(p_lash)) { OSM_LOG(p_log, OSM_LOG_ERROR, "ERR 4D05: Mesh analysis failed\n"); goto Exit; } for (i = 0; i < num_switches; i++) { shortest_path(p_lash, i); if (generate_routing_func_for_mst(p_lash, i, &dests)) { OSM_LOG(p_log, OSM_LOG_ERROR, "ERR 4D06: " "generate_routing_func_for_mst failed\n"); goto Exit; } idest = dests; while (idest != NULL) { dests = dests->next; free(idest); idest = dests; } for (j = 0; j < num_switches; j++) { switches[j]->used_channels = 0; switches[j]->q_state = UNQUEUED; } } switch_bitmap = calloc(num_switches * num_switches, sizeof(int)); if (!switch_bitmap) { OSM_LOG(p_log, OSM_LOG_ERROR, "ERR 4D04: " "Failed allocating switch_bitmap - out of memory\n"); goto Exit; } for (i = 0; i < num_switches; i++) { for (dest_switch = 0; dest_switch < num_switches; dest_switch++) if (dest_switch != i && switch_bitmap[i * num_switches + dest_switch] == 0) { v_lane = 0; stop = 0; while (v_lane < lanes_needed && stop == 0) { if (generate_cdg_for_sp(p_lash, i, dest_switch, v_lane) || generate_cdg_for_sp(p_lash, dest_switch, i, v_lane)) { OSM_LOG(p_log, OSM_LOG_ERROR, "ERR 4D07: generate_cdg_for_sp failed\n"); goto Exit; } output_link = switches[i]->routing_table[dest_switch].out_link; output_link2 = switches[dest_switch]->routing_table[i].out_link; i_next_switch = get_next_switch(p_lash, i, output_link); i_next_switch2 = get_next_switch(p_lash, dest_switch, output_link2); CL_ASSERT(p_lash-> cdg_vertex_matrix[v_lane][i][i_next_switch] != NULL); CL_ASSERT(p_lash-> cdg_vertex_matrix[v_lane][dest_switch] [i_next_switch2] != NULL); cycle_found = cycle_exists(p_lash-> cdg_vertex_matrix[v_lane][i] [i_next_switch], NULL, NULL, 1); cycle_found2 = cycle_exists(p_lash-> cdg_vertex_matrix[v_lane][dest_switch] [i_next_switch2], NULL, NULL, 1); for (j = 0; j < num_switches; j++) for (k = 0; k < num_switches; k++) if (p_lash-> cdg_vertex_matrix[v_lane][j][k] != NULL) { p_lash-> cdg_vertex_matrix[v_lane][j] [k]->visiting_number = 0; p_lash-> cdg_vertex_matrix[v_lane][j] [k]->seen = 0; } if (cycle_found == 1 || cycle_found2 == 1) { remove_temp_depend_for_sp(p_lash, i, dest_switch, v_lane); remove_temp_depend_for_sp(p_lash, dest_switch, i, v_lane); v_lane++; } else { set_temp_depend_to_permanent_for_sp(p_lash, i, dest_switch, v_lane); set_temp_depend_to_permanent_for_sp(p_lash, dest_switch, i, v_lane); stop = 1; p_lash->num_mst_in_lane[v_lane]++; p_lash->num_mst_in_lane[v_lane]++; } } switches[i]->routing_table[dest_switch].lane = v_lane + start_vl; switches[dest_switch]->routing_table[i].lane = v_lane + start_vl; if (cycle_found == 1 || cycle_found2 == 1) { if (++lanes_needed > p_lash->vl_min) goto Error_Not_Enough_Lanes; if (generate_cdg_for_sp(p_lash, i, dest_switch, v_lane) || generate_cdg_for_sp(p_lash, dest_switch, i, v_lane)) { OSM_LOG(p_log, OSM_LOG_ERROR, "ERR 4D08: generate_cdg_for_sp failed\n"); goto Exit; } set_temp_depend_to_permanent_for_sp(p_lash, i, dest_switch, v_lane); set_temp_depend_to_permanent_for_sp(p_lash, dest_switch, i, v_lane); p_lash->num_mst_in_lane[v_lane]++; p_lash->num_mst_in_lane[v_lane]++; } p_lash->virtual_location[i][dest_switch][v_lane] = 1; p_lash->virtual_location[dest_switch][i][v_lane] = 1; switch_bitmap[i * num_switches + dest_switch] = 1; switch_bitmap[dest_switch * num_switches + i] = 1; } } for (i = 0; i < lanes_needed; i++) OSM_LOG(p_log, OSM_LOG_INFO, "Lanes in layer %d: %d\n", i, p_lash->num_mst_in_lane[i]); OSM_LOG(p_log, OSM_LOG_INFO, "Lanes needed: %d, Balancing\n", lanes_needed); if (balance_virtual_lanes(p_lash, lanes_needed)) { OSM_LOG(p_log, OSM_LOG_ERROR, "ERR 4D09: Balancing failed\n"); goto Exit; } for (i = 0; i < lanes_needed; i++) OSM_LOG(p_log, OSM_LOG_INFO, "Lanes in layer %d: %d\n", i, p_lash->num_mst_in_lane[i]); status = 0; goto Exit; Error_Not_Enough_Lanes: OSM_LOG(p_log, OSM_LOG_ERROR, "ERR 4D02: " "Lane requirements (%d) exceed available lanes (%d)" " with starting lane (%d)\n", lanes_needed, p_lash->vl_min, start_vl); Exit: if (switch_bitmap) free(switch_bitmap); OSM_LOG_EXIT(p_log); return status; }
Status ViaRoutePlugin::HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade, const api::RouteParameters &route_parameters, util::json::Object &json_result) { BOOST_ASSERT(route_parameters.IsValid()); if (max_locations_viaroute > 0 && (static_cast<int>(route_parameters.coordinates.size()) > max_locations_viaroute)) { return Error("TooBig", "Number of entries " + std::to_string(route_parameters.coordinates.size()) + " is higher than current maximum (" + std::to_string(max_locations_viaroute) + ")", json_result); } if (!CheckAllCoordinates(route_parameters.coordinates)) { return Error("InvalidValue", "Invalid coordinate value.", json_result); } auto phantom_node_pairs = GetPhantomNodes(*facade, route_parameters); if (phantom_node_pairs.size() != route_parameters.coordinates.size()) { return Error("NoSegment", std::string("Could not find a matching segment for coordinate ") + std::to_string(phantom_node_pairs.size()), json_result); } BOOST_ASSERT(phantom_node_pairs.size() == route_parameters.coordinates.size()); auto snapped_phantoms = SnapPhantomNodes(phantom_node_pairs); const bool continue_straight_at_waypoint = route_parameters.continue_straight ? *route_parameters.continue_straight : facade->GetContinueStraightDefault(); InternalRouteResult raw_route; auto build_phantom_pairs = [&raw_route, continue_straight_at_waypoint]( const PhantomNode &first_node, const PhantomNode &second_node) { raw_route.segment_end_coordinates.push_back(PhantomNodes{first_node, second_node}); auto &last_inserted = raw_route.segment_end_coordinates.back(); // enable forward direction if possible if (last_inserted.source_phantom.forward_segment_id.id != SPECIAL_SEGMENTID) { last_inserted.source_phantom.forward_segment_id.enabled |= !continue_straight_at_waypoint; } // enable reverse direction if possible if (last_inserted.source_phantom.reverse_segment_id.id != SPECIAL_SEGMENTID) { last_inserted.source_phantom.reverse_segment_id.enabled |= !continue_straight_at_waypoint; } }; util::for_each_pair(snapped_phantoms, build_phantom_pairs); if (1 == raw_route.segment_end_coordinates.size()) { if (route_parameters.alternatives && facade->GetCoreSize() == 0) { alternative_path(*facade, raw_route.segment_end_coordinates.front(), raw_route); } else { direct_shortest_path(*facade, raw_route.segment_end_coordinates, raw_route); } } else { shortest_path(*facade, raw_route.segment_end_coordinates, route_parameters.continue_straight, raw_route); } // we can only know this after the fact, different SCC ids still // allow for connection in one direction. if (raw_route.is_valid()) { api::RouteAPI route_api{*facade, route_parameters}; route_api.MakeResponse(raw_route, json_result); } else { auto first_component_id = snapped_phantoms.front().component.id; auto not_in_same_component = std::any_of(snapped_phantoms.begin(), snapped_phantoms.end(), [first_component_id](const PhantomNode &node) { return node.component.id != first_component_id; }); if (not_in_same_component) { return Error("NoRoute", "Impossible route between points", json_result); } else { return Error("NoRoute", "No route found between points", json_result); } } return Status::Ok; }
int main(int argc, char *argv[]){ FILE *fp; char buff[1000]; int ch, i, j, x, y; int line_count = 0; count = -1; count_intersect = 0; k = 0; fp = fopen(argv[1], "r"); if (fp == NULL){ printf("There's no input file.\n"); exit(0); } else{ if( (display = XOpenDisplay(display_name)) == NULL ){ printf("Could not open display. \n"); exit(-1); } printf("Connected to X server %s\n", XDisplayName(display_name) ); screen_num = DefaultScreen(display); screen = DefaultScreenOfDisplay(display); colormap = XDefaultColormap(display, screen_num); display_width = DisplayWidth(display, screen_num); display_height = DisplayHeight(display, screen_num); //obtaining line count while(!feof(fp)){ ch = fgetc(fp); if (ch == '\n'){ line_count++; } } rewind(fp); } //int m[line_count][6]; int m[line_count+1][6]; //Creating window border_width = 10; win_x = 0; win_y = 0; win_width = display_width/2; win_height = display_height * 0.8; //win_height = (int)(win_width/1.7); //rectangular window printf("window width: %d\n window height: %d\n", display_width, display_height); //win = XCreateSimpleWindow(display, RootWindow(display, 0), 1, 1, win_width, win_height, 10, WhitePixel (display, 0), WhitePixel (display, 0)); win = XCreateSimpleWindow(display, RootWindow(display, screen_num), win_x, win_y, win_width, win_height, border_width, BlackPixel(display, screen_num), WhitePixel(display, screen_num)); //Maps window on screen size_hints = XAllocSizeHints(); wm_hints = XAllocWMHints(); class_hints = XAllocClassHint(); if (size_hints == NULL || wm_hints == NULL || class_hints == NULL){ printf("Error allocating memory for hints\n"); exit(-1); } size_hints -> flags = PPosition | PSize | PMinSize; size_hints -> min_width = 60; size_hints -> min_height = 60; XStringListToTextProperty(&win_name_string, 1, &win_name); XStringListToTextProperty(&icon_name_string, 1, &icon_name); wm_hints -> flags = StateHint | InputHint; wm_hints -> initial_state = NormalState; wm_hints -> input = False; class_hints -> res_name = "x_use_example"; class_hints -> res_class = "homework1"; XSetWMProperties(display, win, &win_name, &icon_name, argv, argc, size_hints, wm_hints, class_hints ); XSelectInput(display, win, ExposureMask | KeyPressMask | ButtonPressMask); // put on screen XMapWindow(display, win); XFlush(display); //graphics setup green_gc = XCreateGC(display, win, 0, 0); XParseColor(display, colormap, green, &green_col); if (XAllocColor(display, colormap, &green_col) == 0){ printf("Failed to get color green\n"); exit(-1); } else{ printf("Success green!\n"); XSetForeground(display, green_gc, green_col.pixel); } red_gc = XCreateGC(display, win, 0, 0); XParseColor(display, colormap, red, &red_col); if (XAllocColor(display, colormap, &red_col) == 0){ printf("Failed to get color red\n"); exit(-1); } else{ printf("Success red!\n"); XSetForeground(display, red_gc, red_col.pixel); } black_gc = XCreateGC(display, win, 0, 0); XParseColor(display, colormap, black, &black_col); if (XAllocColor(display, colormap, &black_col) == 0){ printf("Failed to get color black\n"); exit(-1); } else{ printf("Success black!\n"); XSetForeground(display, black_gc, black_col.pixel); } light_purple_gc = XCreateGC(display, win, 0, 0); XParseColor(display, colormap, light_purple, &light_purple_col); if (XAllocColor(display, colormap, &light_purple_col) == 0){ printf("Failed to get color light purple\n"); exit(-1); } else{ printf("Success light purple!\n"); XSetForeground(display, light_purple_gc, light_purple_col.pixel); } white_gc = XCreateGC(display, win, 0, 0); XParseColor(display, colormap, white, &white_col); if (XAllocColor(display, colormap, &white_col) == 0){ printf("Failed to get color white\n"); exit(-1); } else{ printf("Success white!\n"); XSetForeground(display, white_gc, white_col.pixel); } while(1){ XNextEvent(display, &report); switch(report.type){ case Expose: { for (i = 0; i <= line_count; i++){ fscanf(fp, "%*s ( %d, %d) ( %d, %d) (%d, %d)", &m[i][0], &m[i][1], &m[i][2], &m[i][3], &m[i][4], &m[i][5]); } m[line_count+1][0] = -5; for (i = 0; i <= line_count ; i++){ for (j=0; j<6; j+=2){ m[i][j] = m[i][j] + 100; m[i][j+1] = m[i][j+1] + 100; } } for (i = 0; i <=line_count; i++){ //Draw the triangles XDrawLine(display, win, green_gc, m[i][0], m[i][1], m[i][2], m[i][3]); XDrawLine(display, win, green_gc, m[i][2], m[i][3], m[i][4], m[i][5]); XDrawLine(display, win, green_gc, m[i][4], m[i][5], m[i][0], m[i][1]); } rewind(fp); if (valid_vertex[0][0] != 0.0){ for (i= 0; i<k; i++){ if (valid_vertex[i][6] == 0.0 || valid_vertex[i][6] == -4.0){ printf("from (%d, %d) to (%d, %d) length: %f and path length: %f\n", (int)valid_vertex[i][0], (int)valid_vertex[i][1], (int)valid_vertex[i][2], (int)valid_vertex[i][3], valid_vertex[i][4], valid_vertex[i][5]); XDrawLine(display, win, light_purple_gc, valid_vertex[i][0], valid_vertex[i][1], valid_vertex[i][2], valid_vertex[i][3]); } } XFillArc( display, win, black_gc, start_x, start_y, win_width/200, win_width/200, 0, 360*64); XFillArc( display, win, black_gc, target_x, target_y, win_width/200, win_width/200, 0, 360*64); } else{} //printf("exposed\n"); XFlush(display); break; } case ButtonPress: { //printf("Button press %d, %d.\n",report.xbutton.x, report.xbutton.y); double distance1; x = report.xbutton.x; y = report.xbutton.y; int inTriangle; if (report.xbutton.button == Button1){ /* left click */ count++; inTriangle = check_if_in_triangle(line_count, m, x, y); if (inTriangle == 0){ XFillArc( display, win, black_gc, x, y, win_width/200, win_width/200, 0, 360*64); } } else{ printf("Closing Window.\n"); XDestroyWindow(display, win); XCloseDisplay(display); exit(1); } //printf("count: %d\n", count); if (count == 0){ reset(m, line_count); start_x = x; start_y = y; if (inTriangle == 1){ count = -1; } } else if (count == 1){ if (inTriangle == 0){ target_x = x; target_y = y; printf("\n\nStarting point: (%d, %d)\nTarget point: (%d, %d)\n", start_x, start_y, target_x, target_y); int vertex[line_count][6]; int * nearest_triangles; int * result_line_seg; for(i=0;i<=line_count;i++){ //store formatted input file in array m fscanf(fp, "%*s ( %d, %d) ( %d, %d) (%d, %d)", &vertex[i][0], &vertex[i][1], &vertex[i][2], &vertex[i][3], &vertex[i][4], &vertex[i][5]); } rewind(fp); printf("Total triangles: %d\n", line_count+1); for (i = 0; i <= line_count ; i++){ for (j=0; j<6; j+=2){ vertex[i][j] = vertex[i][j] + 100; vertex[i][j+1] = vertex[i][j+1] + 100; } } start_graph(line_count, vertex, start_x, start_y, target_x, target_y); printf("Applied start_graph()\n"); shortest_path(); printf("Applied shortest_path()\n"); organize(); printf("The shortest path: \n"); for (i= 0; i<k; i++){ if (valid_vertex[i][6] == 0.0 || valid_vertex[i][6] == -4.0){ printf("from (%d, %d) to (%d, %d) length: %f and path length: %f\n", (int)valid_vertex[i][0], (int)valid_vertex[i][1], (int)valid_vertex[i][2], (int)valid_vertex[i][3], valid_vertex[i][4], valid_vertex[i][5]); XDrawLine(display, win, light_purple_gc, valid_vertex[i][0], valid_vertex[i][1], valid_vertex[i][2], valid_vertex[i][3]); } } printf("DONE\n"); count = -1; } } XFlush(display); break; } default: break; } } fclose(fp); return 0; }
void Topology::createLinks(bool isReconfiguration) { // Find maximum switchID SwitchID max_switch_id = 0; for (int i=0; i<m_links_src_vector.size(); i++) { max_switch_id = max(max_switch_id, m_links_src_vector[i]); max_switch_id = max(max_switch_id, m_links_dest_vector[i]); } //cerr << "Max switch id "<< max_switch_id<<"\n"; // Initialize weight vector //typedef Vector < Vector <int> > Matrix; Matrix topology_weights; Matrix topology_latency; Matrix topology_bw_multis; int num_switches = max_switch_id+1; topology_weights.setSize(num_switches); topology_latency.setSize(num_switches); topology_bw_multis.setSize(num_switches); m_component_latencies.setSize(num_switches); // FIXME setting the size of a member variable here is a HACK! m_component_inter_switches.setSize(num_switches); // FIXME setting the size of a member variable here is a HACK! for(int i=0; i<topology_weights.size(); i++) { topology_weights[i].setSize(num_switches); topology_latency[i].setSize(num_switches); topology_bw_multis[i].setSize(num_switches); m_component_latencies[i].setSize(num_switches); m_component_inter_switches[i].setSize(num_switches); // FIXME setting the size of a member variable here is a HACK! for(int j=0; j<topology_weights[i].size(); j++) { topology_weights[i][j] = INFINITE_LATENCY; topology_latency[i][j] = -1; // initialize to an invalid value topology_bw_multis[i][j] = -1; // initialize to an invalid value m_component_latencies[i][j] = -1; // initialize to an invalid value m_component_inter_switches[i][j] = 0; // initially assume direct connections / no intermediate switches between components } } // Set identity weights to zero for(int i=0; i<topology_weights.size(); i++) { topology_weights[i][i] = 0; } // Fill in the topology weights and bandwidth multipliers for (int i=0; i<m_links_src_vector.size(); i++) { topology_weights[m_links_src_vector[i]][m_links_dest_vector[i]] = m_links_weight_vector[i]; topology_latency[m_links_src_vector[i]][m_links_dest_vector[i]] = m_links_latency_vector[i]; m_component_latencies[m_links_src_vector[i]][m_links_dest_vector[i]] = m_links_latency_vector[i]; // initialize to latency vector topology_bw_multis[m_links_src_vector[i]][m_links_dest_vector[i]] = m_bw_multiplier_vector[i]; } // Walk topology and hookup the links Matrix dist = shortest_path(topology_weights, m_component_latencies, m_component_inter_switches); for(int i=0; i<topology_weights.size(); i++) { for(int j=0; j<topology_weights[i].size(); j++) { int weight = topology_weights[i][j]; int bw_multiplier = topology_bw_multis[i][j]; int latency = topology_latency[i][j]; if (weight > 0 && weight != INFINITE_LATENCY) { NetDest destination_set = shortest_path_to_node(i, j, topology_weights, dist); assert(latency != -1); makeLink(i, j, destination_set, latency, weight, bw_multiplier, isReconfiguration); } } } }