Exemplo n.º 1
0
int hillClimbingProb(int sol[], int **matrix, int M, int G, int iters, int swap, float prob)
{
    int *newSol, *bestSol;
    int i; double rnd;
    int totalSol, totalNewSol, totalBestSol;
    
    newSol = (int *)malloc(M * sizeof(int));
    bestSol = (int *)malloc(M * sizeof(int));
    
    if(!newSol || !bestSol)
        exit(1);
    
    copySol(sol, bestSol, M);
    totalSol = calFitness(sol, matrix, M, G);
    totalBestSol = totalSol;
    
    for(i=0; i<iters; i++)
    {
        neighbouringGen(sol, newSol, M, G, swap);
        
        totalNewSol = calFitness(newSol, matrix, M, G);
        
        if(totalNewSol >= totalSol)
        {
            copySol(newSol, sol, M);
            totalSol = totalNewSol;
        }
        else
        {
            rnd = randFloatGen();
            if (rnd < prob)
            {
                copySol(newSol, sol, M);
                totalSol = totalNewSol;
            }
        }
        
        if(totalBestSol < totalSol)
        {
            copySol(sol, bestSol, M);
            totalBestSol = totalSol;
        }
    }
    
    copySol(bestSol, sol, M);
    totalSol = totalBestSol;
    
    
    free(newSol);
    free(bestSol);
    
    return totalSol;
}
Exemplo n.º 2
0
int hillClimbing(int sol[], int **matrix, int M, int G, int iters, int swap)
{
    int *newSol;
    int i;
    int totalSol, totalNewSol;
    
    newSol = (int *)malloc(M * sizeof(int));
    
    if(!newSol)
        exit(1);
    
    totalSol = calFitness(sol, matrix, M, G);
    
    for(i=0; i<iters; i++)
    {
        neighbouringGen(sol, newSol, M, G, swap);
        
        totalNewSol = calFitness(newSol, matrix, M, G);
        
        if(totalNewSol > totalSol)
        {
            copySol(newSol, sol, M);
            totalSol = totalNewSol;
        }
    }
    
    free(newSol);
    
    return totalSol;
}
Exemplo n.º 3
0
/*!
 * \brief Heuristique pour trouver la meilleure solution aleatoire
 *
 * Cette fonction lance 100 fois une heuristique constructive aleatoire puis on
 * ameliore cette solution avec la recherche locale pour garder la meilleure solution.
 *
 * \param instance : instance sur laquelle on travaille.
 */
