2018 4 7 包子陽書本39頁例題

2021-08-18 08:14:13 字數 2000 閱讀 7931

**書本包子陽《智慧型演算法以及matlab實現》

**:

%差分進化求函式極值

clear all;

close all;

clc;

np = 50; %種群數量

d = 10; %個體的維度

g = 200; %最大進化代數

f0 = 0.4; %初始的變異因子

cr = 0.1; %交叉運算元

xs = 20; %自變數上限

xx = -20; %自變數下限

yz = 10^-6; %閾值

%%%%%%%%%%%%%%%%%%%賦初值%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

x = zeros(d,np); %初始化種群

v = zeros(d,np); %變異種群

u = zeros(d,np); %選擇種群

x = rand(d,np)*(xs-xx)+xx; %賦初值

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%計算目標函式%%%%%%%%%%%%%%%%%%%

for m = 1:np

ob(m) = func1(x(:,m));

endtrace(1) = min(ob);

%%%%%%%%%%%%%%%%%%%%%%%差分進化迴圈%%%%%%

for gen = 1:g

%%%%%%%%%%變異操作%%%%%%

%%%%%%%%%%自適應變異運算元%%%%%%%%%%

lamda = exp(1 - g/(g+1-gen));

f = f0*2^(lamda);

%%%%%%%%%%r1,r2,r3和m的互不相同%%%%%%%%%%

for m = 1:np

r1 = randint(1,1,[1,np]);

while (r1==m)

r1 = randint(1,1,[1,np]);

endr2 = randint(1,1,[1,np]);

while (r2==m) | (r2==r1)

r2 = randint(1,1,[1,np]);

endr3 = randint(1,1,[1,np]);

while (r2==m) | (r2==r1)|(r3==r2)

r1 = randint(1,1,[1,np]);

end

v(:,m) = x(:,r1)+f*(x(:,r2)-x(:,r3));

end%%%%%%%%%%%%%%%%交叉操作%%%%%%%%%%%%%%%

r = randint(1,1,[1,d]);

for n = 1:d

cr = rand(1);

if (cr<= cr) |(n==r)

u(n,:) = v(n,:);

else

u(n,:)= v(n,:);

endend

%%%%%%%%%%%%%%%%%邊界條件的處理%%%%%%%%%%%

for n =1:d

for m = 1:np

if (u(n,m)xs)

u(n,m) = rand*(xs-xx)+xx;

endend

end%%%%%%%%%%%%%%%%%%%選擇操作%%%%%%%%%%%%

for m = 1:np

ob1(m) = func1(u(:,m));

endfor m = 1:np

if ob1(m)適應函式:

%%%%%%%%%%%%%%%%%%%適應度函式%%%%%%%%%

function result= func1(x)

summ = sum(x.^2);

result = summ;

end

不過感覺這個**時間複雜度應該不小吧

1226 包子湊數

給出 n 個數 a i 求有多少個數不能通過這些數湊出 第一行包含乙個整數 n 接下來 n 行,每行包含乙個整數 a i 輸出乙個整數代表答案。如果湊不出的數目有無限多個,輸出inf。資料範圍 1 n 100,1 a i 100 輸入樣例1 2 45輸出樣例1 6輸入樣例2 2 46輸出樣例2 in...

複雜DP 1226 包子湊數問題

小明幾乎每天早晨都會在一家包子鋪吃早餐。他發現這家包子鋪有 n 種蒸籠,其中第 i 種蒸籠恰好能放 ai 個包子。每種蒸籠都有非常多籠,可以認為是無限籠。每當有顧客想買 x 個包子,賣包子的大叔就會迅速選出若干籠包子來,使得這若干籠中恰好一共有 x 個包子。比如一共有 3 種蒸籠,分別能放 3 4 ...

秋招3 包子科技一面 2020 8 27

包子科技在望京的綠地中心 中國錦。出了地鐵15號線望京東站c 還是d來著 口就是。賊拉高。這是個實習崗。hr小姐姐給我帶到他們的休息區,給了我一杯水,讓我等一下面試官。我看了一下休息區有飲水機 咖啡機 微波爐啥的,工作氛圍也挺好的。問題 2 預設插入 3 同時插入多條資料 刪 改 查 1 查詢表中全...