延迟接受算法简介与matlab实现

延迟接受算法概念

盖尔-沙普利算法(Gale-Shapley algorithm)简称 “GS算法”,也称为 “延迟接受算法”(deferred-acceptance algorithm),是盖尔和沙普利为了寻找一个稳定匹配而设计出的市场机制。市场一方的对象 Ai,i=1,2,…,m 向另一方的对象 Bj,j=1,2,…,n 发出邀约,每个 Bj 会对接到的邀约进行比较,保留自己认为最好的,拒绝其它的。邀约被拒绝的 Ai 继续向其它的 Bj 发出新的邀约,直到没有 Ai 希望再发出邀约为止。此时各 Bj 才最终接受各自保留的邀约。该算法的一个关键之处在于,合意的邀约不会立即被接受,而只是暂时保留不被拒绝,也就是 “延迟接受”。

简单来说DA算法就是用来进行匹配的,而且匹配后的结果是很稳定的。废话少说直接上代码。

  1. 伪代码
Here we assume that the men
 are the suitors and the women the suitees.
 
 procedure stable(M1, M2,...,Ms, W1, W2,...,Ws:
 preference lists)
 for i := 1 to s
 	mark man i as rejected
 for i := 1 to s
 	set man i’s rejection list to be empty
 for j := 1 to s
 	set woman j ’s proposal list to be empty
 
 while rejected men remain
 for i := 1 to s
 	if man i is marked rejected then add i to the
 	proposal list for the woman j who ranks highest
	on his preference list but does not appear on his
 	rejection list, and mark i as not rejected
 for j := 1 to s
 	if woman j ’s proposal list is nonempty then
 	remove from j ’s proposal list all men i
 	except the man i0 who ranks highest on her
 	preference list, and for each such man i mark
 	him as rejected and add j to his rejection list
 
 for j := 1 to s
 	match j with the one man on j ’s proposal list
 {This matching is stable.}

  1. matlab代码
function [pairing_vec,conv_vec,time,sta]=DAalg(PL_matr1,PL_matr2)
%time--叠代次数;sta--稳定性

[m1,n1] = size(PL_matr1);%woman
[m2,n2] = size(PL_matr2);%man
pairing_vec = zeros(m1,m2);%初始化配对结果

number_min = min(m1,m2);%求两个列表中的较小值,求人数少的一方
%number_max = max(m1,m2);%求两个列表中的较大值

% man_flag = zeros(1,m2);%标记mani是否配对
% woman_flag = zeros(1,m1);%标记womanj是否配对

man_reject = ones(1,m2);%标记man被拒绝
man_rejection_list = zeros(m2,n2);%清空man的拒绝列表
woman_proposal_list = zeros(m1,n1);%清空woman的可选列表

% index = 0;index1 = 0;index2 = 0;
num = 1;

while( number_min > nozero(pairing_vec) )%人数少的一方的数目不等于配对矩阵中的非零数目时进行循环配对
    
%index1 = 0;index2 = 0;
for i = 1:m2   
    mark_reject = man_reject(1,i);
    index1 = 0;%index2 = 0;
    if (mark_reject == 1) %&&(man_flag(1,i) == 0)) %如果mani被拒绝       
        for j = 1:m1
            if (man_rejection_list(i,j) == 0) %&&(woman_flag(1,j) == 0))%如果womanj未拒绝mani
                index = PL_matr2(i,j); % 统计mani对womanj的喜欢程度                
                if index > index1
                    index1 = index;
                    %index2 = j;                    
                end 
                man_reject(1,i) = 0; %标记mani为未拒绝
                %break;
            end          
        end
        for j = 1:m1
            %if woman_flag(1,j) == 0
                if PL_matr2(i,j) == index1
                    woman_proposal_list(j,i) = 1;%找到对mani喜欢程度最大的womanj,并将mani加入womanj的可选列        
                    %man_reject(1,i) = 0; %标记mani为未拒绝
                    %break;
                else
                    continue;
                end
            %end
        end
    else
      continue;  
    end    
end %生成woman_proposal_list

%index1 = 0;index2 = 0;
for j = 1:m1
    woman_proposal = sum(woman_proposal_list(j,:));
    index1 = 0 ;%index2 = 0 ;
    if (woman_proposal ~= 0)%&&(woman_flag(1,j) == 0)) %如果woman的可选列表大于0,即有可选对象        
        for i = 1:m2
            %if man_flag(1,i) == 0
                if (woman_proposal_list(j,i) == 1)  
                    index = PL_matr1(j,i); % 统计mani对womanj的喜欢程度            
                    if (index > index1) %&& (man_flag(1,i) == 0))
                    index1 = index;
                    %index2 = i;
                    end  %找到对womanj喜欢程度最大的mani
                end
            %end
        end
        for i = 1:m2
            %if man_flag(1,i) == 0
                if (woman_proposal_list(j,i) == 1)
                    if (PL_matr1(j,i) ~= index1)%将对womanj除了喜欢程度最大的man之外的所有man进行移除
                        woman_proposal_list(j,i) = 0; %从选择列表中移除
                        man_reject(1,i) = 1;%标记为被拒绝
                        man_rejection_list(i,j) = 1;%加入被拒绝列表                       
                    else
                         continue;
                    end                   
                else
                    continue;
                end
%             else
%                 continue;
%             end
        end
    else
        continue;
    end
    
    woman_proposal = sum(woman_proposal_list(j,:));
    index3 = 0;
    index4 = 0;
    if(woman_proposal > 1)%女生的可选列表不止有一个,但是此时只能保留一个
        for i = 1:m2
            if (woman_proposal_list(j,i) == 1)
                index2 = PL_matr1(j,i);
                if index2 > index3
                    index3 = index2 ;
                    index4 = i;                       
                end                   
            else
                continue;
            end
        end
        for i = 1:m2
            if(i ~= index4)
                woman_proposal_list(j,i) = 0; %从选择列表中移除
                man_reject(1,i) = 1;%标记为被拒绝
                man_rejection_list(i,j) = 1;%加入被拒绝列表
            end
        end
    end