void Solution::multiStart(Instance * instance)
{
    Solution * bestSol = new Solution();
    Solution * clearedSol = new Solution();
    float bestDist = FLT_MAX;
    unsigned bestNbTour = 101;

    for (int cpt = 0; cpt < 100; ++cpt)
    {
        copySol(clearedSol);
        HeurisRandom(instance);
        rechercheLocale(instance);
        computeDateDist(instance);

        if(_distParcourue <= bestDist && _first.size() <= bestNbTour)
        {
            bestSol->copySol(this);
            bestDist = _distParcourue;
        }
    }

    copySol(bestSol);
}
Exemplo n.º 4
0
solution * recherche_locale(solution * sol, problem * pb)
{
	solution * s;
	int * tab = (int *)malloc(sizeof(int)* pb->nbTotPcs);
	int i,j,used,v,somme;
	somme = 0;
	used = 0;
	s = copySol(sol);
		i=0;
	while (i < pb->nbTaillesDem)
	{
		v = pb->taille[i];
		for (j=0;j<pb->nbDem[i];j++)
		{
			tab[i] = v;
			printf("val = %d\n",tab[i]);
		}
		i++;
	}
	while (used <= pb->tailleBarre)
	{
		for (i =0;i<pb->nbTotPcs;i++)
		{
			if (sol->elem[i] != sol->elem[i+1])
			{
				v = sol->elem[i];
				s->elem[i] = s->elem[i+1];
				s->elem[i+1] = v;
				if (sol->elem[i] == 0)
					somme+=tab[i];
			}

		}
	}
	s->objVal = used;
	s->tailleTot = used;
	return  s;
}
Exemplo n.º 5
0
/*!
* \brief Recherche locale pour ameliorer la solution
*
* Cette fonction applique les mouvements precedement codes lorsque c'est possible. Toutes les places sont testees.
*
* \param instance : instance sur laquelle on travaille.
*/
void Solution::rechercheLocale(Instance * instance)
{
    bool cont = true;
    Solution * bestSol = new Solution();
    bestSol->copySol(this);

    unsigned bestTime = _dateFin, bestNbTour = _first.size(), bestTTime = _sommeDureeTournee;
    float bestDist = _distParcourue;

    for (unsigned tour1 = 0; tour1 < _first.size(); ++tour1)
    {
        for (unsigned tour2 = tour1; tour2 < _first.size(); ++tour2)
        {
            for (unsigned cl1 = _first[tour1]; cl1 != 0; cl1 = _succ[cl1])
            {
                for (unsigned cl2 = _first[tour2]; cl2 != 0; cl2 = _succ[cl2])
                {
                    if (cl1 != cl2)
                    {
                        cont = true;

                        if (cont && tour1 != tour2 && opt2(cl1, tour1, cl2, tour2, instance) != -1)
                        {
                            computeDateDist(instance);

                            if (_dateFin < bestTime || _distParcourue < bestDist || _first.size() < bestNbTour)
                            {
                                bestTime = std::min(_dateFin, bestTime);
                                bestTTime = std::min(_sommeDureeTournee, bestTTime);
                                bestDist = std::min(_distParcourue, bestDist);
                                bestNbTour = std::min(_first.size(), bestNbTour);
                                bestSol->copySol(this);
                                cont = false;
                                // on recommence quand on a change quelque chose!
                                tour1 = 0;
                                tour2 = 1;
                                cl1   = _first[0];
                                cl2   = _first[1];
                            }
                            else
                            {
                                copySol(bestSol);
                            }
                        }
                        else
                        {
                            copySol(bestSol);
                        }

                        if (cont && swap(cl1, tour1, cl2, tour2, instance) != -1)
                        {
                            computeDateDist(instance);

                            if (_dateFin < bestTime || _distParcourue < bestDist || _first.size() < bestNbTour)
                            {
                                bestTime = std::min(_dateFin, bestTime);
                                bestTTime = std::min(_sommeDureeTournee, bestTTime);
                                bestDist = std::min(_distParcourue, bestDist);
                                bestNbTour = std::min(_first.size(), bestNbTour);
                                bestSol->copySol(this);
                                cont = false;
                                // on recommence quand on a change quelque chose!
                                tour1 = 0;
                                tour2 = 1;
                                cl1   = _first[0];
                                cl2   = _first[1];
                            }
                            else
                            {
                                copySol(bestSol);
                            }
                        }
                        else
                        {
                            copySol(bestSol);
                        }


                        if (cont && shift(cl1, tour1, cl2, tour2, instance, false) != -1)
                        {
                           computeDateDist(instance);
                            if (_dateFin < bestTime || _distParcourue < bestDist || _first.size() < bestNbTour)
                            {
                                bestTime = std::min(_dateFin, bestTime);
                                bestTTime = std::min(_sommeDureeTournee, bestTTime);
                                bestDist = std::min(_distParcourue, bestDist);
                                bestNbTour = std::min(_first.size(), bestNbTour);
                                bestSol->copySol(this);
                                cont = false;
                                // on recommence quand on a change quelque chose!
                                tour1 = 0;
                                tour2 = 1;
                                cl1   = _first[0];
                                cl2   = _first[1];
                            }
                            else
                            {
                                copySol(bestSol);
                            }
                        }
                        else
                        {
                            copySol(bestSol);
                        }


                        if (cont && shift(cl1, tour1, cl2, tour2, instance, true) != -1)
                        {
                           computeDateDist(instance);
                            if (_dateFin < bestTime || _distParcourue < bestDist || _first.size() < bestNbTour)
                            {
                                bestTime = std::min(_dateFin, bestTime);
                                bestTTime = std::min(_sommeDureeTournee, bestTTime);
                                bestDist = std::min(_distParcourue, bestDist);
                                bestNbTour = std::min(_first.size(), bestNbTour);
                                bestSol->copySol(this);
                                cont = false;
                                // on recommence quand on a change quelque chose!
                                tour1 = 0;
                                tour2 = 1;
                                cl1   = _first[0];
                                cl2   = _first[1];
                            }
                            else
                            {
                                copySol(bestSol);
                            }
                        }
                        else
                        {
                            copySol(bestSol);
                        }
                    }
                }
            }
        }
    }
}