【负荷预测】基于改进灰狼算法(IGWO)优化的LSSVM进行负荷预测(Matlab代码实现)

欢迎来到本博客❤️❤️

博主优势:博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。

⛳️座右铭:行百里者,半于九十。

目录

2 运行结果

3 参考文献

4 Matlab代码实现


准确的电力负荷预测对电网稳定运行有着重要作用。电力负荷大小受多种关键信息影响:温度湿度、风速、日期类型、地区GDP等。然而,通常情况下难以获得所有关键信息的准确数据。因此
,若能在关键信息缺失情况下实施较为准确的电力负荷预测,则可以省去收集和处理多种数据的繁琐过程,简化电力负荷预测步骤。
现有短期电力负荷预测的研究主要分为多元时间序列分析和单一时间序列分析。多元时间序列分析考虑天气等多种关键信息数据。

本文基于改进灰狼算法(IGWO)优化的LSSVM进行负荷预测,并用matlab代码实现。

2 运行结果

 部分代码:

namafile = {'Hasil Simulasi IGWO 1';
    'Hasil Simulasi IGWO 2';
    'Hasil Simulasi IGWO 3';
    'Hasil Simulasi IGWO 4';
    'Hasil Simulasi IGWO 5';
    'Hasil Simulasi IGWO 6';
    'Hasil Simulasi IGWO 7';
    'Hasil Simulasi IGWO 8';
    'Hasil Simulasi IGWO 9';
    'Hasil Simulasi IGWO 10';
    'Hasil Simulasi IGWO 11';
};
% hasilsimulasi = zeros(4,30);
hasilsimulasi = zeros(8,30);
for jj = 1:25
    %% I-GWO
    starting = matriksawalhari(jj);
    hasilsimulasi = zeros(8,30);
    for k = 1:30 %% ambil data 30 kali
        %% susun data yang diregress
        barisdata = [];
        %         harilibur = [];
        % weekend = [];
        awalhari = starting;
        panjangdata = 5*7; %%5 minggu -> 4 minggu data forecast + 1 minggu input
        akhirhari = panjangdata+awalhari-1;
        for i = awalhari:akhirhari %%1:35
            barisdata = [barisdata,matriksdata(i,:)];
            %     weekend = [weekend,ones(1,24)*liburdata(i,1)];
            %             harilibur = [harilibur,ones(1,24)*liburdata(i,2)];
        end
        barisdata = barisdata';
        % weekend = weekend';
        %         harilibur = harilibur';
        %% Windowize data
        predictrange = 24*7;
        lagend = 29*24; %%7 hari input+1 output
        lagstep = 24*7;
        datatable = windowize(barisdata,1:lagstep:lagend); %%Y part might be filled by indices
        Xtraindata = datatable(:,1:end-1); %%input training
        Ytraindata = datatable(:,end); %%output training
        skala = 0;
        for ss = 2:168 %% for computing MASE
            skala = skala + abs(Ytraindata(ss) - Ytraindata(ss-1));
        end
        skala = skala/(168-1);
        % datatable = windowize(weekend,1:lagstep:lagend);
        % Xtraindata = [Xtraindata,datatable(:,1)];
        %         datatable = windowize(harilibur,1:lagstep:lagend);
        %         Xtraindata = [Xtraindata,datatable(:,1:end)];
        %% create Xtestdata and Ytestdata
        awalhari = akhirhari-27; %%
        panjangdata = 5*7; %%2 minggu -> 1 input 1 output
        akhirhari = panjangdata+awalhari-1;
        barisdata = [];
        %         harilibur = [];
        % weekend = [];
        for i = awalhari:akhirhari %% 7 dari 7 hari atau 24*7 jam
            barisdata = [barisdata,matriksdata(i,:)];
            %     weekend = [weekend,ones(1,24)*liburdata(i,1)];
            %             harilibur = [harilibur,ones(1,24)*liburdata(i,2)];
        end
        barisdata = barisdata';
        % weekend = weekend';
        %         harilibur = harilibur';
        datatable = windowize(barisdata,1:lagstep:lagend); %%Y part might be filled by indices
        Xtestdata = datatable(:,1:end-1); %%input training
        Ytestdata = datatable(:,end); %%output training
        % datatable = windowize(weekend,1:lagstep:lagend);
        % Xtestdata = [Xtestdata,datatable(:,1)];
        %         datatable = windowize(harilibur,1:lagstep:lagend);
        %         Xtestdata = [Xtestdata,datatable(:,1:end)];
        %% Start 4-fold CV tuning. Error function MAE. Optimization Method Coupled SA then Nelder-Mead Simplex Algorithm
        tic;
        [gam,sig2] = tunelssvmigwo({Xtraindata,Ytraindata,'f',[],[],'RBF_kernel'},'simplex','crossvalidatelssvm',{4,'mae'});
        waktusim = toc;
        %% Get Alpha Beta from best tuning
        [alpha,b] = trainlssvm({Xtraindata,Ytraindata,'f',gam,sig2,'RBF_kernel'});
        %% Start Prediction
        prediction = simlssvm({Xtraindata,Ytraindata,'f',gam,sig2,'RBF_kernel'},{alpha,b},Xtestdata);
        %     plot([prediction Ytestdata]);
        errorMAE = mean(abs(prediction-Ytestdata)); %Forecast error MAE
        errorMAPE = mean(abs(100*(prediction-Ytestdata)./Ytestdata)); %MAPE
        errorMASE = mean(abs((prediction-Ytestdata)./skala)); %error MASE
        [errorR,RPval] = corr(prediction,Ytestdata); %korelasi
        hasilsimulasi(:,k) = [gam;sig2;errorMAE;waktusim;errorMAPE;errorMASE;errorR^2;RPval];
    end
    %     xlswrite(char(namafile(jj)),hasilsimulasi,'GWO','B2');
    xlswrite(char(namafile(jj)),hasilsimulasi,'IGWO','B2');
    % %% GWO
    hasilsimulasi = zeros(8,30);
    for k = 1:30 %% ambil data 30 kali
        %% susun data yang diregress
        barisdata = [];
        %         harilibur = [];
        % weekend = [];
        awalhari = starting;
        panjangdata = 5*7; %%5 minggu -> 4 minggu data forecast + 1 minggu input
        akhirhari = panjangdata+awalhari-1;
        for i = awalhari:akhirhari %%1:35
            barisdata = [barisdata,matriksdata(i,:)];
            %     weekend = [weekend,ones(1,24)*liburdata(i,1)];
            %             harilibur = [harilibur,ones(1,24)*liburdata(i,2)];
        end
        barisdata = barisdata';
        % weekend = weekend';
        %         harilibur = harilibur';
        %% Windowize data
        predictrange = 24*7;
        lagend = 29*24; %%7 hari input+1 output
        lagstep = 24*7;
        datatable = windowize(barisdata,1:lagstep:lagend); %%Y part might be filled by indices
        Xtraindata = datatable(:,1:end-1); %%input training
        Ytraindata = datatable(:,end); %%output training
        skala = 0;
        for ss = 2:168 %% for computing MASE
            skala = skala + abs(Ytraindata(ss) - Ytraindata(ss-1));
        end
        skala = skala/(168-1);
        % datatable = windowize(weekend,1:lagstep:lagend);
        % Xtraindata = [Xtraindata,datatable(:,1)];
        %         datatable = windowize(harilibur,1:lagstep:lagend);
        %         Xtraindata = [Xtraindata,datatable(:,1:end)];
        %% create Xtestdata and Ytestdata
        awalhari = akhirhari-27; %%
        panjangdata = 5*7; %%2 minggu -> 1 input 1 output
        akhirhari = panjangdata+awalhari-1;
        barisdata = [];
        %         harilibur = [];
        % weekend = [];
        for i = awalhari:akhirhari %% 7 dari 7 hari atau 24*7 jam
            barisdata = [barisdata,matriksdata(i,:)];
            %     weekend = [weekend,ones(1,24)*liburdata(i,1)];
            %             harilibur = [harilibur,ones(1,24)*liburdata(i,2)];
        end
        barisdata = barisdata';
        % weekend = weekend';
        %         harilibur = harilibur';
        datatable = windowize(barisdata,1:lagstep:lagend); %%Y part might be filled by indices
        Xtestdata = datatable(:,1:end-1); %%input training
        Ytestdata = datatable(:,end); %%output training
        % datatable = windowize(weekend,1:lagstep:lagend);
        % Xtestdata = [Xtestdata,datatable(:,1)];
        %         datatable = windowize(harilibur,1:lagstep:lagend);
        %         Xtestdata = [Xtestdata,datatable(:,1:end)];
        %% Start 4-fold CV tuning. Error function MAE. Optimization Method Coupled SA then Nelder-Mead Simplex Algorithm
        tic;
        [gam,sig2] = tunelssvmgwo({Xtraindata,Ytraindata,'f',[],[],'RBF_kernel'},'simplex','crossvalidatelssvm',{4,'mae'});
        waktusim = toc;
        %% Get Alpha Beta from best tuning
        [alpha,b] = trainlssvmgwo({Xtraindata,Ytraindata,'f',gam,sig2,'RBF_kernel'});
        %% Start Prediction
        prediction = simlssvm({Xtraindata,Ytraindata,'f',gam,sig2,'RBF_kernel'},{alpha,b},Xtestdata);
        %     plot([prediction Ytestdata]);
        errorMAE = mean(abs(prediction-Ytestdata)); %Forecast error MAE
        errorMAPE = mean(abs(100*(prediction-Ytestdata)./Ytestdata)); %MAPE
        errorMASE = mean(abs((prediction-Ytestdata)./skala)); %error MASE
        [errorR,RPval] = corr(prediction,Ytestdata); %korelasi
        hasilsimulasi(:,k) = [gam;sig2;errorMAE;waktusim;errorMAPE;errorMASE;errorR^2;RPval];
    end

完整代码:基于改进灰狼算法(IGWO)优化的LSSVM进行负荷预测(Matlab代码实现)

3 参考文献

部分理论来源于网络,如有侵权请联系删除。

[1]刘雨薇,刘辉,陈馨凝,李侯君,邹琪骁.基于DE-GWO-LSSVM模型的用电负荷预测[J].湖北工业大学学报,2019,34(4):30-3462

[2]李威. 基于灰狼优化算法地区电网负荷预测研究[D].沈阳农业大学,2022.DOI:10.27327/d.cnki.gshnu.2022.000510.

[3]于军琪,聂己开,赵安军,侯雪妍.基于特征挖掘的ARIMA-GRU短期电力负荷预测[J].电力系统及其自动化学报,2022,34(3):91-99

4 Matlab代码实现

你可能感兴趣的:(负荷预测,matlab,开发语言)