遗传算法求解0/1背包问题

轮班赌算法选择choose.m

function [wh] = choose(sumvalue,num)
    sum = 0;
    for i = 1:num
        sum = sum + sumvalue(i);
    end
    min_value = min(sumvalue);
    min_value = min_value(1);
    o = randi(sum-min_value*num);
    sum = 0;
    for i = 1:num
        sum = sum + sumvalue(i)-min_value;
        if o <= sum
            wh = i;      
            break;
        end
    end
end


按排序选择choose_by_rank.m(同旅行商问题)

function [wh] = choose_by_rank(num,base)
    sum = num*num;
    R = randi(sum);
    now = 1;
    wh = 1;
    now_sum = 1;
    while now_sum < R
        now = now + 2;
        now_sum = now_sum + now;
        wh = wh + 1;
    end
    wh = base+wh;
end


交叉cross.m

function [t1,t2,t1_chose,t2_chose] = cross(t_input1,t_input2,num)
    w1 = randi(num);
    w2 = randi(num);
   % w1 = 6;
   % w2 = 4;
    if w1 > w2
        temp=w1;w1=w2;w2=temp;
    end
    visit = zeros(2,num);
    t_input = [t_input1;t_input2];
    t_output = zeros(2,num);
    t_chose = zeros(2,num);
    for i = 1:2
        other = 3-i;
        for j = w1:w2
            t_output(i,j) = t_input(other,j,1);
            visit(i,t_output(i,j)) = j;
            t_chose(i,j) = t_input(other,j,2);
        end
        j = w2+1;
        if j > num
            j = 1;
        end
        while j~=w1
            now = t_input(i,j,1);
            while visit(i,now)>0
                now = t_input(i,visit(i,now),1);
            end
            t_output(i,j) = now;
            t_chose(i,j) = t_input(i,j,2);
            j = j+1;
            if j > num
                j=1;
            end
        end
    end
    t1 = t_output(1,:);
    t2 = t_output(2,:);
    t1_chose = t_chose(1,:);
    t2_chose = t_chose(2,:);
end


生成选择数组make_choose_vector.m

function [t] = make_choose_vector(num)
    t =  zeros(1,num);
    for i = 1:num
        t(i) = randi(2) - 1;
    end
end


生成不超过背包总容量的可行解make_legal.m

function [t_output, t_sum_value] = make_legal(t_input,num)
    sum=0;
    t_sum_value = 0;
    global w;
    global v;
    global sum_m;
    global opt_value;
    global opt_path;
    t_output = t_input;
    remain = [];
    for i = 1:num
       % t_input(1,i,1)
        sum = sum + w(t_input(1,i,1)) * t_output(1,i,2);
        if sum > sum_m
            sum = sum - w(t_input(1,i,1)) * t_output(1,i,2);
            t_output(1,i,2) = 0;
        end
        if t_output(1,i,2) == 0
            remain = [remain;i];
        end
        t_sum_value = t_sum_value + v(t_input(1,i,1)) * t_output(1,i,2);
    end
    if sum < sum_m
        remainIndex = randperm(size(remain,1));
        remain = remain(remainIndex);
        for i = 1:size(remain,1)
            o = remain(i);
            if (sum + w(t_input(1,o,1)) <= sum_m)
                t_sum_value = t_sum_value + v(t_input(1,o,1));
                t_output(1,o,2) = 1;
                sum = sum + w(t_input(1,o,1));
                if (sum == sum_m)
                    break;
                end
            end
        end
    end
    if opt_value < t_sum_value
        opt_path = t_output(1,:,:);
        opt_value = t_sum_value;
    end
end


生成初始解makeinitsolve.m

function [t] = makeinitsolve(num)
    t = zeros(num,2);
    t(:,1) = randperm(num);
    for i = 1:num
        t(i,2) = randi(2)-1;
    end
end


按权值选择主程序bag01_value.m

