在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
指派授课问题 现有A、B、C、D四门课程,需由甲、乙、丙、丁四人讲授,并且规定: 每人只讲且必须讲1门课;每门课必须且只需1人讲。 四人分别讲每门课的费用示于表中:
带包python代码: from scipy.optimize import linear_sum_assignment import numpy as np #cost =np.array([[4,1,3],[2,0,5],[3,2,2]]) cost =np.array([ [2,10,9,7], [15,4,14,8], [13,14,16,11], [4,15,13,9]]) row_ind,col_ind=linear_sum_assignment(cost) print(row_ind)#开销矩阵对应的行索引 print(col_ind)#对应行索引的最优指派的列索引 print(cost[row_ind,col_ind])#提取每个行索引的最优指派列索引所在的元素,形成数组 print(cost[row_ind,col_ind].sum())#数组求和 #输出指派矩阵 p = np.zeros((4,4)) p[row_ind,col_ind]=1 print(p)
暴力python代码: # -*- coding: utf-8 -*- import numpy as np import copy c=[2,10,9,7, 15,4,14,8, 13,14,16,11, 4,15,13 ,9 ] c = np.array(c) c = c.reshape((4,4)) all_p=[] class obj: def _init_(self): self.p=[] self.cost=0 for i in range(4): for j in range(4): if j==i: continue for u in range(4): if u==i or u==j : continue for v in range(4): if v==i or v==j or v==u: continue p = np.zeros((4,4)) p[0,i]=p[1,j]=p[2,u]=p[3,v]=1 ans = obj() ans.p = copy.deepcopy(p) ans.cost = sum(sum(c*ans.p)) all_p.append(ans) all_p.sort(key=lambda ans: ans.cost, reverse=False) print(all_p[0].p) print(all_p[0].cost)
我写的matlab:
clear C=[2 10 9 7 15 4 14 8 13 14 16 11 4 15 13 9]; A = perms(1:4);%perm显示1,2,3,4四个数的全排列 L = length(A) best=999 best_mat=[] for i=1:L a = zeros(4,4); b = A(i,:);%遍历全排列中的每一种 c = 1:4; a(sub2ind(size(a), b, c))=1;%a矩阵指定的位置赋值为1 s = sum(sum(a.*C));%求出费用和 if best>s %挑出最大的 best_mat=a; best=s; end end best_mat best
老师的matlab代码1: clear n=4; A=perms(1:n); G=size(A); %24 4 size(A,1) 24 size(A,2) 24 n0=G(1); %24 B=[2,10,9,7;15,4,14,8;13,14,16,11;4,15,13,9]; for n1=1:n0 %C为第n1中排列情况下,费用的4个取值 C(1)=B(1,A(n1,1));C(2)=B(2,A(n1,2)); C(3)=B(3,A(n1,3));C(4)=B(4,A(n1,4)); %D{n1}表示第n1种情况下的4个取值 D{n1}=[C(1),C(2),C(3),C(4)]; s(n1)=sum(D{n1}); end %找到最小的,返回a为行左边,b为纵坐标,a=1,b=9 [a,b]=find(s==min(s)); K=A(b,:)
根据老师的代码改进我的代码: clear C=[2 10 9 7 15 4 14 8 13 14 16 11 4 15 13 9]; A = perms(1:4);%perm显示1,2,3,4四个数的全排列 L = length(A) for i=1:L a = zeros(4,4); b = A(i,:);%遍历全排列中的每一种 c = 1:4; a(sub2ind(size(a), b, c))=1;%a矩阵指定的位置赋值为1 D{i}=a; S(i)=sum(sum(a.*C));%求出费用和 end [a,b]=find(S==min(S)) D{b} S(b)
老师的matlab代码2:随机生成,不是很好,看运气 clear A=[2 15 13 4];B=[10 4 14 15];C=[9 14 16 13];D=[7 8 11 9]; Y=zeros(1,1000);s=64;x=zeros(1,4); for i= 1:1000 X=randperm(4); Y(i)=A(X(1))+B(X(2))+C(X(3))+D(X(4)); if Y(i)<s s=Y(i); x=X; end end s,x
网上常见的matlab代码: %适用于任意n阶系数矩阵 clear all; C=[2 10 9 7, 15 4 14 8, 13 14 16 11, 4 15 13 9, ];%效率矩阵C n=size(C,1);%计算C的行列数n C=C(:);%计算目标函数系数,将矩阵C按列排成一个列向量即可。 A=[];B=[];%没有不等式约束 Ae=zeros(2*n,n^2);%计算等约束的系数矩阵a for i=1:n for j=(i-1)*n+1:n*i Ae(i,j)=1; end for k=i:n:n^2 Ae(n+i,k)=1; end end Be=ones(2*n,1);%等式约束右端项b Xm=zeros(n^2,1);%决策变量下界Xm XM=ones(n^2,1);%决策变量上界XM [x,z]=linprog(C,A,B,Ae,Be,Xm,XM);%使用linprog求解 x=reshape(x,n,n);%将列向量x按列排成一个n阶方阵 disp('最优解矩阵为:');%输出指派方案和最优值 Assignment=round(x)%使用round进行四舍五入取整 disp('最优解为:'); z
线性规划matlab代码: %线性规划 c=[2,10,9,7,15,4,14,8,13,14,16,11,4,15,13,9]; Aeq=[1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0; 0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0; 0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1; 1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0; 0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0; 0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0; 0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1]; beq=[1,1,1,1,1,1,1,1]; lb=zeros(16,1); ub=ones(16,1); [x,fval] = linprog(c,[],[],Aeq,beq,lb,ub)
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论