fmincon - penalty function
이전 댓글 표시
Hi,
I have the following problem:
I have a data set with half-hourly data for gas production and electricity demand. These are now to be brought together via a CHP (with a certain capacity). The produced gas is to be converted into electricity to cover the electricity demand as well as possible. So I am looking for a power P at each point in time.
To do this, I have calculated the deviation between demand and power in an objective function e and then summed it up.
q = fmincon(@(x) obj_func(x,P,demand,z,P_max,V_level,V_initial,V_size,V_proz,production,BHKW_mode,d),x0,[],[],[],[],lb,ub);
function e = obj_func(x,P,demand,z,P_max,V_level,V_initial,V_size,V_proz,production,BHKW_mode,d)
e = (demand - P).^2;
e = sum(e) + sum(d);
This objective function is then to be minimized with fmincon. This is done and at any time I get : P = demand (why should it not be so).
But now my constraint comes into play.
The gas comes first into a storage and if gas is converted into electricity, the storage content becomes smaller by this quantity.
V_level(1) = V_initial + production(1) - P(1);
for i = 2:z
V_level(i) = V_level(i-1) + production(i) - P(i);
end
V_proz = 100*V_level/V_size;
And this storage may never be fuller than 100 % and never emptier than 0 %. I tried to realize this with a penalty function, which is added to e.
for i = 1:z
if V_proz(i) > 100
d(i) = 100000;
elseif V_proz(i) < 0
d(i) = 100000;
else
d(i) = 0;
end
end
But no matter how big I choose the penalty for non-compliance with this condition: Nothing changes in the result, it is not considered by the optimizer at all....
Does anyone have any idea where my error lies or how I can solve it differently?
Thank you for your help, Matthias
채택된 답변
추가 답변 (1개)
Matt J
2022년 3월 17일
1 개 추천
Problems
(1) The objective function code you've shown appears to depend on everything except the unknown variables x
(2) Assuming V_proz was supposed to be one of the unknowns, your penalty d is a discontinuous function of it.
댓글 수: 10
Matthias K
2022년 3월 17일
편집: Matthias K
2022년 3월 17일
In that case,
(1) fmincon expects your objective to take a single input argument (a vector), not many. If you have multiple unkowns, they are expected to be the elements of the vector.
(2) The objective and constraint functions must be differentiable, and therefore continuous. Therefore discontinuous things like this are illegal,
if V_proz(i) > 100
d(i) = 100000;
elseif V_proz(i) < 0
d(i) = 100000;
else
d(i) = 0;
end
I am not sure why you are using a penalty function instead of an explicit constraint. If V_proz are independent unknowns, why not just use fmincon's lb, ub inputs to bound them?
Matthias K
2022년 3월 17일
I didn't see how I could constrain V_proz via lb and ub.
Then use one of the other constraint inputs, like A,b, or nonlcon. The 'con' in fmincon stands for constrained. If you have no constraints, it makes little sense to be using fmincon - you may as well use fminunc.
I can't really tell from your code which variables are unknowns and which are fixed problem data, but it looks like V_proz is a linear function A*x of x, so linear inequality constraints of the form A*x<=1000, A*x>=0 should be all you need.
Incidentally, another discontinuous operation in your code is this,
idx = find(x < P_min);
x(idx)=0;
It should be removed. Instead, you should just impose the lower bound x>=P_max*0.25 with the lb input argument and increase all production(i) to be at least 0.25*P_max prior to the optimization,
production=max(production,0.25*P_max)
Here, I'm assuming P_max and production(i) are fixed, known problem data.
Matthias K
2022년 3월 17일
편집: Matthias K
2022년 3월 17일
Matthias K
2022년 3월 17일
편집: Matthias K
2022년 3월 17일
Here where you currently have,
idx = find(x < P_min);
x(idx)=0;
Are you sure that this isn't really supposed to be,
idx = find(x < P_min);
x(idx)=P_min;
Otherwise, V_proz will have jump discontinuities near x(i)=P_min.
Matthias K
2022년 3월 18일
카테고리
도움말 센터 및 File Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
