Esempio n. 1
0
int SSproblem_definition(
    SS *problem,
    int b1,
    int b2,
    double timelimit,
    int combine_method,
    int njobs,
    int nmachines,
    Job *jobarray,
    int lowerbound) {
    int i, val         = 0;
    REFSET *temp_rs = NULL;
    P *temp_p       = NULL;
    /*initialize scatter search data structure */
    SS_init(problem, b1, b2, timelimit);
    problem->combine_method = combine_method;
    problem->nmachines = nmachines;
    problem->njobs = njobs;
    problem->lowerbound = lowerbound;
    problem->upperbound = INT_MAX;
    /* Initialize pool */
    problem->p = CC_SAFE_MALLOC(1, P);
    CCcheck_NULL_2(problem->p, "Failed to allocate memory to problem->p");
    temp_p  = problem->p;
    P_init(temp_p);
    /* Initialize refset */
    problem->rs         = CC_SAFE_MALLOC(1, REFSET);
    CCcheck_NULL_2(problem->rs, "Failed to allocate memory to problem->rs");
    temp_rs             = problem->rs;
    REFSET_init(temp_rs);
    /* Initialize Jobarray of scatter search data structure */
    problem->jobarray = CC_SAFE_MALLOC(njobs, Job *);
    CCcheck_NULL_2(problem->jobarray, "Failed to allocate memory");

    for (i = 0; i < njobs; i++) {
        problem->jobarray[i] = jobarray + i ;
    }

    problem->random = g_rand_new_with_seed(48654642);
    CCcheck_NULL_2(problem->random, "Failed in g_rand_new_with_seed");
CLEAN:

    if (val) {
        SS_free(problem);
    }

    return val;
}
Esempio n. 2
0
int wctlp_init(wctlp **lp, const char *name) {
    int val = 0;
    (*lp) = (wctlp *) CC_SAFE_MALLOC(1, wctlp);
    CCcheck_NULL(lp, "Out of memory for lp");
    (*lp)->env = (GRBenv *) NULL;
    (*lp)->model = (GRBmodel *) NULL;
    val = GRBloadenv(&((*lp)->env), NULL);

    if (val) {
        printf("val = %d\n", val);
    }

    CCcheck_val(val, "GRBloadenv failed");
    val = GRBsetintparam((*lp)->env, GRB_INT_PAR_OUTPUTFLAG,
                         (dbg_lvl() > 1) ? 1 : 0);
    CHECK_VAL_GRB(val, "GRBsetintparam OUTPUTFLAG failed", (*lp)->env);
    val = GRBsetintparam((*lp)->env, GRB_INT_PAR_THREADS, 1);
    CHECK_VAL_GRB(val, "GRBsetintparam TREADS failed", (*lp)->env);
    val = GRBsetintparam((*lp)->env, GRB_INT_PAR_METHOD, GRB_METHOD_PRIMAL);
    CHECK_VAL_GRB(val, "GRBsetintparam LPMETHOD failed", (*lp)->env);
    val = GRBsetintparam((*lp)->env, GRB_INT_PAR_INFUNBDINFO, 1);
    CHECK_VAL_GRB(val, "GRBsetintparam INFUNBDINFO", (*lp)->env);
    val =  GRBsetintparam((*lp)->env, GRB_INT_PAR_PRESOLVE, 0);
    CHECK_VAL_GRB(val, "Failed in setting parameter", (*lp)->env);
    val = GRBnewmodel((*lp)->env, &((*lp)->model), name, 0, (double *)NULL,
                      (double *)NULL, (double *)NULL, (char *)NULL, NULL);
    CHECK_VAL_GRB(val, "GRBnewmodel failed", (*lp)->env);
    return val;
}
Esempio n. 3
0
int CCtsp_init_edgegenerator (CCtsp_edgegenerator *eg, int ncount,
        CCdatagroup *dg, CCtsp_genadj *adj, int nneighbors,
        int silent, CCrandstate *rstate)
{
    int rval;
    int norm;

    if (!silent) {
        printf ("CCtsp_init_edgegenerator (%d)\n", nneighbors); fflush (stdout);
    }

    eg->node_piest = (double *) NULL;
    eg->kdtree = (CCkdtree *) NULL;
    eg->xnear = (CCxnear *) NULL;
    eg->xprice = (CCtsp_xnorm_pricer *) NULL;
    eg->supply = (int *) NULL;
    eg->adjobjspace = (CCtsp_genadjobj *) NULL;
    eg->ncount = ncount;
    eg->dg = dg;
    eg->adj = adj;
    CCutil_dat_getnorm (dg, &norm);

    if (nneighbors == CCtsp_PRICE_COMPLETE_GRAPH) {
        eg->nneighbors = CCtsp_PRICE_COMPLETE_GRAPH;
        eg->supplyhead = 0;
        eg->start = 0;
        eg->current = 0;
        if ((((norm & CC_NORM_BITS) == CC_KD_NORM_TYPE) ||
            ((norm & CC_NORM_BITS) == CC_X_NORM_TYPE)) &&
              dg->ndepot == 0) {
            eg->supply = CC_SAFE_MALLOC (ncount, int);
            if (!eg->supply) {
                fprintf (stderr, "out of memory in CCtsp_init_edgegenerator\n");
                return 1;
            }
            eg->xprice = CC_SAFE_MALLOC (1, CCtsp_xnorm_pricer);
            if (!eg->xprice) {
                fprintf (stderr, "out of memory in CCtsp_init_edgegenerator\n");
                return 1;
            }
            rval = xprice_build (ncount, dg, eg->xprice, silent);
            if (rval) {
                fprintf (stderr, "xprice_build failed\n");
                CC_FREE (eg->xprice, CCtsp_xnorm_pricer);
                return rval;
            }
        }
Esempio n. 4
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;
}
Esempio n. 5
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;
}
Esempio n. 6
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;
}
Esempio n. 7
0
static int verify_problem (char *probname, int silent)
{
    int i;
    int rval;
    int nfail = 0;
    int nsuccess = 0;
    CCtsp_lp *lp = (CCtsp_lp *) NULL;
    CCtsp_lpcuts *cuts;
    CCtsp_lpcut_in cut;
    int ncount = 0;

    lp = CC_SAFE_MALLOC (1, CCtsp_lp);
    if (lp == (CCtsp_lp *) NULL) {
        fprintf (stderr, "Out of memory in verify_problem\n");
        rval = 1; goto CLEANUP;
    }
    CCtsp_init_tsp_lp_struct (lp);
    rval = CCtsp_read_probfile (lp, probname, (char *) NULL, &ncount, silent);
    if (rval) {
        fprintf (stderr, "CCtsp_read_probfile failed\n");
        goto CLEANUP;
    }

    rval = CCtsp_build_lpadj (&lp->graph, 0, lp->graph.ecount);
    if (rval) {
        fprintf (stderr, "CCtsp_build_lpadj failed\n");
        goto CLEANUP;
    }
    
    rval = CCtsp_add_branchhistory_to_lp (lp);
    if (rval) {
        fprintf (stderr, "CCtsp_add_branchhistory_to_lp failed\n");
        goto CLEANUP;
    }

    cuts = &lp->cuts;
    for (i=0; i<cuts->cutcount; i++) {
        if (cuts->cuts[i].branch == 0) {
            rval = CCtsp_lpcut_to_lpcut_in (cuts, &(cuts->cuts[i]), &cut);
            if (rval) {
                fprintf (stderr, "CCtsp_lpcut_to_lpcut_in failed\n");
                goto CLEANUP;
            }
            rval = CCverify_cut (&cut, CC_TYPE_ALL, (int *) NULL);
            if (rval) {
                fprintf (stderr, "CCverify_cut failed\n");
                nfail++;
            } else {
                nsuccess++;
            }
            CCtsp_free_lpcut_in (&cut);
        }
    }

    printf ("%d cuts failed verification, %d passed\n", nfail, nsuccess);

    if (nfail == 0) rval = 0;
    else rval = -1;

  CLEANUP:
    CCtsp_free_tsp_lp_struct (&lp);
    return rval;
}
Esempio n. 8
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;
}