cst['net'] = net

#creating array of initial terminal costs J0
xsteps = np.prod(states['xsteps'].values)
J0 = np.zeros(xsteps)
idx = prd.find_index(np.array([20, 0]), states)
J0[idx] = -9999.9

#define function for simulation that calculates costs and next state
system = model.building_with_storage

#optimize with DP forward algorithm
result = prd.DP_forward(states,
                        U,
                        timesteps,
                        cst,
                        srs,
                        system,
                        J0=J0,
                        verbose=True)

#find cost minimal solution from result
i_mincost = result.loc[cst['t_end'] - 1]['J'].idxmin()
opt_result = result.xs(i_mincost, level='Xidx_end')

#find solution with empty heat-storage from result
#opt_result = result.xs(0,level='Xidx_end')

######################################################
#extracting interesting parameters and plotting them
######################################################
best_massflow = opt_result['massflow'].values[:-1]
#starts at the first timestep, while the DP_backward starts at the last timestep
#and solves the problem "backward in time". The forward algorithm has the
#advantage, that states and calculated parameters from previous timesteps
#can be used in the model, which might be necessary to model the problem.
#The backward algorithm only can access "future" states and parameters, but
#because it is usually faster and therefore should be chosen if past information
#is not necessary to model the problem. Both algorithms are described more
#detailed in the chapter "how prodyn works".

#DP_forward
###

result_fw = prd.DP_forward(states,
                           controls,
                           timesteps,
                           constants,
                           timeseries,
                           storage_model,
                           verbose=True,
                           t_verbose=10)

#Here we want to get the results where the storage is empty at the end (Xidx_end=0)
result0_fw = result_fw.xs(0, level='Xidx_end')

#DP_backward
###
result_bw = prd.DP_backward(states,
                            controls,
                            timesteps,
                            constants,
                            timeseries,
                            storage_model,