Preallocation within an optimization loop

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Matt J
Matt J il 24 Nov 2020
Modificato: Matt J il 24 Nov 2020
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 Commenti
Matt J
Matt J il 24 Nov 2020
Modificato: Matt J il 24 Nov 2020
zeros and ones would work, too. I prefer nans because there can be no confusion that they represent anything but missing data.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Develop Apps Using App Designer in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by