in nevergrad/functions/powersystems/core.py [0:0]
def make_plots(self, filename: str = "ps.png") -> None:
losses = self.losses
num_dams = self.num_dams
consumption_per_ts = self.consumption_per_time_step
hydro_prod_per_ts = self.hydro_prod_per_time_step
total_hydro_prod_per_ts = [sum(h) for h in hydro_prod_per_ts]
# Utility function for plotting per year or per day.
def block(x: tp.List[float]) -> tp.List[float]:
result: tp.List[float] = []
step = int(np.sqrt(len(x)))
for i in range(0, len(x), step):
result += [sum(x[i : i + step]) / len(x[i : i + step])]
return result
def block24(x: tp.List[float]) -> tp.List[float]:
result: tp.List[float] = []
for i in range(0, len(x), 24):
result += [sum(x[i : i + 24]) / len(x[i : i + 24])]
if len(x) != len(result) * 24:
print(len(x), len(result) * 24)
return result
def deblock24(x: tp.List[float]) -> tp.List[float]:
result = [0.0] * 24
for i, _ in enumerate(x):
result[i % 24] += x[i] / 24.0
return result
plt.clf()
# Plot the optimization run.
ax = plt.subplot(2, 2, 1)
ax.set_xlabel("iteration number")
smoothed_losses = block(losses)
ax.plot(np.linspace(0, 1, len(losses)), losses, label="losses")
ax.plot(np.linspace(0, 1, len(smoothed_losses)), smoothed_losses, label="smoothed losses")
ax.legend(loc="best")
# Plotting marginal costs.
ax = plt.subplot(2, 2, 4)
marginal_cost_per_hour = deblock24(self.marginal_costs)
marginal_cost_per_day = block24(self.marginal_costs)
ax.plot(
np.linspace(0, 0.5, len(marginal_cost_per_hour)),
marginal_cost_per_hour,
label="marginal cost per hour",
)
ax.plot(
np.linspace(0.5, 1, len(marginal_cost_per_day)),
marginal_cost_per_day,
label="marginal cost per day",
)
ax.legend(loc="best")
# Plot consumption per day and decomposition of production.
ax = plt.subplot(2, 2, 2)
consumption_per_day = block24(consumption_per_ts)
hydro_prod_per_day = block24(total_hydro_prod_per_ts)
ax.plot(np.linspace(1, 365, len(consumption_per_day)), consumption_per_day, label="consumption")
ax.plot(np.linspace(1, 365, len(hydro_prod_per_day)), hydro_prod_per_day, label="hydro")
for i in range(min(num_dams, 3)):
hydro_ts = [h[i] for h in hydro_prod_per_ts]
hydro_day = block24(hydro_ts)
ax.plot(np.linspace(1, 365, len(hydro_day)), hydro_day, label="dam " + str(i) + " prod")
ax.set_xlabel("time step")
ax.set_ylabel("production per day")
ax.legend(loc="best")
# Plot consumption per hour of the day and decomposition of production.
ax = plt.subplot(2, 2, 3)
consumption_per_hour = deblock24(consumption_per_ts)
hydro_prod_per_hour = deblock24(total_hydro_prod_per_ts)
ax.plot(np.linspace(1, 24, len(consumption_per_hour)), consumption_per_hour, label="consumption")
ax.plot(np.linspace(1, 24, len(hydro_prod_per_hour)), hydro_prod_per_hour, label="hydro")
for i in range(min(num_dams, 3)):
hydro_ts = [h[i] for h in hydro_prod_per_ts]
hydro_hour = deblock24(hydro_ts)
ax.plot(np.linspace(1, 24, len(hydro_hour)), hydro_hour, label="dam " + str(i) + " prod")
ax.set_xlabel("time step")
ax.set_ylabel("production per hour")
ax.legend(loc="best")
plt.savefig(filename)