How to find optimum combination for Cutting Stock Problem using Knapsack
There is no algorithm that will guarantee you the optimal solution other than brute-force checking all possible combinations. That's obviously not a good algorithm, at least not if you have large data sets.
You should have a look at heuristic search algorithms like Simulated Annealing, MCTS or the like. It should be no problem finding existing implementations of heuristic search engines that allow you to provide them with
- an input set (random distribution of pieces on material),
- a transition function (move pieces between materials),
- and an evaluation function (the amount of waste)
and compute a near-optimal solution for you.
I'll echo the sentiment of the other answers: while there might be a "most correct" solution to this problem, what you're really looking for is a "correct enough" solution.
That said, I wrote up this little appendix to help make sense of the code in the project you referenced: cutlist generator design notes
Paraphrased:
Each iteration starts by creating a new instance of the longest stock and placing as many pieces into it as will fit. The stock is then reduced to the smallest stock that all of the selected pieces still fit into. This is all repeated until no pieces remain.
Another word of advice: make sure to clearly identify all of the assumptions you're building into the algorithm. Mine assumes that longer stock is cheaper per unit and that it's always preferred, but I've been asked for variations to optimize for the least number of cuts (bundle cutting) or to keep track of and prefer to use offcuts from previous runs first.
As always: Understand your customer's process very clearly before setting the assumptions.