tic % 计时
%% 清空环境导入数据
clear
clc
close all
format long
load wndspd
%% GWO-SVR
input_train=[
560.318,1710.53; 562.267,1595.17; 564.511,1479.78; 566.909,1363.74; 569.256,1247.72; 571.847,1131.3; 574.528,1015.33;
673.834,1827.52; 678.13,1597.84; 680.534,1482.11; 683.001,1366.24; 685.534,1250.1; 688.026,1133.91; 690.841,1017.81;
789.313,1830.18; 791.618,1715.56; 796.509,1484.76; 799.097,1368.85; 801.674,1252.76; 804.215,1136.49; 806.928,1020.41;
904.711,1832.73; 907.196,1718.05; 909.807,1603.01; 915.127,1371.43; 917.75,1255.36; 920.417,1139.16; 923.149,1023.09;
1020.18,1835.16; 1022.94,1720.67; 1025.63,1605.48; 1028.4,1489.91; 1033.81,1258.06; 1036.42,1141.89; 1039.11,1025.92;
1135.36,1837.45; 1138.33,1722.94; 1141.35,1607.96; 1144.25,1492.43; 1147.03,1376.63; 1152.23,1144.56; 1154.83,1028.73;
1250.31,1839.19; 1253.44,1725.01; 1256.74,1610.12; 1259.78,1494.74; 1262.67,1379.1; 1265.43,1263.29; 1270.48,1031.58;
1364.32,1840.51; 1367.94,1726.52; 1371.2,1611.99; 1374.43,1496.85; 1377.53,1381.5; 1380.4,1265.81; 1382.89,1150.18;
1477.65,1841.49; 1481.34,1727.86; 1485.07,1613.64; 1488.44,1498.81; 1491.57,1383.71; 1494.47,1268.49; 1497.11,1153.04;
1590.49,1842.51; 1594.53,1729.18; 1598.15,1615.15; 1601.61,1500.72; 1604.72,1385.93; 1607.78,1271.04; 1610.43,1155.93;
1702.82,1843.56; 1706.88,1730.52; 1710.65,1616.79; 1714.29,1502.66; 1717.69,1388.22; 1720.81,1273.68; 1723.77,1158.8;
];
input_test=[558.317,1825.04; 675.909,1712.89; 793.979,1600.35; 912.466,1487.32;
1031.17,1374.03; 1149.79,1260.68; 1268.05,1147.33; 1385.36,1034.68;1499.33,1037.87;1613.11,1040.92;1726.27,1044.19;];
output_train=[
235,175; 235,190; 235,205; 235,220; 235,235; 235,250; 235,265;
250,160; 250,190; 250,205; 250,220; 250,235; 250,250; 250,265;
265,160; 265,175; 265,205; 265,220; 265,235; 265,250; 265,265;
270,160; 270,175; 270,190; 270,220; 270,235; 270,250; 270,265;
285,160; 285,175; 285,190; 285,205; 285,235; 285,250; 285,265;
290,160; 290,175; 290,190; 290,205; 290,220; 290,250; 290,265;
305,160; 305,175; 305,190; 305,205; 305,220; 305,235; 305,265;
320,160; 320,175; 320,190; 320,205; 320,220; 320,235; 320,250;
335,160; 335,175; 335,190; 335,205; 335,220; 335,235; 335,250;
350,160; 350,175; 350,190; 350,205; 350,220; 350,235; 350,250;
365,160; 365,175; 365,190; 365,205; 365,220; 365,235; 365,250;
];
output_test=[235,160; 250,175; 265,190;270,205; 285,220; 290,235; 305,250; 320,265; 335,265; 350,265; 365,265;];
% 生成待回归的数据
x = [0.1,0.1;0.2,0.2;0.3,0.3;0.4,0.4;0.5,0.5;0.6,0.6;0.7,0.7;0.8,0.8;0.9,0.9;1,1];
y = [10,10;20,20;30,30;40,40;50,50;60,60;70,70;80,80;90,90;100,100];
X = input_train;
Y = output_train;
Xt = input_test;
Yt = output_test;
%% 利用灰狼算法选择最佳的SVR参数
SearchAgents_no=60; % 狼群数量
Max_iteration=500; % 最大迭代次数
dim=2; % 此例需要优化两个参数c和g
lb=[0.1,0.1]; % 参数取值下界
ub=[100,100]; % 参数取值上界
Alpha_pos=zeros(1,dim); % 初始化Alpha狼的位置
Alpha_score=inf; % 初始化Alpha狼的目标函数值,change this to -inf for maximization problems
Beta_pos=zeros(1,dim); % 初始化Beta狼的位置
Beta_score=inf; % 初始化Beta狼的目标函数值,change this to -inf for maximization problems
Delta_pos=zeros(1,dim); % 初始化Delta狼的位置
Delta_score=inf; % 初始化Delta狼的目标函数值,change this to -inf for maximization problems
PositiOns=initialization(SearchAgents_no,dim,ub,lb);
Convergence_curve=zeros(1,Max_iteration);
l=0; % 循环计数器
% %% SVM网络回归预测
% [output_test_pre,acc,~]=svmpredict(output_test',input_test',model_gwo_svr); % SVM模型预测及其精度
% test_pre=mapminmax('reverse',output_test_pre',rule2);
% test_pre = test_pre';
%
% gam = [bestc bestc]; % Regularization parameter
% sig2 =[bestg bestg];
% model = initlssvm(X,Y,type,gam,sig2,kernel); % 模型初始化
% model = trainlssvm(model); % 训练
% Yp = simlssvm(model,Xt); % 回归
plot(1:length(Yt),Yt,'r+:',1:length(Yp),Yp,'bo:')
title('+为真实值,o为预测值')
% err_pre=wndspd(104:end)-test_pre;
% figure('Name','测试数据残差图')
% set(gcf,'unit','centimeters','position',[0.5,5,30,5])
% plot(err_pre,'*-');
% figure('Name','原始-预测图')
% plot(test_pre,'*r-');hold on;plot(wndspd(104:end),'bo-');
% legend('预测','原始')
% set(gcf,'unit','centimeters','position',[0.5,13,30,5])
%
% result=[wndspd(104:end),test_pre]
%
% MAE=mymae(wndspd(104:end),test_pre)
% MSE=mymse(wndspd(104:end),test_pre)
% MAPE=mymape(wndspd(104:end),test_pre)
%% 显示程序运行时间
toc