Fmincon gives relative maximum constraint violation but still gives output due to conflicting constraints.

15 views (last 30 days)
Hi again,
When finalizing my Monte Carlo optimization model, I encounter the following problem which I am not able to overcome with the resources at hand.
First I will show the code that I am using:
% Set number of runs
runs = 100;
% Pre-allocation to diminish running time.
xopt =nan(runs,2);
Total_Private_Profit =nan(runs,1);
Protein_Supply =nan(runs,1);
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 = [0 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 = -1500; % -(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);
Protein_Supply (i, :) = prot_supply(xopt);
This piece of code optimizes the total profit for a private decision maker. The problem lies within the constraints. I know that any allocation of x(1) and x(2) is not able to fulfill the nonlinear constraint. And I get the following (logical) message in my command window for many of the runs:
Optimization stopped because the relative changes in all elements of x are
less than options.StepTolerance = 1.000000e-10, but the relative maximum constraint
violation, 5.665981e-01, exceeds options.ConstraintTolerance = 1.000000e-06.
The fmincon still assigns values to x(1) and x(2) that optimize the profit, even though it knows that the inequality constraint is not met. I believe this is due to the fact that x(1) + x(2) should be equal to one. And the fmincon function still wants to fulfill this constraint. The model does its work properly when the constraint is set to achievable limits.
My question is, how do I properly feed the model the command that it should not give me outputs when the inequality constraint is not met? In other words, how can I tell the model to allocate zeros to x(1) and x(2) if A*x <= b does not hold and therefore ignore the equality constraint?
Thanks in advance once again.
Barend

Accepted Answer

Matt J
Matt J on 25 Nov 2020
For example,
[tmp,fval,exitflag] = fmincon(Total_Profit,Guess,A,b,Aeq,beq,LB,UB,[],options);
if exitflag>0
xopt(i,:)=tmp;
end

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!