Skip to content

Commit

Permalink
Adhere to Black
Browse files Browse the repository at this point in the history
  • Loading branch information
p-snft committed Dec 11, 2024
1 parent c81fc5b commit 1641a98
Showing 1 changed file with 43 additions and 27 deletions.
70 changes: 43 additions & 27 deletions examples/expected_flow_value/expected_flow_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@

solver = "cbc" # 'glpk', 'gurobi',...
solver_verbose = True # show/hide solver output
time_steps = 24*31 # 8760
time_steps = 24 * 31 # 8760

date_time_index = pd.date_range("1/1/2000", periods=time_steps, freq="H")

rng = np.random.default_rng(seed=1337)
random_costs = rng.exponential(size=time_steps)
random_demands = rng.uniform(size=time_steps)
random_losses = np.minimum(
np.ones(time_steps),
rng.exponential(scale=1e-1, size=time_steps)
np.ones(time_steps), rng.exponential(scale=1e-1, size=time_steps)
)


Expand All @@ -46,35 +45,44 @@ def run_energy_system(index, costs, demands, losses, expected=None):
bus = solph.buses.Bus(label="bus")
source = solph.components.Source(
label="source",
outputs={bus: solph.flows.Flow(
variable_costs=costs,
nonconvex=solph.NonConvex(activity_costs=0.01),
min=0.2,
nominal_value=100,
)},
outputs={
bus: solph.flows.Flow(
variable_costs=costs,
nonconvex=solph.NonConvex(activity_costs=0.01),
min=0.2,
nominal_value=100,
)
},
)
storage = solph.components.GenericStorage(
label="storage",
inputs={bus: solph.flows.Flow(
expected=expected[(("bus", "storage"), "flow")])},
outputs={bus: solph.flows.Flow(
expected=expected[(("storage", "bus"), "flow")])},
inputs={
bus: solph.flows.Flow(
expected=expected[(("bus", "storage"), "flow")]
)
},
outputs={
bus: solph.flows.Flow(
expected=expected[(("storage", "bus"), "flow")]
)
},
nominal_storage_capacity=1e4,
loss_rate=losses,
)
sink = solph.components.Sink(
label="sink",
inputs={
bus: solph.flows.Flow(nominal_value=1, fix=demands)
},
inputs={bus: solph.flows.Flow(nominal_value=1, fix=demands)},
)

energy_system.add(bus, source, sink, storage)
model = solph.Model(energy_system)
model.solve(solver=solver, solve_kwargs={
"tee": solver_verbose,
"warmstart": True,
})
model.solve(
solver=solver,
solve_kwargs={
"tee": solver_verbose,
"warmstart": True,
},
)

_results = solph.processing.results(model)
_meta_results = solph.processing.meta_results(model)
Expand All @@ -84,19 +92,27 @@ def run_energy_system(index, costs, demands, losses, expected=None):
meta_results = {}

results, meta_results["no hints 1"] = run_energy_system(
date_time_index, random_costs, random_demands, random_losses)
date_time_index, random_costs, random_demands, random_losses
)
bus_data = solph.views.node(results, "bus")["sequences"]
_, meta_results["no hints 2"] = run_energy_system(
date_time_index, random_costs, random_demands, random_losses)
date_time_index, random_costs, random_demands, random_losses
)
_, meta_results["with hints"] = run_energy_system(
date_time_index, random_costs, random_demands, random_losses,
date_time_index,
random_costs,
random_demands,
random_losses,
expected=bus_data,
)
_, meta_results["no hints 3"] = run_energy_system(
date_time_index, random_costs, random_demands, random_losses)
date_time_index, random_costs, random_demands, random_losses
)

for meta_result in meta_results:
print("Time to solve run {run}: {time:.2f} s".format(
run=meta_result,
time=meta_results[meta_result]['solver']['Wallclock time'])
print(
"Time to solve run {run}: {time:.2f} s".format(
run=meta_result,
time=meta_results[meta_result]["solver"]["Wallclock time"],
)
)

0 comments on commit 1641a98

Please sign in to comment.