[討論] ga(基因演算法)中如何限制變數為偶數
大家好,我目前想將變數限制為偶數,目前的程式碼如下
function [c, ceq] = simple_constraint(x)
a=mod(x,2)
c = [a]
ceq = [];
end
x0=[16]; % 初始值
lb=x0; % 下限
ub=x0+24; % 上限
options = gaoptimset('PlotFcns', {@gaplotbestf,@gaplotstopping},...
'PopulationSize',10,'Generations',10); %控制選項
Constraint=@simple_constraint;
[x,fval,exitflag]=ga(@myFun,1,[],[],[],[],lb,ub,Constraint,[1],options)
ga會將變數代入myfun並傳回目標值
但是ga仍然會找不是偶數的x代入myfun,有沒有其他方法可以限制變數只選偶數呢?
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.251.170.50
→
06/20 09:03, , 1F
06/20 09:03, 1F
→
06/20 14:33, , 2F
06/20 14:33, 2F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
MATLAB 近期熱門文章
PTT數位生活區 即時熱門文章
7
20