Reputation: 1
This might be a beginner-type question:
I am trying to run a Monte Carlo analysis in Brightway 2.5 using the Beta-PERT distribution and ecoinvent 3.10 (cutoff). However, running even just 100 iterations takes a very long time. For instance, only 10 iterations take 7 minutes, while my goal is to run 10,000 iterations. I was able to pinpoint the issue: as long as my_mc.lci() is within the for loop, the process slows down significantly due to the repetitive recalculation of the inventory. However, as soon as I remove my_mc.lci() from the for loop, I keep getting the same score for every iteration. I've tried nearly every solution I could find online, but nothing seems to work. Does anyone have any advice? Thanks a lot! (I’m using Jupyter Notebook in Anaconda, Python v3.12.7)
#Presetting:
act2 = [x for x in eco_edit if 'carbon black production' in x ['name']
] [0]
heat_exchange= list(act2.technosphere()) [1]
act2.heat_exchange = heat_exchange
#Set up beta-pert distribution since it is not available in stats_arrays
def beta_pert_random(min_val, mode_val, max_val, size=1):
alpha = 1 + 4 * (mode_val - min_val) / (max_val - min_val)
beta_param = 1 + 4 * (max_val - mode_val) / (max_val - min_val)
return beta.rvs(alpha, beta_param, loc=min_val, scale=max_val - min_val, size=size)
#Defining Pert-parameters
min_heat = 0.1 # MJ
mode_heat = 1.5 # MJ (default)
max_heat = 10 # MJ
size = 1
demand = {act2.id: 1}
list_of_methods= [AF, CC, EP, ...]
CC_method = list_of_methods [1] #Climate change
#Set up characterization matrix which is calculated once
def get_C_matrix(demand, list_of_methods):
C_matrix = {}
sacrificial_LCA = bc.LCA(demand)
sacrificial_LCA.lci()
CC_method = list_of_methods[1]
sacrificial_LCA.switch_method(CC_method)
C_matrix[CC_method] = sacrificial_LCA.characterization_matrix
return C_matrix
#Monte Carlo Simulation:
my_iterations = 100
mc_scores = np.empty(shape=[my_iterations])
my_C_matrix = get_C_matrix(demand, list_of_methods)[CC_method]
for iteration in range(my_iterations):
random_heat = beta_pert_random(min_heat, mode_heat, max_heat, size)
heat_exchange['amount'] = random_heat [0]
heat_exchange.save()
my_mc = bc.LCA(demand, method=CC_method)
my_mc.lci()
my_mc.lcia()
next(my_mc)
mc_scores[iteration] = (my_C_matrix * my_mc.inventory).sum()
results = mc_scores.tolist()
Upvotes: 0
Views: 69
Reputation: 1631
in each iteration you're modifying the sql database with your inventory when you do heat_exchange.save()
. This is going to be mega slow. It is much better to use an interface with bw_processing. You can create one that in each iteration returns a value from your function and modify the precise value of your A matrix that you want to change.
An example here or here. You can combine that with the uncertainty coming from your background (ecoinvent) if you want.
Upvotes: 0