Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
394f216
Updated utils file to be compatible with both version of models (det …
Nov 29, 2025
193d90f
Updated ECModel_definitions file to be compatible with both version o…
Nov 29, 2025
12475be
Added all stochastic files to be made compatible
Nov 29, 2025
e5ada53
Updated readme to keep track of all updates/fixes/issues. Created a f…
sclfnc Nov 29, 2025
23be664
fix: ensure src/stochastic_data is tracked by git
sclfnc Nov 29, 2025
cc76a22
Added new file EC_Model_utils to store all the auxiliary methods invo…
Nov 30, 2025
9af9b15
Updated folder structure
Nov 30, 2025
d37b3ed
Remobved old EC_Model_definitions
Nov 30, 2025
8535d2f
Directory and file reorganization. Updated manifest to - hopefully - …
sclfnc Dec 1, 2025
b386cfb
small fixes
sclfnc Dec 8, 2025
593c1ec
Merge remote-tracking branch 'upstream/main'
Dec 9, 2025
dc78185
Provided minimal functioning example
sclfnc Dec 9, 2025
8f9f181
Added docstrings and documentation. Cleaning.
sclfnc Dec 9, 2025
6591714
Minor fix
Dec 9, 2025
40182f1
Minor fix
Dec 9, 2025
bf16ea9
Minor fix
Dec 9, 2025
61a92e3
Minor fix
Dec 9, 2025
62e6aad
Restored original repo version
Dec 9, 2025
6527441
Minor fix
sclfnc Dec 9, 2025
ee6f3bb
Restored README file
Dec 9, 2025
30f50e3
Removed backup file
Dec 9, 2025
0d47b4c
Moved stochastic example
Dec 9, 2025
ed09081
Added example file and fixed duplicate function
Dec 9, 2025
fafa77f
Minor fix
Dec 9, 2025
8c07049
Minor fix
Dec 9, 2025
cef814c
Added small workaround for conflict generated by StochasticPrograms.jl
Dec 9, 2025
a16e985
Minor fix
Dec 9, 2025
d59d814
Minor fix
Dec 9, 2025
2e4e851
Minor fix
Dec 9, 2025
f6d59e4
Restored files to closely match previous versions
Dec 10, 2025
7f1f5ed
Restored files to closely match previous versions
Dec 10, 2025
6c4df46
remove no penalties data
davide-f Dec 11, 2025
baf4d9c
Merge branch 'SPSUnipi:main' into main
enri07 May 6, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,36 @@ version = "0.3.1"
[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
ExportAll = "ad2082ca-a69e-11e9-38fa-e96309a31fe4"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Format = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
PointEstimateMethod = "f5532426-4504-4a0a-b768-abad0680b22a"
SankeyPlots = "8fd88ec8-d95c-41fc-b299-05f2225f2cc5"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
StochasticPrograms = "8b8459f2-c380-502b-8633-9aed2d6c2b35"
TheoryOfGames = "eb50afb4-6f20-4b37-9b66-473e668300bf"
XLSX = "fdbf4ff8-1666-58a4-91e7-1b58723a45e0"
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"

[compat]
CSV = "0.10"
DataFrames = "1"
Distributions = "0.25"
ExportAll = "0.1"
FileIO = "1"
Format = "1"
JuMP = "1"
MathOptInterface = "1.0"
MathOptInterface = "1"
Plots = "1, 2"
PointEstimateMethod = "0.1"
SankeyPlots = "0.2.2, 0.3"
StatsPlots = "0.15"
TheoryOfGames = "0.1, 0.2"
XLSX = "0.9, 0.10"
YAML = "0.4"
julia = "1"
StochasticPrograms = "0.6"
TheoryOfGames = "0.1"
XLSX = "0.10"
YAML = "0.4"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ optimize!(ECModel)

# create some plots
plot(ECModel, output_plot_combined)
```
```
178 changes: 178 additions & 0 deletions examples/RunStochModel(TBD).jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#using StochasticPrograms#master
#using JuMP
#using Base.Threads
#using DataStructures
#using LinearAlgebra
#using Parameters
#using Distributions
#using Random
#using JLD2
#using FileIO
#using PointEstimateMethod
#using YAML
#using DataFrames
#using CSV
#using XLSX
#using Formatting
# Useful package to built plot
#using Makie
#using CairoMakie
#using ColorSchemes
#using StochasticPrograms

#import CPLEX

# # Run this script from EnergyCommunity.jl root!!!
# using Pkg
# Pkg.activate("examples")

using EnergyCommunity, JuMP
using HiGHS, Plots

# Data extraction
file_name = "./src/stochastic/stoch_data/energy_community_model.yml"
data = read_input(file_name)

(gen_data,
users_data,
market_data) = explode_data(data)


n_users = length(user_names(gen_data, users_data))
init_step = field(gen_data, "init_step")
final_step = field(gen_data, "final_step")
n_steps = final_step - init_step + 1
project_lifetime = field(gen_data, "project_lifetime")
peak_categories = profile(market_data, "peak_categories")

# Set definitions
user_set = user_names(gen_data, users_data)
year_set = 1:project_lifetime
time_set = 1:n_steps
peak_set = unique(peak_categories)

# Number of scenarios to be extracted
scen_s_sample = field(gen_data, "n_s")
scen_eps_sample = field(gen_data, "n_eps")
n_scen_sample = scen_s_sample * scen_eps_sample

isdet = false
if scen_eps_sample == 1 && scen_s_sample == 1
isdet = true
end

scen_s_set = 1:scen_s_sample
scen_eps_set = 1:scen_eps_sample

# Standard deviation associated with load and renewable production in long period uncertainty

sigma_load = 0.3

mean_pv = 1.0
sigma_pv = 0.1

mean_wind = 0.95
sigma_wind = 0.15

# Extract Uncertain Variable
unc_var = field(gen_data, "uncertain_var")

# Extraction of the point used to sample the distributions associated to the long period uncertainty
(point_s_load,
point_s_pv,
point_s_wind,
scen_probability) = pem_extraction(scen_s_sample,
sigma_load,
mean_pv,
sigma_pv,
mean_wind,
sigma_wind,
unc_var)

# Include the sampler for distributions associated to short period uncertainty and a function to generate scenarios (new version: sample the normalized distributions)

# include("point_Scen_eps_sampler.jl")

# To define an empty stochastic model we have to declare previously the scenarios

# OUTPUT: sampled_scenarios: array containing all the scenarios created for the first phase
# point_eps_load_sampled: extracted points for the normalized distributions associated with load demand
# point_eps_ren_sampled: extracted points for the normalized distributions associated with renewable production

sampled_scenarios = scenarios_generator(data,
point_s_load,
point_s_pv,
point_s_wind,
scen_s_sample,
scen_eps_sample,
unc_var,
point_probability=scen_probability,
first_stage=true,
deterministic=isdet)

# Initialize the empty non-cooperative version of a EC

EC_NonCooperative = StochasticEC(file_name,GroupNC(),CPLEX.Optimizer,sampled_scenarios,scen_s_sample,scen_eps_sample)

# Build the NC model

build_base_model!(EC_NonCooperative,CPLEX.Optimizer)

# set the technical paraters for the NC optimization
time_lim = 60 * 60 * 10 # max time in second
primal_gap = 1e-2 # primal gap (1e-4 = 1%)
n_threads = 64 # number of threads to be used

set_parameters_ECmodel!(EC_NonCooperative,primal_gap,time_lim,n_threads,1)

optimize_deterministic_ECmodel(EC_NonCooperative) # optimize the deterministic equivalent version and store the results

# save the data of the first stage model
output_file_NC = "first_stage_output_NC_($scen_s_sample,$scen_eps_sample)"

print_first_stage(output_file_NC * ".xlsx",EC_NonCooperative)
save(output_file_NC * ".jld2", EC_NonCooperative)

# get the number of installed resource by users
x_NC_fixed = EC_NonCooperative.results[:x_us].data

# add the installed capacity of the entire EC
x_tot_NC = calculate_x_tot(EC_NonCooperative)

#Free memory
EC_NonCooperative = StochasticEC();

GC.gc() # garbage collector

# Initialize the cooperative version of a EC

EC_Cooperative = StochasticEC(file_name,GroupCO(),CPLEX.Optimizer,sampled_scenarios,scen_s_sample,scen_eps_sample)

# Build the CO model

build_specific_model!(GroupCO(),EC_Cooperative,CPLEX.Optimizer)

# set the technical parameters for the CO optimization
time_lim = 60 * 60 * 24 # max time in second
primal_gap = 1e-2 # primal gap (1e-4 = 1%)
n_threads = 64 # number of threads to be used

set_parameters_ECmodel!(EC_Cooperative,primal_gap,time_lim,n_threads,1)

optimize_deterministic_ECmodel(EC_Cooperative) # optimize the deterministic equivalent version and store the results

# save the data of the first stage model
output_file_CO = "first_stage_output_CO_($scen_s_sample,$scen_eps_sample)"
print_first_stage(output_file_CO * ".xlsx",EC_Cooperative)
save(output_file_CO * ".jld2", EC_Cooperative)
# get the number of installed resource by users
x_CO_fixed = EC_Cooperative.results[:x_us].data

# Plot some useful image of the installed capacity
colors = Makie.wong_colors()

# add the installed capacity of the entire EC
x_tot_CO = calculate_x_tot(EC_Cooperative)

plot_resource("installed_capacity1_($scen_s_sample,$scen_eps_sample).png",["PV","wind"],users_data,x_tot_CO,x_tot_NC,colors[1:2]) # renewable asset
plot_resource("installed_capacity_($scen_s_sample,$scen_eps_sample).png",["batt"],users_data,x_tot_CO,x_tot_NC,[colors[3]]) #battery
Loading
Loading