fid = fopen('data5.txt','r');
global sum_m;
sum_m = fscanf(fid,"%d",1);
n = fscanf(fid,"%d",1);
global w;
w = zeros(1,n);
global v;
v = zeros(1,n);
global opt_value;
opt_value=0;
global opt_path;
opt_path = [];
for i = 1:n
  w(i) = fscanf(fid,"%d",1);  
  v(i) = fscanf(fid,"%d",1);  
end
fclose(fid);
numgeti = 900;
numshidai = 900;
a = zeros(numgeti,n,2);
sum_value = zeros(numgeti);
for i = 1:numgeti
    a(i,:,:) = makeinitsolve(n);
    [a(i,:,:),sum_value(i)] = make_legal(a(i,:,:),n); 
end
fid = fopen('算法过程.txt','w');
for shidailoop = 1:numshidai
    fprintf(fid,"世代:%g\n",shidailoop);
    b = [];
    b_sum_value = zeros(numgeti);
    for zuloop = 1:fix(numgeti/2)
        wh_father = choose(sum_value,numgeti);
        wh_mother = choose(sum_value,numgeti);
        first = zuloop*2-1;
        second = first + 1;
        a(wh_father,:,1);
        a(wh_mother,:,1);
        [t1,t2,t1_chose,t2_chose] = cross(a(wh_father,:,:),a(wh_mother,:,:),n);
        b(first,:,1) = t1;
        b(first,:,2) = t1_chose;
       % b(first,:,2) = a(wh_father,:,2);
        b(second,:,1) = t2;
        b(second,:,2) = t2_chose;
      %  b(second,:,2) = a(wh_mother,:,2);
        [b(first,:,:),b_sum_value(first)] = make_legal(b(first,:,:),n);
        [b(second,:,:),b_sum_value(second)] = make_legal(b(second,:,:),n);
        fprintf(fid,"个体:%g\t",first);
        fprintf(fid,"父亲:%g\t",wh_father);
        fprintf(fid,"母亲:%g\t",wh_mother);
        fprintf(fid,"原方案:\t");
        fprintf(fid,"%g\t",a(first,:,:));
        fprintf(fid,"原总价值:%g\t",sum_value(first));
        fprintf(fid,"现方案:\t");
        fprintf(fid,"%g\t",b(first,:,:));
        fprintf(fid,"现总价值:%g\t",b_sum_value(first));
        fprintf(fid,"\n");
        fprintf(fid,"个体:%g\t",second);
        fprintf(fid,"父亲:%g\t",wh_father);
        fprintf(fid,"母亲:%g\t",wh_mother);
        fprintf(fid,"原方案:\t");
        fprintf(fid,"%g\t",a(second,:,:));
        fprintf(fid,"原总价值:%g\t",sum_value(second));
        fprintf(fid,"现方案:\t");
        fprintf(fid,"%g\t",b(second,:,:));
        fprintf(fid,"现总价值:%g\t",b_sum_value(second));
        fprintf(fid,"\n");
    end
    a=b;
    ma = mean(sum_value);
    mb = mean(b_sum_value);
    fprintf(fid,"世代%d平均价值:%g=>%g\n",shidailoop,ma(1),mb(1));
    sum_value = b_sum_value;
end
fclose(fid);
opt_value
opt_path
sum_v = 0;
sum_w = 0;
for i = 1:size(opt_path,2)
    if opt_path(1,i,2) == 1
        sum_v = sum_v+v(opt_path(1,i,1));
        sum_w = sum_w+w(opt_path(1,i,1));
    end
end
sum_v
sum_w

按排名选择主程序bag01_rank.m

fid = fopen('data1.txt','r');
global sum_m;
sum_m = fscanf(fid,"%d",1);
n = fscanf(fid,"%d",1);
global w;
w = zeros(1,n);
global v;
v = zeros(1,n);
global opt_value;
opt_value=0;
global opt_path;
opt_path = [];
for i = 1:n
  w(i) = fscanf(fid,"%d",1);  
  v(i) = fscanf(fid,"%d",1);  
