How to define constraint in Optimization such that difference in value between two consecutive unknowns is not greater than 50%
이전 댓글 표시
Hello everyone,
I have prepared code in matlab for genetic algorithm from toolbox
The function code is a bit long. In summary, There are 30 unknowns with upper and lower bounds as 1.
How to apply a constraint while running such that difference in value between two consecutive unknowns obtained is not greater than 50% ?.
댓글 수: 2
Ankur Shah
2021년 10월 6일
답변 (2개)
Here is a way to set up the constraint matrices using prob2matrices from,
x=optimvar('x',30,'Lower',0,'Upper',1);
Constraints.diffUB=diff(x)<=+0.5*x(1:end-1);
Constraints.diffLB=diff(x)>=-0.5*x(1:end-1);
p=prob2matrices({x},'Constraints',Constraints)
댓글 수: 6
Ankur Shah
2021년 10월 6일
Matt J
2021년 10월 6일
Yes, it is a linear inequality constraint of the form Aineq*x<=bineq. The matrices Aineq, bineq are given in my solution above.
Ankur Shah
2021년 10월 6일
You don't want to implement the constraint using the nonlinear constraint function inputs. That will make it unecessarily harder for the solver. The constraint as you stated it is,
|x(i+1)-x(i)|<=0.5*x(i)
which is equivalent to linear inequalities,
-0.5*x(i) <= x(i+1)-x(i) <= 0.5*x(i)
or,
0.5*x(i)-x(i+1)<=0
-1.5*x(i)+x(i+1)<=0
Ankur Shah
2021년 10월 6일
The code in my original answer generates the matrices for you. You can use them directly in the call to ga:
x=optimvar('x',30,'Lower',0,'Upper',1);
Constraints.diffUB=diff(x)<=+0.5*x(1:end-1);
Constraints.diffLB=diff(x)>=-0.5*x(1:end-1);
p=prob2matrices({x},'Constraints',Constraints);
x=ga(fun,30,p.Aineq,p.bineq,p.Aeq,p.beq,p.lb,p.ub);
Bjorn Gustavsson
2021년 10월 6일
Perhaps a constraint-function like this would get the job done (might be used with fmincon for example):
function [c,ceq] = your_con(x,a,b)
if nargin < 3
b = 0.1;
end
if nargin < 2
a = 1/2;
end
dx = diff(x);
c = 2*abs(dx)./(max(abs(x(1:end-1))+abs(x((2:end))),...
max(b,...
max(abs(x(1:end-1)),abs(x(2:end)))...
)...
))-a;
ceq = [];
To read up on the details of using this read the help and documentation of fmincon.
HTH
카테고리
도움말 센터 및 File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!