Ejemplo n.º 1
0
void SS_free(SS *problem) {
    REFSET_free(problem->rs);
    P_free(problem->p);
    CC_IFFREE(problem->rs, REFSET);
    CC_IFFREE(problem->p, P);
    CC_IFFREE(problem->jobarray, Job *);
    problem->b2        = 0;
    problem->b1        = 0;
    problem->timelimit = .0;
    problem->njobs        = 0;

    if (problem->random != NULL) {
        g_rand_free(problem->random);
    }
}
Ejemplo n.º 2
0
static int add_feasible_solution(wctproblem *problem, solution *new_sol) {
    int val = 0;
    wctdata *root_pd = &(problem->root_pd);
    SS *scatter_search = &(problem->scatter_search);
    solution_calc(new_sol, root_pd->jobarray);
    localsearch_wrap(new_sol, problem->global_lower_bound, 0);
    solution_unique(new_sol);

    if (!solution_in_pool(scatter_search, new_sol)) {
        add_solution_pool(scatter_search, new_sol);
        update_bestschedule(problem, new_sol);
        scatter_search->p->PSize++;

        if (root_pd->ccount == 0 && problem->parms.construct != 0) {
            update_Schedulesets(&root_pd->cclasses, &root_pd->ccount, problem->bestschedule,
                                problem->nbestschedule);
            root_pd->gallocated = root_pd->ccount;
        } else if (problem->parms.construct != 0) {
            partlist_to_Scheduleset(new_sol->part, new_sol->nmachines, new_sol->njobs,
                                    &(root_pd->newsets), &(root_pd->nnewsets));
            add_newsets(root_pd);
        }
    } else {
        solution_free(new_sol);
        CC_IFFREE(new_sol, solution);
    }

    return val;
}
Ejemplo n.º 3
0
void P_free(P *p) {
    if (p) {
        for (GList *it = p->list; it; it = it->next) {
            solution_free((solution *)it->data);
            CC_IFFREE(it->data, solution);
        }

        p->PSize = 0;
        g_list_free(p->list);
        P_init(p);
    }
}
Ejemplo n.º 4
0
int add_solution_refset(SS *scatter_search) {
    int i;
    int val        = 0;
    REFSET *refset = scatter_search->rs;
    P *pool        = scatter_search->p;

    switch (scatter_search->status) {
    case init:
        pool->list = g_list_sort(pool->list, order_totalweightcomptime_list);

        for (i = 0; i < scatter_search->b1; ++i) {
            void *data = pool->list->data;
            pool->list = g_list_remove(pool->list, data);
            g_ptr_array_add(refset->list1, data);
        }

        scatter_search->status = add;
        break;

    case add:
        for (i = 0; i < scatter_search->b2; ++i) {
            void *data = maximum_distance(scatter_search);
            CCcheck_NULL_2(data, "Failed in maximum_distance");
            pool->list = g_list_remove(pool->list, data);
            update_distance(scatter_search, (solution *)data);
            g_ptr_array_add(refset->list2, data);
        }

        g_ptr_array_sort(refset->list2, order_distance);
        scatter_search->status = update;

        for (GList *it = pool->list; it; it = it->next) {
            solution_free((solution *)it->data);
            CC_IFFREE(it->data, solution);
        }

        g_list_free(pool->list);
        pool->list = (GList *) NULL;
        break;

    case update:
        diversification_update(scatter_search);
        break;

    case opt:
        break;
    }

CLEAN:
    return val;
}
Ejemplo n.º 5
0
solution *new_sol_init(int nmachines, int vcount) {
    int val = 0;
    solution *sol = (solution *) NULL;
    sol = CC_SAFE_MALLOC(1, solution);
    CCcheck_NULL_2(sol, "Failed to allocate memory")
    solution_init(sol);
    val = solution_alloc(sol, nmachines, vcount);
    CCcheck_val_2(val, "Failed in solution_alloc");
CLEAN:

    if (val) {
        solution_free(sol);
        CC_IFFREE(sol, solution);
    }

    return sol;
}
Ejemplo n.º 6
0
void free_sol(void *data, void *user_data) {
    solution *sol = (solution *)data;
    solution_free(sol);
    CC_IFFREE(sol, solution);
    sol = (solution *)user_data;
}
Ejemplo n.º 7
0
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;
}
Ejemplo n.º 8
0
int SSrun_scatter_search(SS *scatter_search, CCutil_timer *timer) {
    guint i;
    REFSET *refset = scatter_search->rs;
    int flag, val = 0;
    int nbnew_sol = 0;
    int nbjobs = scatter_search->njobs;
    int nmachines = scatter_search->nmachines;
    int nb_noimprovements = 0;
    int *totsubset = (int *) NULL;
    double limit = scatter_search->timelimit;
    totsubset = CC_SAFE_MALLOC(scatter_search->b1 + 1, int);
    CCcheck_NULL_2(totsubset, "Failed tot allocate memory to tot subset");

    if (refset->list1->len == 0) {
        printf("We can't run scatter search, refset is empty\n");
        val = 1;
        goto CLEAN;
    }

    printf("PMCombination method\n");
    CCutil_suspend_timer(timer);
    CCutil_resume_timer(timer);

    while (refset->newsol && scatter_search->status != opt &&
            timer->cum_zeit < limit) {
        GPtrArray *list = g_ptr_array_new();

        for (i = 0; i < refset->list1->len; ++i) {
            g_ptr_array_add(list, g_ptr_array_index(refset->list1, i));
        }

        for (i = 0; i < refset->list2->len; ++i) {
            g_ptr_array_add(list, g_ptr_array_index(refset->list2, i));
        }

        compute_fitness(scatter_search);
        refset->newsol = 0;

        if (scatter_search->iter > 0) {
            int best = scatter_search->upperbound;
            CCutil_suspend_timer(timer);
            CCutil_resume_timer(timer);
            double rel_error = ((double)best - (double)scatter_search->lowerbound) /
                               (double)scatter_search->lowerbound;
            printf("iteration %d with best wct %d and rel error %f, number of new solutions %d, time = %f\n",
                   scatter_search->iter, best, rel_error, nbnew_sol, timer->cum_zeit);
        }

        nbnew_sol = 0;
        k_subset_init(list->len, 2, totsubset, &flag);

        while (flag && scatter_search->status != opt) {
            solution *new_sol = CC_SAFE_MALLOC(1, solution);
            solution_init(new_sol);
            solution_alloc(new_sol, nmachines, nbjobs);
            int rval = 1;
            rval = combine_PM(scatter_search, list, totsubset, 2, new_sol);

            if (!rval) {
                solution_calc(new_sol, *(scatter_search->jobarray));
                new_sol->iter = scatter_search->iter + 1;
                localsearch_random_k(new_sol, scatter_search->lowerbound, 2);
                solution_unique(new_sol);

                if (!dynamic_update(scatter_search, list, new_sol)) {
                    if (new_sol->totalweightcomptime < scatter_search->upperbound) {
                        scatter_search->upperbound = new_sol->totalweightcomptime;
                    }

                    if (new_sol->totalweightcomptime == scatter_search->lowerbound) {
                        scatter_search->status = opt;
                        printf("Found optimal with SS with subset generation 1\n");
                    }

                    nbnew_sol++;
                } else {
                    solution_free(new_sol);
                    CC_IFFREE(new_sol, solution);
                }
            } else {
                solution_free(new_sol);
                CC_IFFREE(new_sol, solution);
            }

            k_subset_lex_successor(list->len, 2, totsubset, &flag);
        }

        k_subset_init(list->len, 3, totsubset, &flag);
        solution *sol = (solution *)NULL;
        sol = (solution *) g_ptr_array_index(list, 0);

        while (flag && scatter_search->status != opt) {
            solution *new_sol = CC_SAFE_MALLOC(1, solution);
            solution_init(new_sol);
            solution_alloc(new_sol, nmachines, nbjobs);
            int rval = 1;
            rval = combine_PM(scatter_search, list, totsubset, 3,
                              new_sol);  //Dell'Amico et al.

            if (!rval) {
                solution_calc(new_sol, *(scatter_search->jobarray));
                new_sol->iter = scatter_search->iter + 1;
                localsearch_random_k(new_sol, scatter_search->lowerbound, 2);
                solution_unique(new_sol);

                if (!dynamic_update(scatter_search, list, new_sol)) {
                    if (new_sol->totalweightcomptime < scatter_search->upperbound) {
                        scatter_search->upperbound = new_sol->totalweightcomptime;
                    }

                    if (new_sol->totalweightcomptime == scatter_search->lowerbound) {
                        scatter_search->status = opt;
                        printf("Found optimal with SS\n");
                    }

                    nbnew_sol++;
                } else {
                    solution_free(new_sol);
                    CC_IFFREE(new_sol, solution);
                }
            } else {
                solution_free(new_sol);
                CC_IFFREE(new_sol, solution);
            }

            k_subset_lex_successor(list->len, 3, totsubset, &flag);
            sol = (solution *) g_ptr_array_index(list, totsubset[1] - 1);
        }

        k_subset_init(list->len, 4, totsubset, &flag);
        sol = (solution *) g_ptr_array_index(list, 0);
        solution *temp_sol = (solution *) g_ptr_array_index(list, 1);

        while (flag && scatter_search->status != opt
                && sol->totalweightcomptime <= scatter_search->upperbound
                && temp_sol->totalweightcomptime <= scatter_search->upperbound) {
            solution *new_sol = CC_SAFE_MALLOC(1, solution);
            solution_init(new_sol);
            solution_alloc(new_sol, nmachines, nbjobs);
            int rval = 1;
            rval = combine_PM(scatter_search, list, totsubset, 4, new_sol);

            if (!rval) {
                solution_calc(new_sol, *(scatter_search->jobarray));
                new_sol->iter = scatter_search->iter + 1;
                localsearch_random_k(new_sol, scatter_search->lowerbound, 2);
                solution_unique(new_sol);

                if (!dynamic_update(scatter_search, list, new_sol)) {
                    if (new_sol->totalweightcomptime < scatter_search->upperbound) {
                        scatter_search->upperbound = new_sol->totalweightcomptime;
                    }

                    if (new_sol->totalweightcomptime == scatter_search->lowerbound) {
                        scatter_search->status = opt;
                        printf("Found optimal with SS\n");
                    }

                    nbnew_sol++;
                } else {
                    solution_free(new_sol);
                    CC_IFFREE(new_sol, solution);
                }
            } else {
                solution_free(new_sol);
                CC_IFFREE(new_sol, solution);
            }

            k_subset_lex_successor(list->len, 4, totsubset, &flag);
            sol = (solution *) g_ptr_array_index(list, totsubset[1] - 1);
            temp_sol = (solution *)g_ptr_array_index(list, totsubset[2] - 1);
        }

        for (i = 0; i < refset->list2->len + 1; ++i) {
            totsubset[i] = i;
        }

        for (i = 5; i < refset->list2->len + 1
                && scatter_search->status != opt; i++) {
            solution *new_sol = CC_SAFE_MALLOC(1, solution);
            solution_init(new_sol);
            solution_alloc(new_sol, nmachines, nbjobs);
            int rval = 1;
            rval = combine_PM(scatter_search, list, totsubset, i, new_sol);

            if (!rval) {
                solution_calc(new_sol, *(scatter_search->jobarray));
                new_sol->iter = scatter_search->iter + 1;
                localsearch_random_k(new_sol, scatter_search->lowerbound, 2);
                solution_unique(new_sol);

                if (!dynamic_update(scatter_search, list, new_sol)) {
                    if (new_sol->totalweightcomptime < scatter_search->upperbound) {
                        scatter_search->upperbound = new_sol->totalweightcomptime;
                    }

                    if (new_sol->totalweightcomptime == scatter_search->lowerbound) {
                        scatter_search->status = opt;
                        printf("Found optimal with SS\n");
                    }

                    nbnew_sol++;
                } else {
                    solution_free(new_sol);
                    CC_IFFREE(new_sol, solution);
                }
            } else {
                solution_free(new_sol);
                CC_IFFREE(new_sol, solution);
            }
        }

        scatter_search->iter++;

        if (nbnew_sol == 0) {
            nb_noimprovements++;
        }

        if (refset->newsol == 0 && scatter_search->iter < 10
                && nb_noimprovements < 5) {
            add_solution_refset(scatter_search);
            refset->newsol = 1;
        }

        g_ptr_array_free(list, TRUE);
    }

CLEAN:
    CC_IFFREE(totsubset, int);
    return val;
}
Ejemplo n.º 9
0
static int read_tentative_nodes (FILE *f, int count, tsp_tnode **list,
                                 tsp_bbnode *parent, CCptrworld *bbnode_world)
{
    int rval = 0;
    int i;
    int obtained = 0;
    tsp_tnode *s;
    tsp_bbnode *child0 = (tsp_bbnode *) NULL;
    tsp_bbnode *child1 = (tsp_bbnode *) NULL;

    *list = CC_SAFE_MALLOC (count, tsp_tnode);
    if (*list == (tsp_tnode *) NULL) {
        fprintf (stderr, "out of memory in read_tentative_nodes\n");
        rval = 1;
        goto CLEANUP;
    }

    for (obtained = 0; obtained < count; obtained++) {
        s = &((*list)[obtained]);
        child0 = tsp_bbnode_alloc (bbnode_world);
        if (child0 == (tsp_bbnode *) NULL) {
            fprintf (stderr, "tsp_bbnode_alloc failed\n");
            rval = 1;
            goto CLEANUP;
        }
        init_bbnode (child0);
        child1 = tsp_bbnode_alloc (bbnode_world);
        if (child1 == (tsp_bbnode *) NULL) {
            fprintf (stderr, "tsp_bbnode_alloc failed\n");
            tsp_bbnode_free (bbnode_world, child0);
            rval = 1;
            goto CLEANUP;
        }
        init_bbnode (child1);

        rval = fscanf (f, "%d%d%lf%lf %d%d%lf%lf\n",
                       &child0->status, &child0->id,
                       &child0->lowerbound, &child0->cputime,
                       &child1->status, &child1->id,
                       &child1->lowerbound, &child1->cputime);
        if (rval <= 0) {
            perror ("restart_file");
            fprintf (stderr, "fscanf failed reading tentative line\n");
            rval = 1;
            goto CLEANUP;
        }
        child0->tparent = s;
        child1->tparent = s;
        s->child0 = child0;
        s->child1 = child1;
        s->parent = parent;
    }
    rval = 0;

CLEANUP:

    if (rval) {
        for (i = 0; i < obtained; i++) {
            tsp_bbnode_free (bbnode_world, (*list)[i].child0);
            tsp_bbnode_free (bbnode_world, (*list)[i].child1);
        }
        CC_IFFREE (*list, tsp_tnode);
    }
    return rval;
}