Debian Codegraph @

SYMPHONY/Applications/VRP/src/Heuristics/nearest_ins.c

raw
1 /*===========================================================================*/
2 /* */
3 /* This file is part of a demonstration application for use with the */
4 /* SYMPHONY Branch, Cut, and Price Library. This application is a solver for */
5 /* the Vehicle Routing Problem and the Traveling Salesman Problem. */
6 /* */
7 /* This application was developed by Ted Ralphs ([email protected]) */
8 /* This file was modified by Ali Pilatin January, 2005 ([email protected]) */
9 /* */
10 /* (c) Copyright 2000-2005 Ted Ralphs. All Rights Reserved. */
11 /* */
12 /* This software is licensed under the Eclipse Public License. Please see */
13 /* accompanying file for terms. */
14 /* */
15 /*===========================================================================*/
16
17 #include "nearest_ins.h"
18 #include <stdio.h>
19 void nearest_ins(int parent, heur_prob *p)
20 {
21 printf("\nIn nearest_ins....\n\n");
22 int numroutes, cur_route, nearnode, *starter;
23 int mytid, info, r_bufid;
24 int *intour;
25 int last, cost;
26 neighbor *nbtree;
27 _node *tour;
28 route_data *route_info;
29 int start;
30 best_tours *tours;
31 double t=0;
32
33 mytid = pvm_mytid();
34
35 (void) used_time(&t);
36
37 tours = p->cur_tour = (best_tours *) calloc (1, sizeof(best_tours));
38
39 /*-----------------------------------------------------------------------*\
40 | Receive the VRP data |
41 \*-----------------------------------------------------------------------*/
42
43 PVM_FUNC(r_bufid, pvm_recv(-1, ROUTE_NINS_VRP_DATA));
44
45 PVM_FUNC(info, pvm_upkbyte((char *)tours, sizeof(best_tours), 1));
46 tour = p->cur_tour->tour = (_node *) calloc (p->vertnum, sizeof(_node));
47 PVM_FUNC(info, pvm_upkbyte((char *)tour, (p->vertnum)*sizeof(_node), 1));
48 numroutes = p->cur_tour->numroutes;
49 starter = (int *) calloc (numroutes, sizeof(int));
50 route_info = p->cur_tour->route_info
51 = (route_data *) calloc (numroutes+1, sizeof(route_data));
52
53 PVM_FUNC(r_bufid, pvm_recv(-1, ROUTE_NINS_START_RULE));
54 PVM_FUNC(info, pvm_upkint(&start, 1, 1));/*receive the start
55 rule*/
56
57 if (start != FAR_INS) srand(start); /*if the start rule is random, then*\
58 \*initialize the random number gen.*/
59 starters(p, starter, route_info, start);/*generate the route starters for*\
60 \*all the clusters. */
61 /*-----------------------------------------------------------------------*\
62 | Allocate arrays |
63 \*-----------------------------------------------------------------------*/
64
65 nbtree = (neighbor *) malloc (p->vertnum * sizeof(neighbor));
66 intour = (int *) calloc (p->vertnum, sizeof(int));
67
68 /*-----------------------------------------------------------------------*\
69 | Find the nearest insertion tour from 'starters' |
70 \*-----------------------------------------------------------------------*/
71
72 for (cur_route=1; cur_route<=numroutes; cur_route++){
73 /*---------------------------------------------------------------------*\
74 | The first part of this loop adds the starter and the nearest node to |
75 | it into the route to initialize it. Then a function is called |
76 | which inserts the rest of the nodes into the route in nearest |
77 | insert order. |
78 \*---------------------------------------------------------------------*/
79 if (route_info[cur_route].numcust <= 1) continue;
80 cost = 0;
81 last = 0;
82 intour[0] = 0;
83 intour[starter[cur_route-1]] = IN_TOUR;
84 ni_insert_edges(p, starter[cur_route-1], nbtree, intour, &last, tour, cur_route);
85 nearnode = closest(nbtree, intour, &last);
86 intour[nearnode] = IN_TOUR;
87 ni_insert_edges(p, nearnode, nbtree, intour, &last, tour, cur_route);
88 tour[starter[cur_route-1]].next = nearnode;
89 tour[nearnode].next = starter[cur_route-1];
90 if (starter[cur_route - 1] == 0)
91 route_info[cur_route].first = route_info[cur_route].last = nearnode;
92 if (nearnode == 0)
93 route_info[cur_route].first = route_info[cur_route].last
94 = starter[cur_route-1];
95
96 cost = 2 * ICOST(&p->dist, starter[cur_route-1], nearnode);
97 cost = nearest_ins_from_to(p, tour, cost, 2, route_info[cur_route].numcust+1,
98 starter[cur_route-1],
99 nbtree, intour, &last, route_info, cur_route);
100 route_info[cur_route].cost = cost;
101 }
102
103 tour[0].next = route_info[1].first;
104
105 /*-------------------------------------------------------------------------*\
106 | This loop points the last node of each route to the first node of the next|
107 | route. At the end of this procedure, the last node of each route is |
108 | pointing at the depot, which is not what we want. |
109 \*-------------------------------------------------------------------------*/
110 for (cur_route = 1; cur_route< numroutes; cur_route++)
111 tour[route_info[cur_route].last].next = route_info[cur_route+1].first;
112
113 cost = compute_tour_cost(&p->dist, tour);
114
115 /*-----------------------------------------------------------------------*\
116 | Transmit the tour back to the parent |
117 \*-----------------------------------------------------------------------*/
118
119 send_tour(tour, cost, numroutes, tours->algorithm, used_time(&t), parent,
120 p->vertnum, 1, route_info);
121
122 if ( nbtree ) free ((char *) nbtree);
123 if ( intour ) free ((char *) intour);
124 if ( starter) free ((char *) starter);
125
126 free_heur_prob(p);
127
128 }
129
130