void dfs(){
     int city, length;
     tipeTour* tour;
     tipeStack* stack;
     tour = malloc(sizeof(tipeTour));

     initTour(tour);
     stack->tour = tour;
     stack->city = HOME;
     stack->length = 0;
     stack->nextTour = NULL;

     while(!empty(stack)){
         pop(&tour, &city, &length, &stack);
         tour->cities[tour->countCities] = city;
         tour->length += length;
         tour->countCities += 1;
         if(tour->countCities == N)
            isABestTour(city, &tour);
         else
            for(int neighbor = N-1; neighbor > 0; neighbor--)   
               if(feasible(city, neighbor, tour))
                  push(tour, neighbor, graphCity[city][neighbor], &stack);
     }
     free(tour);
}
int main(){
   initGraphCity();
   initTour(&bestTour);
   bestTour.length = INF;
   dfs();
   printTour(&bestTour);
   return 0;
}
Esempio n. 3
0
int main (int argc, char** argv)
{
	//Input arguments:
	//.tsp file name to read
	char*tspfile = argv[1];

	fprintf(stderr,"Reading in the TSP file at: %s\n",tspfile);
	Instance instance(tspfile);
	int nc = instance.getCityCount();
	fprintf(stderr,"City Count: %d\n\n",nc);
	fprintf(stderr,"Done reading tsp input file.\n");

	fprintf(stderr,"Initializing Host and GPU Data...\n");
	/* declare and initialize data */
	//Initialize Tour
	int h_initialTour[instance.getCityCount()];
	initTour (h_initialTour, nc);
	randomRestart(h_initialTour,nc);
	//int *d_initialTour;
	//initCudaTour (&d_initialTour, h_initialTour, nc);
	//Initialize Coordinates (ordered in the tour order)
	//d_coords and ordered store the first point again in the last element to capture all edges.
	COORD*h_coords = instance.getCoords();
	COORD ordered[nc];
	for(int i=0;i<nc;++i) ordered[i]=h_coords[h_initialTour[i]];
	COORD*d_coords;
	initCudaCoords(&d_coords,ordered,nc);
	fprintf(stderr,"Done.\n\n");
	
	//2Opt Runs
	//Sequential test
	if(RUNSERIAL>0)
	{
		std::clock_t start;
	    double duration=0;
	    BESTIMPROVEMENT bi;
	    for(int i=0;i<NUM_ITER;++i)
	    {
	    	start = std::clock();
			bi = iterate2Opt_cudaCompare(instance,h_initialTour);
	    	duration += ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
	    }
		fprintf (stderr, "Sequential run time: %f ms\n",
			duration/((double)NUM_ITER)*1000.0);
		fprintf (stderr, "Sequential min diff, k: %f, %d\n\n",
			bi.difference,bi.k);
		//Swap test
		std::clock_t start2;
	    double duration2=0;
	    //Get i,j to swap
	    int pair[2];
	    ijfromk(bi.k,nc,pair);
	    cerr << "ijfromk: " << pair[0] << "," << pair[1] << endl;
		dtype rsc = pairSwapCost(ordered,pair);
		fprintf(stderr,"Recomputed swap cost: %f\n",rsc);
	    //end i,j retrieval
	    dtype original = costFromCoords(ordered,nc);
		fprintf(stderr,"Cost before swap: %f\n",original);
		//printCoords(ordered,nc);
		dtype c1 = sectionCostFromCoords(ordered,pair);

int length = pair[1]-pair[0];
dtype mycompare1[length];
for(int i=0;i<length;++i)
{
	int in = pair[0]+i;
	mycompare1[i]=euc2d_distance(ordered[in],ordered[in+1]);
}


	COORD orderedoriginal[nc];
	for(int i=0;i<nc;++i) orderedoriginal[i]=ordered[i];


	    //timing
    	start2 = std::clock();
		fprintf(stderr,"Swapping %d,%d\n",pair[0],pair[1]);
	    swapCoords(ordered,pair);
    	duration2 = ( std::clock() - start2 ) / (double) CLOCKS_PER_SEC;
    	//end timing



		fprintf (stderr, "Full swap run time: %f ms\n",
			duration2*1000.0);
		dtype newcost = costFromCoords(ordered,nc);
		fprintf(stderr,"Cost after swap: %f\n",newcost);

dtype mycompare2[length];
for(int i=0;i<length;++i)
{
	int in = i+pair[0];
	mycompare2[length-1-i] = 
		euc2d_distance(ordered[in],ordered[in+1]);
}
		compareSums(mycompare1,mycompare2,length,0);

		//printCoords(ordered,nc);
		dtype costdiff = newcost-original;
		fprintf(stderr,"Difference: %f\n",costdiff);
		dtype c2 = sectionCostFromCoords(ordered,pair);
		fprintf(stderr,"Swap section costs: %f, %f (difference: %f)\n",c1,c2,c1-c2);
		fprintf(stderr,"\n");
		//End swap test

		assessSwap(orderedoriginal,ordered,nc,pair);


	}
	//End sequential test
	if (RUNALL == 0)
	{
		cudaOpt2 (d_coords, nc, VERSION);
	}
	else
	{
		int vnum = 10;
		int vv[] = {1,2,3,31,4,5,6,7,8,9};
		for(int i=0;i<vnum;++i)
		{
			cudaOpt2 (d_coords, nc, vv[i]);
		}
	}
	//End 2opt Run

/*
	//dummy run
	unsigned int N = 16777216;
	unsigned int OPT = 1;
	dtype *h_A, *d_A, ans;
	h_A = (dtype*) malloc (N * sizeof (dtype));
	if(h_A==NULL)
	{
		fprintf(stderr, "h_A malloc failed!\n");
	}
	initArray (h_A, N);
	initCudaArray (&d_A, h_A, N);
	reduce(d_A, N, OPT, &ans);
	if(fabs ((double) ans - (double) reduceCpu (h_A, N)) > (1e-8 * N)) {
		fprintf (stderr, "Answer is %f\n", ans);
		fprintf (stderr, "Reference answer is %f\n", reduceCpu (h_A, N));
	} else {
		fprintf (stderr, "Answer is correct\n");
		fprintf (stderr, "Reference answer is %f\n", reduceCpu (h_A, N));
		fprintf (stderr, "GPU answer is %f\n", ans);
	}

	free(h_A);
	//cudaFree(d_A);
*/
	
	return 0;
}