我自己对这个问题的解决方案可能不是最好的或有效的算法,但至少它确实给我一个可能的匹配列表(我认为)。
首先,我需要筛选和排序我拥有的物料资源批次。伪代码如下:
func filter_sort_batches(product, batches)
newlist = []
product_keys = product.keys
foreach batch in batches:
score = 0
if split doesn't contain a product_key, ignore batch
if the split doesn't contain a product_key but also contain other keys that the product doesn't require, ignore batch
for each key in batch:
if product[key] >= batch[key]:
score += (product[key] - batch[key])
else:
score += (batch[key] - product[key]) * product[key]
put [score, batch] to newlist
newlist.sort_by score, where the lowest score is the best
要返回比赛需要2个功能,该 主要功能如下:
func find_match(list, product)
newlist = list.clone
matches = Array.new
find_match_recursive(newlist, product, matches, Array.new)
result = matches.sort_by score, where the lowest score is the best
print result
递归函数如下:
func find_match_recursive(list, product, matches, group)
return if list is empty
newlist = list.clone
tuple = newlist.shift
batch = tuple.last
new_group = group.clone
new_group << batch
if has_met_quantity(check_outsanding_materials(product, new_group)):
grp_tuple = score_group(new_group, product)
score = grp_tuple[0]
if score less than 2x of the sum of product's values:
add grp_tuple to matches
else:
# recurse to get the next batch and try to meet quantity check
find_match_recursive(newlist, product, matches, new_group)
#ignore the current recursion and skip to next batch in the list
find_match_recursive(newlist, product, matches, group)
其他功能我没有包括伪代码:
score_group: computes the wastage for each material used.
check_outsanding_materials: calculate how much material is required after substracting the batches
has_met_quantity: check if the above for any materials qty has met or not.
对于正在使用的批次数是否有任何意义(例如,批次越少越好)? – FDavidov