Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Permutations - Explained solution in Clear category for Most Efficient Cutting by Selindian
from itertools import permutations # We need that to get all possible combinations of our bom.
def most_efficient_cutting(bom):
def f(bom): # Function that calculates was for one of our combinations.
wasted = 0 # Initial waste value = 0
remain = 6000 # max length = 6000 mm
for item in bom: # Loop over all items in the bom ...
if item <= remain: # ... if we can cut item from remaining material ...
remain -= item # ... ... reduce remaining material
else: # ... Otherwise
wasted += remain # ... add remaining material to waste (as it is to small)
remain = 6000 - item # ... and cut the item from a new 6000 mm material
wasted += remain # After the loop add the remaining material to the waste
return wasted # return the waste value
all_combinations = permutations(bom) # Create iterartor of all possible combinations to be able to try out all combinations.
wasted = min(f(v) for v in all_combinations) # Run function on all combinations, select minimum of all returned values.
return wasted # return that minimum value
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert(most_efficient_cutting([3000, 2200, 2000, 1800, 1600, 1300]) == 100)
assert (most_efficient_cutting([4000, 4000, 4000]) == 6000), "wasted: 3x2000"
assert(most_efficient_cutting([1]) == 5999), "5999"
assert(most_efficient_cutting([3001, 3001]) == 5998), "2x2999"
assert(most_efficient_cutting([3000, 2200, 1900, 1800, 1600, 1300]) == 200), "2x5900"
assert(most_efficient_cutting([3000]) == 3000)
assert(most_efficient_cutting([3000, 2200, 2000, 1800, 1600, 1400]) == 0)
print('"Run" is good. How is "Check"?')
Aug. 29, 2022
Comments: