std::shared_ptr<Schedule> SimulatedAnnealing::findSolutionSerial(double time_limit) const
{
    std::shared_ptr<Schedule> best_sched = generateRandomSolution();
    bool finished = false;
    while (not finished)
    {
        std::shared_ptr<Schedule> curr_sched = generateRandomSolution();
        double T = initialTemperature;
        while (T >= endTemperature and not finished)
        {
            for (unsigned i = 0;
                 i < temperatureRounds and not(finished = isTimeLimitReached(time_limit)); ++i)
            {
                auto comp_sched = generateNeighbourSolution(*curr_sched);
                if (comp_sched->execTime() < best_sched->execTime())
                {
                    best_sched = comp_sched;
                }

                double accept_new_prob
                    = 1.0 / (1.0 + exp((static_cast<int>(comp_sched->execTime())
                                           - static_cast<int>(curr_sched->execTime()))
                                       / T));

                if (comp_sched->execTime() < curr_sched->execTime()
                    or std::generate_canonical<double, std::numeric_limits<double>::digits>(
                           currentRandomEngine())
                        < accept_new_prob)
                {
                    curr_sched = comp_sched;
                }
            }

            T *= coolDownRate;
        }
    }
    return best_sched;
}
Esempio n. 2
0
Chromosome::Chromosome(int size, Chromosome::GenerationType gt)
{
	switch (gt)
	{
	case Chromosome::OPTIMAL:
		_solution = generateOptimalSolutionForAssignmentData(size);
		break;
	case Chromosome::RANDOM:
		_solution = generateRandomSolution(size);
		break;
	case Chromosome::GREEDY:
	default:
		_solution = generateGRCSolution(size);
		return;
		//_solution = Chromosome::GRCsolution();
		break;
	}
	
	_score = calcScore();
}
Esempio n. 3
0
int main(int argc, char *argv[]){
    int maxTotal;
    int numCorners,numBorders,numCenters;
    int perc_shake_border=0;
    int perc_shake_center=0;
    int perc_shake_coner=0;
    int width;
    int heigth;
    int corners [4];
    int *borders;
    int *centers;
    pos_t pos_corners[4];
    pos_t *pos_borders;
    pos_t *pos_centers;
    int currBestScore=0,bestScore=BEST_SCORE;

    int fine=0;
    int p=1;
    int levelMax=LEVEL_MAX;
    int stepStarts=0;

    int iterMax = ITER_MAX;
    clock_t start,stop;
    float difference;


    switch(argc){
       case 5:
                if ( argv[1][1] == 't')
                    sscanf(argv[2],"%d", &maxTime);
                else if ( argv[1][1] == 'p')
                      sscanf(argv[2],"%d", &maxScore);
                else{
                    printf("Option not valid\n");
                    goto error;
                }
                FILE_IN = strdup(argv[3]);
                FILE_OUT = strdup(argv[4]);
                break;
        case 7:
                if ( argv[1][1] == 't')
                    sscanf(argv[2],"%d", &maxTime);
                else{
                    printf("Option not valid\n");
                    goto error;
                }
                if ( argv[3][1] == 'p')
                      sscanf(argv[4],"%d", &maxScore);
                else{
                    printf("Option not valid\n");
                    goto error;
                }
                FILE_IN = strdup(argv[5]);
                FILE_OUT = strdup(argv[6]);
                break;
        default:
             error: printf("\nUSAGE: %s [-t <time in seconds>] [-p <maximum score>] <input file> <output file>\n",argv[0]);
             return 0;
    }

    srand(time(NULL));
    parser(corners,&borders,&centers,&vector,&width,&heigth);

    numCorners=4;
    numBorders=2*(heigth-2)+2*(width-2);
    numCenters=(heigth-2)*(width-2);
    pos_borders= (pos_t *) malloc( numBorders*sizeof(pos_t) );
    pos_centers= (pos_t*) malloc(numCenters*sizeof(pos_t));
    init_positions(pos_corners,pos_borders,pos_centers,width,heigth);
    maxTotal=heigth*(width-1)+width*(heigth-1);
    printf("Max Total: %d\n",maxTotal);

    elem_sol **currBest,**best;
    currBest=allocaMatrix(width,heigth);
    best=allocaMatrix(width,heigth);
    elem_sol**neighborOfcurrBest;
    neighborOfcurrBest=allocaMatrix(width,heigth);

    globalTime=clock();
    while (!fine){
        stepStarts++;
        start=clock();
        stop=clock();
        difference=(float)(stop-globalTime)/CLOCKS_PER_SEC;
        printf("\nGlobal Time %.2f Start %d - global best %d\n",difference,stepStarts,bestScore);
        //leggiFile(vector,currBest,width,heigth);
        generateRandomSolution(currBest,vector,corners,borders,centers,width,heigth);
        currBestScore=CheckMatchingEdgesSol(currBest,width,heigth);
        //printGame(heigth, width, currBest);
        //printf ("parto da %d \n", currBestScore);

        p=LEVEL_MIN;
        fine=improve(0,start,p,levelMax,iterMax,&currBestScore,&bestScore,currBest,width,heigth,pos_centers,pos_borders,pos_corners,
        numCenters,numBorders,numCorners,perc_shake_coner,perc_shake_border,perc_shake_center,maxTotal,best,stepStarts,MAX_UGUALE);

        //printGame(heigth, width, currBest);
        if ( currBestScore > bestScore ){
            storeSolution(best,currBest,width,heigth);
            bestScore=currBestScore;
            //stop = clock();
            //difference = (stop - start)/CLOCKS_PER_SEC;
            //printf ("\nBEST edges matching = %d \t tempo: %f\n", bestScore, difference);
            //printGame(heigth, width,best);
            printer(width,heigth,best);
        }
    }
    freeThings(currBest, best, neighborOfcurrBest, pos_borders, pos_centers, borders, centers, vector, heigth);
    return 0;
}