示例#1
0
    PopulationMap<Point,Class>
        IteratedLocalSearch(const PopulationMap<Point,Class>& map,
                            int iterations) {

    const int local_search_its = 100;

    PopulationMap<Point,Class> initial_solution(map);

    PopulationMap<Point,Class> best_solution = LocalSearchFirstFound(initial_solution, local_search_its);
    float curr_quality = best_solution.EvaluateQuality();

    repeat (iterations) {

        PopulationMap<Point,Class> perturbated_solution(best_solution);
        perturbated_solution.reset();

        // TODO: add diversification if the solution is not improving a lot (define 'a lot')
        perturbated_solution.NeighborhoodOperator(false);

        const PopulationMap<Point,Class>& candidate_solution = LocalSearchFirstFound<Point, Class>(perturbated_solution, local_search_its);
        float candidate_quality = candidate_solution.EvaluateQuality();


        // If the quality is better than the previous map, we found a new map
        if (candidate_quality > curr_quality) {
            best_solution = candidate_solution;
            curr_quality  = candidate_quality;
        }
    }

    return best_solution;
}
示例#2
0
Graph* tabu_search(Graph* graph, int tabu_len, int candidates_len, int max_iter, 
        int* colors){
    Graph* best = initial_solution(graph);
    int best_cost = cost(best);

    int new_best_cost = best_cost - 1;
    Graph* s = reduce_color(best, new_best_cost);

    int conflict_c;
    char** conflict_vt = conflict_vertices(s, &conflict_c);

    int tabu_pos = 0;
    tabu_t** tabu_list = (tabu_t**) malloc(sizeof(tabu_t*) * candidates_len 
            * graph->iterator_size);

    int iter = 0;

    while (iter < max_iter){
        Graph** candidates = (Graph**) malloc(sizeof(Graph*) * candidates_len);
        
        if (conflict_c > 0){
            for (int i = 0; i < candidates_len; i++){
                candidates[i] = generate_candidates(s, conflict_vt, conflict_c,
                        tabu_list, &tabu_pos, new_best_cost);
            }

            int conflict_cand_c;
            Graph* best_candidate = get_best_candidate(candidates, candidates_len, 
                    conflict_vt, conflict_c, &conflict_cand_c);

            if (conflict_c > conflict_cand_c){
                free_graph(s);
                free(conflict_vt);
                s = best_candidate;
                conflict_vt = conflict_vertices(s, &conflict_c);
            }
            else if (best_candidate != NULL)
                free_graph(best_candidate);
        }

        if (conflict_c == 0){
            best_cost = new_best_cost;
            free_graph(best);
            best = s;

            new_best_cost = best_cost - 1;
            s = reduce_color(best, new_best_cost);
            free(conflict_vt);
            conflict_vt = conflict_vertices(s, &conflict_c);
        }

        if (tabu_pos > tabu_len)
            update_tabu_list(tabu_list, &tabu_pos, tabu_len);

        free(candidates);
        iter++;
    }

    free(conflict_vt);
    free(tabu_list);
    if (colors != NULL)
        *colors = best_cost;

    return best;
}