tabu_search


Tabu Search is a Global Optimization algorithm and a Metaheuristic or Meta-strategy for controlling an embedded heuristic technique. Tabu Search is a parent for a large family of derivative approaches that introduce memory structures in Metaheuristics, such as Reactive Tabu Search and Parallel Tabu Search.


CITY_SIZE = 52;

CITIES =  [565,575,25,185,345,750,945,685,845,655,880,660,25,230,525,1000,580,1175,650,1130,1605,620,1220,580 , 1465,200 , 1530,5 , 845,680 , 725,370 , 145,665 ,415,635 , 510,875 , 560,365 , 300,465 , 520,585 , 480,415 ,835,625 , 975,580 , 1215,245 , 1320,315 , 1250,400 , 660,180 ,410,250 , 420,555 , 575,665 , 1150,1160 , 700,580 , 685,595 ,685,610 , 770,610 , 795,645 , 720,635 , 760,650 , 475,960 ,95,260 , 875,920 , 700,500 , 555,815 , 830,485 , 1170,65 ,830,610,605,625,595,360,1340,725,1740,245 ];
CITIES = matrix(CITIES,2,CITY_SIZE);
CITY_SIZE = 52;
CITIES = CITIES';
CITIES = CITIES(1:CITY_SIZE,:)
function y = euc_2d(c1, c2)
    x1 = c1(1) - c2(1);
    x1 = x1 * x1;
    x2 = c1(2) - c2(2);
    x2 = x2 * x2;
    y = round(sqrt( x1 + x2));
endfunction


function distance = cost(perm, cities)
    distance = 0;
    c1 = [1:CITY_SIZE];
    c2 = [2:CITY_SIZE,1];
    for i = 1:CITY_SIZE
        distance = distance + euc_2d(cities(perm(c1(i)),:), cities(perm(c2(i)),:));
    end
endfunction

function perm = random_permutation()
    perm = [1:CITY_SIZE];
    for i = 1:CITY_SIZE
        r = rand(1) * (CITY_SIZE - i) + i;
        r = round(r);
        if r == 0 then
            r = 1;
        end
        tp = perm(i);
        perm(i) = perm(r);
        perm(r) = tp; 
    end 
endfunction


function [perm, edges] = stochastic_two_opt(parent)
    perm =  parent;
    c = round(rand(1,2) * (CITY_SIZE - 2) + 2);
    while c(1) == c(2)
        c = round(rand(1,2) * (CITY_SIZE - 2) + 2);
    end
    c1 = c(1);
    c2 = c(2);
    if c2 < c1 then
        tp = c2;
        c2 = c1;
        c1 = tp;
    end
    
    //perm(c1:c2) = perm(c2:-1:c1);
    tp = perm(c1);
    perm(c1) = perm(c2);
    perm(c2) = tp;
    //edges = [parent(c1-1), parent(c1),parent(c2-1), parent(c2)];
    edges = [ parent(c1),parent(c2)];
endfunction

function ist = is_tabu(permutation, tabu_list)
    sz = size(tabu_list);
    sz = sz(2)
    for i = 1:CITY_SIZE
        c1 = i;
        c2 = i + 1;
        if i == CITY_SIZE then
            c2 = 1;
        end
        for j = 1 : sz
           if permutation(c1) == tabu_list(1,j) & permutation(c2) == tabu_list(2,j) then
               ist = 1;
               return;
            end
        end
    end
    ist = 0;
endfunction


function [candidate, edges] = generate_candidate(best, tabu_list)
    [perm, edges] = stochastic_two_opt(best)
    while is_tabu(perm, tabu_list) == 1 do
        [perm, edges] = stochastic_two_opt(best);
    end
    candidate = perm;
    edges = edges;
endfunction

function [best,tabu_list] = search(cities, tabu_list_size, candidate_list_size, max_iter)
    current = random_permutation();
    best = current;
    tabu_list = [];
    candidates=[];
    edges =[];
    [candidates, edges] = generate_candidate(current, tabu_list);
    for i = 1: max_iter
        for j = 1 : candidate_list_size
            [tp, edges_] = generate_candidate(current, tabu_list);
            if cost(candidates, cities) > cost(tp, cities) then
                disp([cost(candidates, cities) ,cost(tp, cities),i]);
                candidates = tp;
                edges = edges_;
            end
       end
       if cost(candidates, cities) < cost(current, cities) then
           current = candidates;
           if cost(candidates, cities) < cost(best, cities) then
               best = candidates;
           end
           //tabu_list = [tabu_list, [edges(1:2)'],[edges(3:4)']];
           sz = size(tabu_list);
           sz = sz(2);
           has = 0;
           for k = 1:sz
               if edges(1) == tabu_list(1,k) & edges(2) == tabu_list(2,k) then 
                   has = 1;
                   break;
                end
           end
           if has == 0 then 
               tabu_list = [tabu_list, [edges(1:2)']];
           end
           sz = size(tabu_list);
           sz = sz(2);
           if sz > tabu_list_size then
               tabu_list = tabu_list(:, sz - tabu_list_size  + 1: sz);
           end
       end
    end
endfunction

max_iter = 200;
tabu_list_size = 15;
candidate_list_size = 100;
cities = CITIES;
[best,tabu_list] = search(cities, tabu_list_size, candidate_list_size, max_iter);
cost_ = cost(best, cities);
disp(cost_)
plot(cities( [best,best(1)] ,1), cities([best,best(1)],2), '-o');




發佈了146 篇原創文章 · 獲贊 5 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章