主要操作:变异 - 交叉 - 选择
%载入数据
data = csvread('D:\matlab2016a\OA\sample.csv');
%划分数据集
rv = rand(1,size(data,1));
[value,index] = sort(rv);
x_train = data(index(1:150),1:2);y_train = data(index(1:150),end);
x_test = data(index(151:end),1:2);y_test = data(index(151:end),end);
%归一化处理
[train_input,inputps] = mapminmax(x_train');
test_input = mapminmax('apply',x_test',inputps);
[train_output,outputps] = mapminmax(y_train');
%设置节点
inputnum = size(train_input,1);
hiddennum = 5;
outputnum = size(train_output,1);
%设置DE参数
sizepop = 20;
dim = inputnum*hiddennum+hiddennum++hiddennum*outputnum+outputnum;
lb = [-1];ub = [1];
mutation = 0.5;
crossover = 0.2;
maxgen = 50;
if size(lb,1) == 1
lb = ones(dim,1).*lb;
ub = ones(dim,1).*ub;
end
%个体初始化
net = newff(train_input,train_output,hiddennum);
Targetfitness = inf;
for i = 1:sizepop
position(i,:) = lb'+(ub'-lb').*rand(1,dim);
predict = DEoptimization(inputnum,hiddennum,outputnum,net,train_input,train_output,position(i,:));
predict = mapminmax('reverse',predict,outputps);
fit(i) = sqrt(sum((predict'-y_train).^2)/length(y_train));
if fit(i) < Targetfitness
Targetfitness = fit(i);
Targetposition = position(i,:);
end
end
%迭代寻优
converage = zeros(1,maxgen);
for L = 1:maxgen
for j = 1:sizepop
%变异操作
ri = randperm(sizepop,3);
while isempty(find(ri==j))==0
ri = randperm(sizepop,3);
end
mpop = position(ri(1),:)+mutation*(position(ri(2),:)-position(ri(3),:));
if isempty(find(mpop>ub'))== 0
mpop(find(mpop>ub')) = (ub(find(mpop>ub')))';
end
if isempty(find(mpop mpop(find(mpop
end
%交叉操作
tmp = zeros(1,dim);
for i = 1:dim
if rand < crossover
tmp(i) = mpop(i);
else
tmp(i) = position(j,i);
end
end
%选择操作
cpredict = DEoptimization(inputnum,hiddennum,outputnum,net,train_input,train_output,tmp);
cpredict = mapminmax('reverse',cpredict,outputps);
cfit = sqrt(sum((cpredict'-y_train).^2)/length(y_train));
%更新
if cfit < fit(j)
position(j,:) = tmp;
fit(j) = cfit;
end
if fit(j) < Targetfitness
Targetposition = position(j,:);
Targetfitness = fit(j);
end
end
converage(L) = Targetfitness;
end
w1 = Targetposition(1:inputnum*hiddennum);
b1 = Targetposition(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
w2 = Targetposition(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum);
b2 = Targetposition(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum);
net.iw{1,1} = reshape(w1,hiddennum,inputnum);
net.lw{2,1} = reshape(w2,outputnum,hiddennum);
net.b{1} = reshape(b1,hiddennum,1);
net.b{2} = b2;
net.trainParam.epochs = 100;
net.trainParam.lr = 0.01;
net.trainParam.goal = 0.001;
net = train(net,train_input,train_output);
predict = sim(net,train_input);
predict_train = mapminmax('reverse',predict,outputps);
train_rmse = sqrt(sum((predict_train'-y_train).^2)/length(y_train));
predict = sim(net,test_input);
predict_test = mapminmax('reverse',predict,outputps);
test_rmse = sqrt(sum((predict_test'-y_test).^2)/length(y_test));
disp(['Train RMSE = ',num2str(train_rmse),' Test RMSE = ',num2str(test_rmse)])
Train RMSE = 0.61411 Test RMSE = 0.61139