Preallocation within an optimization loop

조회 수: 1 (최근 30일)
Barend Kok
Barend Kok 2020년 11월 24일
댓글: Barend Kok 2020년 11월 24일
Hi everyone,
Bear with me, since I am new to Matlab. But I am doing some monte carlo analysis of an (economic) optimization problem. Which looks as follows:
runs = 1000;
nsamples = 1:runs;
for i = 1:length(nsamples)
% Distributions
price_B = random(dist_price_B);
yield_B = random(dist_yield_B);
seedc_B = random(dist_seedc_B);
fertc_B = random(dist_fertc_B);
herbc_B = random(dist_herbc_B);
prot_cont_B = random(dist_prot_cont_B);
yield_CD = random(dist_yield_CD);
% Intermediates profit function
benefitkg_B = price_B + nit_fix_B;
benefitha_B = yield_B * benefitkg_B + subs_B;
costha_B = seedc_B + fertc_B + herbc_B;
benefitl_CD = price_CD;
benefitha_CD = yield_CD * density_C * benefitl_CD;
benefitkg_CM = price_CM;
benefitha_CM = yield_CM * density_C * benefitkg_CM;
benefitha_C = benefitha_CD + benefitha_CM;
costcow_C = initialc_C + feedc_C + operatc_C;
costha_C = costcow_C * density_C;
% Intermediates protein supply
prot_supply_B = yield_B * prot_cont_B;
prot_supply_C = density_C * (yield_CD * prot_cont_CD + yield_CM * prot_cont_CM);
prot_supply = @(x)(x(1) * prot_supply_B + x(2) * prot_supply_C);
% Profit function, minimizing this is the objective.
Total_Profit = @(x)(-1 * (x(1) * (benefitha_B - costha_B) + x(2) * (benefitha_C - costha_C)));
% Inputs for fmincon.
Guess = [1 0];
LB = [0, 0];
UB = [1, 1];
A = [-prot_supply_B, -prot_supply_C]; % A*x <= b --> protein_supply >= protein_demand --> -protein_supply <= -protein_demand
b = -1000; % -(x(1) * prot_supply_B + x(2) * prot_supply_C) <= b
Aeq = [1, 1]; % x(1) + x(2) = 1
beq = 1;
% Minimization
xopt (i, :) = fmincon(Total_Profit,Guess,A,b,Aeq,beq,LB,UB,[],options);
% Create the value for total profit over the years and plot
Total_Private_Profit (i, :) = - Total_Profit (xopt);
The code runs perfectly and gives me the output I need (I left the plotting of histograms and other non-important stuff out).
A warning tells me to pre-allocate both 'xopt' and 'Total_Private_Profit' to save time. But as I am trying to do so, the outcomes change and become unreasonable.
My question is therefore: How should I preallocate 'xopt' and 'Total_Private_Profit' properly without screwing the outcomes?
Thank you in advance.
Barend

채택된 답변

Matt J
Matt J 2020년 11월 24일
편집: Matt J 2020년 11월 24일
xopt=nan(runs,2); %Pre-allocate
Total_Private_Profit=nan(runs,2); %Pre-allocate
for i = 1:length(nsamples)
...
xopt(i, :) = fmincon(Total_Profit,Guess,A,b,Aeq,beq,LB,UB,[],options);
% Create the value for total profit over the years and plot
Total_Private_Profit(i, :) = -Total_Profit (xopt);
end
  댓글 수: 3
Matt J
Matt J 2020년 11월 24일
편집: Matt J 2020년 11월 24일
zeros and ones would work, too. I prefer nans because there can be no confusion that they represent anything but missing data.
Barend Kok
Barend Kok 2020년 11월 24일
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by