Skip to content

Commit

Permalink
Update TDR/MoM tests - compare output files
Browse files Browse the repository at this point in the history
  • Loading branch information
lbonaldo committed Nov 12, 2023
1 parent 8f21623 commit c3c0671
Show file tree
Hide file tree
Showing 16 changed files with 5,746 additions and 59 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ Example_Systems/SmallNewEngland/ThreeZones_full/*

Example_Systems/SmallNewEngland/ThreeZones_MultiStage/Inputs/*
test_get_retirement_period.jl

# Test files
test/TDR/TDR_Results_test
test/MethodofMorris/morris.csv
11 changes: 8 additions & 3 deletions src/additional_tools/method_of_morris.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const SEED = 1234


@doc raw"""
morris(EP::Model, path::AbstractString, setup::Dict, inputs::Dict, outpath::AbstractString, OPTIMIZER)
Expand All @@ -14,6 +17,7 @@ struct MorrisResult{T1,T2}
variances::T1
elementary_effects::T2
end

function generate_design_matrix(p_range, p_steps, rng;len_design_mat,groups)
ps = [range(p_range[i][1], stop=p_range[i][2], length=p_steps[i]) for i in 1:length(p_range)]
indices = [rand(rng, 1:i) for i in p_steps]
Expand Down Expand Up @@ -74,8 +78,9 @@ function sample_matrices(p_range,p_steps, rng;num_trajectory,total_num_trajector
reduce(hcat,matrices)
end

function my_gsa(f, p_steps, num_trajectory, total_num_trajectory, p_range::AbstractVector,len_design_mat,groups)
function my_gsa(f, p_steps, num_trajectory, total_num_trajectory, p_range::AbstractVector, len_design_mat, groups, random)
rng = Random.default_rng()
if !random; Random.seed!(SEED); end
design_matrices_original = sample_matrices(p_range, p_steps, rng;num_trajectory,
total_num_trajectory,len_design_mat,groups)
println(design_matrices_original)
Expand Down Expand Up @@ -158,7 +163,7 @@ function my_gsa(f, p_steps, num_trajectory, total_num_trajectory, p_range::Abstr
end
MorrisResult(reduce(hcat, means),reduce(hcat, means_star),reduce(hcat, variances),effects)
end
function morris(EP::Model, path::AbstractString, setup::Dict, inputs::Dict, outpath::AbstractString, OPTIMIZER)
function morris(EP::Model, path::AbstractString, setup::Dict, inputs::Dict, outpath::AbstractString, OPTIMIZER; random=true)

# Reading the input parameters
Morris_range = load_dataframe(joinpath(path, "Method_of_morris_range.csv"))
Expand Down Expand Up @@ -197,7 +202,7 @@ function morris(EP::Model, path::AbstractString, setup::Dict, inputs::Dict, outp
end

# Perform the method of morris analysis
m = my_gsa(f1,p_steps,num_trajectory,total_num_trajectory,p_range,len_design_mat,groups)
m = my_gsa(f1,p_steps,num_trajectory,total_num_trajectory,p_range,len_design_mat,groups,random)
println(m.means)
println(DataFrame(m.means', :auto))
#save the mean effect of each uncertain variable on the objective fucntion
Expand Down
21 changes: 13 additions & 8 deletions src/time_domain_reduction/time_domain_reduction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ using Distances
using CSV
using GenX


const SEED = 1234

@doc raw"""
rmse_score(y_true, y_pred)
Expand Down Expand Up @@ -202,7 +205,7 @@ function get_worst_period_idx(R)
end

@doc raw"""
cluster(ClusterMethod, ClusteringInputDF, NClusters, nIters)
cluster(ClusterMethod, ClusteringInputDF, NClusters, nIters, v=false, random=true)
Get representative periods using cluster centers from kmeans or kmedoids.
Expand All @@ -212,12 +215,13 @@ https://juliastats.org/Clustering.jl/dev/kmeans.html
K-Medoids:
https://juliastats.org/Clustering.jl/stable/kmedoids.html
"""
function cluster(ClusterMethod, ClusteringInputDF, NClusters, nIters, v=false)
function cluster(ClusterMethod, ClusteringInputDF, NClusters, nIters, v=false, random=true)
if ClusterMethod == "kmeans"
DistMatrix = pairwise(Euclidean(), Matrix(ClusteringInputDF), dims=2)
R = kmeans(Matrix(ClusteringInputDF), NClusters, init=:kmcen)

for i in 1:nIters
if !random; Random.seed!(SEED); end
R_i = kmeans(Matrix(ClusteringInputDF), NClusters)

if R_i.totalcost < R.totalcost
Expand All @@ -243,6 +247,7 @@ function cluster(ClusterMethod, ClusteringInputDF, NClusters, nIters, v=false)
R = kmedoids(DistMatrix, NClusters, init=:kmcen)

for i in 1:nIters
if !random; Random.seed!(SEED); end
R_i = kmedoids(DistMatrix, NClusters)
if R_i.totalcost < R.totalcost
R = R_i
Expand All @@ -257,7 +262,7 @@ function cluster(ClusterMethod, ClusteringInputDF, NClusters, nIters, v=false)
M = R.medoids # get the cluster centers - M for Medoids
else
println("INVALID ClusterMethod. Select kmeans or kmedoids. Running kmeans instead.")
return cluster("kmeans", ClusteringInputDF, NClusters, nIters)
return cluster("kmeans", ClusteringInputDF, NClusters, nIters, v, random)
end
return [R, A, W, M, DistMatrix]
end
Expand Down Expand Up @@ -485,7 +490,7 @@ end


@doc raw"""
cluster_inputs(inpath, settings_path, v=false, norm_plot=false, silh_plot=false, res_plots=false, indiv_plots=false, pair_plots=false)
cluster_inputs(inpath, settings_path, mysetup, stage_id=-99, v=false; random=true)
Use kmeans or kmedoids to cluster raw demand profiles and resource capacity factor profiles
into representative periods. Use Extreme Periods to capture noteworthy periods or
Expand Down Expand Up @@ -538,7 +543,7 @@ to separate Vre_and_stor_solar_variability.csv and Vre_and_stor_wind_variability
and wind profiles for co-located resources will be separated into different CSV files to be read by loading the inputs
after the clustering of the inputs has occurred.
"""
function cluster_inputs(inpath, settings_path, mysetup, stage_id=-99, v=false)
function cluster_inputs(inpath, settings_path, mysetup, stage_id=-99, v=false; random=true)
if v println(now()) end

##### Step 0: Load in settings and data
Expand Down Expand Up @@ -781,7 +786,7 @@ function cluster_inputs(inpath, settings_path, mysetup, stage_id=-99, v=false)
cluster_results = []

# Cluster once regardless of iteration decisions
push!(cluster_results, cluster(ClusterMethod, ClusteringInputDF, NClusters, nReps, v))
push!(cluster_results, cluster(ClusterMethod, ClusteringInputDF, NClusters, nReps, v, random))

# Iteratively add worst periods as extreme periods OR increment number of clusters k
# until threshold is met or maximum periods are added (If chosen in inputs)
Expand All @@ -790,15 +795,15 @@ function cluster_inputs(inpath, settings_path, mysetup, stage_id=-99, v=false)
if IterateMethod == "cluster"
if v println("Adding a new Cluster! ") end
NClusters += 1
push!(cluster_results, cluster(ClusterMethod, ClusteringInputDF, NClusters, nReps, v))
push!(cluster_results, cluster(ClusterMethod, ClusteringInputDF, NClusters, nReps, v, random))
elseif (IterateMethod == "extreme") & (UseExtremePeriods == 1)
if v println("Adding a new Extreme Period! ") end
worst_period_idx = get_worst_period_idx(last(cluster_results)[1])
removed_period = string(names(ClusteringInputDF)[worst_period_idx])
select!(ClusteringInputDF, Not(worst_period_idx))
push!(ExtremeWksList, parse(Int, removed_period))
if v println(worst_period_idx, " (", removed_period, ") ", ExtremeWksList) end
push!(cluster_results, cluster(ClusterMethod, ClusteringInputDF, NClusters, nReps, v))
push!(cluster_results, cluster(ClusterMethod, ClusteringInputDF, NClusters, nReps, v, random))
elseif IterateMethod == "extreme"
println("INVALID IterateMethod ", IterateMethod, " because UseExtremePeriods is off. Set to 1 if you wish to add extreme periods.")
break
Expand Down
9 changes: 9 additions & 0 deletions test/MethodofMorris/morris_true.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Resource,Zone,ID,Lower_bound,Upper_bound,Parameter,Group,p_steps,total_num_trajectory,num_trajectory,len_design_mat,policy,mean,variance
natural_gas_combined_cycle,1,1,-5,5,Inv_Cost_per_MWyr,ng,10,2,2,15,reference,19.876405206036726,52.14720602710861
solar_pv,1,2,-10,10,Inv_Cost_per_MWyr,solar,20,,,,,0.01587702025497309,NaN
onshore_wind,1,3,-7,7,Inv_Cost_per_MWyr,wind,14,,,,,0.9168892311220421,NaN
battery,1,4,-20,20,Inv_Cost_per_MWyr,battery,40,,,,,1.2587368193702644,0.02628898806467195
natural_gas_combined_cycle,1,1,-5,5,Fixed_OM_Cost_per_MWyr,ng,10,,,,,19.876405206036726,52.14720602710861
solar_pv,1,2,-10,10,Fixed_OM_Cost_per_MWyr,solar,20,,,,,0.01587702025497309,NaN
onshore_wind,1,3,-7,7,Fixed_OM_Cost_per_MWyr,wind,14,,,,,0.9168892311220421,NaN
battery,1,4,-20,20,Fixed_OM_Cost_per_MWyr,battery,40,,,,,1.2587368193702644,0.02628898806467195
Loading

0 comments on commit c3c0671

Please sign in to comment.