-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel_Resolution.py
executable file
·204 lines (164 loc) · 11.5 KB
/
Model_Resolution.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pyomo.opt import SolverFactory
from pyomo.environ import Objective, minimize, Constraint
def Model_Resolution(model,Renewable_Penetration, Battery_Independency,datapath="Example/data.dat"):
'''
This function creates the model and call Pyomo to solve the instance of the proyect
:param model: Pyomo model as defined in the Model_creation library
:param datapath: path to the input data file
:return: The solution inside an object call instance.
'''
from Constraints import Net_Present_Cost, State_of_Charge,\
Maximun_Charge, Minimun_Charge, Max_Power_Battery_Charge,\
Max_Power_Battery_Discharge, Max_Bat_in, Max_Bat_out,Battery_Integer_Constraint, \
Energy_balance, Maximun_Lost_Load, Renewable_Energy_Penetration,\
Maximun_Generator_Energy,Generator_Bounds_Min_Integer, PV_Integer_Constraint,\
Battery_Min_Capacity,Generator_Bounds_Max_Integer,Energy_Genarator_Energy_Max_Integer\
# OBJETIVE FUNTION:
model.ObjectiveFuntion = Objective(rule=Net_Present_Cost, sense=minimize)
# CONSTRAINTS
#Energy constraints
model.EnergyBalance = Constraint(model.scenario,model.periods, rule=Energy_balance)
if model.Lost_Load_Probability > 0:
model.MaximunLostLoad = Constraint(rule=Maximun_Lost_Load)
if Renewable_Penetration > 0:
model.RenewableEnergyPenetration = Constraint(rule=Renewable_Energy_Penetration)
# Renewable constraints
model.PVIntegerConstraint = Constraint(model.renewable_source, rule=PV_Integer_Constraint)
# Battery constraints
model.StateOfCharge = Constraint(model.scenario, model.periods, rule=State_of_Charge)
model.MaximunCharge = Constraint(model.scenario, model.periods, rule=Maximun_Charge)
model.MinimunCharge = Constraint(model.scenario, model.periods, rule=Minimun_Charge)
model.MaxPowerBatteryCharge = Constraint(rule=Max_Power_Battery_Charge)
model.MaxPowerBatteryDischarge = Constraint(rule=Max_Power_Battery_Discharge)
model.MaxBatIn = Constraint(model.scenario, model.periods, rule=Max_Bat_in)
model.Maxbatout = Constraint(model.scenario, model.periods, rule=Max_Bat_out)
model.BatteryIntegerConstraint = Constraint(rule=Battery_Integer_Constraint)
if Battery_Independency > 0:
model.BatteryMinCapacity = Constraint(rule=Battery_Min_Capacity)
# Diesel Generator constraints
if model.formulation == 'LP':
model.MaximunFuelEnergy = Constraint(model.scenario, model.generator_type,
model.periods, rule=Maximun_Generator_Energy)
instance = model.create_instance(datapath) # load parameters
# opt = SolverFactory('gurobi') # Solver use during the optimization
# results = opt.solve(instance, tee=True) # Solving a model instance
# instance.solutions.load_from(results) # Loading solution into instance
elif model.formulation == 'MILP':
model.GeneratorBoundsMin = Constraint(model.scenario,model.generator_type,
model.periods, rule=Generator_Bounds_Min_Integer)
model.GeneratorBoundsMax = Constraint(model.scenario,model.generator_type,
model.periods, rule=Generator_Bounds_Max_Integer)
model.EnergyGenaratorEnergyMax = Constraint(model.scenario, model.generator_type,
model.periods, rule=Energy_Genarator_Energy_Max_Integer)
instance = model.create_instance("Example/data_Integer.dat") # load parameters
# opt = SolverFactory('gurobi') # Solver use during the optimization
## opt.options['emphasis_memory'] = 'y'
## opt.options['timelimit'] = 20000
## opt.options['node_select'] = 3
## opt.options['emphasis_mip'] = 2
# results = opt.solve(instance, tee=True, options_string="mipgap=0.06",
# warmstart=True,keepfiles=False) # Solving a model instance
#
# # instance.write(io_options={'emphasis_memory':True})
# #options_string="mipgap=0.03", timelimit=1200
# instance.solutions.load_from(results) # Loading solution into instance
return instance
def Model_Resolution_binary(model,datapath="Example/data_binary.dat"):
'''
This function creates the model and call Pyomo to solve the instance of the proyect
:param model: Pyomo model as defined in the Model_creation library
:return: The solution inside an object call instance.
'''
from Constraints_binary import Net_Present_Cost, Solar_Energy, State_of_Charge, Maximun_Charge, \
Minimun_Charge, Max_Power_Battery_Charge, Max_Power_Battery_Discharge, Max_Bat_in, Max_Bat_out, \
Financial_Cost, Energy_balance, Maximun_Lost_Load, Generator_Cost_1_binary, Generator_Total_Period_Energy_binary, \
Total_Cost_Generator_binary, Initial_Inversion, Operation_Maintenance_Cost,Total_Finalcial_Cost,\
Battery_Reposition_Cost, Scenario_Lost_Load_Cost, Sceneario_Generator_Total_Cost, \
Scenario_Net_Present_Cost, Generator_Bounds_Min_binary, Generator_Bounds_Max_binary,Energy_Genarator_Energy_Max_binary
# OBJETIVE FUNTION:
model.ObjectiveFuntion = Objective(rule=Net_Present_Cost, sense=minimize)
# CONSTRAINTS
#Energy constraints
model.EnergyBalance = Constraint(model.scenario,model.periods, rule=Energy_balance) # Energy balance
model.MaximunLostLoad = Constraint(model.scenario,rule=Maximun_Lost_Load) # Maximum permissible lost load
# PV constraints
model.SolarEnergy = Constraint(model.scenario,model.periods, rule=Solar_Energy) # Energy output of the solar panels
# Battery constraints
model.StateOfCharge = Constraint(model.scenario,model.periods, rule=State_of_Charge) # State of Charge of the battery
model.MaximunCharge = Constraint(model.scenario,model.periods, rule=Maximun_Charge) # Maximun state of charge of the Battery
model.MinimunCharge = Constraint(model.scenario,model.periods, rule=Minimun_Charge) # Minimun state of charge
model.MaxPowerBatteryCharge = Constraint(rule=Max_Power_Battery_Charge) # Max power battery charge constraint
model.MaxPowerBatteryDischarge = Constraint(rule=Max_Power_Battery_Discharge) # Max power battery discharge constraint
model.MaxBatIn = Constraint(model.scenario,model.periods, rule=Max_Bat_in) # Minimun flow of energy for the charge fase
model.Maxbatout = Constraint(model.scenario,model.periods, rule=Max_Bat_out) #minimun flow of energy for the discharge fase
#Diesel Generator constraints
model.GeneratorBoundsMin = Constraint(model.scenario,model.periods, rule=Generator_Bounds_Min_binary)
model.GeneratorBoundsMax = Constraint(model.scenario,model.periods, rule=Generator_Bounds_Max_binary)
model.GeneratorCost1 = Constraint(model.scenario, model.periods, rule=Generator_Cost_1_binary)
model.EnergyGenaratorEnergyMax = Constraint(model.scenario,model.periods, rule=Energy_Genarator_Energy_Max_binary)
model.TotalCostGenerator = Constraint(model.scenario, rule=Total_Cost_Generator_binary)
model.GeneratorTotalPeriodEnergybinary = Constraint(model.scenario,model.periods, rule=Generator_Total_Period_Energy_binary)
# Financial Constraints
model.FinancialCost = Constraint(rule=Financial_Cost) # Financial cost
model.InitialInversion = Constraint(rule=Initial_Inversion)
model.OperationMaintenanceCost = Constraint(rule=Operation_Maintenance_Cost)
model.TotalFinalcialCost = Constraint(rule=Total_Finalcial_Cost)
model.BatteryRepositionCost = Constraint(rule=Battery_Reposition_Cost)
model.ScenarioLostLoadCost = Constraint(model.scenario, rule=Scenario_Lost_Load_Cost)
model.ScenearioGeneratorTotalCost = Constraint(model.scenario, rule=Sceneario_Generator_Total_Cost)
# model.ScenarioNetPresentCost = Constraint(model.scenario, rule=Scenario_Net_Present_Cost)
instance = model.create_instance("Example/data_binary.dat") # load parameters
opt = SolverFactory('cplex') # Solver use during the optimization
# opt.options['emphasis_memory'] = 'y'
# opt.options['node_select'] = 3
results = opt.solve(instance, tee=True,options_string="mipgap=0.8") # Solving a model instance
# instance.write(io_options={'emphasis_memory':True})
#options_string="mipgap=0.03", timelimit=1200
instance.solutions.load_from(results) # Loading solution into instance
return instance
def Model_Resolution_Dispatch(model,datapath="Example/data_Dispatch.dat"):
'''
This function creates the model and call Pyomo to solve the instance of the proyect
:param model: Pyomo model as defined in the Model_creation library
:return: The solution inside an object call instance.
'''
from Constraints_Dispatch import Net_Present_Cost, State_of_Charge,\
Maximun_Charge, Minimun_Charge, Max_Bat_in, Max_Bat_out, Battery_Reposition_Cost,\
Energy_balance, Maximun_Lost_Load, Generator_Cost_1_Integer, \
Total_Cost_Generator_Integer, Scenario_Lost_Load_Cost, Max_Power_Battery_Charge, \
Max_Power_Battery_Discharge, Generator_Bounds_Min_Integer,\
Generator_Bounds_Max_Integer,Energy_Genarator_Energy_Max_Integer
# OBJETIVE FUNTION:
model.ObjectiveFuntion = Objective(rule=Net_Present_Cost, sense=minimize)
# CONSTRAINTS
#Energy constraints
model.EnergyBalance = Constraint(model.periods, rule=Energy_balance) # Energy balance
model.MaximunLostLoad = Constraint(rule=Maximun_Lost_Load) # Maximum permissible lost load
# Battery constraints
model.StateOfCharge = Constraint(model.periods, rule=State_of_Charge) # State of Charge of the battery
model.MaximunCharge = Constraint(model.periods, rule=Maximun_Charge) # Maximun state of charge of the Battery
model.MinimunCharge = Constraint(model.periods, rule=Minimun_Charge) # Minimun state of charge
model.MaxPowerBatteryCharge = Constraint(rule=Max_Power_Battery_Charge) # Max power battery charge constraint
model.MaxPowerBatteryDischarge = Constraint(rule=Max_Power_Battery_Discharge) # Max power battery discharge constraint
model.MaxBatIn = Constraint(model.periods, rule=Max_Bat_in) # Minimun flow of energy for the charge fase
model.Maxbatout = Constraint(model.periods, rule=Max_Bat_out) #minimun flow of energy for the discharge fase
model.BatteryRepositionCost = Constraint(rule=Battery_Reposition_Cost)
#Diesel Generator constraints
model.GeneratorBoundsMin = Constraint(model.periods, rule=Generator_Bounds_Min_Integer)
model.GeneratorBoundsMax = Constraint(model.periods, rule=Generator_Bounds_Max_Integer)
model.GeneratorCost1 = Constraint(model.periods, rule=Generator_Cost_1_Integer)
model.EnergyGenaratorEnergyMax = Constraint(model.periods, rule=Energy_Genarator_Energy_Max_Integer)
model.TotalCostGenerator = Constraint(rule=Total_Cost_Generator_Integer)
# Financial Constraints
model.ScenarioLostLoadCost = Constraint(rule=Scenario_Lost_Load_Cost)
instance = model.create_instance("Example/data_dispatch.dat") # load parameters
opt = SolverFactory('cplex') # Solver use during the optimization
# opt.options['emphasis_memory'] = 'y'
# opt.options['node_select'] = 3
results = opt.solve(instance, tee=True,options_string="mipgap=0.05") # Solving a model instance
# instance.write(io_options={'emphasis_memory':True})
#options_string="mipgap=0.03", timelimit=1200
instance.solutions.load_from(results) # Loading solution into instance
return instance