int new_start(int *intour, heur_prob *p, int start, int num_cust) { int starter = 0, start_pos, cust, count=0; int cost = -MAXINT, temp_cost; int vertnum = p->vertnum; if (start == FAR_INS) for (cust=1; cust<vertnum; cust++) { if (((temp_cost = ICOST(&p->dist, 0, cust)) > cost) && (intour[cust] != IN_TOUR)) { starter = cust; cost = temp_cost; } } else { start_pos = rand()%num_cust+1; for (cust = 1, count = 0; count<start_pos; cust ++) if (intour[cust] != IN_TOUR) count ++; starter = cust-1; } return(starter); }
void make_routes(heur_prob *p, _node *tsp_tour, int start, best_tours *new_tour) { int cur_node, prev_node, route_beg, prev_route_beg; int weight, i, capacity = p->capacity; int cur_route = 1; int cost; _node *tour = new_tour->tour; adj_list **adj; adj_list *temp, *path; int *pi, route_end; int vertnum = p->vertnum; int *demand = p->demand; adj = (adj_list **) calloc (vertnum, sizeof(adj_list *)); /*--------------------------------------------------------------------*\ | First we must construct the graph to give to the shortest path | | algorithm. In this graph, the edge costs are as follows. The cost of | | edge between nodes i and j is the cost of a route beginning with i's | | successor on the TSP tour and ending with j (in bewtween, the route | | follows the same ordering as the TSP tour itself) if this is a | | feasible route and infinity otherwise. Now if we find a | | shortest cycle in this graph, then the nodes in the cycle will be the| | endpoints of the routes in an optimal partition. This is what I do. | | We need to arbitrarily specify the fist endpoint in advance. | | This is the value contained the start variable. The tsp_tour | | structure contains the original TSP tour. | \*--------------------------------------------------------------------*/ for (prev_route_beg = start, route_beg = tsp_tour[start].next, i=0; i<vertnum-1; prev_route_beg = route_beg, route_beg = tsp_tour[route_beg].next, i++){ cur_node = route_beg; prev_node = 0; temp = adj[prev_route_beg] = (adj_list *) calloc (1, sizeof(adj_list)); weight = demand[cur_node]; cost = ICOST(&p->dist, prev_node, cur_node); temp->cost = cost + ICOST(&p->dist, 0, cur_node); temp->custnum = cur_node; prev_node = cur_node; cur_node = tsp_tour[cur_node].next; while (weight + demand[cur_node] <= capacity){ weight += demand[cur_node]; cost += ICOST(&p->dist, prev_node, cur_node); temp->next = (adj_list *) calloc (1, sizeof(adj_list)); temp = temp->next; temp->cost = cost + ICOST(&p->dist, 0, cur_node); temp->custnum = cur_node; prev_node = cur_node; cur_node = tsp_tour[cur_node].next; } } /* The graph is constructed. Now find a shortest cycle in it */ pi = sp(adj, vertnum, start, start); /*--------------------------------------------------------------------*\ | Now the shortest cycle information is contained in pi. However, using| | pi, we can only trace the shortest cycle in one direction since it is| | actually stored as a directed path from the start node to itself. | | Also, we can only trace the TSp tour in one direction since it is | | stored in the same way. So we must store the cycle in the reverse | | direction in order to break up the TSP tour. That is what this piece | | of code does. It stores the nodes on the cycle in opposite order in a| | linked list to be used in the next part of the code. | \*--------------------------------------------------------------------*/ path = (adj_list *) calloc (1, sizeof(adj_list)); path->custnum = start; cur_node = pi[start]; while (cur_node){ temp = (adj_list *) calloc (1, sizeof(adj_list)); temp->custnum = cur_node; temp->next = path; path = temp; cur_node = pi[cur_node]; } temp = path; /*--------------------------------------------------------------------*\ | Now we have the optimal break points stored in order in a liked list.| | We trace the TSP tour, copying each node into the VRP tour until we | | reach the endpoint of a route and then we change the route number. | \*--------------------------------------------------------------------*/ tour[0].next = tsp_tour[start].next; cur_node = tour[0].next; route_end = temp->custnum; for (i=1; i<vertnum-1; i++){ tour[cur_node].next = tsp_tour[cur_node].next; tour[cur_node].route = cur_route; if (cur_node == route_end){ cur_route++; if (temp->next){ temp = temp->next; route_end = temp->custnum; } else break; } cur_node = tsp_tour[cur_node].next; } tour[cur_node].next = 0; tour[cur_node].route = cur_route; new_tour->cost = compute_tour_cost(&p->dist, tour); new_tour->numroutes = cur_route; }
void nearest_ins(int parent, heur_prob *p) { printf("\nIn nearest_ins....\n\n"); int numroutes, cur_route, nearnode, *starter; int mytid, info, r_bufid; int *intour; int last, cost; neighbor *nbtree; _node *tour; route_data *route_info; int start; best_tours *tours; double t=0; mytid = pvm_mytid(); (void) used_time(&t); tours = p->cur_tour = (best_tours *) calloc (1, sizeof(best_tours)); /*-----------------------------------------------------------------------*\ | Receive the VRP data | \*-----------------------------------------------------------------------*/ PVM_FUNC(r_bufid, pvm_recv(-1, ROUTE_NINS_VRP_DATA)); PVM_FUNC(info, pvm_upkbyte((char *)tours, sizeof(best_tours), 1)); tour = p->cur_tour->tour = (_node *) calloc (p->vertnum, sizeof(_node)); PVM_FUNC(info, pvm_upkbyte((char *)tour, (p->vertnum)*sizeof(_node), 1)); numroutes = p->cur_tour->numroutes; starter = (int *) calloc (numroutes, sizeof(int)); route_info = p->cur_tour->route_info = (route_data *) calloc (numroutes+1, sizeof(route_data)); PVM_FUNC(r_bufid, pvm_recv(-1, ROUTE_NINS_START_RULE)); PVM_FUNC(info, pvm_upkint(&start, 1, 1));/*receive the start rule*/ if (start != FAR_INS) srand(start); /*if the start rule is random, then*\ \*initialize the random number gen.*/ starters(p, starter, route_info, start);/*generate the route starters for*\ \*all the clusters. */ /*-----------------------------------------------------------------------*\ | Allocate arrays | \*-----------------------------------------------------------------------*/ nbtree = (neighbor *) malloc (p->vertnum * sizeof(neighbor)); intour = (int *) calloc (p->vertnum, sizeof(int)); /*-----------------------------------------------------------------------*\ | Find the nearest insertion tour from 'starters' | \*-----------------------------------------------------------------------*/ for (cur_route=1; cur_route<=numroutes; cur_route++){ /*---------------------------------------------------------------------*\ | The first part of this loop adds the starter and the nearest node to | | it into the route to initialize it. Then a function is called | | which inserts the rest of the nodes into the route in nearest | | insert order. | \*---------------------------------------------------------------------*/ if (route_info[cur_route].numcust <= 1) continue; cost = 0; last = 0; intour[0] = 0; intour[starter[cur_route-1]] = IN_TOUR; ni_insert_edges(p, starter[cur_route-1], nbtree, intour, &last, tour, cur_route); nearnode = closest(nbtree, intour, &last); intour[nearnode] = IN_TOUR; ni_insert_edges(p, nearnode, nbtree, intour, &last, tour, cur_route); tour[starter[cur_route-1]].next = nearnode; tour[nearnode].next = starter[cur_route-1]; if (starter[cur_route - 1] == 0) route_info[cur_route].first = route_info[cur_route].last = nearnode; if (nearnode == 0) route_info[cur_route].first = route_info[cur_route].last = starter[cur_route-1]; cost = 2 * ICOST(&p->dist, starter[cur_route-1], nearnode); cost = nearest_ins_from_to(p, tour, cost, 2, route_info[cur_route].numcust+1, starter[cur_route-1], nbtree, intour, &last, route_info, cur_route); route_info[cur_route].cost = cost; } tour[0].next = route_info[1].first; /*-------------------------------------------------------------------------*\ | This loop points the last node of each route to the first node of the next| | route. At the end of this procedure, the last node of each route is | | pointing at the depot, which is not what we want. | \*-------------------------------------------------------------------------*/ for (cur_route = 1; cur_route< numroutes; cur_route++) tour[route_info[cur_route].last].next = route_info[cur_route+1].first; cost = compute_tour_cost(&p->dist, tour); /*-----------------------------------------------------------------------*\ | Transmit the tour back to the parent | \*-----------------------------------------------------------------------*/ send_tour(tour, cost, numroutes, tours->algorithm, used_time(&t), parent, p->vertnum, 1, route_info); if ( nbtree ) free ((char *) nbtree); if ( intour ) free ((char *) intour); if ( starter) free ((char *) starter); free_heur_prob(p); }
void tsp_fini(int parent, heur_prob *p) { printf("\nIn tsp_fini....\n\n"); int mytid, info, r_bufid; int starter, farnode, v0, v1, cur_start; _node *tsp_tour, *tour, *opt_tour; int maxdist; int *intour; int last, cost; int i, j; neighbor *nbtree; int trials, interval; int farside, vertnum; best_tours *opt_tours, *tours; double t=0; mytid = pvm_mytid(); (void) used_time(&t); /*-----------------------------------------------------------------------*\ | Receive the VRP data | \*-----------------------------------------------------------------------*/ PVM_FUNC(r_bufid, pvm_recv(-1, TSP_FINI_TRIALS)); PVM_FUNC(info, pvm_upkint(&trials, 1, 1)); PVM_FUNC(r_bufid, pvm_recv(parent, TSP_FINI_RATIO)); PVM_FUNC(info, pvm_upkint(&farside, 1, 1)); /*-----------------------------------------------------------------------*\ | Receive the starting point | \*-----------------------------------------------------------------------*/ PVM_FUNC(r_bufid, pvm_recv(-1, TSP_START_POINT)); PVM_FUNC(info, pvm_upkint(&starter, 1, 1)); vertnum = p->vertnum; if (starter == vertnum) for (starter=v0=1, maxdist=ICOST(&p->dist, 0,1); v0<vertnum-1; v0++) for (v1=v0+1; v1<vertnum; v1++) if (maxdist < ICOST(&p->dist, v0, v1)){ maxdist = ICOST(&p->dist, v0, v1); starter = v0; } /*-----------------------------------------------------------------------*\ | Allocate arrays | \*-----------------------------------------------------------------------*/ tsp_tour = (_node *) malloc (vertnum * sizeof(_node)); nbtree = (neighbor *) malloc (vertnum * sizeof(neighbor)); intour = (int *) calloc (vertnum, sizeof(int)); tours = p->cur_tour = (best_tours *) calloc (1, sizeof(best_tours)); tour = p->cur_tour->tour = (_node *) calloc (vertnum, sizeof(_node)); opt_tours = (best_tours *) malloc (sizeof(best_tours)); opt_tour = (_node *) malloc (vertnum*sizeof(_node)); /*------------------------------------------------------------------------*\ | This heuristic is a so-called route-first, cluster-second heuristic. | | We first construct a TSP route by farnear insert and then partition it | | into feasible routes by finding a shortest cycle of a graph with edge | | costs bewtween nodes being defined to be the cost of a route from one | | endpoint of the edge to the other. | \*------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*\ | Find the first 'farside node with farthest insertion from 'starter' | \*-----------------------------------------------------------------------*/ last = 0; intour[0] = IN_TOUR; intour[starter] = IN_TOUR; tsp_fi_insert_edges(p, starter, nbtree, intour, &last); farnode = tsp_farthest(nbtree, intour, &last); intour[farnode] = IN_TOUR; tsp_fi_insert_edges(p, farnode, nbtree, intour, &last); tsp_tour[starter].next = farnode; tsp_tour[farnode].next = starter; cost = 2 * ICOST(&p->dist, starter, farnode); farside = MAX(farside, 2); cost = tsp_farthest_ins_from_to(p, tsp_tour, cost, 2, farside, starter, nbtree, intour, &last); /*------------------------------------------------------------------------*\ | Order the elements in nbtree (and fix the intour values) so after that | | nbtree is suitable to continue with nearest insertion. | \*------------------------------------------------------------------------*/ qsort((char *)(nbtree+1), last, sizeof(neighbor), compar); for (i=1; i<=last; i++) intour[nbtree[i].nbor] = i; /*-----------------------------------------------------------------------*\ | Continue with nearest insertion | \*-----------------------------------------------------------------------*/ cost = tsp_nearest_ins_from_to(p, tsp_tour, cost, farside, vertnum-1, starter, nbtree, intour, &last); /*------------------------------------------------------------------------*\ | We must arbitrarily choose a node to be the first node on the first | | route in order to start the partitioning algorithm. The trials variable | | tells us how many starting points to try. Its value is contained in | | p->par.tsp.numstarts. | \*------------------------------------------------------------------------*/ if (trials > vertnum-1) trials = vertnum-1; interval = (vertnum-1)/trials; opt_tours->cost = MAXINT; /*------------------------------------------------------------------------*\ | Try various partitionings and take the solution that has the least cost | \*------------------------------------------------------------------------*/ for (i=0, cur_start = starter; i<trials; i++){ make_routes(p, tsp_tour, cur_start, tours); if (tours->cost < opt_tours->cost){ (void) memcpy ((char *)opt_tours, (char *)tours, sizeof(best_tours)); (void) memcpy ((char *)opt_tour, (char *)tour, vertnum*sizeof(_node)); } for (j=0; j<interval; j++) cur_start = tsp_tour[cur_start].next; } /*-----------------------------------------------------------------------*\ | Transmit the tour back to the parent | \*-----------------------------------------------------------------------*/ send_tour(opt_tour, opt_tours->cost, opt_tours->numroutes, TSP_FINI, used_time(&t), parent, vertnum, 0, NULL); if ( nbtree ) free ((char *) nbtree); if ( intour ) free ((char *) intour); if ( opt_tours ) free ((char *) opt_tours); if ( opt_tour ) free ((char *) opt_tour); if ( tsp_tour ) free ((char *) tsp_tour); free_heur_prob(p); }