Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. # PACKAGES
  2. using CSV
  3. using Ipopt
  4. using JuMP
  5. using Plots
  6. using Statistics
  7. using DataFrames
  8.  
  9. function generate_heat_demand(yearly_target::Int64)
  10. # This function returns a time series for the electricity consumption
  11. # of a heat pump given a total yearly consumption
  12.  
  13. df = CSV.read(joinpath(@__DIR__, "temperature.csv"))
  14. hours = length(df[!,:T])
  15.  
  16. # Degree days method
  17. heat_demand = zeros(Float64,hours)
  18. for d = 1:366
  19. daily_mean = 0
  20. for h = 1:24
  21. daily_mean = daily_mean + df[!,:T][d*24-24+h]
  22. end
  23. daily_mean = daily_mean/24
  24. for h = 1:24
  25. heat_demand[d*24-24+h] = max(17-daily_mean,0)
  26. end
  27. end
  28. total = sum(heat_demand)
  29. for h = 1:hours
  30. heat_demand[h] = (heat_demand[h]/total)*0.8+0.2*(1/hours)
  31. end
  32.  
  33. # COP
  34. COP = zeros(Float64,hours)
  35. for h = 1:hours
  36. COP[h] = 2.77+0.155*df[!,:T][h]+0.00918*df[!,:T][h]^2
  37. end
  38.  
  39. # Electricity demand
  40. demand = zeros(Float64,hours)
  41. for h = 1:hours
  42. demand[h] = heat_demand[h]/COP[h]
  43. end
  44. total = sum(demand)
  45. for h = 1:hours
  46. demand[h] = demand[h]*(1/total)*yearly_target
  47. end
  48. return demand
  49. end
  50.  
  51. function run_model(enable_HP::Bool,enable_EV::Bool,enable_PV::Bool,power_tariff::Float64,power_interval::String,time_of_use::Bool)
  52.  
  53. model = Model(with_optimizer(Ipopt.Optimizer, max_cpu_time=300.0))
  54. df = CSV.read(joinpath(@__DIR__, "data.csv"))
  55. hours = length(df[!,:Hour])
  56.  
  57. ToU = zeros(Float64,hours)
  58. for i = 1:hours
  59. ToU[i] = 0.3356
  60. if time_of_use
  61. if i <= 3*30*24 || i >= 9*30*24
  62. if mod(i,24) >= 17 && mod(i,24) <= 20
  63. ToU[i] = 0.8469
  64. end
  65. end
  66. end
  67. end
  68.  
  69. @variable(model, pt[i = 1:hours] >= 0)
  70. @variable(model, CA[i = 1:hours] >= 0)
  71. @variable(model, WA[i = 1:hours] >= 0)
  72. @variable(model, HP[i = 1:hours] >= 0)
  73. @variable(model, 3 >= EV[i = 1:hours] >= 0)
  74. @variable(model, SC[i = 1:hours] >= 0)
  75.  
  76. @NLconstraint(model, [i = 1:hours], CA[i] >= df[!,:CA_fixed][i])
  77. @NLconstraint(model, [p = 1:2928], sum(CA[i] for i in (p*3-2):(p*3)) == sum((df[!,:CA_fixed][i]+df[!,:CA_3hr][i]) for i in (p*3-2):(p*3)))
  78.  
  79. @NLconstraint(model, [i = 1:hours], WA[i] >= df[!,:WA_fixed][i])
  80. @NLconstraint(model, [p = 1:366], sum(WA[i] for i in (p*24-23):(p*24)) == sum((df[!,:WA_fixed][i]+df[!,:WA_24hr][i]) for i in (p*24-23):(p*24)))
  81.  
  82. if enable_HP
  83. demand_HP = generate_heat_demand(3200)
  84. HP_capacity = 1.1*maximum(demand_HP)
  85. @NLconstraint(model, [p = 1:2196], sum(HP[i] for i in (p*4-3):(p*4)) == sum(demand_HP[i] for i in (p*4-3):(p*4)))
  86. @NLconstraint(model, [i = 1:hours], HP[i] <= HP_capacity)
  87. else
  88. @NLconstraint(model, [i = 1:hours], HP[i] == 0)
  89. demand_HP = zeros(Int64,hours)
  90. end
  91.  
  92. if enable_EV
  93. @NLconstraint(model, [i = 1:hours], EV[i] >= df[!,:EV_fixed][i])
  94. @NLconstraint(model, [p = 1:732], sum(EV[i] for i in (p*12-11):(p*12)) >= sum((df[!,:EV_fixed][i]+df[!,:EV_12hr][i]) for i in (p*12-11):(p*12)))
  95. @NLconstraint(model, [p = 1:366], sum(EV[i] for i in (p*24-23):(p*24)) >= sum((df[!,:EV_fixed][i]+df[!,:EV_12hr][i]+df[!,:EV_24hr][i]) for i in (p*24-23):(p*24)))
  96. else
  97. @NLconstraint(model, [i = 1:hours], EV[i] == 0)
  98. end
  99.  
  100. if enable_PV
  101. @NLconstraint(model, [i = 1:hours], SC[i] <= df[!,:PV][i])
  102. @NLconstraint(model, [i = 1:hours], SC[i] <= EV[i]+HP[i]+CA[i]+WA[i]+df[!,:Other_fixed][i])
  103. else
  104. @NLconstraint(model, [i = 1:hours], SC[i] == 0)
  105. end
  106.  
  107. if power_interval == "year"
  108. @NLconstraint(model, [i = 2:hours], pt[i] == pt[1])
  109. elseif power_interval == "month"
  110. month_duration = [31,29,31,30,31,30,31,31,30,31,30,31]
  111. month_sum = 0
  112. for m = 1:12
  113. @NLconstraint(model, [i = (month_sum*24+2):((month_sum+month_duration[m])*24)], pt[i] == pt[month_sum*24+1])
  114. month_sum += month_duration[m]
  115. end
  116. elseif power_interval == "week"
  117. for w = 1:52
  118. @NLconstraint(model, [i = ((w-1)*7*24+2):(w*7*24)], pt[i] == pt[((w-1)*7*24+1)])
  119. end
  120. @NLconstraint(model, [i = 8738:8784], pt[i] == pt[8737])
  121. elseif power_interval == "day"
  122. @NLconstraint(model, [i = ])
  123. end
  124.  
  125. @NLconstraint(model, [i = 1:hours], CA[i]+WA[i]+HP[i]+EV[i]+df[!,:Other_fixed][i]-SC[i] <= pt[i])
  126.  
  127. # OBJECTIVE
  128. @NLobjective(model, Min, sum(pt[i]*power_tariff+((CA[i]+WA[i]+HP[i]+EV[i]+df[!,:Other_fixed][i]-SC[i])*(0.884+0.08+df[!,:Spot_price][i]/1000) - (df[!,:PV][i]-SC[i])*df[!,:Spot_price][i]/1000) for i in 1:hours))
  129.  
  130. # RESULTS
  131. optimize!(model)
  132. return objective_value(model), JuMP.value.(CA), JuMP.value.(WA), JuMP.value.(HP), JuMP.value.(EV), df[!,:Other_fixed], JuMP.value.(SC), df[!,:PV], demand_HP+df[!,:CA_fixed]+df[!,:CA_3hr]+df[!,:WA_fixed]+df[!,:WA_24hr]+df[!,:EV_fixed]+df[!,:EV_12hr]+df[!,:EV_24hr]+df[!,:Other_fixed], df[!,:Spot_price]/1000, JuMP.value.(pt)
  133. end
  134.  
  135. obj, CA, WA, HP, EV, Other, SC, PV, without, spot, pt = run_model(true,true,true,0.28,"month",false)
  136.  
  137. total = CA+WA+HP+EV+Other-PV
  138. #old = without-PV
  139. sort!(total,rev=true)
  140. #sort!(old,rev=true)
  141.  
  142. plotsize = 1:8784
  143. plot(
  144. plotsize,
  145. [
  146. total[plotsize],
  147. pt[plotsize]
  148. ],
  149. ylim = (-10,10),
  150. label = ["Consumption" "Old"]
  151. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement