在Matlab中使用遗传算法时如何设置整数约束?

How to set the integer constraints when use the Genetic Algorithm in Matlab?

如果有人能告诉我如何设置变量的整数约束(即变量只能是 0 或 1),我将不胜感激。

x = ga(fitnessfcn,nvars);

使用可选的 ga 函数参数 LB(下限)、UB(上限)和 IntCon(整数约束)。您要使用的 MATLAB 遗传算法函数的签名是:

x = ga(fitnessfcn,nvars,A,b,[],[],LB,UB,nonlcon,IntCon)

例如,您可以使用 MATLAB GA 解决 10 个二进制变量问题,如下所示:

% Number of variables
nVars = 10

% Lower and upper bounds
LB = zeros(1, nVars);
UB = ones(1, nVars);

% Variables with integer constraints (all in this case)
IntCon = 1:nVars;

% Run the GA solver
x = ga(fitnessfcn, nVars, [], [], [], [], LB, UB, [], IntCon);

请注意,线性不等式约束 Ab 以及非线性约束 nonlcon 是可选的,如果不需要,可以用 [] 替换存在。