看板 MATLAB 關於我們 聯絡資訊
大家好,我目前想將變數限制為偶數,目前的程式碼如下 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
H45:做一個myfun2,這函數先把x改成偶數再呼叫myfun 06/20 09:03
gn01189425:謝謝,這樣的確可行! 06/20 14:33