matlab 如何完善这一功能?

vbkedwbf  于 2022-12-13  发布在  Matlab
关注(0)|答案(1)|浏览(145)

问题是有c家公司对p个项目投标。中标者应该集体对客户有最低的成本。每个公司最多可以赢得2个项目。
我已经写了这段代码。它可以工作,但是要花很长时间才能产生结果,而且效率很低。

function FINANCIAL_RESULTS
clear all; clc;
%This Matlab Program aims to select a large number of random combinations,
%filter those with more than two allocations per firm, and select the
%lowest price.

%number of companies
c = 7;

%number of projects
p = 9;

%max number of projects per company
lim = 2;

%upper and lower random limits
a = 1;
b = c;

%Results Matrix: each row represents the bidding price of one firm on all projects
Results = [382200,444050,725200,279250,750800,190200,528150,297700,297700;339040,393420,649520,243960,695760,157960,454550,259700,256980;388032,499002,721216,9999999,773184,204114,512148,293608,300934;385220,453130,737860,287480,9999999,188960,506690,274260,285670;351600,9999999,9999999,276150,722400,9999999,484150,266000,281400;404776,476444,722540,311634,778424,210776,521520,413130,442160;333400,403810,614720,232200,656140,165660,9999999,274180,274180];

Output = zeros(1,p+1);

n=1;
i=1;
for i = 1:10000000
    rndm = round(a + (b-a).*rand(1,p));
    %random checker with the criteria (max 2 allocations)
    Check = tabulate(rndm);
    if max(Check(:,2)) > lim
        continue
    end
    
    
    
    Output(n,1:end-1) = rndm;
    %Cumulative addition of random results
    for k = 1:p
        Output(n,end) = Output(n,end) + Results(rndm(k),k);
    end
    
    n = n+1;    
  
end

disp(Results);
[Min_pay,Indx] = min(Output(:,end));
disp(Output(Indx,:));

%You know the program is done when Handel plays
load handel
sound(y,Fs);
%Done !
end
knpiaxh1

knpiaxh11#

由于第一维度远大于第二维度,因此沿着第二维度执行循环将更有效:

i = 10000000;
rndm = round(a + (b-a) .* rand(i, p));
Check = zeros(size(rndm, 1), 1);
for k = 1:p
    Check = max(Check, sum(rndm == k, 2));
end
rndm = rndm(Check <= lim, :);
OutputEnd = zeros(size(rndm, 1), 1);
for k = 1:p
    OutputEnd = OutputEnd + Results(rndm(:, k), k);
end
Output = [rndm OutputEnd];

请注意,如果计算的内存有限,请将上述代码放入循环中,并将迭代的结果连接起来,以生成最终结果:

n = 10;
Outputc = cell(n, 1);
for j = 1:n
    i = 1000000;
    ....
    Outputc{j} = Output;
end
Output = cat(1, Outputc{:});

相关问题