Exemplo n.º 1
0
// Algorithm to get the shortest round trip for the 
// input array passed in. The algorithm should populate 
// the output array that is passed in with the first row 
// being the total distance and then the subsequent rows 
// being the cities in the order they are to be visited, The 
// size parameter is the number of cities.
int resultTSP(struct structCity *input, int *output, int size, int StartNode)
{
	// Local declares
	struct structCity visitedCities[size + 1]; // Put the size in the first index
	int TotalDist = -999; // Total distance traveled
	int i = -999; // Global indexer
	
	// Set the size of visitedCities 
	visitedCities[0].iId = 1;
	visitedCities[0].iX = -999;
	visitedCities[0].iY = -999;
	
	// Add the initial city to the first index of visited cities 
	visitedCities[1] = input[StartNode];
	
	// Get the nearest neighbor for the first time through
	TotalDist = NearestNeighbor(input, visitedCities, size, StartNode);
	
	// Now loop through the rest of the input list 
	while(visitedCities[0].iId < size)
	{
		TotalDist = TotalDist + NearestNeighbor(input, visitedCities, size, 
					visitedCities[visitedCities[0].iId].iId);
	}

	// Move the visited cities to the output
	for(i = 1; i < size + 1; i++)
	{
		output[i] = visitedCities[i].iId;
	}
    // add the distance from the last city to the first city;
    int distToFinishLoop=getDistance(visitedCities[1],visitedCities[size]); 	
    TotalDist+=distToFinishLoop;
	// Move the total distance traveled to index 0 in the output list
	output[0] = TotalDist;
	
	// Bounce 
	return 0;
}
Exemplo n.º 2
0
   void getsseq(
       float *sseq,    /* (o) the pitch-synchronous sequence */
       float *idata,       /* (i) original data */
       int idatal,         /* (i) dimension of data */
       int centerStartPos, /* (i) where current block starts */
       float *period,      /* (i) rough-pitch-period array */
       float *plocs,       /* (i) where periods of period array
                                  are taken */
       int periodl,    /* (i) dimension period array */
       int hl              /* (i) 2*hl+1 is the number of sequences */
   ){
       int i,centerEndPos,q;
       float blockStartPos[2*ENH_HL+1];
       int lagBlock[2*ENH_HL+1];
       float plocs2[ENH_PLOCSL];
       float *psseq;

       centerEndPos=centerStartPos+ENH_BLOCKL-1;

       /* present */

       NearestNeighbor(lagBlock+hl,plocs,
           (float)0.5*(centerStartPos+centerEndPos),periodl);

       blockStartPos[hl]=(float)centerStartPos;





       psseq=sseq+ENH_BLOCKL*hl;
       memcpy(psseq, idata+centerStartPos, ENH_BLOCKL*sizeof(float));

       /* past */

       for (q=hl-1; q>=0; q--) {
           blockStartPos[q]=blockStartPos[q+1]-period[lagBlock[q+1]];
           NearestNeighbor(lagBlock+q,plocs,
               blockStartPos[q]+
               ENH_BLOCKL_HALF-period[lagBlock[q+1]], periodl);


           if (blockStartPos[q]-ENH_OVERHANG>=0) {
               refiner(sseq+q*ENH_BLOCKL, blockStartPos+q, idata,
                   idatal, centerStartPos, blockStartPos[q],
                   period[lagBlock[q+1]]);
           } else {
               psseq=sseq+q*ENH_BLOCKL;
               memset(psseq, 0, ENH_BLOCKL*sizeof(float));
           }
       }

       /* future */

       for (i=0; i<periodl; i++) {
           plocs2[i]=plocs[i]-period[i];
       }
       for (q=hl+1; q<=2*hl; q++) {
           NearestNeighbor(lagBlock+q,plocs2,
               blockStartPos[q-1]+ENH_BLOCKL_HALF,periodl);

           blockStartPos[q]=blockStartPos[q-1]+period[lagBlock[q]];
           if (blockStartPos[q]+ENH_BLOCKL+ENH_OVERHANG<idatal) {
               refiner(sseq+ENH_BLOCKL*q, blockStartPos+q, idata,
                   idatal, centerStartPos, blockStartPos[q],
                   period[lagBlock[q]]);
           }
           else {
               psseq=sseq+q*ENH_BLOCKL;
               memset(psseq, 0, ENH_BLOCKL*sizeof(float));
           }
       }
   }