未成熟收敛问题
未成熟收敛现象
当遗传算法没有找到最优解或者满意解时,群体中不能个体到达非常相似的情况,特征是群体中个体结构的多样性急剧减少
.
原因
- 选择,交叉,变异等操作并不能搜索到全部解空间
- 存在随机误差(取样误差和选择误差)
- 求解问题可能是GA欺骗问题
解决方案
- 重新启动法
- 匹配策略(
Mating Strategies
) - 重组策略(
Recombination Strategies
) - 替代策略(
Replacement Strategies
)
GA终止规则
- 给定一个最大遗传代数
MaxGen
,算法迭代到MaxGen
时算法停止. - 当进化两代中最优个体小于要求的偏差时,算法终止
- 所有个体或者指定比例以上个体趋同时,要求停止计算
- 算法迭代达到最大时间限制
Matlab中GA接口
x = ga(fun, nvars, A, b);
finds a local minimum x
to fun
, subject to the linear inequalities A*x<=b
. ga evaluates the matrix A*x
as if x
is transposed A*x'
.
x = ga(fun, nvars, A, b, Aeq, beq);
finds a local minimum x
to fuin
, subject to the linear equalities Aeq*x=beq
and A*x<=b
. (Set A=[]
and b=[]
if no linear inequalities exist.) ga evaluates the matrix product Aeq*x
as if x
is transposed Aeq*x'
.
x = ga(fun, nvars, A, b, Aeq, beq, lb, ub);
defines a set of lower and upper bounds on the design variables, x
, so that a solution is found in the range lb<=x<=ub
.
x = ga(fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon);
subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x
and returns vectors C
and Ceq
, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such as C(x)<=0
and Ceq(x)=0
.
x=ga(fun, nvars, A, b, Aeq, lb, ub, nonlcon, options);
minimizes with the default optimization parameters replaced by values in options. Create options using optimoptions
.
x=ga(fun, nvars, A, b, [], [], lb, ub, nonlcon, IntCon)
requires that the variables listed in IntCon
take integer values.
Note
When there are integer constraints, ga does not accept linear or nonlinear equality constraints, only inequalitiy constraints.
xi=linspace(-6, 2, 300);
yi=linspace(-4, 4, 300);
[X, Y]=meshgrid(xi, yi);
Z=ps_example([X(:), Y(:)]);
Z=reshape(Z, size(X));
surf(X, Y, Z, 'MeshStyle', 'none');
colormap 'jet';
view(-26, 43);
xlabel('x(1)');
ylabel('x(2)');
title('ps\_example(x)');
Use the genetic algorithm to minimize the ps_example
function
rng default
x = ga(@ps_example, 2);
Minimize a Nonsmooth function with linear constraints,set the region is x(1)+x(2)>=1
and x(2)<=5+x(1)
. Convert the two inequality constraints to the matrix form A*x<=b
. In other words, get the x
variables on the left-hand side of the inequality, and make both inequalities less than or equal.
A = [-1, -1; -1, 1];
b = [-1; 5];
x_2 = ga(@ps_example, 2, A, b);
Minimize a Nonsmooth function with linear equality and inequality constraints,set the inequalities and equalities as follows
x(1)+x(2)>=1
x(2)==5+x(1)
rng default
A = [-1 -1];
b = -1;
Aeq = [-1 1];
beq = 5;
x_3 = ga(@ps_example, 2, A, b, Aeq, beq);
Optimize with Linear Constraints and Bounds,set bounds 1<=x(1)<=6
and -3<=x(2)<=8
.
A = [-1 -1];
b = -1;
Aeq = [-1 1];
beq = 5;
lb = [1 -3];
ub = [6 8];
x_4 = ga(@ps_example, 2, A, b, Aeq, beq, lb, ub);
Optimize with Nonlinear using ga, set the region as
{
2
x
1
2
+
x
2
2
≤
3
(
x
1
+
1
)
2
=
(
x
2
/
2
)
4
\begin{cases} 2x_1^2+x_2^2\leq 3\\ (x_1+1)^2=(x_2/2)^4 \end{cases}
{2x12+x22≤3(x1+1)2=(x2/2)4
function ellipsecons
is as follows
function [c, ceq] = ellipsecons(x)
c = 2*x(1)^2+x(2)^2-3;
ceq = (x(1)+1)^2-(x(2)/2)^4;
end
和
x_5 = ga(@ps_example, 2, [], [], [], [], [], [], @ellipsecons);
Minimize with nondefault options, use the ga to minimize function on the region
{
−
x
1
−
x
2
≤
−
1
−
x
1
+
x
2
=
=
5
\begin{cases} -x_1-x_2\leq -1\\ -x_1+x_2==5 \end{cases}
{−x1−x2≤−1−x1+x2==5
To obtain a more accurate solution, set a constraint tolerance of 1e-6
. And to monitor the solver progress, set a plot function.
options = optimoptions('ga', 'ConstraintTolerance', 1e-6, 'PlotFcn', @gaplotbestf);
x_6 = ga(@ps_example, 2, A, b, Aeq, beq, [], [], [], options);
Minimize a nonlinear function with Integer Constraints, use ga to minimize an intger-constrained nonlinear problem.
IntCon = 1;
fun = @ps_example;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
nonlcon = [];
[x_7, fval] = ga(fun, 2, A, b, Aeq, beq, lb, ub, nonlcon, IntCon);
Obtain Diagnostic information, constrained to have x 1 x_1 x1 integer-valued.
IntCon = 1;
fun = @ps_example;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
nonlcon = [];
options = optimoptions('ga', 'PlotFcn', @gaplotbestf);
[x_8, fval, exitflag, output] = ga(fun, 2, A, b, Aeq, beq, lb, ub, nonlcon, IntCon, options);
Reason that ga stopped, returned as an integer
Exit Flag | Meaning |
---|---|
1 | Without nonlinear constraints :Average cumulative change in value of the fitness function over MaxStallGenerations generations is less than FunctionToTolerance. With nonlinear constraints :Magnitude of the complementary measure. |
3 | Value of the fitness function did not change in MaxStallGenerations generations and the constraint violation is less than ConstraintTolerance . |
4 | Magnitude of step smaller than machine precision and the constraint violation is less than ConstraintTolerance . |
5 | Minimum fitness limit FitnessLimit reached and constraint violation is less than ConstraintTolerance . |
0 | Maximum number of generations MaxGenerations exceeded. |
-1 | Optimization terminated by an output function or plot function. |
-2 | No feasible point found. |
-4 | Stall time limit MaxStallTime exceeded |
-5 | Time limit MaxTime exceeded. |