void gray_mage_cast_spell(void) { _slot_info_ptr slot_ptr; /* Blind is OK!!! */ if (p_ptr->confused) { msg_print("You are too confused!"); return; } slot_ptr = _choose("Cast", _ALLOW_EXCHANGE | _SHOW_INFO); if (slot_ptr) { magic_type *spell_ptr = _get_spell_info(slot_ptr->realm, slot_ptr->spell); int cost = calculate_cost(spell_ptr->smana); int fail = calculate_fail_rate(spell_ptr->slevel, spell_ptr->sfail, p_ptr->stat_ind[A_INT]); if (spell_ptr->slevel > p_ptr->lev) /* Experience Drain? */ { msg_format("You need to be level %d to use that spell.", spell_ptr->slevel); return; } if (cost > p_ptr->csp) { msg_print("You do not have enough mana to cast this spell."); return; } p_ptr->csp -= cost; energy_use = 100; if (randint0(100) < fail) { if (flush_failure) flush(); cmsg_format(TERM_VIOLET, "You failed to cast %s!", do_spell(slot_ptr->realm, slot_ptr->spell, SPELL_NAME)); if (demigod_is_(DEMIGOD_ATHENA)) p_ptr->csp += cost/2; spell_stats_on_fail_old(slot_ptr->realm, slot_ptr->spell); sound(SOUND_FAIL); do_spell(slot_ptr->realm, slot_ptr->spell, SPELL_FAIL); } else { if (!do_spell(slot_ptr->realm, slot_ptr->spell, SPELL_CAST)) { /* Canceled */ p_ptr->csp += cost; energy_use = 0; return; } sound(SOUND_ZAP); spell_stats_on_cast_old(slot_ptr->realm, slot_ptr->spell); } } p_ptr->redraw |= PR_MANA; p_ptr->window |= PW_SPELL; }
//Select initial clients from each 360/nV angle void CVRP::init_solution(vector<Route*> &solution, vector<double> &time, vector<int> &route_capacity, int &n){ double angle = 360/dep.nVehicles; for(int i = 0; i < clients.size(); i++){ //Angle between the deposit and the i-th client double ang_b2p = angle_b2p(dep.coord, clients[i]->coord); //Calculate pos of Ci double posd = ang_b2p/angle; int pos = ceil(posd); int cost = calculate_cost(dep.coord, clients[i]->coord); clients[i]->cost = cost; if(solution[pos]->clients.size() == 0){ solution[pos]->clients.push_back(clients[i]); route_capacity[pos] = clients[i]->demand; time[pos] = clients[i]->cost; }else if(cost < solution[pos]->clients[0]->cost){ //if the client i is better than the i - 1 for that route solution[pos]->clients[0] = clients[i]; route_capacity[pos] = clients[i]->demand; time[pos] = clients[i]->cost; } } for(int r = 0; r < solution.size(); r++){ if(!solution[r]->clients.empty()){ visited[solution[r]->clients[0]->id - 1] = true; time[r] += solution[r]->clients[0]->cost + solution[r]->clients[0]->servTime; solution[r]->clients[0]->arrTime = solution[r]->clients[0]->cost; n++; } } }
int main(int argc, char** argv) { if (argc > 2) if (strcmp(argv[1], "-d") == 0) { debug = 1; debug_file = fopen(argv[2], "w"); } char namestring[MAXNAME]; printf("what will you define?\n"); fgets(namestring, MAXNAME, stdin); *(strchr(namestring, '\n')) = '\0'; struct node* root = setup_tree(namestring); if (debug) print_tree(root); printf("total cost for %s is %.2f\n", namestring, calculate_cost(root, 1)); free_tree(root); if (debug) fclose(debug_file); return 0; }
int main(int argc, char const *argv[]) { srand(time(NULL)); tests_descr=fopen("tests.txt","r"); int n_tests; fscanf(tests_descr,"%d",&n_tests); for(int n=0; n<n_tests; n++) { distance_t opt_cost,day_limit; char filename[1<<10]; fscanf(tests_descr,"%s %f %f\n",filename,&opt_cost,&day_limit); auto G = graph_from_file_2D_coords(filename); auto M = day_limit; if(G.size()==0)continue; std::cout<<filename<<" "<<G.size()<<" "<<opt_cost<<" "; //auto route = solve_tsp_daily(G, M, solve_tsp_greedy{}, divide_route_greedy{}); //std::cout << "route:\n"; //print_route(route); //auto cost = calculate_cost(route, G, M); //std::cout << opt_cost<<" "; //std::cout /*<< "route cost: "*/ << cost.first<<" ";/* <<"("<<100.*(cost.first-opt_cost)/opt_cost<<"% err)\n"; //std::cout << "total cost: " << cost.second << "\n";*/ { auto route = solve_tsp_daily(G, M, solve_tsp_greedy{}, divide_route_greedy{}); auto cost = calculate_cost(route, G, M); std::cout << cost.first <<" "; } { auto route = solve_tsp_daily(G, M, solve_tsp_tabu{}, divide_route_greedy{}); auto cost = calculate_cost(route, G, M); std::cout << cost.first <<" "; } { auto route = solve_tsp_daily(G, M, solve_tsp_GRASP{}, divide_route_greedy{}); auto cost = calculate_cost(route, G, M); std::cout << cost.first <<" "; } std::cout <<"\n"; } return 0; }
static void _list_spell(doc_ptr doc, int realm, int spell, int choice, int options) { magic_type *spell_ptr = _get_spell_info(realm, spell); int cost = calculate_cost(spell_ptr->smana); int fail = calculate_fail_rate(spell_ptr->slevel, spell_ptr->sfail, p_ptr->stat_ind[A_INT]); if (cost > p_ptr->csp) doc_insert(doc, "<color:D>"); else if (choice == _browse_choice) doc_insert(doc, "<color:B>"); else if (spell_ptr->slevel > p_ptr->lev) { if (options & _FROM_BOOK) doc_insert(doc, "<color:D>"); else doc_insert(doc, "<color:y>"); } else doc_insert(doc, "<color:w>"); if (spell_ptr->slevel > p_ptr->lev) doc_printf(doc, " <color:D>%c)</color> ", I2A(choice)); else doc_printf(doc, " %c) ", I2A(choice)); doc_printf(doc, "%-20.20s ", do_spell(realm, spell, SPELL_NAME)); doc_printf(doc, "%3d %3d %3d%% ", spell_ptr->slevel, cost, fail); if (spell_ptr->slevel > p_ptr->lev) { if (options & _FROM_BOOK) doc_printf(doc, "%-15.15s", ""); else doc_printf(doc, "%-15.15s", "Forgotten"); } else if (options & _SHOW_INFO) doc_printf(doc, "%-15.15s", do_spell(realm, spell, SPELL_INFO)); if (options & _SHOW_STATS) { spell_stats_ptr stats = spell_stats_old(realm, spell); if (stats->ct_cast + stats->ct_fail) { doc_printf(doc, " %5d %4d %3d%%", stats->ct_cast, stats->ct_fail, spell_stats_fail(stats) ); } } doc_insert(doc, "</color>\n"); }
/* * Finds the best trajectory according to WEIGHTED_COST_FUNCTIONS (global). * * @param: ref_state - [s, s_dot, s_ddot, d, d_dot, d_ddot] * @param: target_state - a length 6 array indicating the offset we are aiming for between us and the target_state. * @param: T - the desired time at which we will be at the goal (relative to now as t=0) * @param: predictions - dictionary of {v_id : vehicle }. * * @return: (best_s, best_d, best_t) where best_s are the 6 coefficients representing s(t) best_d gives coefficients for d(t) and best_t gives duration associated w/ this trajectory. */ struct trajectory* Ptg::PTG(struct state* ref_state, struct state* target_state, double T, std::map<int8_t, Vehicle>* predictions) { struct trajectory* best_traj = NULL; int8_t target_vehicle = -1; // generate alternative goals std::vector<struct goal> all_goals; double timestep = 0.2; double t = T - 4 * timestep; while (t <= T + 4 * timestep) // loop 8 times = 8 traj { struct model goal_s = target_state->s; struct model goal_d = target_state->d; struct goal goals; goals.s = goal_s; goals.d = goal_d; goals.t = t; all_goals.push_back(goals); t += timestep; } // find best trajectory std::map<double, struct trajectory*> cost_traj; for (std::vector<struct goal>::iterator it=all_goals.begin(); it!=all_goals.end(); ++it) { double cost = 0; struct trajectory* traj = new trajectory(); struct model s_goal = it->s; struct model d_goal = it->d; double t = it->t; JMT(traj->s_coeffs, &ref_state->s, &s_goal, t); JMT(traj->d_coeffs, &ref_state->d, &d_goal, t); traj->T = t; cost = calculate_cost(traj, target_vehicle, target_state, T, predictions); cost_traj[cost] = traj; // Sort by cost #ifdef VISUAL_DEBUG graph->plot_trajectory(ref_state->s.m, traj); #endif // VISUAL_DEBUG } if (cost_traj.size() > 0) { best_traj = cost_traj.begin()->second; // Minimum cost } return best_traj; }
static void _add_extra_costs(spell_info* spells, int max) { int i; /* Some spells give extra abilities depending on player level ... Ideally, these spells should scale the costs as well! */ for (i = 0; i < max; i++) { spell_info* current = &spells[i]; current->cost += get_spell_cost_extra(current->fn); current->fail = MAX(current->fail, get_spell_fail_min(current->fn)); current->cost = calculate_cost(current->cost); } }
void switch_next_matching(struct sw *s) { const struct matching **neighbors; const struct matching *hamilton; int index; int max; int i; neighbors = neighbors_matching(s->m); index = -1; max = calculate_cost(s->queue, s->m); for (i = 0; i < (s->ports * (s->ports - 1)) / 2; i++) { int temp = calculate_cost(s->queue, neighbors[i]); if (max < temp) { index = i; max = temp; } } hamilton = hamiltonian_matching(s->t + 1, s->ports); int temp = calculate_cost(s->queue, hamilton); if (max < temp) { index = -2; max = temp; } if (index >= 0) switch_set_current_matching(s, neighbors[index]->match); if (index == -2) switch_set_current_matching(s, hamilton->match); for (i = 0; i < (s->ports * (s->ports - 1))/ 2; i++) matching_delete(neighbors[i]); free(neighbors); matching_delete(hamilton); }
void dump_spells_aux(FILE *fff, spell_info *table, int ct) { int i; variant vn, vd, vc, vfm; if (!ct) return; var_init(&vn); var_init(&vd); var_init(&vc); var_init(&vfm); if (character_dump_hack) { fprintf(fff, "=================================== Spells ====================================\n\n"); fprintf(fff, "%-20.20s Lvl Cost Fail %-15.15s Cast Fail\n", "", "Desc"); } else { fprintf(fff, "\n[[[[r|%-20.20s Lvl Cost Fail %-15.15s Cast Fail\n", "Spells", "Desc"); } for (i = 0; i < ct; i++) { spell_info *spell = &table[i]; spell_stats_ptr stats = spell_stats(spell); spell->fn(SPELL_NAME, &vn); spell->fn(SPELL_INFO, &vd); spell->fn(SPELL_COST_EXTRA, &vc); spell->fn(SPELL_FAIL_MIN, &vfm); fprintf(fff, "%-20.20s %3d %4d %3d%% %-15.15s %4d %4d %3d%%\n", var_get_string(&vn), spell->level, calculate_cost(spell->cost + var_get_int(&vc)), MAX(spell->fail, var_get_int(&vfm)), var_get_string(&vd), stats->ct_cast, stats->ct_fail, spell_stats_fail(stats) ); } var_clear(&vn); var_clear(&vd); var_clear(&vc); var_clear(&vfm); }
float calculate_cost(struct node* node, int ct) { if (node->internal) { /* if internal node */ float sum = 0.0; int i; for (i = 0; i < node->data->nd_i.entries; i++) sum += calculate_cost(node->data->nd_i.parts[i].nd, node->data->nd_i.parts[i].ct); if (debug) fprintf(debug_file, "\n[DEBUG] calculate_cost -- %s: returning %f\n\n", node->name, ct * sum); return (ct * sum); } else { if (debug) fprintf(debug_file, "\n[DEBUG] calculate_cost -- %s: returning %f\n\n", node->name, ct * node->data->cost); return (ct * node->data->cost); } }
int allocate(int start, int end, int time) { if (end == 602) { printf("***"); } memset(s2v_ntmp, 0, sizeof(struct s2v_node)* sub.nodes); memset(s2v_ltmp, 0, sizeof(struct s2v_link)* sub.links); memset(v2stmp, 0, sizeof(struct req2sub) * n); memset(s2v_ntmp2, 0, sizeof(struct s2v_node)* sub.nodes); memset(s2v_ltmp2, 0, sizeof(struct s2v_link)* sub.links); memset(v2stmp2, 0, sizeof(struct req2sub) * n); //print_s2v_l(s2v_l); //print_map(v2s); /*double T, Te, a, b, c; a = 2; b = 0.97; c = 1.5; T = 100;*/ //should choose different algo here. int i, trycount; for (i = 0; i < sub.nodes; i ++) { s2v_ntmp[i].req_count = 0; s2v_ntmp2[i].req_count = 0; } printf("allocate %d %d\n", start, end); int checked[MAX_REQUESTS]; memset(checked, 0, MAX_REQUESTS); int more = 1; while (more == 1) { more = 0; int t; int rid = -1, rev = -1; for (t = start; t < end + 1; t ++) { if ((v2s[t].map == STATE_NEW || v2s[t].map == STATE_MAP_NODE_FAIL) && req[t].revenue > rev && checked[t] == 0) { rid = t; rev = req[t].revenue; more = 1; } } if (rid == -1) break; checked[rid] = 1; switch (req[rid].topo) { case TOPO_GENERAL: map_node_greedy(s2v_n, s2v_l, v2s, rid); break; case TOPO_STAR: //map_node_star(s2v_n, s2v_l, v2s, rid); map_node_greedy(s2v_n, s2v_l, v2s, rid); break; } } if (exist_req(v2s, start, end+1) == -1) return -1; double cost = -1, newcost = -1; double load, min_rest_load; int minid, reqid; int j, index, flag =1; int t = -1; int bottleneck_req, slink, vlink; int vnode, snode; int original; //simulated annealing // while (T > 0.1) { // Te = T/(double)a; // while ( T - Te > 0.1) { //TODO:checkpoint trycount = 0; bottleneck_req = unsplittable_flow(s2v_n, s2v_l, v2s, start, end, NO_MIGRATION, &slink, &vlink, time); if (bottleneck_req == -1) { int m = multicommodity_flow(s2v_n, s2v_l, v2s, start, end, NO_MIGRATION); if (m == -1) { printf("multi error\n"); return -1; } if (m != -2) bottleneck_req = check_flow(s2v_n, s2v_l, v2s, start, end, NO_MIGRATION, &slink, &vlink, time); else bottleneck_req = -1; } //printf("bottleneck_req %d\n", bottleneck_req); while (flag) { flag = 0; //printf("map link %d %d\n", start, end); memcpy(s2v_ntmp, s2v_n, sizeof(struct s2v_node)*sub.nodes); memcpy(s2v_ltmp, s2v_l, sizeof(struct s2v_link)*sub.links); memcpy(v2stmp, v2s, sizeof(struct req2sub)*n); if (bottleneck_req == -1) { //success in get flow break; /*if (newcost == -1) { cost = calculate_cost(v2stmp, start, end); printf("cost %.2f\n", cost); } else { cost = newcost; printf("recalculating cost, old cost %.2f\n", cost); }*/ cost = calculate_cost(v2stmp, start, end); min_rest_load = -1; minid = -1; //sub node no. for (i = 0; i < sub.nodes; i ++) { load = 0; for (j = 0; j < sub.links; j ++) { if (sub.link[j].from == i || sub.link[j].to ==i) { load += s2v_ltmp[j].rest_bw; } } load *= s2v_ntmp[i].rest_cpu; if (load < min_rest_load || min_rest_load == -1) { min_rest_load = load; minid = i; } } reqid = (int)(rand()/(double)RAND_MAX * s2v_ntmp[minid].req_count); //find one req on this sub node index = s2v_ntmp[minid].req[reqid];//get the request number i = s2v_ntmp[minid].vnode[reqid]; //now we get "index" req vnode "i" on sub node "minid" //remove old node snode = minid; vnode = i; printf("remove vnode %d %d on sub node %d\n", index, i, minid); original = minid; t = find_MaxNeighborResource_node(s2v_ntmp, s2v_ltmp, s2v_ntmp[snode].cpu[reqid], index, original); if (t == -1) { printf("req %d unsatisfied\n", index); //release_resource(s2v_ntmp, s2v_ltmp, v2stmp, index); //v2stmp[index].map = STATE_MAP_NODE_FAIL; break; } else { //add new mapping remove_node_map(s2v_ntmp, minid, reqid); add_node_map(s2v_ntmp, v2stmp, t, index, vnode); } trycount = 0; if (exist_req(v2stmp, start, end+1) == -1) break; } else { if (bottleneck_req == -3) { trycount = TIMES_TRY + 1; int m; for (m = start; m <=end; m ++) { if (v2stmp[m].map == STATE_MAP_NODE && req[m].split == LINK_SPLITTABLE) { bottleneck_req = m; break; } } } //cost = -1; cost = -1; trycount ++; printf("trycount %d\n", trycount); if (trycount > TIMES_TRY) { release_resource(s2v_ntmp, s2v_ltmp, v2stmp, bottleneck_req); //printf("remove0\n"); v2stmp[bottleneck_req].map = STATE_MAP_NODE_FAIL; trycount = 0; /*for (i = 0; i < req[bottleneck_req].nodes; i ++) { int snode = v2stmp[bottleneck_req].snode[i]; for (j = 0; j < s2v_ntmp[snode].req_count; j ++) { if (s2v_ntmp[snode].req[j] == bottleneck_req) { break; } } printf("remove1\n"); remove_node_map(s2v_ntmp, snode, j); } */ } else { if (rand()/(double)RAND_MAX < 0.5) { vnode = req[bottleneck_req].link[vlink].from; } else { vnode = req[bottleneck_req].link[vlink].to; } int snode = v2stmp[bottleneck_req].snode[vnode]; for (i = 0; i < s2v_ntmp[snode].req_count; i ++) { if (s2v_ntmp[snode].req[i] == bottleneck_req) { break; } } //printf("remove2\n"); original = snode; reqid = i; index = bottleneck_req; t = find_MaxNeighborResource_node(s2v_ntmp, s2v_ltmp, s2v_ntmp[snode].cpu[reqid], index, original); if (t == -1) { printf("req %d unsatisfied\n", index); release_resource(s2v_ntmp, s2v_ltmp, v2stmp, index); v2stmp[index].map = STATE_MAP_NODE_FAIL; trycount = 0; } else { //add new mapping remove_node_map(s2v_ntmp, snode, i); add_node_map(s2v_ntmp, v2stmp, t, index, vnode); } } if (exist_req(v2stmp, start, end+1) == -1) { memcpy(s2v_n, s2v_ntmp, sizeof(struct s2v_node)*sub.nodes); memcpy(s2v_l, s2v_ltmp, sizeof(struct s2v_link)*sub.links); memcpy(v2s, v2stmp, sizeof(struct req2sub)*n); break; } } //move node //find an optional node bottleneck_req = unsplittable_flow(s2v_ntmp, s2v_ltmp, v2stmp, start, end, NO_MIGRATION, &slink, &vlink, time); if (bottleneck_req == -1) { int m = multicommodity_flow(s2v_ntmp, s2v_ltmp, v2stmp, start, end, NO_MIGRATION); if (m == -1) return -1; if (m != -2) bottleneck_req = check_flow(s2v_ntmp, s2v_ltmp, v2stmp, start, end, NO_MIGRATION, &slink, &vlink, time); else bottleneck_req = -1; } //printf("bottleneck_req %d\n", bottleneck_req); if (cost == -1) { memcpy(s2v_n, s2v_ntmp, sizeof(struct s2v_node)*sub.nodes); memcpy(s2v_l, s2v_ltmp, sizeof(struct s2v_link)*sub.links); memcpy(v2s, v2stmp, sizeof(struct req2sub)*n); flag = 1; continue; } newcost = calculate_cost(v2stmp, start, end); printf("new cost %.2f\n", newcost); if (newcost < cost || newcost == -1) { //TODO: copy new solution memcpy(s2v_n, s2v_ntmp, sizeof(struct s2v_node)*sub.nodes); memcpy(s2v_l, s2v_ltmp, sizeof(struct s2v_link)*sub.links); memcpy(v2s, v2stmp, sizeof(struct req2sub)*n); cost = newcost; flag = 1; } /*else { int t = rand()/(double)RAND_MAX; if (t < exp((cost - newcost)/T)) { //TODO:copy new solution memcpy(s2v_n, s2v_ntmp, sizeof(struct s2v_node)*sub.nodes); memcpy(s2v_l, s2v_ltmp, sizeof(struct s2v_link)*sub.links); memcpy(v2s, v2stmp, sizeof(struct req2sub)*n); } }*/ // T = b* T; // } // T = c*Te; //} } //try route migration #ifdef OPTION_ALLOW_MIGRATION cost = calculate_cost(v2s, start, end); newcost = -1; memcpy(s2v_ntmp, s2v_n, sizeof(struct s2v_node)*sub.nodes); memcpy(s2v_ltmp, s2v_l, sizeof(struct s2v_link)*sub.links); memcpy(v2stmp, v2s, sizeof(struct req2sub)*n); for (i = start; i <=end; i ++) { if (req[i].split == LINK_SPLITTABLE && v2stmp[i].map == STATE_MAP_LINK) release_split_link(s2v_ltmp, v2stmp, i); } //print_s2v_l(s2v_ltmp); int m = multicommodity_flow(s2v_ntmp, s2v_ltmp, v2stmp, start, end, ROUTE_MIGRATION); if (m == -1) return -1; if (m != -2) { bottleneck_req = check_flow(s2v_ntmp, s2v_ltmp, v2stmp, start, end, ROUTE_MIGRATION, &slink, &vlink, time); } else { bottleneck_req = -1; } if (bottleneck_req == -1) { newcost = calculate_cost(v2stmp, start, end); printf("migrate cost %lf\n", newcost); } else { printf("real error! route migration error\n"); return 0; //exit(0); //break; } if (newcost < cost && newcost != -1) { printf("migrate route\n"); memcpy(s2v_n, s2v_ntmp, sizeof(struct s2v_node)*sub.nodes); memcpy(s2v_l, s2v_ltmp, sizeof(struct s2v_link)*sub.links); memcpy(v2s, v2stmp, sizeof(struct req2sub)*n); cost = newcost; } newcost = -1; memcpy(s2v_ntmp, s2v_n, sizeof(struct s2v_node)*sub.nodes); memcpy(s2v_ltmp, s2v_l, sizeof(struct s2v_link)*sub.links); memcpy(v2stmp, v2s, sizeof(struct req2sub)*n); init_s2v_l(s2v_ltmp); init_v2s(v2stmp); //print_s2v_l(s2v_ltmp); bottleneck_req = unsplittable_flow(s2v_ntmp, s2v_ltmp, v2stmp, start, end, ROUTE_MIGRATION, &slink, &vlink, time); if (bottleneck_req == -1) { int m = multicommodity_flow(s2v_ntmp, s2v_ltmp, v2stmp, start, end, ROUTE_MIGRATION); if (m == -1) return -1; if (m != -2) { bottleneck_req = check_flow(s2v_ntmp, s2v_ltmp, v2stmp, start, end, ROUTE_MIGRATION, &slink, &vlink, time); } else { bottleneck_req = -1; } } if (bottleneck_req == -1) { newcost = calculate_cost(v2stmp, start, end); printf("migrate cost %lf\n", newcost); } else { printf("route migration error\n"); return 0; //exit(0); //break; } if (newcost < cost && newcost != -1) { printf("migrate route\n"); memcpy(s2v_n, s2v_ntmp, sizeof(struct s2v_node)*sub.nodes); memcpy(s2v_l, s2v_ltmp, sizeof(struct s2v_link)*sub.links); memcpy(v2s, v2stmp, sizeof(struct req2sub)*n); } #endif return 0; }
int main(int argc, char ** argv) { //init //fetch all requests n = atoi(argv[1]); double delay = atof(argv[2]); //request dir argv[3] FILE * fp; char filename[LEN_FILENAME]; int i, j; req = (struct request *)malloc(sizeof(struct request) * n); memset(req, 0, sizeof(struct request) *n); memset(&sub, 0, sizeof(struct substrate_network)); v2s = (struct req2sub *)malloc(sizeof(struct req2sub)*n); memset(v2s, 0, sizeof(struct req2sub) * n); //fetch substrate network sprintf(filename, "sub.txt"); fp = fopen(filename, "r"); fscanf(fp, "%d %d\n", &sub.nodes, &sub.links); s2v_n = (struct s2v_node *)malloc(sizeof(struct s2v_node)*sub.nodes); memset(s2v_n, 0, sizeof(struct s2v_node)* sub.nodes); s2v_l = (struct s2v_link *)malloc(sizeof(struct s2v_link)*sub.links); memset(s2v_l, 0, sizeof(struct s2v_link)* sub.links); for (j = 0; j < sub.nodes; j ++) { fscanf(fp, "%lf\n", &sub.cpu[j]); s2v_n[j].rest_cpu = sub.cpu[j]; s2v_n[j].req_count = 0; } for (j = 0; j < sub.links; j ++) { fscanf(fp, "%d %d %lf\n", &sub.link[j].from, &sub.link[j].to, &sub.link[j].bw); s2v_l[j].rest_bw = sub.link[j].bw; } fclose(fp); for (i = 0; i < n; i ++) { sprintf(filename, "%s/req%d.txt", argv[3], i); v2s[i].map = STATE_NEW; req[i].revenue = 0; fp = fopen(filename, "r"); fscanf(fp, "%d %d %d %d %d %d\n", &req[i].nodes, &req[i].links, &req[i].split, &req[i].time, &req[i].duration, &req[i].topo); //req[i].split = LINK_UNSPLITTABLE; for (j = 0; j < req[i].nodes; j ++) { fscanf(fp, "%lf\n", &req[i].cpu[j]); } for (j = 0; j < req[i].links; j ++) { fscanf(fp, "%d %d %lf\n", &req[i].link[j].from, &req[i].link[j].to, &req[i].link[j].bw); req[i].revenue += req[i].link[j].bw; } fclose(fp); } s2v_ntmp = (struct s2v_node *)malloc(sizeof(struct s2v_node)*sub.nodes); s2v_ltmp = (struct s2v_link *)malloc(sizeof(struct s2v_link)*sub.links); v2stmp = (struct req2sub *)malloc(sizeof(struct req2sub)*n); s2v_ntmp2 = (struct s2v_node *)malloc(sizeof(struct s2v_node)*sub.nodes); s2v_ltmp2 = (struct s2v_link *)malloc(sizeof(struct s2v_link)*sub.links); v2stmp2 = (struct req2sub *)malloc(sizeof(struct req2sub)*n); spath = (struct shortest_path *)malloc(sizeof(struct shortest_path) * sub.nodes * sub.nodes); calc_shortest_path(spath, &sub); int time = TIME_INTERVAL; int start, end;//, reqcount; start = 0; end = 0; /* double opcost = 0; int count = 0; double bwsum = 0, reqcost = 0; reqcount = 0; double total_revenue = 0; */ int done_count = 0; int map_count = 0; int rej_count = 0; double done_cost = 0; double done_rev = 0; double map_cost = 0; double map_rev = 0; int finish = 0; char tracename[LEN_FILENAME]; sprintf(tracename,"%s-%d.trace", argv[3], (int)delay); FILE * ftrace = fopen(tracename, "w"); while (end < n || finish == 0) { finish = 1; for (i = 0; i < end; i ++) { if (v2s[i].map != STATE_MAP_LINK && v2s[i].map != STATE_DONE && v2s[i].map != STATE_EXPIRE) finish = 0; if (v2s[i].map == STATE_MAP_LINK && v2s[i].maptime + req[i].duration <= time) { printf("req %d done\n", i); done_rev += req[i].revenue; done_cost += calculate_cost(v2s, i, i); v2s[i].map = STATE_DONE; done_count ++; release_resource(s2v_n, s2v_l, v2s, i); } } map_cost = calculate_cost(v2s, 0, end); map_rev = 0; map_count = 0; for (i = 0; i <end; i ++) { if (v2s[i].map == STATE_MAP_LINK) { map_rev += req[i].revenue; map_count ++; } } fprintf(ftrace, "%d %d %d %lf %lf %lf %lf %d %lf %lf\n", done_count, map_count, rej_count, done_cost, done_rev, map_cost, map_rev, done_count+map_count, done_cost + map_cost, done_rev+map_rev); while (end < n && req[end].time < time) end ++; allocate(0, end - 1, time); for (i = 0; i < end; i ++) { if (req[i].time +delay < time && v2s[i].map != STATE_DONE && v2s[i].map != STATE_MAP_LINK) { if (v2s[i].map != STATE_MAP_NODE_FAIL && v2s[i].map != STATE_EXPIRE) printf("***error*** %d\n", v2s[i].map); v2s[i].map = STATE_EXPIRE; rej_count ++; } } /*reqcount = 0; for (i = 0; i < end; i ++) { if (v2s[i].map == STATE_MAP_LINK && v2s[i].maptime == time) reqcount++; }*/ //fprintf(fp, "%d %d %d\n", time, end - start, reqcount); //start = end; time += TIME_INTERVAL; } fp = fopen("stat.dat", "a"); for (i = 0; i < end; i ++) { if (v2s[i].map != STATE_MAP_LINK && v2s[i].map != STATE_DONE && v2s[i].map != STATE_EXPIRE) finish = 0; if (v2s[i].map == STATE_MAP_LINK && v2s[i].maptime + req[i].duration <= time) { printf("req %d done\n", i); done_rev += req[i].revenue; done_cost += calculate_cost(v2s, i, i); v2s[i].map = STATE_DONE; done_count ++; release_resource(s2v_n, s2v_l, v2s, i); } } map_cost = calculate_cost(v2s, 0, end); map_rev = 0; map_count = 0; for (i = 0; i <end; i ++) { if (v2s[i].map == STATE_MAP_LINK) { map_rev += req[i].revenue; map_count ++; } } fprintf(ftrace, "%d %d %d %lf %lf %lf %lf %d %lf %lf\n", done_count, map_count, rej_count, done_cost, done_rev, map_cost, map_rev, done_count+map_count, done_cost + map_cost, done_rev+map_rev); fprintf(fp, "%s %d %d %lf %lf\n", tracename, done_count+map_count, time, (done_cost+map_cost)/time, (done_rev+map_rev)/time); /*double maxlinkload; for (i = 0; i < sub.links; i ++) { double t = sub.link[i].bw - s2v_l[i].rest_bw; if (t > maxlinkload) maxlinkload = t; } fprintf(fp, " %lf\n", maxlinkload); */ fclose(ftrace); fclose(fp); return 0; }
int main() { double data[M][N] = {0.0}; double coeff[N]= {0.0}; double cost[M] = {0.0}; double alpha = 0.000001; char *line = NULL; size_t len = 0; ssize_t read; char *token = NULL; char **tokens; int count = 0; int i,j,k; srand(time(NULL)); //Read data.csv file and pass the data to 2d array called data FILE *f = fopen("../data/data.csv", "r"); if(f == NULL) { printf("Failed to read file!\nExiting...\n"); return -1; } while((read = getline(&line, &len, f)) != -1) { count++; if(DEBUG == 1) { printf("Retrieved line of length %zu Count : %d:\n", read, count); printf("%s", line); } token = strtok(line, ","); sscanf(token, "%lf", &data[count-1][0]); for(i = 1 ; i < N; i ++) { token = strtok(NULL,","); sscanf(token, "%lf", &data[count-1][i]); } } fclose(f); if(DEBUG==1) { printf("Data Matrix: \n"); for (i=0; i<M; i++) { for(j=0; j<N; j++) printf("%lf ", data[i][j]); printf("\n"); } } FILE *f2 = fopen("../data/all_cost.dat", "w"); double temp_cost = 0.0; //First Stage printf("Calculation is started...\n\n"); reset_coeff(coeff); for(i = 1 ; i <= M ; i ++) { for(j = 0 ; j < 5 ; j ++) { train(data, coeff, i, alpha); temp_cost = temp_cost + calculate_cost(data, coeff); reset_coeff(coeff); } cost[i -1] = temp_cost/5.0; temp_cost = 0.0; fprintf(f2, "%.12e\n",cost[i-1]); printf("For %d samples, the cost is %f\n", i,cost[i-1]); } printf("\n\n The cost data for %d different samples are written into all_cost.dat file!\n\nExiting..\n", M); fclose(f2); }
vector<vector<Costumer*> > CVRP::update_list(vector<Route*> &solution, vector<double> ×, int &n){ vector<Costumer*>::iterator itr = clients.begin(); vector<vector<Costumer*> > candidates(solution.size(), vector<Costumer*> ()); for(; itr != clients.end(); itr++){ Costumer* co = *(itr); for(int r = 0; r < solution.size(); r++){ //candidates[r].clear(); //Erro aqui!!! Nao esquece!!! if(!solution[r]->clients.empty()){ int c = solution[r]->clients.size()-1; Costumer* org = solution[r]->clients[c]; int cost = calculate_cost(org->coord, co->coord); //Time arrival to verify if the vehicle can get to the client before the due time double arrivalTime = times[r] + cost; //cout << arrivalTime << endl; //Verify if the client wasn't visited and if the vehicle is on time if(!visited[co->id - 1] && co->id != org->id && co->dTime >= arrivalTime){ Point *i = solution[r]->clients[c]->coord; Point *j = co->coord; co->ratio = calculate_cost(j, dep.coord) + cost; co->saving = calculate_cost(j, dep.coord) + calculate_cost(dep.coord, i) - calculate_cost(i, j); co->cost = cost; co->arrTime = arrivalTime; candidates[r].push_back(co); } } } } for(int i = 0; i < candidates.size(); i++){ if(candidates[i].size() != 0){ return candidates; } } candidates.push_back(vector<Costumer*> ()); int k = candidates.size() - 1; for(int i = 0; i < clients.size(); i++){ Costumer *cos = clients[i]; if(!visited[cos->id - 1]){ int cost = calculate_cost(dep.coord, cos->coord); if(candidates[k].empty()){ candidates[k].push_back(cos); }else if(candidates[k][0]->cost > cost){ candidates[k][0] = cos; } } } visited.push_back(false); visited[candidates[k][0]->id - 1] = true; times.push_back(0.0); times[k] += candidates[k][0]->cost + candidates[k][0]->servTime; candidates[k][0]->arrTime = candidates[k][0]->cost; n++; solution.push_back(new Route); solution[solution.size()-1]->clients.push_back(candidates[k][0]); return candidates; }
Solution::Solution(int *_vertex, int _length,Instance *_instance) { vertex = _vertex; length = _length; instance = _instance; cost = calculate_cost(); }