使用遗传算法查找最小功能
找到当地的不受约束,x
= ga(乐趣
,nvars
)x
, to the objective function,乐趣
。nvars
is the dimension (number of design variables) of乐趣
。
Note
Passing Extra Parameters如有必要,如何将额外参数传递给客观函数和非线性约束函数。
ga
Theps_example.m.
file ships with your software. Plot the function.
ξ= linspace (2300);易= linspace (4 4300); [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)')标题('ps\_example(x)')
Find the minimum of this function usingga
。
rngdefault重复性的%X = GA(@ PS_EXAMPLE,2)
优化终止:FITHS值的平均变化小于选项。功能化。
X =1×2-4。6793 -0.0860
使用遗传算法最小化ps_example.
乐趣ction on the regionx(1) + x(2) >= 1
andx(2)<= 5 + x(1)
。
First, convert the two inequality constraints to the matrix forma * x <= b
。In other words, get thex
在不等式的左侧的变量,并使不等式少于或等于:
-x(1) -x(2) <= -1
-x(1) + x(2) <= 5
A = [-1,-1; -1,1]; b = [-1;5];
Solve the constrained problem usingga
。
rngdefault重复性的%有趣= @ps_example;X =ga(fun,2,A,b)
优化终止:FITHS值的平均变化小于选项。功能化。
X =1×20.9991 0.0000
约束对约束公差的默认值满意,1e-3
。To see this, computeA*x' - b
, which should have negative components.
disp(A*x' - b)
0.0009 -5.9991
使用遗传算法最小化ps_example.
乐趣ction on the regionx(1) + x(2) >= 1
andx(2)== 5 + x(1)
。
First, convert the two constraints to the matrix forma * x <= b
andAeq*x = beq
。In other words, get thex
变量on the left-hand side of the expressions, and make the inequality into less than or equal form:
-x(1) -x(2) <= -1
-x(1) + x(2) == 5
A = [-1 -1]; b = -1; Aeq = [-1 1]; beq = 5;
Solve the constrained problem usingga
。
rngdefault重复性的%有趣= @ps_example;X = GA(有趣,2,A,B,AEQ,BEQ)
优化终止:FITHS值的平均变化小于选项。功能化。
X =1×2-2。0000 2.9990
检查约束是否满足于默认值ConstraintTolerance
,1e-3
。
disp(A*x' - b)
9.9998e-04
disp(Aeq*x' - beq)
-9.9937E-04.
使用遗传算法最小化ps_example.
乐趣ction on the regionx(1) + x(2) >= 1
andx(2)== 5 + x(1)
。In addition, set bounds1 <= x(1) <= 6
and-3 <= x(2) <= 8
。
First, convert the two linear constraints to the matrix forma * x <= b
andAeq*x = beq
。In other words, get thex
变量on the left-hand side of the expressions, and make the inequality into less than or equal form:
-x(1) -x(2) <= -1
-x(1) + x(2) == 5
A = [-1 -1]; b = -1; Aeq = [-1 1]; beq = 5;
设定界限磅
andUB.
。
磅= [1 -3]; ub = [6 8];
Solve the constrained problem usingga
。
rngdefault重复性的%有趣= @ps_example;X =ga(fun,2,A,b,Aeq,beq,lb,ub)
优化终止:FITHS值的平均变化小于选项。功能化。
X =1×21.0000 5.9991
Check that the linear constraints are satisfied to within the default value ofConstraintTolerance
,1e-3
。
disp(A*x' - b)
-5.9991.
disp(Aeq*x' - beq)
-9.0713E-04.
ga
使用遗传算法最小化ps_example.
乐趣ction on the region
and
。
To do so, first write a function省号.m.
that returns the inequality constraint in the first output,c
,以及第二个输出中的平等约束,ceq
。Save the file省号.m.
至a folder on your MATLAB® path.
typeellipsecons
乐趣ction [c,ceq] = ellipsecons(x) c = 2*x(1)^2 + x(2)^2 - 3; ceq = (x(1)+1)^2 - (x(2)/2)^4;
包括功能句柄ellipsecons
as thenonlcon.
argument.
nonlcon.= @ellipsecons; fun = @ps_example; rngdefault重复性的%X =ga(fun,2,[],[],[],[],[],[],nonlcon)
Optimization terminated: average change in the fitness value less than options.FunctionTolerance and constraint violation is less than options.ConstraintTolerance.
X =1×2-0.9766 0。0362
Check that the nonlinear constraints are satisfied atx
。The constraints are satisfied whenc
≤0且ceq
= 0到默认值ConstraintTolerance
,1e-3
。
[c,ceq] = nonlcon(x)
c = -1.0911.
ceq = 5.4645e-04
使用遗传算法最小化ps_example.
乐趣ction on the regionx(1) + x(2) >= 1
andx(2)== 5 + x(1)
using a constraint tolerance that is smaller than the default.
First, convert the two constraints to the matrix forma * x <= b
andAeq*x = beq
。In other words, get thex
变量on the left-hand side of the expressions, and make the inequality into less than or equal form:
-x(1) -x(2) <= -1
-x(1) + x(2) == 5
A = [-1 -1]; b = -1; Aeq = [-1 1]; beq = 5;
To obtain a more accurate solution, set a constraint tolerance of1e-6
。And to monitor the solver progress, set a plot function.
选择= optimoptions('ga','ConstraintTolerance',1e-6,'plotfcn', @gaplotbestf);
解决最小化问题。
rngdefault重复性的%有趣= @ps_example;X =ga(fun,2,A,b,Aeq,beq,[],[],[],options)
优化终止:FITHS值的平均变化小于选项。功能化。
X =1×2-2。0000 3.0000
Check that the linear constraints are satisfied to within1e-6
。
disp(A*x' - b)
9.9999E-07.
disp(Aeq*x' - beq)
-9.9503E-07.
使用遗传算法最小化ps_example.
函数受约束的影响x(1)
是一个整数。
IntCon = 1; rngdefault重复性的%有趣= @ps_example;a = [];b = [];AEQ = [];beq = [];lb = [];UB = [];nonlcon = [];X = GA(乐趣,2,A,B,AEQ,BEQ,LB,UB,NONLCCON,INTCON)
Optimization terminated: average change in the penalty fitness value less than options.FunctionTolerance and constraint violation is less than options.ConstraintTolerance.
X =1×2-5.0000 -0.0000
Use to genetic algorithm to minimize an integer-constrained nonlinear problem. Obtain both the location of the minimum and the minimum function value.
IntCon = 1; rngdefault重复性的%有趣= @ps_example;a = [];b = [];AEQ = [];beq = [];lb = [];UB = [];nonlcon = [];[,fval] = ga(fun,2,a,b,AEQ,Beq,LB,UB,Nonlcon,Intcon)
Optimization terminated: average change in the penalty fitness value less than options.FunctionTolerance and constraint violation is less than options.ConstraintTolerance.
X =1×2-5.0000 -0.0000
fval = -1.9178
Compare this result to the solution of the problem with no constraints.
[x,fval] = ga(乐趣,2)
Optimization terminated: maximum number of generations exceeded.
X =1×2-4。7121 0.0051
fval = -1.9949.
使用遗传算法最小化ps_example.
乐趣ction constrained to havex(1)
整数值。To understand the reason the solver stopped and howga
searched for a minimum, obtain theexitflag
andoutput
results. Also, plot the minimum observed objective function value as the solver progresses.
IntCon = 1; rngdefault重复性的%有趣= @ps_example;a = [];b = [];AEQ = [];beq = [];lb = [];UB = [];nonlcon = [];选择= optimoptions('ga','plotfcn', @gaplotbestf); [x,fval,exitflag,output] = ga(fun,2,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)
Optimization terminated: average change in the penalty fitness value less than options.FunctionTolerance and constraint violation is less than options.ConstraintTolerance.
X =1×2-5.0000 -0.0000
fval = -1.9178
exitflag = 1
output =struct with fields:问题型:'Integercontraints'rngstate:[1x1 struct]代:96 Funccount:3691消息:'优化终止:惩罚健身值的平均变化低于选项.FunctionTolecta ...'MaxConstraint:0 HybridFlag:[]
使用遗传算法最小化ps_example.
乐趣ction constrained to havex(1)
整数值。获取所有输出,包括最终种群和分数矢量。
IntCon = 1; rngdefault重复性的%有趣= @ps_example;a = [];b = [];AEQ = [];beq = [];lb = [];UB = [];nonlcon = [];[x,fval,exitflag,output,population,scores] = ga(fun,2,A,b,Aeq,beq,lb,ub,nonlcon,IntCon);
Optimization terminated: average change in the penalty fitness value less than options.FunctionTolerance and constraint violation is less than options.ConstraintTolerance.
Examine the first 10 members of the final population and their corresponding scores. Notice thatx(1)
对所有这些人口成员都是整数的重视。整数ga
算法仅生成整数可行的群体。
DISP(人口(1:10,:))
-5.0000 -0.0000-5.0000 -0.0000-5。0000 0.0014 -6.0000 0.0008 -13.0000 -0.0124 -10.0000 0.0011 -4.0000 -0.0010 0 0.0072 -4.0000 0.0010 -5.0000 -0.0000
DISP(分数(1:10))
-1.9178 -1.9178 -1.9165 1.0008 64.0124 25.0011 -1.5126 2.5072 -1.5126 -1.9178
乐趣
—目标职能目标职能, specified as a function handle or function name. Write the objective function to accept a row vector of lengthnvars
and return a scalar value.
当。。。的时候'undervectorized'
option is真正
,写乐趣
至accept apop
-通过-nvars
矩阵,其中pop
is the current population size. In this case,乐趣
returns a vector the same length aspop
包含健身功能值。确保这件事乐趣
does not assume any particular size forpop
,自从ga
即使在矢量化计算中也可以通过人口的单个成员。
Example:Fun = @(x)(x-[4,2])。^ 2
数据类型:char
|乐趣ction_handle
|string
nvars
—Number of variablesNumber of variables, specified as a positive integer. The solver passes row vectors of lengthnvars
至乐趣
。
Example:4
数据类型:双
A
—线性不平等约束线性不平等约束, specified as a real matrix.A
是一个M
-通过-nvars
矩阵,其中M
is the number of inequalities.
A
encodes theM
linear inequalities
a * x <= b
,
wherex
是栏矢量nvars
变量x(:)
,和b
is a column vector withM
元素。
例如,至specify
x1+ 2x2≤ 10
3x1+ 4x2≤ 20
5x1+ 6x2≤ 30,
give these constraints:
a = [1,2; 3,4; 5,6];B = [10; 20; 30];
Example:要指定控制变量总和1或更少,请给出约束A = ones(1,N)
andb = 1
。
数据类型:双
b
—线性不平等约束线性不平等约束, specified as a real vector.b
是一个M
- 与...相关的矢量A
矩阵。If you passb
as a row vector, solvers internally convertb
至the column vectorb(:)
。
b
encodes theM
linear inequalities
a * x <= b
,
wherex
是栏矢量N
变量x(:)
,和A
是大小的矩阵M
-通过-N
。
例如,至specify
x1+ 2x2≤ 10
3x1+ 4x2≤ 20
5x1+ 6x2≤ 30,
give these constraints:
a = [1,2; 3,4; 5,6];B = [10; 20; 30];
Example:要指定控制变量总和1或更少,请给出约束A = ones(1,N)
andb = 1
。
数据类型:双
Aeq
—Linear equality constraints线性平等约束,指定为真实矩阵。Aeq
是一个Me
-通过-nvars
矩阵,其中Me
是平等的数量。
Aeq
encodes theMe
线性平等
Aeq*x = beq
,
wherex
是栏矢量N
变量x(:)
,和beq
is a column vector withMe
元素。
例如,至specify
x1+ 2x2+ 3x3= 10
2x1+ 4x2+x3= 20,
give these constraints:
Aeq = [1,2,3;2,4,1]; beq = [10;20];
Example:To specify that the control variables sum to 1, give the constraintsAeq = ones(1,N)
andbeq = 1
。
数据类型:双
beq
—Linear equality constraintsLinear equality constraints, specified as a real vector.beq
是一个Me
- 与...相关的矢量Aeq
矩阵。If you passbeq
as a row vector, solvers internally convertbeq
至the column vectorBeq(:)
。
beq
encodes theMe
线性平等
Aeq*x = beq
,
wherex
是栏矢量N
变量x(:)
,和Aeq
是大小的矩阵MEQ.
-通过-N
。
例如,至specify
x1+ 2x2+ 3x3= 10
2x1+ 4x2+x3= 20,
give these constraints:
Aeq = [1,2,3;2,4,1]; beq = [10;20];
Example:To specify that the control variables sum to 1, give the constraintsAeq = ones(1,N)
andbeq = 1
。
数据类型:双
磅
—下限[]
(默认)|real vector or array下限,指定为真正的矢量或双打数组。磅
表示下限元素明智磅
≤x
≤UB.
。
Internally,ga
converts an array磅
至the vector磅(:)
。
Example:lb = [0; -inf; 4]
手段x(1) ≥ 0
,x(3) ≥ 4
。
数据类型:双
UB.
—上限[]
(默认)|real vector or array上限, specified as a real vector or array of doubles.UB.
represents the upper bounds element-wise in磅
≤x
≤UB.
。
Internally,ga
converts an arrayUB.
至the vectorUB.(:)
。
Example:UB.= [Inf;4;10]
手段X(2)≤4
,x(3) ≤ 10
。
数据类型:双
nonlcon.
—Nonlinear constraintsNonlinear constraints, specified as a function handle or function name.nonlcon.
is a function that accepts a vector or arrayx
并返回两个数组,C(x)
andceq(x)
。
C(x)
is the array of nonlinear inequality constraints atx
。ga
试图满足
C(x)<= 0
对于所有参赛作品c
。
ceq(x)
is the array of nonlinear equality constraints atx
。ga
试图满足
ceq(x) = 0
对于所有参赛作品ceq
。
例如,
X =ga(@myfun,4,A,b,Aeq,beq,lb,ub,@mycon)
wheremycon
is a MATLAB®乐趣ction such as
乐趣ction [c,ceq] = mycon(x) c = ... % Compute nonlinear inequalities at x. ceq = ... % Compute nonlinear equalities at x.
要了解如何使用矢量化约束,请参阅Vectorized Constraints。
Note
ga
does not enforce nonlinear constraints to be satisfied when the人民型
option is set to'bitString'
要么'习俗'
。
IfIntCon
is not empty, the second output ofnonlcon.
(ceq
) must be an empty entry ([]
).
对于information on howga
用途nonlcon.
, see非线性约束求解器算法。
数据类型:char
|乐趣ction_handle
|string
选择
—Optimization optionsoptimoptions
|结构体Optimization options, specified as the output ofoptimoptions
要么a structure.
optimoptions
hides the options listed initalics。看到Optimoptions隐藏的选项。
Values in{}
denote the default value.
{}*
represents the default when there are linear constraints, and forMutationFcn
还有界限。
I*indicates thatga
handles options for integer constraints differently; this notation does not apply togamultiobj.
。
NM表示该选项不适用于gamultiobj.
。
Options forga
, Integerga
,和gamultiobj.
Option | Description | Values |
---|---|---|
ConstraintTolerance |
Determines the feasibility with respect to nonlinear constraints. Also, 对于an options structure, use |
Positive scalar | |
|
I*Function that creates the initial population. Specify as a name of a built-in creation function or a function handle. SeePopulation Options。 |
|
|
I*Function that the algorithm uses to create crossover children. Specify as a name of a built-in crossover function or a function handle. SeeCrossover Options。 |
|
|
交叉函数的下一代,不包括精英儿童的人口的一小部分。 |
Positive scalar | |
|
Level of display. |
|
|
函数,计算距离衡量个体所uals. Specify as a name of a built-in distance measure function or a function handle. The value applies to decision variable or design space (genotype) or to function space (phenotype). The default 对于an options structure, use a function handle, not a name. |
|
|
NMPositive integer specifying how many individuals in the current generation are guaranteed to survive to the next generation. Not used in |
Positive integer | |
|
NM如果健身功能达到价值 |
标量| |
|
尺寸缩放健身功能值的功能。指定为内置缩放功能或函数句柄的名称。选项不可用 |
|
FunctionTolerance |
如果最佳健身功能值的平均相对变化,则该算法停止 对于 对于an options structure, use |
Positive scalar | |
|
I*Function that continues the optimization after Alternatively, a cell array specifying the hybrid function and its options. Seega Hybrid Function。 对于 |
Function name or handle | 要么 1-by-2 cell array | |
InitialPenalty |
NMI*Initial value of penalty parameter |
Positive scalar | |
|
Initial population used to seed the genetic algorithm. Has up to 对于an options structure, use |
矩阵| |
|
Matrix or vector specifying the range of the individuals in the initial population. Applies to 对于an options structure, use |
Matrix or vector | |
|
I*Initial scores used to determine fitness. Has up to 对于an options structure, use |
单个目标的列向量|多目标矩阵| |
|
算法停止之前的最大迭代次数。 对于an options structure, use |
Positive integer | |
|
如果最佳健身功能值的平均相对变化,则该算法停止 对于 对于an options structure, use |
Positive integer | |
|
NM如果目标函数没有改善,则算法停止 对于an options structure, use |
Positive scalar |
|
算法在运行后停止 对于an options structure, use |
Positive scalar | |
MigrationDirection |
Direction of migration. SeeMigration Options |
|
MigrationFraction |
标量从0到1指定每个亚父群中的个体的分数,该群体迁移到不同的子群。看到Migration Options |
标量| |
MigrationInterval |
Positive integer specifying the number of generations that take place between migrations of individuals between subpopulations. SeeMigration Options。 |
Positive integer | |
|
I*产生突变儿童的功能。指定为内置突变函数或函数句柄的名称。看到突变选择。 |
|
|
非线性约束算法。看到非线性约束求解器算法。Option unchangeable for 对于an options structure, use |
|
|
Functions that 对于an options structure, use |
Function handle or cell array of function handles | |
|
从0到1标量,指定个人的一部分,以保持第一帕捕前线,而求解器选择来自较高前部的个体 |
标量| |
罚款 |
NMI*惩罚更新参数。 |
Positive scalar | |
|
函数绘制由算法计算的数据。指定为内置绘图函数,函数句柄或内置名称或功能句柄的单元格数组的名称。看到情节选项。 对于an options structure, use |
|
PlotInterval |
正整数指定与绘制函数的连续调用之间的几代数量。 |
Positive integer | |
|
Size of the population. |
Positive integer | |
|
Data type of the population. Must be |
|
|
I*选择交叉和变异儿童的父母的功能。指定为内置选择功能或函数句柄的名称。
|
|
StallTest |
NMStopping test type. |
|
UseParallel |
Compute fitness and nonlinear constraint functions in parallel. See矢量化和并行选项(用户功能评估)andHow to Use Parallel Processing in Global Optimization Toolbox。 |
|
|
Specifies whether functions are vectorized. See矢量化和并行选项(用户功能评估)andVectorize the Fitness Function。 对于an options structure, use |
|
Example:optimoptions('ga','PlotFcn',@gaplotbestf)
IntCon
—整数变量整数变量, specified as a vector of positive integers taking values from1
至nvars
。Each value inIntCon
represents anx
component that is integer-valued.
Note
WhenIntCon
is nonempty,Aeq
andbeq
must be an empty entry ([]
), andnonlcon.
必须返回空ceq
。对于more information on integer programming, see混合整数GA优化。
Example:指定偶数条目x
are integer-valued, setIntCon
至2:2:NVARS
数据类型:双
problem
—Problem description问题描述,指定为包含这些字段的结构。
fitnessfcn |
Fitness functions |
nvars |
Number of design variables |
Aineq |
|
Bineq |
|
Aeq |
|
Beq |
|
磅 |
下限 |
UB. |
Upper bound on |
nonlcon. |
非线性约束函数 |
rngstate. |
Optional field to reset the state of the random number generator |
solver |
|
选择 |
使用创建的选项 |
数据类型:struct
x
— Solution解决方案,作为真正的矢量返回。x
是最好的一点ga
located during its iterations.
fval
- 解决方案的目标函数值目标职能value at the solution, returned as a real number. Generally,fval
=有趣(x)
。
exitflag
- 理由ga
停了下来Reason thatga
停了下来, returned as an integer.
Exit Flag | 含义 |
---|---|
1 |
没有非线性约束— Average cumulative change in value of the fitness function over |
With nonlinear constraints— Magnitude of the complementarity measure (seeComplementarity Measure) is less than |
|
3 |
Value of the fitness function did not change in |
4 |
Magnitude of step smaller than machine precision and the constraint violation is less than |
5 |
最小健身限制 |
0 |
Maximum number of generations |
-1 |
优化由输出函数或绘图函数终止。 |
-2 |
No feasible point found. |
-4 |
Stall time limit |
-5 |
时限 |
当。。。的时候re are integer constraints,ga
用途the penalty fitness value instead of the fitness value for stopping criteria.
output
- 有关优化过程的信息Information about the optimization process, returned as a structure with these fields:
问题型
— Problem type, one of:
'unconstrained'
'绑定'
'linearconstraints'
'nonlinearconstr'
'integerconstraints'
rngstate.
— State of the MATLAB random number generator, just before the algorithm started. You can use the values inrngstate.
重现产出ga
。看到再现结果。
几代人
— Number of generations computed.
Funccount.
— Number of evaluations of the fitness function.
message
- 理由the algorithm terminated.
maxconstraint
- 最大约束违规,如果有的话。
hybridflag.
— Exit flag from the hybrid function. Relates to theHybridFcn
选择
。不适合gamultiobj.
。
population
— Final populationFinal population, returned as aPopulationSize
-通过-nvars
矩阵。行的行population
是个人。
得分
— Final scoresFinal scores, returned as a column vector.
对于non-integer problems, the final scores are the fitness function values of the rows ofpopulation
。
对于整数问题,最终分数是人口成员的惩罚适应价值。看到Integer ga Algorithm。
In the Augmented Lagrangian nonlinear constraint solver, thecomplementarity measureis the norm of the vector whose elements areciλ.i,在哪里ciis the nonlinear inequality constraint violation, andλ.iis the corresponding Lagrange multiplier. SeeAugmented Lagrangian Genetic Algorithm。
将函数与其他可以调用的独立变量写入函数ga
, seePassing Extra Parameters。
对于problems that use the population type双载体
(the default),ga
does not accept functions whose inputs are of typecomplex
。To solve problems involving complex data, write your functions so that they accept real vectors, by separating the real and imaginary parts.
对于a description of the genetic algorithm, seeHow the Genetic Algorithm Works。
对于a description of the mixed integer programming algorithm, seeInteger ga Algorithm。
对于a description of the nonlinear constraint algorithms, see非线性约束求解器算法。
TheOptimizeLive Editor task provides a visual interface forga
。
ga
执行更少的健身功能评估Behavior changed in R2019b
当健身功能是确定性的,ga
does not reevaluate the fitness function on elite (current best) individuals. You can control this behavior by accessing the newstate.EvalElites
field and modifying it in a custom output function or custom plot function. Similarly, when the initial population has duplicate members,ga
只评估每个唯一的成员一次。您可以通过访问和修改新的自定义输出功能或自定义绘图功能来控制此行为state.HaveDuplicates
field. For details, see遗传算法的自定义输出功能要么自定义绘图功能。
对于details about the two new fields, seeThe State Structure。
[1]戈德伯格,大卫E.,Genetic Algorithms in Search, Optimization & Machine Learning, Addison-Wesley, 1989.
[2] A. R. Conn, N. I. M. Gould, and Ph. L. Toint. “A Globally Convergent Augmented Lagrangian Algorithm for Optimization with General Constraints and Simple Bounds”,暹罗杂志数值分析, Volume 28, Number 2, pages 545–572, 1991.
[3] A. R. Conn,N. I. M. Gould和pH。L. Toint。“全球会聚增强拉格朗日屏障算法,用于优化一般不等式约束和简单边界”,计算数学,第66卷,217页,第211页,第261-288页,1997。
To run in parallel, set the'UseParallel'
选择真正
。
选择= optimoptions('
solvername
','使用adplallel',true)
有关更多信息,请参阅How to Use Parallel Processing in Global Optimization Toolbox。
A modified version of this example exists on your system. Do you want to open this version instead?
You clicked a link that corresponds to this MATLAB command:
在MATLAB命令窗口中输入它来运行命令。Web浏览器不支持MATLAB命令。金宝app
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.