end
fclose(fid);
numgeti = 100;
numshidai = 100;
a = zeros(numgeti,n,2);
sum_value = zeros(numgeti);
for i = 1:numgeti
    a(i,:,:) = makeinitsolve(n);
    [a(i,:,:),sum_value(i)] = make_legal(a(i,:,:),n); 
end
fid = fopen('算法过程.txt','w');
[E,I] = sort(sum_value);
for shidailoop = 1:numshidai
    fprintf(fid,"世代:%g\n",shidailoop);
    b = [];
    old_sum_value = sum_value;
    I_index = zeros(1,numgeti);
    for i = 1:numgeti
        I_index(I(i)) = numgeti-i+1;
    end
    for zuloop = 1:fix(numgeti/2)
        wh_father = choose_by_rank(fix(numgeti*0.6),fix(numgeti*0.4));
        wh_mother = choose_by_rank(fix(numgeti*0.6),fix(numgeti*0.4));
        first = zuloop*2-1;
        second = first + 1;
       % a(wh_father,:,1);
       % a(wh_mother,:,1);
        [t1,t2,t1_chose,t2_chose] = cross(a(I(wh_father),:,:),a(I(wh_mother),:,:),n);
        b(first,:,1) = t1;
        b(first,:,2) = t1_chose;
       % b(first,:,2) = a(wh_father,:,2);
        b(second,:,1) = t2;
        b(second,:,2) = t2_chose;
      %  b(second,:,2) = a(wh_mother,:,2);
        [b(first,:,:),sum_value(first)] = make_legal(b(first,:,:),n);
        [b(second,:,:),sum_value(second)] = make_legal(b(second,:,:),n);
        fprintf(fid,"个体序号:%g\t",first);
        fprintf(fid,"排名:%g\t",I_index(first));
        fprintf(fid,"父亲排名:%g\t",numgeti-wh_father+1);
        fprintf(fid,"父亲序号:%g\t",I(wh_father));
        fprintf(fid,"母亲排名:%g\t",numgeti-wh_mother+1);
        fprintf(fid,"母亲序号:%g\t",I(wh_mother));
        fprintf(fid,"原方案:\t");
        fprintf(fid,"%g\t",a(first,:,:));
        fprintf(fid,"原总价值:%g\t",old_sum_value(first));
        fprintf(fid,"现方案:\t");
        fprintf(fid,"%g\t",b(first,:,:));
        fprintf(fid,"现总价值:%g\t",sum_value(first));
        fprintf(fid,"\n");
        fprintf(fid,"个体序号:%g\t",second);
        fprintf(fid,"排名:%g\t",I_index(second));
        fprintf(fid,"父亲排名:%g\t",numgeti-wh_father+1);
        fprintf(fid,"父亲序号:%g\t",I(wh_father));
        fprintf(fid,"母亲排名:%g\t",numgeti-wh_mother+1);
        fprintf(fid,"母亲序号:%g\t",I(wh_mother));
        fprintf(fid,"原方案:\t");
        fprintf(fid,"%g\t",a(second,:,:));
        fprintf(fid,"原总价值:%g\t",old_sum_value(second));
        fprintf(fid,"现方案:\t");
        fprintf(fid,"%g\t",b(second,:,:));
        fprintf(fid,"现总价值:%g\t",sum_value(second));
        fprintf(fid,"\n");
    end
    a=b;
    ma = mean(old_sum_value);
    mb = mean(sum_value);
    fprintf(fid,"世代%d平均价值:%g=>%g\n",shidailoop,ma(1),mb(1));
    [E,I] = sort(sum_value);
end
fclose(fid);
opt_value
opt_path
sum_v = 0;
sum_w = 0;
for i = 1:size(opt_path,2)
    if opt_path(1,i,2) == 1
        sum_v = sum_v+v(opt_path(1,i,1));
        sum_w = sum_w+w(opt_path(1,i,1));
    end
end
sum_v
sum_w
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值