如果有人能告诉我如何设置变量的整数约束(即它们的变量只能为0或1),将不胜感激。
x = ga(fitnessfcn,nvars);
Use the optional ga
function parameters LB
(lower bound), UB
(upper bound) and IntCon
(integer constraints). The signature for the MATLAB genetic algorithm function that you want to use is:
x = ga(fitnessfcn,nvars,A,b,[],[],LB,UB,nonlcon,IntCon)
For example, you could use the MATLAB GA to solve a 10 binary variable problem as follows:
% 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);
Notice that the linear inequality constraints A
and b
, and non-linear constraints nonlcon
are optional and can be replaced with []
if they don't exist.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.