size_t Item::import_from_table(prototypes::ItemTable data) { size_t response = 0; response = set_name(data._name); response = set_description(data._description); response = set_bonuses(data._bonuses); response = set_penalties(data._penalties); response = set_slots(data._slots); response = set_cost(data._cost); return response; }
void solution::route_traffics(){ vector< vector< double > > traffics = instance.get_traffics(); vector< vector< double > > distances = instance.get_distances(); vector< vector< double > > cost; vector< vector< unsigned > > H1, H2; for(int i = 0; i < instance.get_n(); i++){ vector< double > temp_cost; vector< unsigned > temp_H1, temp_H2; for(int j = 0; j < instance.get_n(); j++){ // Calculating all costs using all hubs assigned to i vector< vector< double > > c_ij; for(int hi = 0; hi < r; hi++){ vector< double > aux; for(int hj = 0; hj < r; hj++){ double temp = traffics[i][j] * ((instance.get_collection_rate() * distances[i][assigned_hubs[i][hi]]) + (instance.get_transfer_rate() * distances[assigned_hubs[i][hi]][assigned_hubs[j][hj]]) + (instance.get_distribution_rate() * distances[assigned_hubs[j][hj]][j])); aux.push_back(temp); } c_ij.push_back(aux); } // Finding the minimum distance and hubs vector< double >::iterator it = min_element(c_ij[0].begin(), c_ij[0].end()); int h1 = 0; int h2 = it - c_ij[0].begin(); for(int hi = 1; hi < r; hi++){ vector< double >::iterator it2 = min_element(c_ij[hi].begin(), c_ij[hi].end()); if(*it2 < *it){ it = it2; h1 = hi; h2 = it - c_ij[hi].begin(); } } temp_cost.push_back(*it); temp_H1.push_back(assigned_hubs[i][h1]); temp_H2.push_back(assigned_hubs[j][h2]); } cost.push_back(temp_cost); H1.push_back(temp_H1); H2.push_back(temp_H2); } set_cost(cost); set_f_chosen(H1); set_s_chosen(H2); generate_hubs_cost(); }
//function to find goal from a given start point void findGoal(int start_x, int start_y, int goal_x, int goal_y){ int lowestx; int lowesty; init_map(); createopen(); insert_by_priority(node_map[start_y][start_x]); while(true){ get_lowest_f_cost(lowestx, lowesty); add_to_closed(lowestx, lowesty); if(is_goal(lowestx, lowesty,goal_x,goal_y)){ displayTextLine(2, "found path"); wait1Msec(100); //get_path(); get_distance(start_x, start_y, goal_x, goal_y); return; } //get neighbours get_neighbours(lowestx, lowesty); for(int i=0; i<CONNECTIVITY;i++){ if((current_neighbours[i].val==OBST)||(current_neighbours[i].onclosed==true)){} else if((current_neighbours[i].g>(get_g_cost(current_neighbours[i].x,current_neighbours[i].y,lowestx, lowesty)))|| current_neighbours[i].onopen==false){ set_cost(current_neighbours[i].x,current_neighbours[i].y,lowestx, lowesty); current_neighbours[i].parentx=lowestx; current_neighbours[i].parenty=lowesty; memcpy(&node_map[current_neighbours[i].y][current_neighbours[i].x],¤t_neighbours[i],sizeof(node_map[current_neighbours[i].y][current_neighbours[i].x])); if(current_neighbours[i].onopen==false){ insert_by_priority(current_neighbours[i]); } } } } }
void Generic_map::set_cost(Tripoint p, int c) { set_cost(p.x, p.y, p.z, c); }
void Generic_map::set_cost(int x, int y, int c) { set_cost(x, y, 0, c); }
//function to get the neighbours of a given cell (north, north-east, east, south-east, south, south-west, west, north-west) void get_neighbours(int x, int y){ //north if (((y-1)<Y_SIZE) && ((y-1)>=0) && ((x)<X_SIZE) && ((x)>=0)){ //checks if node is out of bounds/ out of the world if(node_map[y-1][x].g==0){ //checks if g cost has not been set. if not, set it. set_cost(node_map[y-1][x].x,node_map[y-1][x].y,x,y); } memcpy(¤t_neighbours[0], &node_map[y-1][x], sizeof(current_neighbours[0]));//copy cell from neighbours array into node map } else { current_neighbours[0].val=1; //else neighbour is an obstacle so set its value attribute to 1 } if (((y)<Y_SIZE) && ((y)>=0) && ((x+1)<X_SIZE) && ((x+1)>=0)){ if(node_map[y][x+1].g==0){ set_cost(node_map[y][x+1].x,node_map[y][x+1].y,x,y); } memcpy(¤t_neighbours[2], &node_map[y][x+1], sizeof(current_neighbours[2]));//east } else { current_neighbours[2].val=1; } if (((y+1)<Y_SIZE) && ((y+1)>=0) && ((x)<X_SIZE) && ((x)>=0)){ if(node_map[y+1][x].g==0){ set_cost(node_map[y+1][x].x,node_map[y+1][x].y,x,y); } memcpy(¤t_neighbours[4], &node_map[y+1][x], sizeof(current_neighbours[4]));//south } else { current_neighbours[4].val=1; } if (((y)<Y_SIZE) && ((y)>=0) && ((x-1)<X_SIZE) && ((x-1)>=0)){ if(node_map[y][x-1].g==0){ set_cost(node_map[y][x-1].x,node_map[y][x-1].y,x,y); } memcpy(¤t_neighbours[6], &node_map[y][x-1], sizeof(current_neighbours[6]));//west } else { current_neighbours[6].val=1; } if (((y-1)<Y_SIZE) && ((y-1)>=0) && ((x+1)<X_SIZE) && ((x+1)>=0)&&(current_neighbours[0].val==FREE)&&(current_neighbours[2].val==FREE)){ if(node_map[y-1][x+1].g==0){ set_cost(node_map[y-1][x+1].x,node_map[y-1][x+1].y,x,y); } memcpy(¤t_neighbours[1], &node_map[y-1][x+1], sizeof(current_neighbours[1]));//north-east } else { current_neighbours[1].val=1; } if (((y+1)<Y_SIZE) && ((y+1)>=0) && ((x+1)<X_SIZE) && ((x+1)>=0)&&(current_neighbours[2].val==FREE)&&(current_neighbours[4].val==FREE)){ if(node_map[y+1][x+1].g==0){ set_cost(node_map[y+1][x+1].x,node_map[y+1][x+1].y,x,y); } memcpy(¤t_neighbours[3], &node_map[y+1][x+1], sizeof(current_neighbours[3]));//south-east } else { current_neighbours[3].val=1; } if (((y-1)<Y_SIZE) && ((y-1)>=0) && ((x-1)<X_SIZE) && ((x-1)>=0)&&(current_neighbours[0].val==FREE)&&(current_neighbours[6].val==FREE)){ if(node_map[y-1][x-1].g==0){ set_cost(node_map[y-1][x-1].x,node_map[y-1][x-1].y,x,y); } memcpy(¤t_neighbours[7], &node_map[y-1][x-1], sizeof(current_neighbours[7]));//north-west } else { current_neighbours[7].val=1; } if (((y+1)<Y_SIZE) && ((y+1)>=0) && ((x-1)<X_SIZE) && ((x-1)>=0)&&(current_neighbours[4].val==FREE)&&(current_neighbours[6].val==FREE)){ if(node_map[y+1][x-1].g==0){ set_cost(node_map[y+1][x-1].x,node_map[y+1][x-1].y,x,y); } memcpy(¤t_neighbours[5], &node_map[y+1][x-1], sizeof(current_neighbours[5]));//south-west } else { current_neighbours[5].val=1; } }