-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel_Creation.py
executable file
·425 lines (338 loc) · 24.9 KB
/
Model_Creation.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def Model_Creation(model, Renewable_Penetration,Battery_Independency,type_generator):
'''
This function creates the instance for the resolution of the optimization in Pyomo.
:param model: Pyomo model as defined in the Micro-Grids library.
'''
from pyomo.environ import Param, RangeSet, NonNegativeReals, Var, NonNegativeIntegers,Binary
# Import library with initialitation funtions for the parameters
from Initialize import Initialize_years, Initialize_Demand, Battery_Reposition_Cost,\
Initialize_Renewable_Energy, Marginal_Cost_Generator_1,Min_Bat_Capacity, Start_Cost,\
Marginal_Cost_Generator, Capital_Recovery_Factor
# Time parameters
model.Periods = Param(within=NonNegativeReals) # Number of periods of analysis of the energy variables
model.Years = Param() # Number of years of the project
model.StartDate = Param() # Start date of the analisis
model.Scenarios = Param()
model.Renewable_Source = Param()
model.Generator_Type = Param()
#SETS
model.periods = RangeSet(1, model.Periods) # Creation of a set from 1 to the number of periods in each year
model.years = RangeSet(1, model.Years) # Creation of a set from 1 to the number of years of the project
model.scenario = RangeSet(1, model.Scenarios) # Creation of a set from 1 to the numbero scenarios to analized
model.renewable_source = RangeSet(1, model.Renewable_Source)
model.generator_type = RangeSet(1, model.Generator_Type)
# PARAMETERS
model.Scenario_Weight = Param(model.scenario, within=NonNegativeReals) #########
# Parameters of the PV
model.Renewable_Nominal_Capacity = Param(model.renewable_source,
within=NonNegativeReals) # Nominal capacity of the PV in W/unit
model.Renewable_Inverter_Efficiency = Param(model.renewable_source) # Efficiency of the inverter in %
model.Renewable_Invesment_Cost = Param(model.renewable_source,
within=NonNegativeReals, mutable=True) # Cost of solar panel in USD/W
model.Renewable_Energy_Production = Param(model.scenario,model.renewable_source,
model.periods, within=NonNegativeReals,
initialize=Initialize_Renewable_Energy
, mutable=True) # Energy produccion of a solar panel in W
model.Fix_Invesment_PV = Param(model.renewable_source, within=NonNegativeReals)
# Parameters of the battery bank
model.Charge_Battery_Efficiency = Param() # Efficiency of the charge of the battery in %
model.Discharge_Battery_Efficiency = Param() # Efficiency of the discharge of the battery in %
model.Deep_of_Discharge = Param(mutable=True) # Deep of discharge of the battery (Deep_of_Discharge) in %
model.Maximun_Battery_Charge_Time = Param(within=NonNegativeReals) # Minimun time of charge of the battery in hours
model.Maximun_Battery_Discharge_Time = Param(within=NonNegativeReals) # Maximun time of discharge of the battery in hours
model.Battery_Invesment_Cost = Param(mutable=True) # Cost of battery
model.Battery_Electronic_Invesmente_Cost = Param(within=NonNegativeReals)
model.Battery_Cycles = Param(within=NonNegativeReals, mutable=True)
model.Unitary_Battery_Reposition_Cost = Param(within=NonNegativeReals,
initialize=Battery_Reposition_Cost, mutable=True)
model.Battery_Initial_SOC = Param(within=NonNegativeReals)
model.Fix_Invesment_Battery = Param(within=NonNegativeReals)
if Battery_Independency > 0:
model.Battery_Independency = Battery_Independency
model.Battery_Min_Capacity = Param(initialize=Min_Bat_Capacity)
# Parametes of the diesel generator
if model.formulation == 'LP':
model.Generator_Efficiency = Param(model.generator_type, mutable=True)
model.Low_Heating_Value = Param(model.generator_type, mutable=True)
model.Fuel_Cost = Param(model.generator_type, within=NonNegativeReals, mutable=True)
model.Generator_Invesment_Cost = Param(model.generator_type, within=NonNegativeReals
, mutable=True)
model.Marginal_Cost_Generator_1 = Param(model.generator_type,
initialize=Marginal_Cost_Generator_1, mutable=True)
elif model.formulation == 'MILP':
model.Generator_Min_Out_Put = Param(model.generator_type,
within=NonNegativeReals)
model.Generator_Efficiency = Param(model.generator_type,mutable=True) # Generator efficiency to trasform heat into electricity %
model.Low_Heating_Value = Param(model.generator_type, mutable=True) # Low heating value of the diesel in W/L
model.Fuel_Cost = Param(model.generator_type,
within=NonNegativeReals, mutable=True) # Cost of diesel in USD/L
model.Generator_Invesment_Cost = Param(model.generator_type,within=NonNegativeReals
, mutable=True) # Cost of the diesel generator
model.Marginal_Cost_Generator_1 = Param(model.generator_type,
initialize=Marginal_Cost_Generator_1
, mutable=True)
model.Cost_Increase = Param(model.generator_type,
within=NonNegativeReals, mutable=True)
model.Generator_Nominal_Capacity = Param(model.generator_type,
within=NonNegativeReals, mutable=True)
model.Start_Cost_Generator = Param(model.generator_type,
within=NonNegativeReals, initialize=Start_Cost, mutable=True)
model.Marginal_Cost_Generator = Param(model.generator_type,
initialize=Marginal_Cost_Generator
, mutable=True)
# Parameters of the Energy balance
model.Energy_Demand = Param(model.scenario, model.periods,
initialize=Initialize_Demand, mutable=True) # Energy Energy_Demand in W
model.Value_Of_Lost_Load = Param(within=NonNegativeReals) # Value of lost load in USD/W
if Renewable_Penetration > 0:
model.Renewable_Penetration = Renewable_Penetration
# Parameters of the proyect
model.Delta_Time = Param(within=NonNegativeReals) # Time step in hours
model.Project_Years = Param(model.years, initialize= Initialize_years) # Years of the project
model.Maintenance_Operation_Cost_Renewable = Param(model.renewable_source,
within=NonNegativeReals
, mutable=True) # Percentage of the total investment spend in operation and management of solar panels in each period in %
model.Maintenance_Operation_Cost_Battery = Param(within=NonNegativeReals
, mutable=True) # Percentage of the total investment spend in operation and management of solar panels in each period in %
model.Maintenance_Operation_Cost_Generator = Param(model.generator_type,
within=NonNegativeReals
, mutable=True) # Percentage of the total investment spend in operation and management of solar panels in each period in %
model.Discount_Rate = Param() # Discount rate of the project in %
model.Capital_Recovery_Factor = Param(within=NonNegativeReals,
initialize= Capital_Recovery_Factor)
# VARIABLES
# Variables associated to the solar panels
model.Renewable_Units = Var(model.renewable_source,
within=NonNegativeReals,bounds= (0,1500)) # Number of units of solar panels
model.Integer_PV = Var(model.renewable_source, within=Binary)
#200
# Variables associated to the battery bank
# 300
bat = 2000
model.Battery_Nominal_Capacity = Var(within=NonNegativeReals,bounds= (0,bat)) # Capacity of the battery bank in Wh
model.Energy_Battery_Flow_Out = Var(model.scenario, model.periods,
within=NonNegativeReals,bounds=(0,bat)) # Battery discharge energy in wh
model.Energy_Battery_Flow_In = Var(model.scenario, model.periods,
within=NonNegativeReals,bounds=(0,bat)) # Battery charge energy in wh
model.State_Of_Charge_Battery = Var(model.scenario, model.periods,
within=NonNegativeReals) # State of Charge of the Battery in wh
model.Maximun_Charge_Power = Var(within=NonNegativeReals,bounds=(0,bat))
model.Maximun_Discharge_Power = Var(within=NonNegativeReals,bounds=(0,bat))
model.Integer_Battery = Var(within=Binary)
# Variables associated to the diesel generator
def gen(model,g):
if g == 1:
return 0
else:
return 0
def bounds_N(model,g):
if g == 1:
return (0,1)
else:
return (0,2)
def bounds_E(model,s,g,t):
if g == 1:
return (0,1)
else:
return (0,2)
if model.formulation == 'LP':
model.Generator_Nominal_Capacity = Var(model.generator_type,
within=NonNegativeReals) # Capacity of the diesel generator in Wh
model.Generator_Energy = Var(model.scenario,model.generator_type,
model.periods, within=NonNegativeReals) # Energy generated for the Diesel generator
elif model.formulation == 'MILP':
print('hola q')
model.Generator_Energy = Var(model.scenario, model.generator_type,
model.periods, within=NonNegativeReals)
if type_generator == 'Fix':
print('comida')
model.Integer_generator = {1:1}
else:
print('hola')
model.Integer_generator = Var(model.generator_type,
within=NonNegativeIntegers,
initialize=gen,
bounds=bounds_N)
model.Generator_Total_Period_Energy = Var(model.scenario,
model.generator_type,
model.periods,
within=NonNegativeReals)
model.Generator_Energy_Integer = Var(model.scenario, model.generator_type,
model.periods, within=Binary)
model.Last_Energy_Generator = Var(model.scenario, model.generator_type,
model.periods, within=NonNegativeReals)
# Varialbles associated to the energy balance
if model.Lost_Load_Probability > 0:
model.Lost_Load = Var(model.scenario, model.periods, within=NonNegativeReals) # Energy not suply by the system kWh
model.Energy_Curtailment = Var(model.scenario, model.periods, within=NonNegativeReals
,bounds=(0,100)) # Curtailment of solar energy in kWh
def Model_Creation_binary(model):
'''
This function creates the instance for the resolution of the optimization in Pyomo.
The problem is solved by discretizing the efficiency curve of the generators and uses binary variables
:param model: Pyomo model as defined in the Micro-Grids library.
'''
from pyomo.environ import Param, RangeSet, NonNegativeReals, Var, Binary, NonNegativeIntegers
from Initialize import Initialize_years, Initialize_Demand, Initialize_PV_Energy, Marginal_Cost_Generator, Start_Cost,Marginal_Cost_Generator_1 # Import library with initialitation funtions for the parameters
# Time parameters
model.Periods = Param(within=NonNegativeReals) # Number of periods of analysis of the energy variables
model.Years = Param() # Number of years of the project
model.StartDate = Param() # Start date of the analisis
model.PlotTime = Param() # Quantity of days that are going to be plot
model.PlotDay = Param() # Start day for the plot
model.Scenarios = Param()
model.PlotScenario = Param()
#SETS
model.periods = RangeSet(1, model.Periods) # Creation of a set from 1 to the number of periods in each year
model.years = RangeSet(1, model.Years) # Creation of a set from 1 to the number of years of the project
model.Slops = RangeSet(1,2)
model.scenario = RangeSet(1, model.Scenarios) # Creation of a set from 1 to the numbero scenarios to analized
# PARAMETERS
# Parameters of the PV
model.PV_Nominal_Capacity = Param(within=NonNegativeReals) # Nominal capacity of the PV in W/unit
model.Inverter_Efficiency = Param() # Efficiency of the inverter in %
model.PV_invesment_Cost = Param(within=NonNegativeReals) # Cost of solar panel in USD/W
model.PV_Energy_Production = Param(model.scenario, model.periods, within=NonNegativeReals, initialize=Initialize_PV_Energy) # Energy produccion of a solar panel in W
# Parameters of the battery bank
model.Charge_Battery_Efficiency = Param() # Efficiency of the charge of the battery in %
model.Discharge_Battery_Efficiency = Param() # Efficiency of the discharge of the battery in %
model.Deep_of_Discharge = Param() # Deep of discharge of the battery (Deep_of_Discharge) in %
model.Maximun_Battery_Charge_Time = Param(within=NonNegativeReals) # Minimun time of charge of the battery in hours
model.Maximun_Battery_Discharge_Time = Param(within=NonNegativeReals) # Maximun time of discharge of the battery in hours
model.Battery_Reposition_Time = Param(within=NonNegativeReals) # Period of repocition of the battery in years
model.Battery_Invesment_Cost = Param(within=NonNegativeReals) # Cost of battery
# Parametes of the diesel generator
model.Generator_Effiency = Param(within=NonNegativeReals)
model.Generator_Min_Out_Put = Param(within=NonNegativeReals)
model.Generator_Efficiency = Param() # Generator efficiency to trasform heat into electricity %
model.Low_Heating_Value = Param() # Low heating value of the diesel in W/L
model.Diesel_Cost = Param(within=NonNegativeReals) # Cost of diesel in USD/L
model.Generator_Invesment_Cost = Param(within=NonNegativeReals) # Cost of the diesel generator
model.Marginal_Cost_Generator_1 = Param(initialize=Marginal_Cost_Generator_1)
model.Cost_Increase = Param(within=NonNegativeReals)
model.Generator_Nominal_Capacity = Param(within=NonNegativeReals)
model.Start_Cost_Generator = Param(within=NonNegativeReals, initialize=Start_Cost)
model.Marginal_Cost_Generator = Param(initialize=Marginal_Cost_Generator)
# Parameters of the Energy balance
model.Energy_Demand = Param(model.scenario,model.periods, initialize=Initialize_Demand) # Energy Energy_Demand in W
model.Lost_Load_Probability = Param() # Lost load probability in %
model.Value_Of_Lost_Load = Param(within=NonNegativeReals) # Value of lost load in USD/W
# Parameters of the proyect
model.Delta_Time = Param(within=NonNegativeReals) # Time step in hours
model.Porcentage_Funded = Param(within=NonNegativeReals) # Porcentaje of the total investment that is Porcentage_Porcentage_Funded by a bank or another entity in %
model.Project_Years = Param(model.years, initialize= Initialize_years) # Years of the project
model.Maintenance_Operation_Cost_PV = Param(within=NonNegativeReals) # Percentage of the total investment spend in operation and management of solar panels in each period in %
model.Maintenance_Operation_Cost_Battery = Param(within=NonNegativeReals) # Percentage of the total investment spend in operation and management of solar panels in each period in %
model.Maintenance_Operation_Cost_Generator = Param(within=NonNegativeReals) # Percentage of the total investment spend in operation and management of solar panels in each period in %
model.Discount_Rate = Param() # Discount rate of the project in %
model.Interest_Rate_Loan = Param() # Interest rate of the loan in %
model.Scenario_Weight = Param(model.scenario, within=NonNegativeReals)
# VARIABLES
# Variables associated to the solar panels
model.PV_Units = Var(within=NonNegativeReals) # Number of units of solar panels
model.Total_Energy_PV = Var(model.scenario,model.periods, within=NonNegativeReals) # Energy generated for the Pv sistem in Wh
# Variables associated to the battery bank
model.Battery_Nominal_Capacity = Var(within=NonNegativeReals) # Capacity of the battery bank in Wh
model.Energy_Battery_Flow_Out = Var(model.scenario,model.periods, within=NonNegativeReals) # Battery discharge energy in wh
model.Energy_Battery_Flow_In = Var(model.scenario,model.periods, within=NonNegativeReals) # Battery charge energy in wh
model.State_Of_Charge_Battery = Var(model.scenario,model.periods, within=NonNegativeReals) # State of Charge of the Battery in wh
model.Maximun_Charge_Power= Var() # Maximun charge power in w
model.Maximun_Discharge_Power = Var() #Maximun discharge power in w
# Variables associated to the diesel generator
model.Period_Total_Cost_Generator = Var(model.scenario,model.periods, within=NonNegativeReals)
model.Energy_Generator_Total = Var(model.scenario, model.periods, within=NonNegativeReals)
model.Binary_generator_1 = Var(model.scenario,model.periods, within=Binary)
model.Integer_generator = Var(within=NonNegativeIntegers)
model.Total_Cost_Generator = Var(model.scenario,within=NonNegativeReals)
model.Generator_Total_Period_Energy = Var(model.scenario,model.periods, within=NonNegativeReals)
model.Generator_Energy_Integer = Var(model.scenario, model.periods, within=NonNegativeIntegers)
model.Last_Energy_Generator = Var(model.scenario, model.periods, within=NonNegativeReals)
# Varialbles associated to the energy balance
model.Lost_Load = Var(model.scenario,model.periods, within=NonNegativeReals) # Energy not suply by the system kWh
model.Energy_Curtailment = Var(model.scenario,model.periods, within=NonNegativeReals) # Curtailment of solar energy in kWh
# Variables associated to the project
model.Cost_Financial = Var(within=NonNegativeReals) # Financial cost of each period in USD
model.Initial_Inversion = Var(within=NonNegativeReals)
model.Operation_Maintenance_Cost = Var(within=NonNegativeReals)
model.Total_Finalcial_Cost = Var(within=NonNegativeReals)
model.Battery_Reposition_Cost = Var(within=NonNegativeReals)
model.Scenario_Lost_Load_Cost = Var(model.scenario, within=NonNegativeReals) ####
model.Sceneario_Generator_Total_Cost = Var(model.scenario, within=NonNegativeReals)
model.Scenario_Net_Present_Cost = Var(model.scenario, within=NonNegativeReals)
def Model_Creation_Dispatch(model):
'''
This function creates the instance for the resolution of the optimization in Pyomo.
The problem is solved by discretizing the efficiency curve of the generators and uses binary variables
:param model: Pyomo model as defined in the Micro-Grids library.
'''
from pyomo.environ import Param, RangeSet, NonNegativeReals, Var, NonNegativeIntegers
from Initialize import Initialize_Demand,Initialize_Renewable_Energy, Initialize_PV_Energy,\
Initialize_Demand_Dispatch, Initialize_PV_Energy_Dispatch, Marginal_Cost_Generator_Dispatch,\
Start_Cost_Dispatch,Marginal_Cost_Generator_1_Dispatch, Battery_Reposition_Cost # Import library with initialitation funtions for the parameters
# Time parameters
model.Periods = Param(within=NonNegativeReals) # Number of periods of analysis of the energy variables
model.StartDate = Param() # Start date of the analisis
model.Generator_Type = Param()
model.Renewable_Source = Param()
#SETS
model.periods = RangeSet(1, model.Periods) # Creation of a set from 1 to the number of periods in each year
model.generator_type = RangeSet(1, model.Generator_Type)
model.renewable_source = RangeSet(1, model.Renewable_Source)
# PARAMETERS
# Parameters of the Renewable energy
model.Renewable_Inverter_Efficiency = Param(model.renewable_source) # Efficiency of the inverter in %
model.Renewable_Energy_Production = Param(model.scenario,model.renewable_source,
model.periods, within=NonNegativeReals,
initialize=Initialize_Renewable_Energy) # Energy produccion of a solar panel in W
# Parameters of the battery bank
model.Charge_Battery_Efficiency = Param() # Efficiency of the charge of the battery in %
model.Discharge_Battery_Efficiency = Param() # Efficiency of the discharge of the battery in %
model.Deep_of_Discharge = Param() # Deep of discharge of the battery (Deep_of_Discharge) in %
model.Maximun_Battery_Charge_Time = Param(within=NonNegativeReals) # Minimun time of charge of the battery in hours
model.Maximun_Battery_Discharge_Time = Param(within=NonNegativeReals) # Maximun time of discharge of the battery in hours
model.Battery_Nominal_Capacity = Param(within=NonNegativeReals) # Capacity of the battery bank in Wh
model.Battery_Initial_SOC = Param(within=NonNegativeReals)
model.Battery_Electronic_Invesmente_Cost = Param(within=NonNegativeReals)
model.Battery_Invesment_Cost = Param(within=NonNegativeReals) # Cost of battery
model.Battery_Cycles = Param(within=NonNegativeReals)
model.Unitary_Battery_Reposition_Cost = Param(within=NonNegativeReals,
initialize=Battery_Reposition_Cost)
# Parametes of the diesel generator
model.Generator_Efficiency = Param(within=NonNegativeReals)
model.Generator_Min_Out_Put = Param(within=NonNegativeReals)
model.Low_Heating_Value = Param() # Low heating value of the diesel in W/L
model.Diesel_Cost = Param(within=NonNegativeReals) # Cost of diesel in USD/L
model.Marginal_Cost_Generator_1 = Param(initialize=Marginal_Cost_Generator_1_Dispatch)
model.Cost_Increase = Param(within=NonNegativeReals)
model.Generator_Nominal_Capacity = Param(within=NonNegativeReals)
model.Start_Cost_Generator = Param(within=NonNegativeReals, initialize=Start_Cost_Dispatch)
model.Marginal_Cost_Generator = Param(initialize=Marginal_Cost_Generator_Dispatch)
# Parameters of the Energy balance
model.Energy_Demand = Param(model.periods, initialize=Initialize_Demand_Dispatch) # Energy Energy_Demand in W
model.Lost_Load_Probability = Param() # Lost load probability in %
model.Value_Of_Lost_Load = Param(within=NonNegativeReals) # Value of lost load in USD/W
# Parameters of the proyect
model.Delta_Time = Param(within=NonNegativeReals) # Time step in hours
# VARIABLES
# Variables associated to the battery bank
model.Energy_Battery_Flow_Out = Var(model.periods, within=NonNegativeReals) # Battery discharge energy in wh
model.Energy_Battery_Flow_In = Var(model.periods, within=NonNegativeReals) # Battery charge energy in wh
model.State_Of_Charge_Battery = Var(model.periods, within=NonNegativeReals) # State of Charge of the Battery in wh
model.Maximun_Charge_Power= Var(within=NonNegativeReals) # Maximun charge power in w
model.Maximun_Discharge_Power = Var(within=NonNegativeReals) #Maximun discharge power in w
model.Battery_Yearly_cost = Var(within=NonNegativeReals)
# Variables associated to the diesel generator
model.Period_Total_Cost_Generator = Var(model.periods, within=NonNegativeReals)
model.Energy_Generator_Total = Var(model.periods, within=NonNegativeReals)
model.Integer_generator = Var(within=NonNegativeIntegers)
model.Total_Cost_Generator = Var(within=NonNegativeReals)
model.Generator_Total_Period_Energy = Var(model.periods, within=NonNegativeReals)
model.Generator_Energy_Integer = Var(model.periods, within=NonNegativeIntegers)
model.Last_Energy_Generator = Var(model.periods, within=NonNegativeReals)
# Varialbles associated to the energy balance
model.Lost_Load = Var(model.periods, within=NonNegativeReals) # Energy not suply by the system kWh
model.Energy_Curtailment = Var(model.periods, within=NonNegativeReals) # Curtailment of solar energy in kWh
# Variables associated to the project
model.Scenario_Lost_Load_Cost = Var(within=NonNegativeReals) ####