Пример #1
0
Direction pathDirFor(int sr, int sc, int er, int ec) {
    Direction map[ROWS][COLS], map2[ROWS][COLS];

    int i, j;
    bool changes = true;

    for (i = 0; i < ROWS; ++i) {
        for (j = 0; j < COLS; ++j) {
            map[i][j] = none;
            map2[i][j] = none;
        }
    }
    map[er][ec] = toRight;
    map2[er][ec] = toRight;


    while (changes) {
        changes = false;

        for (i = 0; i < ROWS; ++i) {
            for (j = 0; j < COLS; ++j) {
                if (localSearch(map, map2, i, j))changes = true;
            }
        }
        for (i = 0; i < ROWS; ++i) {
            for (j = 0; j < COLS; ++j) {
                map2[i][j] = map[i][j];
            }
        }
    }
    return map[sr][sc];
}
Пример #2
0
Archive multiObjSearch( ReferenceSet& ref, StringSet<IupacString>& goodPairs, Parameters& pars )
{
	uint i;
	// initialize rng
	std::mt19937 rng( pars.seed );
	
	// build and populate archive
	Archive ar;
	for( i = 0; i < seqan::length(goodPairs); i+=2 )
		ar.add( PairSet(goodPairs[i], goodPairs[i+1], ref, pars) );
	
	// compute Pareto front
	vector<uint> pFront = ar.paretoFront();
	vector<double> alpha = sampleAlpha( rng );
	Bounds b = ar.getBounds();
	PairSet init = ar.get( 0 );
	
	// Cycle, sampling from Pareto front and running local search
	for( i = 0; i < pars.rest; i++ )
	{
		std::cout << "Restart " << i << "\n";
		init = ar.get( pFront[ rng() % pFront.size()] );
		ar.add( localSearch( init, alpha, pars, b ) );
		// update state
		pFront = ar.paretoFront();
		alpha = sampleAlpha( rng );
		b = ar.getBounds();
	}
	
	return ar;
}
Пример #3
0
void run(std::string inputFile) {
	srand(1607);
	loadData(inputFile);

	clock_t begin = clock();

	int bestCost = 0;
	int* bestSolution;
	int it = 1;
	int lastImprovement = 0;
	const int numExchanges = 10;
	const int itNoImprovement = 100;
	const int maxIterations = 500;
	
	int* s = initialSolution();
	localSearch(s, n);
	//printf("f(0) = %d\n", cost);
	bestCost = cost;
	bestSolution = s;

	while((it-lastImprovement < itNoImprovement) && (it < maxIterations)) {
		s = perturbation(s, numExchanges, n);
		localSearch(s, n);
		if(cost > bestCost) {
			bestCost = cost;
			bestSolution = s;
			lastImprovement = it;
		} 
		if((1.0*cost) < ((1-epslon)*bestCost)) {
			s = bestSolution;
		}
		//printf("f(%d) = %d lastImprovement = %d\n", it, bestCost, lastImprovement);
		it++;
	}

	double elapsedSecs = double(clock() - begin) / CLOCKS_PER_SEC;
	
	avgTime = avgTime + elapsedSecs;

	verifySolution(inputFile, bestSolution, bestCost, elapsedSecs);

	deleteMatrix(matrix, n);
	deleteMatrix(diff, n);
	//deleteMatrix(simDegrees, n);
	//delete [] pos;
	delete [] s;	
}
Пример #4
0
int LSearch::step(int steps) {
	TRACE_IN("LSearch::LSearch");

	for (int i=0; i<steps; i++) {
		currentVal = localSearch(currSolution, currentVal);

		stat->addGlobalBest(currentVal);
		stat->psoStepDone();

	}
	TRACE_OUT("LSearch::LSearch", 0);
	return 0;
}
GuidedLocalSearch::Candidate GuidedLocalSearch::search(const std::vector<std::pair<float, float>>& cities,
                                                       const int kIterLimit,
                                                       const int kNoImproveLimit,
                                                       const float kLambda)
{
    srand(static_cast<unsigned>(time(nullptr)));

    std::vector<std::vector<float>> penalties(cities.size(), std::vector<float>(cities.size(), 0.0f));
    GuidedLocalSearch::Candidate current, best;
    current.permutation = randomPermutation(cities);
    for (int iter = 0; iter < kIterLimit; ++iter)
    {
        localSearch(current, cities, penalties, kNoImproveLimit, kLambda);
        std::vector<float> utilities = calcualateFeaturesUtilities(cities, current.permutation, penalties);
        updatePenalties(penalties, cities, current.permutation, utilities);
        if (!iter || current.ordinaryCost < best.ordinaryCost)
        {
            best.permutation = current.permutation;
            best.ordinaryCost = current.ordinaryCost;
            best.augmentedCost = current.augmentedCost;
        }
    }
    return best;
}
Пример #6
0
/*
*   Function: streamCluster
*   -----------------------
*   Kernel main function.
*/
void streamCluster( SimStream* stream,
		    long kmin, long kmax, int dim,
		    long chunksize, long centersize, char* outfile ) {

    float* block = (float*)malloc( chunksize*dim*sizeof(float) );
    float* centerBlock = (float*)malloc(centersize*dim*sizeof(float) );
    long* centerIDs = (long*)malloc(centersize*dim*sizeof(long));

    if( block == NULL ) {
        fprintf(stderr,"not enough memory for a chunk!\n");
        exit(EXIT_FAILURE);
    }

    Points points;
    points.dim = dim;
    points.num = chunksize;
    points.p =
    (Point *)malloc(chunksize*sizeof(Point));

    for( int i = 0; i < chunksize; i++ ) {
        points.p[i].coord = &block[i*dim];
    }

    Points centers;
    centers.dim = dim;
    centers.p =
    (Point *)malloc(centersize*sizeof(Point));
    centers.num = 0;

    for( int i = 0; i < centersize; i++ ) {
        centers.p[i].coord = &centerBlock[i*dim];
        centers.p[i].weight = 1.0;
    }

    long IDoffset = 0;
    long kfinal;

    while(1) {

        size_t numRead = syn_read(block, dim, chunksize, stream);
        fprintf(stderr,"read %d points\n", (int)numRead);

        if(numRead < (unsigned int)chunksize && !syn_feof(stream) ) {
            fprintf(stderr, "error reading data!\n");
            exit(EXIT_FAILURE);
        }

        points.num = numRead;
        for( int i = 0; i < points.num; i++ ) {
            points.p[i].weight = 1.0;
        }
        switch_membership = (int*)malloc(points.num*sizeof(int));
        is_center = (int*)calloc(points.num,sizeof(int));
        center_table = (int*)malloc(points.num*sizeof(int));

        localSearch(&points, kmin, kmax, &kfinal); // parallel

        contcenters(&points); /* sequential */

        if( kfinal + centers.num > centersize ) {
            //here we don't handle the situation where # of centers gets too large.
            fprintf(stderr,"oops! no more space for centers\n");
            exit(EXIT_FAILURE);
        }

        copycenters(&points, &centers, centerIDs, IDoffset); /* sequential */
        IDoffset += numRead;

        free(is_center);
        free(switch_membership);
        free(center_table);

        if(syn_feof(stream)) {
            break;
        }
    }

    //finally cluster all temp centers
    switch_membership = (int*)malloc(centers.num*sizeof(int));
    is_center = (int*)calloc(centers.num,sizeof(int));
    center_table = (int*)malloc(centers.num*sizeof(int));

    localSearch( &centers, kmin, kmax ,&kfinal ); // parallel
    contcenters(&centers);
    outcenterIDs( &centers, centerIDs, outfile);

    free(block);
    free(centerBlock);
    free(centerIDs);

    free(centers.p);
    free(points.p);

    free(switch_membership);
    free(is_center);
    free(center_table);
}
Пример #7
0
bool iteratedLocalSearchAlgorithm(SolvObject* solvO, int maxNeighbours, unsigned short rfils, unsigned short ifrfils, int seed){
    
	// initialize random generator
	srand( seed ) ;
    unsigned int satClauses = solvO->getNumberOfSatisfiedClauses();
    unsigned int Clauses = solvO->getNumberOfClauses();
    
	 
	 
	 
	 
	 // temporaly flipper for saving last local minima state
    flippercopy tempFlipper;
    solvO->initializeCopyFlipper(tempFlipper);
    
    unsigned int tempSatClauses = satClauses;
    
    
    while (satClauses < Clauses){
        
		 
        //use local search for find best solution candidate
        while (localSearch(solvO, maxNeighbours, satClauses) == 0){
			  satClauses = solvO->getNumberOfSatisfiedClauses();
			  
			  if (satClauses == Clauses)
				  // Sat
				  return 0;
			  
			  // add calculated best flipper to temp flipper
			  solvO->addFlippers(tempFlipper);
			  
			  
		  }
		  // at this point local search have reached a local minimum
		  
		  
        // check whether current local minimum is better than old one
		  if (satClauses < tempSatClauses){
			  // it's not better
			 // jump back to old state
			  solvO->useFlipperCopy(tempFlipper);
			  solvO->flipVariablesByFlipperVector();
			  
			  // increment chance of random flip
			  if (rfils + ifrfils < 1000)
				rfils = rfils + ifrfils;
			  
			  
			  
		   } else{
				// update current satisfied clauses
				tempSatClauses = satClauses;  
			}
        
		  
		  // do a random jump from current local minima
        
		  unsigned int numberFlips = 0;
		  solvO->resetFlipper();
		  solvO->resetFlipperCopy(tempFlipper);
		  
        do{
            
				
			  // go through all variables and flip randomly
			  for (unsigned int i = 0; i < solvO->getListOfVariables()->size(); i++){
				  
					if (rand()% 1000 < rfils){
						numberFlips++;
						solvO->flipBitInFlipper(i);
					}  
			  }
			  
			  
			  
        }while(numberFlips == 0);
		  
		  // save random flips to temp flipper for jumping back
        solvO->copyFlipper(tempFlipper);
		  
		  // flip variables by random flip
        solvO->flipVariablesByFlipperVector();
		  
		  satClauses = solvO->getNumberOfSatisfiedClauses();
        
		  
        
    }
    
	 return 0;
}