flexibility/optimized.py
2024-09-03 17:35:09 -04:00

76 lines
3.2 KiB
Python

import math
import cvxpy as cp
import pandas as pd
data = pd.read_csv('new_file.csv')
demand = data['Q_tot_mpc'].to_list()
demand_watts = [x * 1000 for x in demand]
t_out = data['T_out'].to_list()
p_electricity = data['Electricity_Price'].to_list()
data['Datetime'] = pd.to_datetime(data['Datetime'])
cp_water = 4182 # J/kgK
# Heat Pump Sizing
hp_cap = max(demand_watts) * 0.7
hp_cop_curve_coefficients = [1.039924, 0.0146, 6e-06, -0.05026, 0.000635, -0.000154]
hp_nominal_efficiency = 2.5
hp_delta_t = 5
# TES Sizing
volume = round(max(demand_watts) * 3.6e3 / (1000 * cp_water * 15))
height = 2
d = math.sqrt((4 * volume) / (math.pi * height))
ua = 0.28
# problem setting
time_horizon = 1440 # Number of time steps (15-minute intervals)
time_step_size = 900 # Time step size in seconds (15 minutes)
initial_temperature = 40
lower_limit_TES = 40
upper_limit_TES = 55
# Define problem variables
storage_charge = cp.Variable(time_horizon, nonneg=True) # Energy from heat pump to tank
storage_discharge = cp.Variable(time_horizon, nonneg=True) # Energy from tank to house
heat_pump_energy = cp.Variable(time_horizon, nonneg=True) # Heat pump energy
tank_temperature = cp.Variable(time_horizon) # Tank temperature (°C)
# Define problem parameters
thermal_demand = demand_watts # Heating demand
electricity_price = p_electricity
# Calculate the change in tank temperature
temperature_change = (time_step_size / (1000 * volume *
cp_water)) * (storage_charge[:-1] - storage_discharge[:-1])
# Calculate the electricity cost
electricity_cost = ((heat_pump_energy[:-1] * time_step_size) / (hp_nominal_efficiency * 3600)) @ electricity_price[:-1]
# Define the objective function
objective = cp.Minimize(cp.sum_squares(thermal_demand - storage_discharge)) + cp.Minimize(cp.sum(electricity_cost)) + cp.Minimize(cp.sum(heat_pump_energy))
# Define the constraints
constraints = [heat_pump_energy <= hp_cap, tank_temperature >= lower_limit_TES,
tank_temperature <= upper_limit_TES, tank_temperature[0] == initial_temperature,
storage_charge <= heat_pump_energy]
# Specify the tank temperature in the next time step
constraints.extend([tank_temperature[i+1] == tank_temperature[i] + temperature_change[i] for i in range(time_horizon - 1)])
# Create the optimization problem
problem = cp.Problem(objective, constraints)
# Solve the problem
problem.solve(solver=cp.GUROBI)
# Get the optimized values
optimized_storage_charge = storage_charge.value
optimized_storage_discharge = storage_discharge.value
optimized_heat_pump_energy = heat_pump_energy.value
optimized_tank_temperature = tank_temperature.value
electricity_consumption = (optimized_heat_pump_energy*time_step_size)/(hp_nominal_efficiency*3600)
optimized_cost = electricity_consumption * electricity_price / 1000
output = pd.DataFrame(index=data['Datetime'])
output["optimized_storage_charge"] = optimized_storage_charge
output["optimized_storage_discharge"] = optimized_storage_discharge
output["optimized_heat_pump_energy"] = optimized_heat_pump_energy
output["demand"] = thermal_demand
output["T"] = optimized_tank_temperature
output["electricity_consumption"] = electricity_consumption
output["electricity_cost"] = optimized_cost
out_file = output.to_csv("results_multi_objective.csv")