How to define constraint in Optimization such that difference in value between two consecutive unknowns is not greater than 50%

3 ビュー (過去 30 日間)
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 件のコメント
Matt J
Matt J 2021 年 10 月 6 日
編集済み: Matt J 2021 年 10 月 6 日
50% of what? Do you mean |x(i+1)-x(i)|<=0.5*x(i) ?
Ankur Shah
Ankur Shah 2021 年 10 月 6 日
Yes, 50% greater than the previous.

サインインしてコメントする。

回答 (2 件)

Matt J
Matt J 2021 年 10 月 6 日
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)
p = struct with fields:
intcon: [] lb: [30×1 double] ub: [30×1 double] Aineq: [58×30 double] bineq: [58×1 double] Aeq: [] beq: []
  6 件のコメント
Ankur Shah
Ankur Shah 2021 年 10 月 6 日
I get your point, but i am not sure how to apply in code
for example if linear equalities are as below
x(1) -x(2) <= -1
-x(1) + x(2) <= 5
We define
A = [-1,-1;
-1,1];
b = [-1;5];
because above is 2 variables, it is easy to apply as follows
fun = @ps_example;
x = ga(fun,2,A,b)
How to apply the solution which you gave in coding ?
Matt J
Matt J 2021 年 10 月 6 日
編集済み: Matt J 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
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

カテゴリ

Help Center および File ExchangeGenetic Algorithm についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by