├── LICENSE ├── MAIL_code ├── GM 2024_Matlab │ ├── Cooperative_system.m │ ├── F_costval_DP_FLT.m │ ├── F_costval_RL_FLT.m │ ├── comparative_batt_pol_soc51.csv │ ├── comparative_cost_soc51.csv │ ├── cost_calculation.m │ ├── desktop.ini │ ├── init.m │ ├── interrupt10-13_dp51.xls │ ├── interrupt10-15_dp51.xls │ ├── interrupt11-14_dp51.xls │ ├── interrupt12-15_dp51.xls │ ├── mainDP_Interrupted.m │ ├── plot_batt_pol.m │ ├── plot_cost.m │ ├── plot_results.m │ ├── price.xlsx │ └── result_of_interruption10-15_max3_dp51.csv └── GM2024_Python │ ├── calculate_cost.py │ ├── install.ipynb │ ├── interrupt10-13_dp11.xls │ ├── interrupt10-13_dp51.xls │ ├── interrupt10-13_dp51_meaning.xls │ ├── interrupt10-15_dp11.xls │ ├── interrupt10-15_dp51.xls │ ├── interrupt11-14_dp11.xls │ ├── interrupt11-14_dp51.xls │ ├── interrupt12-15_dp11.xls │ ├── interrupt12-15_dp51.xls │ ├── main.py │ ├── policy10-15_max3_dp11.csv │ ├── policy10-15_max3_dp51.csv │ ├── price.xlsx │ ├── requirements.txt │ ├── result_of_interruption10-15_max3_dp11.csv │ ├── result_of_interruption10-15_max3_dp51.csv │ ├── result_of_interruption10-15_max_dp51.csv │ └── result_of_interruption10-15_max_dp51_meaning.csv └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/Cooperative_system.m: -------------------------------------------------------------------------------- 1 | clc 2 | clear all 3 | 4 | init 5 | 6 | N=50; %soc discrete 7 | delta=(s_u-s_l)/N; 8 | states=s_l:delta:s_u; 9 | combine_st = []; 10 | 11 | for st_add = 1:size(states,2) 12 | combine_st=[combine_st;states(st_add)*ones(size(states,2),1) states']; 13 | end 14 | 15 | Each_cell = zeros(1,size(combine_st,1)); 16 | 17 | for tab_time = 1:K 18 | for tab_stat = 1:size(combine_st,1) 19 | value_table{tab_stat,tab_time} = Each_cell; 20 | end 21 | end 22 | 23 | states_power = states*Es; 24 | initial_pos = 6; 25 | gamma = 1; 26 | z_value = -100:4:100; 27 | a = 1; 28 | Run = 50; 29 | iter = 4000; 30 | epsilon = 0.5; 31 | 32 | N_repeat = 1; 33 | help_ep = 100; 34 | 35 | lambda = -lambda; 36 | event_prob = 0; 37 | fault_time = [10 11 12 13 14 15]; 38 | 39 | for rr = 1:Run 40 | for tab_time = 1:K 41 | for tab_stat = 1:size(combine_st,1) 42 | value_table{tab_stat,tab_time} = Each_cell; 43 | end 44 | end 45 | epsilon = 0.5; 46 | help_ep = 50; 47 | tic 48 | for i = 1:iter 49 | initial_pow = L0*Es; 50 | stateK0 = [L0 L0]; 51 | 52 | alpha = 0.05; 53 | if i == help_ep 54 | epsilon = epsilon/1.1; 55 | help_ep = help_ep+50; 56 | end 57 | save_transition = initial_pos; 58 | for time = 1:K 59 | help_combination = []; 60 | power = []; 61 | status = ones(1,size(combine_st,1)); 62 | powerbatt=(combine_st-stateK0)*Es; %energy change rate in batt 63 | power=powerbatt; 64 | for indx = 1:size(combine_st,2) 65 | index_ch=find(powerbatt(:,indx)>0); % charging power index 66 | index_dis=find(powerbatt(:,indx)<0); % discharing power index 67 | power(index_ch,indx)=power(index_ch,indx)/eta_ch; % charging power at AC 68 | power(index_dis,indx)=power(index_dis,indx)*eta_dis; %discharing power at AC 69 | index_inf=find(power(:,indx)>pmax_ch | power(:,indx)<-pmax_dis ); % infeasible solution 70 | status(index_inf) = 0; 71 | end 72 | help_assign = find(status>0); 73 | 74 | 75 | if rand < epsilon 76 | taking_action = help_assign(randi([1,size(help_assign,2)],1,1)); 77 | h_pos = find(help_assign == taking_action); 78 | 79 | for tw_st = 1:2 80 | if tw_st == 1 81 | real_batt = power(help_assign(h_pos),tw_st); 82 | [cost_cal,val_out] = F_costval_RL(real_batt,time,DA_load,DA_E,DA_price); 83 | cost_agts(tw_st) = cost_cal; 84 | else 85 | real_batt = power(help_assign(h_pos),tw_st); 86 | [cost_cal,val_out] = F_costval_RL_FLT(real_batt,time,DA_load,DA_E,DA_price); 87 | cost_agts(tw_st) = cost_cal; 88 | end 89 | end 90 | optimized_cost = sum(cost_agts.*[event_prob (1-event_prob)]); 91 | 92 | else 93 | if time < K 94 | cost_cal = value_table{save_transition,time}(1,help_assign); 95 | [opt_cost,pos] = min(cost_cal); 96 | taking_action = help_assign(pos); 97 | for tw_st = 1:2 98 | if tw_st == 1 99 | real_batt = power(taking_action,tw_st); 100 | [cost_cal,val_out] = F_costval_RL(real_batt,time,DA_load,DA_E,DA_price); 101 | cost_agts(tw_st) = cost_cal; 102 | else 103 | real_batt = power(taking_action,tw_st); 104 | [cost_cal,val_out] = F_costval_RL_FLT(real_batt,time,DA_load,DA_E,DA_price); 105 | cost_agts(tw_st) = cost_cal; 106 | end 107 | 108 | end 109 | optimized_cost = sum(cost_agts.*[event_prob (1-event_prob)]); 110 | 111 | else 112 | for all_poss = 1:size(help_assign,2) 113 | for tw_st = 1:2 114 | if tw_st == 1 115 | real_batt = power(help_assign(all_poss),tw_st); 116 | [cst_cal,val_out] = F_costval_RL(real_batt,time,DA_load,DA_E,DA_price); 117 | cost_agts(tw_st) = cst_cal; 118 | else 119 | real_batt = power(help_assign(all_poss),tw_st); 120 | [cst_cal,val_out] = F_costval_RL_FLT(real_batt,time,DA_load,DA_E,DA_price); 121 | cost_agts(tw_st) = cst_cal; 122 | end 123 | 124 | end 125 | cost_cal(all_poss) = sum(cost_agts.*[event_prob (1-event_prob)]); 126 | save_mic_pol(all_poss,:) = val_out; 127 | end 128 | [optimized_cost,pos] = min(cost_cal); 129 | taking_action = help_assign(pos); 130 | val_out = save_mic_pol(pos,:); 131 | end 132 | 133 | end 134 | pos_soc = combine_st(taking_action,:); 135 | 136 | if time < K-1 137 | max_q_val = min(value_table{taking_action,time+1}); 138 | end 139 | 140 | if time < K-1 141 | value_table{save_transition,time}(1,taking_action) = optimized_cost + max_q_val; 142 | else if time == K-1 143 | value_table{save_transition,time}(1,taking_action) = optimized_cost; 144 | end 145 | end 146 | save_transition = taking_action; 147 | stateK0 = pos_soc; 148 | batt_pol(time,:) = pos_soc; 149 | save_cost_t(time) = optimized_cost; 150 | save_microgrid_pol(time,:) = val_out; 151 | end 152 | 153 | save_cost_iter(i) = sum(save_cost_t); 154 | save_timQ(i) = toc; 155 | end 156 | save_cost_run(rr,:) = save_cost_iter; 157 | save_time_run(rr,:) = save_timQ; 158 | end 159 | plot_avg_cost = sum(save_cost_run)/Run; 160 | Plot_avg_time = sum(save_time_run)/Run; 161 | plot(plot_avg_cost) 162 | toc 163 | %%%%%%%%%%%%%%For Real-time Decision%%%%%%%%%%%%%%%%%%%%%%%%%%% 164 | 165 | 166 | -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/F_costval_DP_FLT.m: -------------------------------------------------------------------------------- 1 | function [temp] = F_costval_DP_FLT(power,k,DA_load,DA_E,DA_price,fault_time) 2 | dg_a = 0.0009; 3 | dg_b = 0.0213; 4 | dg_c = 1.1; 5 | min_dg = 0.3*100; 6 | j = k; 7 | pow_siz = size(power,2); 8 | 9 | sig = 0; 10 | time = k; 11 | %if time == fault_time(1) || time == fault_time(2) || time == fault_time(3) || time == fault_time(4) || time == fault_time(5) || time == fault_time(6) 12 | % if interrupted hours are larger than 4, please change this line; 13 | % for example, interrupted time is 10-15 14 | 15 | if time == fault_time(1) || time == fault_time(2) || time == fault_time(3) || time == fault_time(4) 16 | sig = 1; 17 | end 18 | 19 | for each_pow = 1:pow_siz 20 | real_batt = power(each_pow); 21 | fi_cost = []; 22 | if DA_load(j) > DA_E(j) 23 | 24 | if real_batt <= 0 25 | batt_discharge = -real_batt; 26 | batt_dch = min(batt_discharge,DA_load(j)-DA_E(j)); 27 | remaining_demd = DA_load(j) - DA_E(j) - batt_dch; 28 | batt_charge = 0; 29 | from_grid = max(0,remaining_demd); 30 | to_grid = 0; 31 | 32 | if sig == 1 33 | dg_cost = (dg_a*(max(from_grid,min_dg))^2)+(dg_b*max(from_grid,min_dg))+dg_c; 34 | dg_output = max(from_grid,min_dg); 35 | from_grid = 0; 36 | fi_cost = dg_cost; 37 | else 38 | 39 | grid_cost = from_grid*DA_price(j); 40 | dg_cost = (dg_a*(max(from_grid,min_dg))^2)+(dg_b*max(from_grid,min_dg))+dg_c; 41 | cost_com = [grid_cost dg_cost]; 42 | [c_opt,pos_opt]=min(cost_com); 43 | if pos_opt == 1 44 | dg_output = 0; 45 | else 46 | dg_output = max(from_grid,min_dg); 47 | from_grid = 0; 48 | end 49 | 50 | fi_cost = c_opt; 51 | end 52 | 53 | output = [from_grid to_grid batt_discharge batt_charge dg_output]; 54 | 55 | else 56 | remaining_demd = DA_load(j) - DA_E(j) + real_batt; 57 | batt_discharge = 0; 58 | batt_charge = real_batt; 59 | from_grid = max(0,remaining_demd); 60 | to_grid = 0; 61 | 62 | if sig == 1 63 | dg_cost = (dg_a*(max(from_grid,min_dg))^2)+(dg_b*max(from_grid,min_dg))+dg_c; 64 | dg_output = max(from_grid,min_dg); 65 | from_grid = 0; 66 | fi_cost = dg_cost; 67 | else 68 | 69 | 70 | grid_cost = from_grid*DA_price(j); 71 | dg_cost = (dg_a*(max(from_grid,min_dg))^2)+(dg_b*max(from_grid,min_dg))+dg_c; 72 | cost_com = [grid_cost dg_cost]; 73 | [c_opt,pos_opt]=min(cost_com); 74 | if pos_opt == 1 75 | dg_output = 0; 76 | else 77 | dg_output = max(from_grid,min_dg); 78 | from_grid = 0; 79 | end 80 | 81 | fi_cost = c_opt; 82 | end 83 | output = [from_grid to_grid batt_discharge batt_charge dg_output]; 84 | end 85 | 86 | else 87 | if real_batt <= 0 88 | batt_discharge = -real_batt; 89 | extra_enrg = DA_E(j) - DA_load(j); 90 | batt_charge = 0; 91 | from_grid = 0; 92 | to_grid = extra_enrg + batt_discharge; 93 | fi_cost = -to_grid*DA_price(j); 94 | dg_output = 0; 95 | 96 | 97 | if sig == 1 98 | to_grid = 0; 99 | fi_cost = 0; 100 | end 101 | output = [from_grid to_grid batt_discharge batt_charge dg_output]; 102 | else 103 | enrgy_req = DA_load(j) + real_batt - DA_E(j); 104 | batt_discharge = 0; 105 | batt_charge = real_batt; 106 | from_grid = max(0,enrgy_req); 107 | to_grid = min(0,enrgy_req); 108 | dg_output = 0; 109 | posi_or_negi = (from_grid+to_grid); 110 | fi_cost = (from_grid+to_grid)*DA_price(j); 111 | 112 | if sig == 1 113 | if from_grid > 0 114 | 115 | dg_output = max(from_grid,min_dg); 116 | dg_cost = (dg_a*(max(from_grid,min_dg))^2)+(dg_b*max(from_grid,min_dg))+dg_c; 117 | fi_cost = dg_cost; 118 | end 119 | from_grid = 0; 120 | to_grid = 0; 121 | 122 | else 123 | 124 | if posi_or_negi>0 125 | 126 | grid_cost = posi_or_negi*DA_price(j); 127 | dg_cost = (dg_a*(max(from_grid,min_dg))^2)+(dg_b*max(from_grid,min_dg))+dg_c; 128 | cost_com = [grid_cost dg_cost]; 129 | [c_opt,pos_opt]=min(cost_com); 130 | if pos_opt == 1 131 | dg_output = 0; 132 | else 133 | dg_output = max(from_grid,min_dg); 134 | from_grid = 0; 135 | fi_cost = c_opt; 136 | end 137 | 138 | end 139 | end 140 | output = [from_grid to_grid batt_discharge batt_charge dg_output]; 141 | end 142 | 143 | end 144 | sav_cost_pow(each_pow) = fi_cost; 145 | end 146 | temp = sav_cost_pow; 147 | end -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/F_costval_RL_FLT.m: -------------------------------------------------------------------------------- 1 | function [c_SR,val_out] = F_costval_RL_FLT(real_batt,time,DA_load,DA_E,DA_price,fault_time) 2 | dg_a = 0.0009; 3 | dg_b = 0.0213; 4 | dg_c = 1.1; 5 | min_dg = 0.3*100; 6 | j = time; 7 | 8 | sig = 0; 9 | %if time == fault_time(1) || time == fault_time(2) || time == fault_time(3) || time == fault_time(4) || time == fault_time(5) || time == fault_time(6) 10 | % if interrupted hours are larger than 4, please change this line; 11 | % for example, interrupted time is 10-15 12 | if time == fault_time(1) || time == fault_time(2) || time == fault_time(3) || time == fault_time(4) 13 | sig = 1; 14 | end 15 | 16 | if DA_load(j) > DA_E(j) 17 | 18 | if real_batt <= 0 19 | batt_discharge = -real_batt; 20 | batt_dch = min(batt_discharge,DA_load(j)-DA_E(j)); 21 | remaining_demd = DA_load(j) - DA_E(j) - batt_dch; 22 | batt_charge = 0; 23 | from_grid = max(0,remaining_demd); 24 | to_grid = 0; 25 | %% 26 | if sig == 1 27 | dg_cost = (dg_a*(max(from_grid,min_dg))^2)+(dg_b*max(from_grid,min_dg))+dg_c; 28 | dg_output = max(from_grid,min_dg); 29 | from_grid = 0; 30 | fi_cost = dg_cost; 31 | else 32 | %% 33 | grid_cost = from_grid*DA_price(j); 34 | dg_cost = (dg_a*(max(from_grid,min_dg))^2)+(dg_b*max(from_grid,min_dg))+dg_c; 35 | cost_com = [grid_cost dg_cost]; 36 | [c_opt,pos_opt]=min(cost_com); 37 | if pos_opt == 1 38 | dg_output = 0; 39 | else 40 | dg_output = max(from_grid,min_dg); 41 | from_grid = 0; 42 | end 43 | 44 | fi_cost = c_opt; 45 | end 46 | 47 | output = [from_grid to_grid batt_discharge batt_charge dg_output]; 48 | 49 | else 50 | remaining_demd = DA_load(j) - DA_E(j) + real_batt; 51 | batt_discharge = 0; 52 | batt_charge = real_batt; 53 | from_grid = max(0,remaining_demd); 54 | to_grid = 0; 55 | 56 | if sig == 1 57 | dg_cost = (dg_a*(max(from_grid,min_dg))^2)+(dg_b*max(from_grid,min_dg))+dg_c; 58 | dg_output = max(from_grid,min_dg); 59 | from_grid = 0; 60 | fi_cost = dg_cost; 61 | else 62 | 63 | grid_cost = from_grid*DA_price(j); 64 | dg_cost = (dg_a*(max(from_grid,min_dg))^2)+(dg_b*max(from_grid,min_dg))+dg_c; 65 | cost_com = [grid_cost dg_cost]; 66 | [c_opt,pos_opt]=min(cost_com); 67 | if pos_opt == 1 68 | dg_output = 0; 69 | else 70 | dg_output = max(from_grid,min_dg); 71 | from_grid = 0; 72 | end 73 | 74 | fi_cost = c_opt; 75 | end 76 | 77 | output = [from_grid to_grid batt_discharge batt_charge dg_output]; 78 | end 79 | 80 | else 81 | 82 | if real_batt <= 0 83 | batt_discharge = -real_batt; 84 | extra_enrg = DA_E(j) - DA_load(j); 85 | batt_charge = 0; 86 | from_grid = 0; 87 | to_grid = extra_enrg + batt_discharge; 88 | fi_cost = -to_grid*DA_price(j); 89 | dg_output = 0; 90 | 91 | if sig == 1 92 | to_grid = 0; 93 | fi_cost = 0; 94 | end 95 | 96 | output = [from_grid to_grid batt_discharge batt_charge dg_output]; 97 | else 98 | enrgy_req = DA_load(j) + real_batt - DA_E(j); 99 | batt_discharge = 0; 100 | batt_charge = real_batt; 101 | from_grid = max(0,enrgy_req); 102 | to_grid = min(0,enrgy_req); 103 | dg_output = 0; 104 | posi_or_negi = (from_grid+to_grid); 105 | fi_cost = (from_grid+to_grid)*DA_price(j); 106 | 107 | if sig == 1 108 | if from_grid > 0 109 | 110 | dg_output = max(from_grid,min_dg); 111 | dg_cost = (dg_a*(max(from_grid,min_dg))^2)+(dg_b*max(from_grid,min_dg))+dg_c; 112 | fi_cost = dg_cost; 113 | end 114 | from_grid = 0; 115 | to_grid = 0; 116 | 117 | else 118 | 119 | if posi_or_negi>0 120 | 121 | grid_cost = posi_or_negi*DA_price(j); 122 | dg_cost = (dg_a*(max(from_grid,min_dg))^2)+(dg_b*max(from_grid,min_dg))+dg_c; 123 | cost_com = [grid_cost dg_cost]; 124 | [c_opt,pos_opt]=min(cost_com); 125 | if pos_opt == 1 126 | dg_output = 0; 127 | else 128 | dg_output = max(from_grid,min_dg); 129 | from_grid = 0; 130 | fi_cost = c_opt; 131 | end 132 | end 133 | end 134 | output = [from_grid to_grid batt_discharge batt_charge dg_output]; 135 | end 136 | 137 | end 138 | 139 | c_SR = fi_cost; 140 | val_out = output; 141 | 142 | end 143 | -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/comparative_batt_pol_soc51.csv: -------------------------------------------------------------------------------- 1 | 0.499999434,0.579999328,0.579999208,0.739998937,0.899998784,0.899998844,0.878665566,0.793332458,0.649332583,0.611999273,0.563999295,0.553332567,0.56933248,0.539999187,0.547999263,0.627999187,0.627999246,0.55866611,0.366666317,0.20133324,0.100000001,0.100000001,0.100000001,0.100000001 2 | 0.5,0.58,0.58,0.74,0.9,0.9,0.804,0.708,0.564,0.58,0.596,0.612,0.628,0.644,0.66,0.66,0.66,0.58,0.388,0.212,0.1,0.1,0.1,0.1 3 | 0.468,0.484,0.484,0.484,0.644,0.676,0.484,0.292,0.148,0.164,0.18,0.196,0.212,0.228,0.26,0.26,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1 4 | -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/comparative_cost_soc51.csv: -------------------------------------------------------------------------------- 1 | -0.514805712,1.420690692,0.255033393,2.127239084,2.652566736,2.658266466,3.468853571,2.902466976,0.060525452,-1.129590782,-2.540019436,-3.153513636,-3.266978193,-3.578987083,-2.588704388,-0.951816212,-1.577820743,2.940862168,3.031760544,2.94602574,2.965995546,4.380634687,3.531717634,2.543279777 2 | -0.514800785,1.420691802,0.25503442,2.127241903,2.652568331,2.658265683,2.549611761,2.77145292,0.060523706,-0.406611556,-1.701609817,-2.777743752,-3.266976908,-3.042287159,-2.487259423,-1.959816732,-1.577821543,2.808527839,3.031757872,2.813514079,2.832804979,4.380634687,3.531717634,2.543279777 3 | -0.793212305,0.751332394,0.25503442,0.467790051,2.652568331,3.07818035,1.367730321,1.638171015,0.060523706,-0.406611556,-1.701609817,-2.777743752,-3.266976908,-3.042287159,-2.284367571,-1.959816732,-3.318174343,3.913336633,5.711602069,5.193086589,4.401773337,4.380634687,3.531717634,2.543279777 4 | -2.185269905,0,0.11731522,0.467790051,2.652568331,2.658265683,3.337532721,2.969697796,1.396859546,-0.656777482,-1.946044188,-3.021749678,-3.503842389,-3.258263159,-2.690151275,-1.959816732,-1.577821543,3.913336633,5.711602069,5.193086589,4.401773337,4.380634687,3.531717634,2.543279777 5 | -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/cost_calculation.m: -------------------------------------------------------------------------------- 1 | function [y] = cost_calculation(power,combine_states,k,Fault_hour) 2 | 3 | init; 4 | dg_cap = 100; 5 | DA_price(Fault_hour) = 100; 6 | for i_c = 1:2*(N+1) 7 | if DA_load(k) > DA_E(k) 8 | 9 | if power(i_c) <= 0 10 | 11 | batt_discharge = min(-power(i_c),max(0,DA_load(k)-DA_E(k))); 12 | remaining_demd = DA_load(k) - DA_E(k) - batt_discharge; 13 | final_cost(i_c) = 0; 14 | if remaining_demd > 0 15 | cost_comparison1 = (combine_states(i_c,2)*dg_a*(max(15,min(remaining_demd,dg_cap)))^2) + (dg_b*(max(15,min(remaining_demd,dg_cap))*combine_states(i_c,2))) + dg_c*combine_states(i_c,2); 16 | cc1_rem_demd = max(0,remaining_demd - (max(15,min(remaining_demd,dg_cap))*combine_states(i_c,2))); 17 | cost_comparison1 = cost_comparison1 + (cc1_rem_demd*DA_price(k)); 18 | final_cost(i_c) = cost_comparison1; 19 | end 20 | 21 | else 22 | remaining_demd = DA_load(k) - DA_E(k) + power(i_c); 23 | final_cost(i_c) = 0; 24 | if remaining_demd > 0 25 | cost_comparison1 = (combine_states(i_c,2)*dg_a*(max(15,min(remaining_demd,dg_cap)))^2) + (dg_b*(max(15,min(remaining_demd,dg_cap))*combine_states(i_c,2))) + dg_c*combine_states(i_c,2); 26 | cc1_rem_demd = max(0,remaining_demd - (max(15,min(remaining_demd,dg_cap))*combine_states(i_c,2))); 27 | cost_comparison1 = cost_comparison1 + (cc1_rem_demd*DA_price(k)); 28 | final_cost(i_c) = cost_comparison1; 29 | end 30 | 31 | end 32 | else 33 | if power(i_c) <= 0 34 | cost_comparison1 = (combine_states(i_c,2)*dg_a*(15)^2) + (dg_b*(15)*combine_states(i_c,2)) + dg_c*combine_states(i_c,2); 35 | extra_enrg = DA_load(k) - DA_E(k) + power(i_c); 36 | if k == Fault_hour 37 | final_cost(i_c) = cost_comparison1-(extra_enrg*DA_price(k)); 38 | else 39 | final_cost(i_c) = cost_comparison1 + (DA_price(k)*(extra_enrg)); 40 | end 41 | else 42 | enrgy_req = DA_load(k) + power(i_c) - DA_E(k); 43 | final_cost(i_c) = 0; 44 | if enrgy_req > 0 45 | cost_comparison1 = (combine_states(i_c,2)*dg_a*(max(15,min(enrgy_req,dg_cap)))^2) + (dg_b*(max(15,min(enrgy_req,dg_cap))*combine_states(i_c,2))) + dg_c*combine_states(i_c,2); 46 | cc1_rem_demd = max(0,enrgy_req - (max(15,min(enrgy_req,dg_cap))*combine_states(i_c,2))); 47 | cost_comparison1 = cost_comparison1 + (cc1_rem_demd*DA_price(k)); 48 | final_cost(i_c) = cost_comparison1; 49 | else 50 | cost_comparison1 = (combine_states(i_c,2)*dg_a*(15)^2) + (dg_b*(15)*combine_states(i_c,2)) + dg_c*combine_states(i_c,2); 51 | if k == Fault_hour 52 | final_cost(i_c) = cost_comparison1 - (enrgy_req*DA_price(k)); 53 | else 54 | final_cost(i_c) = cost_comparison1 + (enrgy_req*DA_price(k)); 55 | end 56 | end 57 | 58 | end 59 | 60 | end 61 | 62 | end 63 | 64 | 65 | y = final_cost; 66 | 67 | end 68 | -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/desktop.ini: -------------------------------------------------------------------------------- 1 | [.ShellClassInfo] 2 | ConfirmFileOp=0 3 | IconResource=C:\Program Files\Google\Drive File Stream\64.0.4.0\GoogleDriveFS.exe,23 4 | -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/init.m: -------------------------------------------------------------------------------- 1 | eta_dis=0.9; batt_para(1)=eta_dis; %discharging 2 | eta_ch=0.9; batt_para(2)=eta_ch; %charging 3 | Es=150/eta_dis; batt_para(3)=Es; % batt energy capacity 4 | pmax_dis=30; batt_para(4)=pmax_dis; %batt discharging power capacity 5 | pmax_ch=30; batt_para(5)=pmax_ch; %batt charging power capacity 6 | s_l=0.1; s_u=0.9; %soc lower and upper bound 7 | 8 | L0=0.5; %initial SOC 9 | LK=0.1; %specified SOC at the end 10 | K=24; 11 | %N=2; %soc discrete 12 | batt_cost = 0.05; 13 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 14 | % file = 'testing.xlsx'; 15 | % data = xlsread(file); 16 | 17 | DA_load = [61.05,55.39,53.15,53.10,52.98,55.53,66.27,83.15,78.98,67.34,67.83,64.36,61.35,59.11,59.04,63.01,76.97,104.91,119.59,117.77,110.19,102.31,87.24,72.45]; 18 | DA_load = ((DA_load*90)/max(DA_load)); 19 | PV_irradiation = [0 0 0 0 0 1 115 320 528 702 838 922 949 922 838 702 528 320 115 1 0 0 0 0]; 20 | DA_wind = [24.87,14.22,16.13,14.34,10.22,1.94,0,0,0,0,0,0,0,0,0,1.6,10.55,10.10,9.61,13.09,14.75,9.33,3.80,4.19]; 21 | PV = [0,0,0,0,0,0,2.00,6.95,16.87,26.52,33.86,38.62,40.83,40.46,37.98,32.98,25.60,5.16,1.83,0,0,0,0,0]; 22 | DA_E = PV + DA_wind; 23 | DA_E = ((DA_E*90)/max(DA_E)); 24 | %%%%%%%%%%%%%%%%%%%%DR Incentives %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 25 | %w_dr_incentive =0.02; 26 | %DA_price = (DA_load./DA_E)*w_dr_incentive; 27 | 28 | lambda=xlsread('price.xlsx','B2:B8761'); %load price information 29 | DA_price = lambda(1:24)/1000; 30 | 31 | %%%% DG Parameters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 32 | dg_a = 0.00104; 33 | dg_b = 0.0304; 34 | dg_c = 1.3; 35 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 36 | -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/interrupt10-13_dp51.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YanbinLin94/An-Imitation-Learning-Method-with-Multi-Virtual-Agents-for-Microgrid-Energy-Optimization/99dfc3d48b1de152f6f4b9d4579424c2a4016a9b/MAIL_code/GM 2024_Matlab/interrupt10-13_dp51.xls -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/interrupt10-15_dp51.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YanbinLin94/An-Imitation-Learning-Method-with-Multi-Virtual-Agents-for-Microgrid-Energy-Optimization/99dfc3d48b1de152f6f4b9d4579424c2a4016a9b/MAIL_code/GM 2024_Matlab/interrupt10-15_dp51.xls -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/interrupt11-14_dp51.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YanbinLin94/An-Imitation-Learning-Method-with-Multi-Virtual-Agents-for-Microgrid-Energy-Optimization/99dfc3d48b1de152f6f4b9d4579424c2a4016a9b/MAIL_code/GM 2024_Matlab/interrupt11-14_dp51.xls -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/interrupt12-15_dp51.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YanbinLin94/An-Imitation-Learning-Method-with-Multi-Virtual-Agents-for-Microgrid-Energy-Optimization/99dfc3d48b1de152f6f4b9d4579424c2a4016a9b/MAIL_code/GM 2024_Matlab/interrupt12-15_dp51.xls -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/mainDP_Interrupted.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%% This code was original written by Avijit Das in 2021 and was modified and maintained by Yanbin Lin in 2023.%%%%%%%%% 2 | % If this code is used for any research purpose, please cite our PESGM’24 paper below. 3 | % Yanbin Lin, Zhen Ni, and Yufei Tang, “An Imitation Learning Method with Multi virtual Agents for Microgrid Energy Optimization under Interrupted Periods,” in Proc. of IEEE Power & Energy Society General Meeting (PESGM’24), pp.1-5, Washington, DC, USA, Jul. 21-25, 2024. 4 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 5 | 6 | clear,clc 7 | 8 | init 9 | tic 10 | N=50; %SOC discrete size; if SOC size=51, then N is 50 11 | delta=(s_u-s_l)/N; 12 | states=s_l:delta:s_u; 13 | SS{K}=0; 14 | 15 | stateNum=1; 16 | stateK0=L0; 17 | costK0=0; %stateNum by 1; 18 | fault_time = [10 11 12 13]; % Interrupted time slots. You can change it to [11 12 13 14] or [12 13 14 15]. 19 | % create interruption_status 20 | Interruption_status = zeros(24,1); 21 | time_slots = length(fault_time); 22 | Time = 1:1:24; 23 | 24 | for index=1:24 25 | if any(fault_time(:) == index) 26 | Interruption_status(index) = 1; 27 | end 28 | end 29 | 30 | for k=1:K 31 | powercost=zeros(stateNum,N+1); 32 | for n=1:stateNum 33 | powerbatt=(states-stateK0(n))*Es; %energy change rate in batt 34 | index_ch=find(powerbatt>0); % charging power index 35 | index_dis=find(powerbatt<0); % discharing power index 36 | power=powerbatt; 37 | power(index_ch)=power(index_ch)/eta_ch; % charging power at AC 38 | power(index_dis)=power(index_dis)*eta_dis; %discharing power at AC 39 | index_inf=find(power>pmax_ch | power<-pmax_dis ); % infeasible solution 40 | %temp=power*lambda(k); %charging/discharging cost 41 | [temp] = F_costval_DP_FLT(power,k,DA_load,DA_E,DA_price,fault_time); 42 | temp(index_inf)=inf; %penalty infeasible solution with inf cost 43 | powercost(n,:)=temp; 44 | end 45 | costtemp=costK0*ones(1,N+1)+powercost; 46 | toc2=toc; 47 | 48 | 49 | [costK0,indexK0]=min(costtemp,[],1); 50 | index_fea=find(costK00 76 | pkDP(i)=pbatt/eta_ch; 77 | elseif pbatt<0 78 | pkDP(i)=pbatt*eta_dis; 79 | end 80 | time = i; 81 | real_batt = pkDP(i); 82 | [c_SR,val_out] = F_costval_RL_FLT(real_batt,time,DA_load,DA_E,DA_price,fault_time); 83 | save_dp_cost(i) = c_SR; 84 | save_outs(i,:) = val_out; 85 | end 86 | pkDP=-pkDP; 87 | socDP(1)=[]; 88 | Total_cost = sum(save_dp_cost) 89 | 90 | % save a table with soc policy, other units outputs and conditions 91 | Table = [DA_E', DA_load', DA_price, DA_wind', PV', PV_irradiation', Interruption_status, Time', socDP', save_outs]; 92 | writematrix(Table,'interrupt10-13_dp51.xls');% remember to change the file name according to the interrupted times and the discrete size. 93 | 94 | %% 95 | figure 96 | subplot(3,1,1) 97 | stairs(0:24,[lambda(1:24);lambda(24)]) 98 | set(gca, 'xlim', [0,24], 'xtick', 0:24) 99 | ylim([20,100]) 100 | xlabel('Time(hour)') 101 | ylabel('$/kWh') 102 | title('Grid Power Price') 103 | 104 | subplot(3,1,2) 105 | stairs(0:24,[pkDP pkDP(24)]) 106 | set(gca, 'xlim', [0,24], 'xtick', 0:24) 107 | xlabel('Time(hour)') 108 | ylabel('kW') 109 | title('Power Output') 110 | 111 | subplot(3,1,3) 112 | plot(0:24,[L0 socDP]) 113 | set(gca, 'xlim', [0,24], 'xtick', 0:24) 114 | ylim([0,1]) 115 | xlabel('Time(hour)') 116 | ylabel('SOC') 117 | title('Battery SOC') 118 | 119 | 120 | -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/plot_batt_pol.m: -------------------------------------------------------------------------------- 1 | clear all;clc; 2 | %plot the results of multiple imitation learning 3 | data=csvread('comparative_batt_pol_soc51.csv'); 4 | proposed_method = data(1,:); 5 | dp = data(2,:); 6 | Aggregated = data(3,:); 7 | 8 | t=1:1:24; 9 | figure() 10 | subplot(3,1,1) 11 | stairs(t,proposed_method,'r') 12 | ylim([0 1]) 13 | set(gca, 'xlim', [1,24], 'xtick', 1:24) 14 | ylabel('SOC',fontsize=12) 15 | legend('MAIL',fontsize=12) 16 | ax = gca; 17 | ax.FontSize = 12; 18 | subplot(3,1,2) 19 | stairs(t,dp) 20 | ylim([0 1]) 21 | set(gca, 'xlim', [1,24], 'xtick', 1:24) 22 | ylabel('SOC',fontsize=12) 23 | legend('DP',fontsize=12) 24 | ax = gca; 25 | ax.FontSize = 12; 26 | subplot(3,1,3) 27 | stairs(t,Aggregated) 28 | ylim([0 1]) 29 | set(gca, 'xlim', [1,24], 'xtick', 1:24) 30 | xlabel('Time(hour)',fontsize=12) 31 | ylabel('SOC',fontsize=12) 32 | legend('Aggregated Q',fontsize=12) 33 | ax = gca; 34 | ax.FontSize = 12; 35 | % title('Output of different methods') 36 | 37 | a=data(1:3,:); 38 | figure() 39 | stairs(a') 40 | ylim([0 1]) 41 | set(gca, 'xlim', [1,24], 'xtick', 1:24) 42 | xlabel('Time(hour)',fontsize=12) 43 | ylabel('SOC',fontsize=12) 44 | legend('MAIL','DP','Aggregated Q',fontsize=12) 45 | ax = gca; 46 | ax.FontSize = 12; 47 | title('SOC Comparations of different methods') 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/plot_cost.m: -------------------------------------------------------------------------------- 1 | clear all;clc; 2 | %plot the results of multiple imitation learning 3 | data=csvread('comparative_cost_soc51.csv'); 4 | proposed_method = data(1,:); 5 | dp = data(2,:); 6 | Aggregated_Q = data(3,:); 7 | %cooperative_method = data(4,:); 8 | 9 | t=1:1:24; 10 | figure() 11 | % plot(t,proposed_method,t,dp,t,Aggregated_Q,t,cooperative_method,'LineWidth', 2.5) 12 | plot(t,proposed_method,t,dp,t,Aggregated_Q,'LineWidth', 2.5) 13 | ylim([-4 6]) 14 | set(gca, 'xlim', [1,24], 'xtick', 1:24) 15 | xlabel('Time(hour)',fontsize=12) 16 | ylabel('Cost($)',fontsize=12) 17 | % legend('Proposed Method','DP','Aggregated Q','Cooperative Q-learning',fontsize=12) 18 | legend('MAIL','DP','Aggregated Q',fontsize=12) 19 | ax = gca; 20 | ax.FontSize = 12; 21 | title('Cost of different units') 22 | 23 | 24 | t=1:1:24; 25 | figure() 26 | plot(t,proposed_method,t,dp,'LineWidth', 2.5) 27 | ylim([-4 6]) 28 | set(gca, 'xlim', [1,24], 'xtick', 1:24) 29 | xlabel('Time(hour)',fontsize=12) 30 | ylabel('Cost($)',fontsize=12) 31 | legend('MAIL','DP',fontsize=12) 32 | ax = gca; 33 | ax.FontSize = 12; 34 | title('Cost of MAIL and DP methods') 35 | 36 | 37 | -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/plot_results.m: -------------------------------------------------------------------------------- 1 | clear all;clc; 2 | %plot the results of multiple imitation learning 3 | data=csvread('result_of_interruption10-15_max3_dp51.csv'); 4 | batt_soc = data(1,:); 5 | from_grid = data(2,:); 6 | to_grid = data(3,:); 7 | batt_discharge = data(4,:); 8 | batt_charge = data(5,:); 9 | dg_out = data(6,:); 10 | cost = data(7,:); 11 | batt_out = batt_discharge - batt_charge; 12 | 13 | t=1:1:24; 14 | figure() 15 | plot(t,from_grid,t,to_grid,t,batt_out,t,dg_out,'LineWidth', 2.5) 16 | ylim([-30 66]) 17 | set(gca, 'xlim', [1,24], 'xtick', 1:24) 18 | xlabel('Time(hour)',fontsize=12) 19 | ylabel('Power(kW)',fontsize=12) 20 | legend('From Grid','To Grid','BESS Output','DG Output',fontsize=12) 21 | ax = gca; 22 | ax.FontSize = 12; 23 | title('Power outputs of the proposed MAIL method') 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/price.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YanbinLin94/An-Imitation-Learning-Method-with-Multi-Virtual-Agents-for-Microgrid-Energy-Optimization/99dfc3d48b1de152f6f4b9d4579424c2a4016a9b/MAIL_code/GM 2024_Matlab/price.xlsx -------------------------------------------------------------------------------- /MAIL_code/GM 2024_Matlab/result_of_interruption10-15_max3_dp51.csv: -------------------------------------------------------------------------------- 1 | 4.985559582710266113e-01,5.781683921813964844e-01,5.780562758445739746e-01,7.368409633636474609e-01,8.969079852104187012e-01,8.978770971298217773e-01,8.668473958969116211e-01,7.788441181182861328e-01,6.348497867584228516e-01,5.467217564582824707e-01,5.556592345237731934e-01,5.703502297401428223e-01,5.842569470405578613e-01,5.936498045921325684e-01,6.011499762535095215e-01,6.338479518890380859e-01,6.334861516952514648e-01,5.628291368484497070e-01,3.709980249404907227e-01,2.029190063476562500e-01,1.000000014901161194e-01,1.000000014901161194e-01,1.000000014901161194e-01,1.000000014901161194e-01 2 | 0.000000000000000000e+00,2.508336724617347713e+01,4.427607196779106857e+00,3.775699606038076439e+01,4.698571450427566276e+01,3.769348118837314843e+01,4.080992074214570664e+01,0.000000000000000000e+00,6.530376771160533167e-01,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,5.642980772107787857e+01,5.727812494796849307e+01,4.528793362627232000e+01 3 | 9.092114601312097477e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,2.274866781742430888e+01,2.181291404663009814e+01,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00 4 | 2.166062593460083008e-01,0.000000000000000000e+00,1.681745052337646484e-02,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,4.654455184936523438e+00,1.320049166679382324e+01,2.159914970397949219e+01,1.321920454502105713e+01,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,5.427002906799316406e-02,1.059855222702026367e+01,2.877466678619384766e+01,2.521185278892517090e+01,1.543785072863101959e+01,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00 5 | 0.000000000000000000e+00,1.474304331673516089e+01,0.000000000000000000e+00,2.940457176279138451e+01,2.964204108273541394e+01,1.794651702598289156e-01,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,1.655088530646430023e+00,2.720554669698079575e+00,2.575318018595377456e+00,1.739418065106427047e+00,1.388920678032768974e+00,6.055180673246030487e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00 6 | 0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,3.405619277163065561e+01,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,3.471666971146794367e+01,3.600858082585611442e+01,3.456468348960028436e+01,3.497512099775300243e+01,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00 7 | -5.273644679511447775e-01,1.416638348635588907e+00,2.540693837727754922e-01,2.114637199855715188e+00,2.653269202342095401e+00,2.670982693096834559e+00,3.349478325903683551e+00,2.869238745524354428e+00,6.060261477781458267e-02,-1.772888140819580682e+00,-1.809505011120159157e+00,-2.797706559588266639e+00,-3.297966058923096355e+00,-3.131473671463216313e+00,-2.595043542527919911e+00,-1.547821633164331523e+00,-1.581756918217167573e+00,2.924187505123907460e+00,3.033938875373726418e+00,2.911473368591107835e+00,2.945903257178855927e+00,4.380634687425731144e+00,3.531717633979284177e+00,2.543279776584201013e+00 8 | -------------------------------------------------------------------------------- /MAIL_code/GM2024_Python/calculate_cost.py: -------------------------------------------------------------------------------- 1 | """ 2 | Copyright 2024 Yanbin Lin. All rights reserved. 3 | This code is written by Yanbin Lin in 2023. 4 | If this code is used for any research purpose, please cite our PESGM’24 paper below. 5 | Yanbin Lin, Zhen Ni, and Yufei Tang, “An Imitation Learning Method with Multi virtual Agents for Microgrid Energy Optimization under Interrupted Periods,” in Proc. of IEEE Power & Energy Society General Meeting (PESGM’24), pp.1-5, Washington, DC, USA, Jul. 21-25, 2024. 6 | """ 7 | 8 | import numpy as np 9 | import pandas as pd 10 | from numpy import genfromtxt 11 | 12 | data = pd.read_excel(r'price.xlsx', engine='openpyxl') 13 | price = data[['Energy price']][0:24]/1000 14 | 15 | my_data = genfromtxt('policy10-15_max3_dp51.csv', delimiter=',') 16 | DA_load = [61.05,55.39,53.15,53.10,52.98,55.53,66.27,83.15,78.98,67.34,67.83,64.36,61.35,59.11,59.04,63.01,76.97,104.91,119.59,117.77,110.19,102.31,87.24,72.45] 17 | max_DA_load = max(DA_load) 18 | for i in range(24): 19 | DA_load[i] = (DA_load[i]*90)/max_DA_load 20 | DA_load = np.array(DA_load) 21 | DA_wind = np.array([24.87,14.22,16.13,14.34,10.22,1.94,0,0,0,0,0,0,0,0,0,1.6,10.55,10.10,9.61,13.09,14.75,9.33,3.80,4.19]) 22 | PV = np.array([0,0,0,0,0,0,2.00,6.95,16.87,26.52,33.86,38.62,40.83,40.46,37.98,32.98,25.60,5.16,1.83,0,0,0,0,0]) 23 | DA_E = PV + DA_wind 24 | max_DA_E = max(list(DA_E)) 25 | for i in range(24): 26 | DA_E[i] = (DA_E[i]*90)/max_DA_E 27 | Interruption_time = [9,10,11,12,13,14] 28 | 29 | # calculate real battery energy for 24 times 30 | Soc_diff = [] 31 | initial_soc = 0.5 32 | for soc in my_data: 33 | Soc_diff.append(soc-initial_soc) 34 | initial_soc = soc 35 | 36 | Real_batt = [] 37 | Batt_Charge = [] 38 | Batt_Discharge = [] 39 | for soc_d in Soc_diff: 40 | if soc_d > 0: 41 | Real_batt.append(soc_d*150/0.9/0.9) 42 | Batt_Charge.append(soc_d*150/0.9/0.9) 43 | Batt_Discharge.append(0) 44 | else: 45 | Real_batt.append(soc_d * 150) 46 | Batt_Discharge.append(-soc_d * 150) 47 | Batt_Charge.append(0) 48 | 49 | NET_load = DA_load - DA_E 50 | Batt_Charge = abs(np.array(Batt_Charge)) 51 | Batt_Discharge = abs(np.array(Batt_Discharge)) 52 | Remain_load = DA_load - DA_E - Batt_Discharge + Batt_Charge 53 | From_Grid = [] 54 | To_Grid = [] 55 | for i in range(24): 56 | if Remain_load[i]>0: 57 | From_Grid.append(Remain_load[i]) 58 | To_Grid.append(0) 59 | else: 60 | From_Grid.append(0) 61 | To_Grid.append(-Remain_load[i]) 62 | 63 | for interruption in Interruption_time: 64 | From_Grid[interruption] = 0 65 | To_Grid[interruption] = 0 66 | 67 | Grid_Cost = [] 68 | for t in range(24): 69 | Grid_Cost.append(Remain_load[t] * (price['Energy price'].iloc[t])) 70 | 71 | Dg_Cost = [] 72 | min_dg = 30 73 | for t in range(24): 74 | if Remain_load[t]<30: 75 | Dg_Cost.append(0.0009*30*30+0.0213*30+1.1) 76 | else: 77 | Dg_Cost.append(0.0009 * Remain_load[t] *Remain_load[t] + 0.0213 * Remain_load[t] + 1.1) 78 | 79 | Cost = [] 80 | Dg_output = [] 81 | for t in range(24): 82 | if Dg_Cost[t] 73 |

74 | To implement our proposed MAIL method, we still need to generate other scenarios' expert demonstrations, since there will be several virtual agents to solve different cases in the MAIL method. 75 | 76 | For example, we consider three virtual environments that the power supply interruptions happen at the time-period $10^{th}-13^{th}$ hours, $11^{th}-14^{th}$ hours, and $12^{th}-15^{th}$ hours. Three local virtual agents are applied to imitate the corresponding virtual environment's actions. So we need to change **fault_time = [10 11 12 13]** to **fault_time = [11 12 13 14]** in the **mainDP_Interrupted.m** file to generate **interrupt11-14_dp51.xls**, and change **fault_time = [10 11 12 13]** to **fault_time = [12 13 14 15]** to generate **interrupt12-15_dp51.xls**. 77 | 78 | These files are already included in the MAIL/GM 2024_Python file folder now. 79 | 80 | ## **Implementation of the Proposed Method** 81 | 82 | In our paper, we consider a grid-connected microgrid consisting of four units from the perspective of energy generation and load demand shown as below. The four units are the battery energy storage system (BESS), the distributed generations, including diesel generator (DG) and renewable generations (RG), the main grid, and the residential load. The optimization problem is to make hourly dispatch decisions over a time period of T(24 hours). 83 | 84 | 85 | 86 | This is the framework of our proposed multi-virtual-agent imitation learning approach for the microgrid system. 87 | 88 | ![framework](https://github.com/YanbinLin94/Multi-virtual-agent-Imitation-Learning-for-Microgrid-Energy-Scheduling/assets/97860537/bc17c283-cefc-4fe4-bd39-f40418d531ec) 89 | 90 | 91 | Several virtual environments' expert demonstrations are used to imitate actions in parallel, and the maximum crossover discriminators are calculated as the loss function. 92 | 93 | ### Generation of Learner Policy and Results 94 | 95 | Once we have the expert demonstrations for different cases, we can implement the MAIL method in the MAIL/GM 2024_Python file folder. 96 | 97 | ```Python3 main.py``` 98 | 99 | We will get the result of the MAIL method: policy10-15_max3_dp11.csv. This is a policy learned for the scenario that extreme weather-related events happen at time $10^{th}-15^{th}$ (total 6 hours), which is different from expert scenarios. 100 | 101 | ```Run the calculate_cost.py file``` 102 | 103 | We will get the cost of this learned policy and a "result_of_interruption10-15_max3_dp51.csv" file including 7 rows of output results: Battery_soc, power from grid, power to grid, battery discharged power, battery charged power, and dg output. 104 | 105 | ### Generation of Figures 106 | 107 | Copy "result_of_interruption10-15_max3_dp51.csv" to MAIL/GM 2024_Matlab filefolder. 108 | 109 | ```Run the plot_result.m file``` 110 | 111 | We will get the output figure of the proposed MAIL method. 112 | 113 | 114 |

115 | We can also use plot_batt_pol.m and plot_cost.m files to compare the results of different methods: such as the Aggregated Q-learning method and DP method (These two methods' implementations are also included in our MAIL/GM 2024_Matlab filefolder: Cooperative_system.m and mainDP_Interrupted.m). If we want to compare different methods' results, we need to copy their result to comparative_batt_pol_soc51.csv and comparative_cost_soc51.csv before we run plot_batt_pol.m and plot_cost.m files (They are already included in the file folders now). 116 | 117 | The figures are shown below. 118 |

119 | 120 |

121 | 122 |

123 | 124 |

125 | ## Case Studies 126 | ### Case 1: Change of the Discrete Size of SOC 127 | Change the value of N in the MAIL/GM 2024_Matlab/mainDP_Interrupted.m to change the discrete size. For example, if we want to discrete the SOC to 11 states, change N=11-1=10; if we want to discrete the SOC to 1001 states, change N=1001-1=1000. 128 | 129 | ### Case 2: Change of the Interrupted Time Period 130 | Change **fault_time = [10 11 12 13]** in the MAIL/GM 2024_Matlab/mainDP_Interrupted.m to any time slot we want to try. Remember, the current code only supports time slots equal to 4 hours. If we want to implement the scenario that the interrupted time slot doesn't equal 4 hours, such as [10 11 12 13 14 15] (6 hours), we need to change the sentence in the MAIL/GM 2024_Matlab/F_costval_DP_FLT.m file. 131 | ``` 132 | if time == fault_time(1) || time == fault_time(2) || time == fault_time(3) || time == fault_time(4) 133 | sig = 1; 134 | ``` 135 | to 136 | ``` 137 | if time == fault_time(1) || time == fault_time(2) || time == fault_time(3) || time == fault_time(4) || time == fault_time(5) || time == fault_time(6) 138 | sig = 1; 139 | ``` 140 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | --------------------------------------------------------------------------------