【ALGO】遗传算法(2)

本文探讨了遗传算法中的未成熟收敛问题及其解决方法,包括重新启动法、匹配策略等,并介绍了Matlab中遗传算法的接口使用方法及多种约束条件下的优化案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

未成熟收敛问题

未成熟收敛现象

当遗传算法没有找到最优解或者满意解时,群体中不能个体到达非常相似的情况,特征是群体中个体结构的多样性急剧减少.

原因

  • 选择,交叉,变异等操作并不能搜索到全部解空间
  • 存在随机误差(取样误差和选择误差)
  • 求解问题可能是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)');

ps
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+x223(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} {x1x21x1+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 FlagMeaning
1Without 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.
3Value of the fitness function did not change in MaxStallGenerations generations and the constraint violation is less than ConstraintTolerance.
4Magnitude of step smaller than machine precision and the constraint violation is less than ConstraintTolerance.
5Minimum fitness limit FitnessLimit reached and constraint violation is less than ConstraintTolerance.
0Maximum number of generations MaxGenerations exceeded.
-1Optimization terminated by an output function or plot function.
-2No feasible point found.
-4Stall time limit MaxStallTime exceeded
-5Time limit MaxTime exceeded.

参考资料

Matlb Global Optimization Toolbox

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Quant0xff

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值