end   %保证woman_proposal_list每一行只有一个1

% for i = 1:m2
%     man_proposal = sum(woman_proposal_list(:,i));
%     index5 = 0;
%     if man_proposal > 1
%         for j = 1:m1
%             if woman_proposal_list(j,i) == 1
%                 index6 = PL_matr2(i,j);
%                 if index6 > index5
%                     index5 = index6;
%                     index7 = j;
%                 end
%             end
%         end
%         for j = 1:m1
%             if  woman_proposal_list(j,i) == 1
%                 if j ~= index7
%                     woman_proposal_list(j,i) = 0;
%                     
%                 end
%             end
%         end
%     end    
% end   %保证woman_proposal_list每一列只有一个1


% for i = 1:m2
%     for j = 1:m1
%         %if (woman_proposal_list(j,i) == 1) %&&(man_flag(1,i) == 0)&&(woman_flag(1,j) == 0)
%             pairing_vec(j,i) = woman_proposal_list(j,i); %womanj与mani配对成功
%             %man_reject(1,i) = 0;
%             %woman_flag(1,j) = 1;
%         %end
%     end
% end

pairing_vec = woman_proposal_list;%%进行配对


%man_rejection_list = zeros(m2,n2);%清空man的拒绝列表
%woman_proposal_list = zeros(m1,n1);%清空woman的可选列表

conv_vec = pairing_vec;
% 
 num = num+1;
% 
% fprintf('匹配%d次:\n',num-1);
% 
% [row,col]=size(conv_vec);
% fprintf('conv_vec= \n');
% for i=1:row
%     fprintf('           ');
%     for j=1:col
%         fprintf('%d     ',conv_vec(i,j));%直接输出到屏幕;类似于C语言的输出格式??
%     end
%     fprintf(' \n');
% end

% x(num-1) = num-1;
% y(num-1) = nozero(pairing_vec);

%firgue1(1,num) = nozero(pairing_vec);


% if firgue(1,num - 1) == firgue(1,num)
%     for j = 1:m2
%         for i = 1:m1
%             if man_flag(1,i) == 0 && woman_flag(1,j) == 0
%                 man_rejection_list(i,j) = 0;
%             end
%         end
%     end
% end

end

time = num;
sta1 = 0;
sta2 = 0;
for i = 1:m1
    for j = 1:m2
        if (woman_proposal_list(i,j) == 1) 
            sta1 = sta1+woman_proposal_list(i,j)*PL_matr1(i,j);
        end
    end
end
for i = 1:m2
    for j = 1:m1
        if (woman_proposal_list(i,j) == 1) 
            sta2 = sta2+woman_proposal_list(i,j)*PL_matr2(i,j);
        end
    end
end

sta = sta1 + sta2;
% sta = sum(sum(PL_matr1*woman_proposal_list))+sum(sum(PL_matr2*(woman_proposal_list')));
% plot(x,y);

function result=nozero(A) %求矩阵中非零个数的函数
    
B = (A~=0);

result=sum(B(:));
   
end


end
  1. 实现结果
    我们先使用这段代码随机生成两个6X6的偏好矩阵
Num = 6;
 M = zeros(Num,Num);
 F = zeros(Num,Num);
 for i=1:Num
     M(i,:) = randperm(Num);
     F(i,:) = randperm(Num);
 end

生成的两个矩阵分别如下
在这里插入图片描述
在这里插入图片描述

再运行

[pairing_vec,conv_vec,time,sta]=DAalg(M,F)

得到结果如下
在这里插入图片描述

好了到这里就结束了,如有问题请多指正。

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
最大延迟优先算法(Maximum Delay First,MDF)是一种在通信网络中用于传输数据分组的调度算法。当网络中存在不同的数据流并且每个数据流有不同的延迟要求时,MDF算法可以根据每个数据流的最大延迟要求进行调度,以保证在最长延迟要求内传输数据分组。 以下是一个简单的MATLAB代码实现MDF算法的示例: ```matlab function [schedule] = MDFAlgorithm(dataFlows, linkDataRates, linkDelays) numFlows = length(dataFlows); numLinks = length(linkDataRates); delays = zeros(numFlows, numLinks); % 计算每个数据流在每个链路上的延迟 for i = 1:numFlows for j = 1:numLinks delays(i, j) = dataFlows(i) / linkDataRates(j) + linkDelays(j); end end % 按照延迟升序排序 [~, index] = sort(delays(:)); % 生成调度顺序 schedule = zeros(size(index)); for i = 1:length(index) [flow, link] = ind2sub(size(delays), index(i)); schedule(i) = link; end end ``` 上述代码中,输入参数包括数据流的需求率(dataFlows)、链路的传输速率(linkDataRates)和链路的传输延迟(linkDelays)。函数首先创建一个矩阵`delays`,用于存储每个数据流在每个链路上的延迟。 然后,通过两个嵌套的for循环,计算每个数据流在每个链路上的延迟,并存储在`delays`矩阵中。 接下来,使用`sort`函数将`delays`矩阵中的延迟值升序排序,并将排序后的索引存储在`index`向量中。 最后,通过一个循环遍历`index`向量,根据索引值将链路编号存储在`schedule`向量中,从而生成最终的调度顺序。 需要注意的是,以上代码仅为MDF算法的简化实现,没有处理并发传输和链路资源冲突等情况。实际应用中,还需要根据具体的网络条件和需求进行进一步的算法优化和改进。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值