Exemple #1
0
/*Copia un elemento en otro, reservando memoria*/
EleList * elelist_copy(const EleList *pele1){
	EleList *pele2 = NULL;
	Solution *info1 = NULL;
	Solution *info2 = NULL;
	if(!pele1 || !pele1->info){
		return NULL;
	}
	pele2 = elelist_ini();
    if(!pele2){
        return NULL;
    }
    info1 = (Solution *)elelist_getInfo((EleList *)pele1);
    if(!info1){
    	elelist_free(pele2);
    	return NULL;
    }
    info2 = solution_copy(info1);
    if(!info2){
    	elelist_free(pele2);
    	return NULL;
    }
    if(!elelist_setInfo(pele2, info2)){
    	free(info2);
    	elelist_free(pele2);
    }
	return pele2;
}
int combinePathRelinking(SS *scatter_search, GPtrArray *array, int *subsetsol,
                         int *found) {
    int i, val = 0;
    int njobs = scatter_search->njobs;
    int nmachines = scatter_search->nmachines;
    int dist = 0;
    int counter = 0;
    Job **jobarray = scatter_search->jobarray;
    REFSET *refset = scatter_search->rs;
    GList *list = (GList *) NULL;
    GList *it = (GList *) NULL;
    GList *pool = (GList *) NULL;
    solution *prev = (solution *) NULL;
    solution sol_g;
    solution sol_c;
    solution *sol1 = (solution *)g_ptr_array_index(array, subsetsol[1] - 1);
    solution *sol2 = (solution *)g_ptr_array_index(array, subsetsol[2] - 1);

    if (sol1->totalweightcomptime < sol2->totalweightcomptime) {
        val = solution_copy(&sol_g, *sol1);
        val = solution_copy(&sol_c, *sol2);
    } else {
        val = solution_copy(&sol_c, *sol1);
        val = solution_copy(&sol_g, *sol2);
    }

    for (i = 0; i < njobs; ++i) {
        partlist *temp1 = sol_g.vlist[i].part;
        partlist *temp2 = sol_c.vlist[i].part;

        if (temp1->key != temp2->key) {
            dist++;
            list = g_list_append(list, jobarray[i]);
        }
    }

    prev = &(sol_c);

    while (dist > 0) {
        Job *minjob = (Job *) NULL;
        partlist *minmach = (partlist *) NULL;
        solution *sol = CC_SAFE_MALLOC(1, solution);
        solution_init(sol);
        solution_alloc(sol, nmachines, njobs);
        solution_update(sol, *prev);
        int max = INT_MIN;
        int temp;

        for (it = list; it; it = it->next) {
            Job *j = (Job *)it->data;
            partlist *mach_c = sol->vlist[j->job].part;
            partlist *mach_g = sol->part + sol_g.vlist[j->job].part->key;
            temp = moveSS(j, mach_c, mach_g);

            if (temp > max) {
                minjob = j;
                minmach = mach_g;
                max = temp;
            }
        }

        partlist_move_order(minmach, sol->vlist, minjob, njobs);
        sol->totalweightcomptime -= max;
        pool = g_list_append(pool, sol);
        list = g_list_remove(list, minjob);
        dist--;
        prev = sol;
    }

    counter = 0;
    GList *l = pool;

    while (l != NULL) {
        GList *next = l->next;

        if (counter % 10 == 0) {
            solution *sol = (solution *)l->data;
            localsearch_wrap(sol, scatter_search->lowerbound, 0);
            solution_unique(sol);
        } else {
            solution *sol = (solution *)l->data;
            solution_free(sol);
            CC_IFFREE(sol, solution);
            pool = g_list_delete_link(pool, l);
        }

        counter++;
        l = next;
    }

    l = pool;

    while (l != NULL) {
        solution *last1 = (solution *) g_ptr_array_index(refset->list1,
                          refset->list1->len - 1);
        solution *last2 = (solution *) g_ptr_array_index(refset->list2,  0);
        GList *next = l->next;
        solution *new_sol = (solution *)l->data;
        int not_in_refset = !solution_in_refset(scatter_search, new_sol);

        if (new_sol->totalweightcomptime <  last1->totalweightcomptime &&
                not_in_refset) {
            new_sol->dist = 0;
            refset->newsol = 1;
            new_sol->iter = scatter_search->iter + 1;
            g_ptr_array_remove(refset->list1, last1);
            g_ptr_array_remove(array, last1);
            g_ptr_array_add(refset->list1, new_sol);
            g_ptr_array_add(array, new_sol);
            g_ptr_array_sort(refset->list1, order_totalweightcomptime);
            g_ptr_array_sort(array, order_totalweightcomptime);
            solution_free(last1);
            CC_IFFREE(last1, solution);
            *found = 1;
        } else if (not_in_refset) {
            SSrefset_distance(scatter_search, new_sol);

            if (new_sol->dist > last2->dist) {
                refset->newsol = 1;
                new_sol->iter = scatter_search->iter + 1;
                g_ptr_array_remove(refset->list2, last2);
                g_ptr_array_remove(array, last2);
                g_ptr_array_add(refset->list2, new_sol);
                g_ptr_array_add(array, new_sol);
                g_ptr_array_sort(refset->list2, order_distance);
                g_ptr_array_sort(array, order_distance);
                solution_free(last2);
                CC_IFFREE(last2, solution);
                *found = 1;
            } else {
                solution_free(new_sol);
                CC_IFFREE(new_sol, solution);
            }
        } else {
            solution_free(new_sol);
            CC_IFFREE(new_sol, solution);
        }

        pool = g_list_delete_link(pool, l);
        l = next;
    }

    g_list_free(pool);
    solution_free(&sol_g);
    solution_free(&sol_c);
    return val;
}