LarrySnyder / stockpyl

Python inventory optimization and simulation tools.
GNU General Public License v3.0
107 stars 20 forks source link

Incorrect RM to FG conversion during simulation #175

Open ziegeo opened 1 month ago

ziegeo commented 1 month ago

In the simulation module, the function raw_materials_to_finished_goods(node) uses the following line to determine the total order placed for a raw material LT periods ago: units_ordered = sum([node.state_vars[period - OLT - SLT].order_quantity[pred_index][rm_index] for pred_index in node.raw_material_suppliers_by_raw_material(rm_index, return_indices=True, network_BOM=True)])

A few lines later, the variable units_ordered is used to determine share_frac, the fraction of available RM that can be allocated to a specific FG: share_frac = {prod_index: node.state_vars[period - OLT - SLT].order_quantity_fg[prod_index] * node.NBOM(product=prod_index, predecessor=None, raw_material=rm_index) / units_ordered for prod_index in prods_for_rm}

The variable share_frac is then used to calculate the share of available RM that can be converted to a specific FG: share[rm_index] = {prod_index: avail_rm * share_frac[prod_index] for prod_index in prods_for_rm}

This can lead to a larger quantity of FG being produced than there is available RM.

I have not looked at the root cause for this in too much detail, but my initial thought is that this is caused when there is a order_quantity_override passed to the function _generate_downstream_orders. It looks to me like the function applies that override to the number of ordered RM, but not to the number of ordered FG, which in turn can lead to the incorrect ratio mentioned above.

LarrySnyder commented 1 month ago

@ziegeo Can you share a code snippet that reproduces this bug?