-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMicro-Grids_Surrugate_Fix.py
244 lines (191 loc) · 10.6 KB
/
Micro-Grids_Surrugate_Fix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# -*- coding: utf-8 -*-
from pyomo.environ import AbstractModel
from Results import Load_results1, Energy_Mix, Print_Results
from Model_Creation import Model_Creation
from Model_Resolution import Model_Resolution
from Initialize import Solar_Energy_Data
import pandas as pd
from pyDOE import lhs
from pyomo.opt import SolverFactory
import time
from joblib import dump, load
start = time.time()
#%%
''' This script is used to create the data base for the machine learning Technique'''
model = AbstractModel() # define type of optimization problem
# Optimization model
# Type of problem formulation:
model.formulation = 'MILP'
model.Lost_Load_Probability = 0
model.Curtailment_Unitary_Cost = 0
Renewable_Penetration = 0
Battery_Independency = 0
type_generator = 'Fix' # Fix or Variable
solar_energy = 'Fix' # Fix or Variable
Model_Creation(model, Renewable_Penetration, Battery_Independency,type_generator)
instance = Model_Resolution(model, Renewable_Penetration, Battery_Independency)
#%%
# not use parameters
Battery_Independency = 0 # number of days of battery independency
Curtailment_Unitary_Cost = 0 # probando curtailment cost 0
#%%
# Renewable energy parameters
Renewable_Invesment_Cost = [1500, 1500]
Maintenance_Operation_Cost_Renewable = [0.02, 0.02]
#%%
# Battery parameters
Battery_Invesment_Cost = [550, 550]
Maintenance_Operation_Cost_Battery = [0.02, 0.02]
Deep_of_Discharge = [0.2,0.2]
Battery_Cycles = [5500, 5500]
#%%
# Generator parameters
Generator_Invesment_Cost = [1480,1480]
Generator_Efficiency = [0.314, 0.314]
Low_Heating_Value = [9.89,9.89]
Fuel_Cost = [0.18,2]
Generator_Nominal_Capacity = 15
Maintenance_Operation_Cost_Generator = [0.02, 0.02]
#%%
# Other parameters
#Lost_Load_Probability = 0 # Allowed a percentage of unmet demand in the system
#%%
Number_Scenarios = int(instance.Scenarios.extract_values()[None])
Number_Periods = int(instance.Periods.extract_values()[None])
Number_Renewable_Source = int(instance.Renewable_Source.extract_values()[None])
foo = 0
Data = pd.DataFrame()
Results = pd.DataFrame()
Renewable_Nominal_Capacity = instance.Renewable_Nominal_Capacity.extract_values()[1]
village = [550] #range(50, 570, 50)
Nruns = 150
Data_Villages = pd.read_excel('Data_Base_Low_Lands.xls',index_col=0,Header=None)
Villages_Already = pd.read_excel('status1.xls',index_col=0,Header=None)
Data_Villages = Data_Villages.drop(list(Villages_Already['Index']))
Status = pd.DataFrame()
for i in village:
#
# Demand
Village = 'village_' + str(i)
Energy_Demand = pd.read_excel('Example/Demand.xls',sheet_name=Village
,index_col=0,Header=None)
Energy_Demand = Energy_Demand/1000
Energy_Demand = round(Energy_Demand,3)
for s in range(1,Number_Scenarios+1):
for t in range(1, Number_Periods+1):
instance.Energy_Demand[s,t] = Energy_Demand.loc[t,s]
lh = lhs(11, samples=Nruns)
filename = 'latin/lh_' + str(i) + '.joblib'
dump(lh, filename)
max_energy = Energy_Demand.max()
max_energy = max_energy[1]
max_bound_PV = (max_energy*8)/0.25
max_boumd_bat = max_energy*50
instance.Renewable_Units[1].setub(max_bound_PV)
instance.Battery_Nominal_Capacity.setub(max_boumd_bat)
#75
for n in range(Nruns):
print(i)
print(n)
start_1 = time.time()
location = Data_Villages.sample(n=1)
foo = location.index[0]
Data_Villages = Data_Villages.drop([foo])
Solar_Data = Solar_Energy_Data(location,Number_Scenarios,solar_energy )
Solar_Data = Solar_Data/1000
Solar_Data = round(Solar_Data,3)
for s in range(1,Number_Scenarios+1):
for t in range(1, Number_Periods+1):
instance.Renewable_Energy_Production[s,1,t] = Solar_Data.loc[t,s]
name = str(i) + '_' + str(n)
Status.loc[name, 'Index'] = foo
Status.loc[name, 'X_deg'] = location['X_deg'][foo]
Status.loc[name, 'Y_deg'] = location['Y_deg'][foo]
# Renewable energy parameters
Renewable_Invesment_Cost_1 = Renewable_Invesment_Cost[0] + lh[n,0]*(Renewable_Invesment_Cost[-1]-Renewable_Invesment_Cost[0])
Renewable_Invesment_Cost_1 = round(Renewable_Invesment_Cost_1,0)
instance.Renewable_Invesment_Cost[1] = round(Renewable_Invesment_Cost_1,0)
Maintenance_Operation_Cost_Renewable_1 = Maintenance_Operation_Cost_Renewable[0] + lh[n,1]*(Maintenance_Operation_Cost_Renewable[-1]
-Maintenance_Operation_Cost_Renewable[0])
Maintenance_Operation_Cost_Renewable_1 = round(Maintenance_Operation_Cost_Renewable_1,2)
instance.Maintenance_Operation_Cost_Renewable[1] = Maintenance_Operation_Cost_Renewable_1
# Battery parameters
Battery_Invesment_Cost_1 = Battery_Invesment_Cost[0] + lh[n,2]*(Battery_Invesment_Cost[-1] - Battery_Invesment_Cost[0])
Battery_Invesment_Cost_1 = round(Battery_Invesment_Cost_1,0)
instance.Battery_Invesment_Cost = Battery_Invesment_Cost_1
Deep_of_Discharge_1 = Deep_of_Discharge[0] + lh[n,3]*(Deep_of_Discharge[-1]-Deep_of_Discharge[0])
Deep_of_Discharge_1 = round(Deep_of_Discharge_1,2)
instance.Deep_of_Discharge = Deep_of_Discharge_1
Battery_Cycles_1 = Battery_Cycles[0] + lh[n,4]*(Battery_Cycles[-1]-Battery_Cycles[0])
Battery_Cycles_1 = round(Battery_Cycles_1,2)
instance.Battery_Cycles = Battery_Cycles_1
Maintenance_Operation_Cost_Battery_1 = Maintenance_Operation_Cost_Battery[0] + lh[n,5]*(Maintenance_Operation_Cost_Battery[-1]
-Maintenance_Operation_Cost_Battery[0])
Maintenance_Operation_Cost_Battery_1 = round(Maintenance_Operation_Cost_Battery_1,2)
instance.Maintenance_Operation_Cost_Battery = Maintenance_Operation_Cost_Battery_1
# Genset parameters
Generator_Invesment_Cost_1 = Generator_Invesment_Cost[0] + lh[n,6]*(Generator_Invesment_Cost[-1] - Generator_Invesment_Cost[0])
Generator_Invesment_Cost_1 = round(Generator_Invesment_Cost_1,0)
instance.Generator_Invesment_Cost[1] = Generator_Invesment_Cost_1
Generator_Efficiency_1 = Generator_Efficiency[0] + lh[n,7]*(Generator_Efficiency[-1] - Generator_Efficiency[0])
Generator_Efficiency_1 = round(Generator_Efficiency_1,2)
instance.Generator_Efficiency[1] = Generator_Efficiency_1
Low_Heating_Value_1 = Low_Heating_Value[0] + lh[n,8]*(Low_Heating_Value[-1] - Low_Heating_Value[0])
Low_Heating_Value_1 = round(Low_Heating_Value_1,2)
instance.Low_Heating_Value[1] = Low_Heating_Value_1
Fuel_Cost_1 = Fuel_Cost[0] + lh[n,9]*(Fuel_Cost[-1] - Fuel_Cost[0])
Fuel_Cost_1 = round(Fuel_Cost_1,1)
instance.Fuel_Cost[1] =Fuel_Cost_1
Maintenance_Operation_Cost_Generator_1 = Maintenance_Operation_Cost_Generator[0] + lh[n,10]*(Maintenance_Operation_Cost_Generator[-1]
-Maintenance_Operation_Cost_Generator[0])
Maintenance_Operation_Cost_Generator_1 = round(Maintenance_Operation_Cost_Generator_1,2)
instance.Maintenance_Operation_Cost_Generator[1] = Maintenance_Operation_Cost_Generator_1
Marginal_Cost_Generator_1 = Fuel_Cost_1/(Low_Heating_Value_1*Generator_Efficiency_1)
Marginal_Cost_Generator_1 = round(Marginal_Cost_Generator_1,3)
instance.Marginal_Cost_Generator_1[1] = Marginal_Cost_Generator_1
if model.formulation == 'MILP':
if Number_Scenarios == 1:
foo = Energy_Demand.max()
foo = foo[1]
Generator_Nominal_Capacity_1 = round(foo*0.75, 0)
else:
foo = Energy_Demand.max()
foo = foo.max()
foo = foo[1]
Generator_Nominal_Capacity_1 = round(foo*0.75, 0)
instance.Generator_Nominal_Capacity[1] = Generator_Nominal_Capacity_1
Cost_Increase_1 = instance.Cost_Increase.extract_values()[1]
Start_Cost_Generator_1 = Marginal_Cost_Generator_1*Generator_Nominal_Capacity_1*Cost_Increase_1
Start_Cost_Generator_1 = round(Start_Cost_Generator_1,3)
instance.Start_Cost_Generator[1] = Start_Cost_Generator_1
Marginal_Cost_Generator_2 = (Marginal_Cost_Generator_1*Generator_Nominal_Capacity_1 \
-Start_Cost_Generator_1)/Generator_Nominal_Capacity_1
instance.Marginal_Cost_Generator[1] = round(Marginal_Cost_Generator_2,3)
Battery_Electronic_Invesmente_Cost = instance.Battery_Electronic_Invesmente_Cost.extract_values()[None]
unitary_battery_cost = Battery_Invesment_Cost_1 - Battery_Electronic_Invesmente_Cost
Unitary_Battery_Reposition_Cost_1 = unitary_battery_cost/(Battery_Cycles_1*2*(1- Deep_of_Discharge_1))
instance.Unitary_Battery_Reposition_Cost = round(Unitary_Battery_Reposition_Cost_1,3)
opt = SolverFactory('gurobi') # Solver use during the optimization
opt.options['timelimit'] = 1800
opt.options['Presolve'] = 2
filename = 'Instance/instance_' + str(i) + '_' + str(n) + '.joblib'
dump(instance, filename)
results = opt.solve(instance, tee=True, options_string="mipgap=0.01",
warmstart=False,keepfiles=True,
load_solutions=False) # Solving a model instance
Status.loc[name,'Time'] = results.solver.wall_time
Status.loc[name,'Upper Bound'] = results.problem.upper_bound
Status.loc[name,'Lower Bound'] = results.problem.lower_bound
Status.loc[name,'Gap'] = (Status.loc[name,'Upper Bound']- Status.loc[name,'Lower Bound']) \
/Status.loc[name,'Upper Bound']
Status.loc[name,'Gap'] = Status.loc[name,'Gap']*100
instance.solutions.load_from(results) # Loading solution into instance
Data = Load_results1(instance, i, n,type_generator)
NPC = Data[0]
Status.loc[name,'Status'] = NPC.loc['Status','Data']
end_1 = time.time()
Status.loc[name,'Time'] = end_1 - start_1
print('The optimization took ' + str(round(end_1 - start_1,0)) + ' segundos')
Status.to_excel('status.xls')
end = time.time()
print('The optimizations took ' + str(round(end - start,0)) + ' segundos')