जिस समस्या को मैं हल करने की कोशिश कर रहा हूं वह यह है कि हमारे पास ऐसे टुकड़े हैं जिन्हें हमें काटने की जरूरत है और ऐसा करने का सबसे कुशल तरीका खोजना चाहते हैं।
var lengthsArray = [20, 24, 40, 48];
var piecesNeeded = 8;
var lengthPerPiece = 11.75;
ऐसा करने का सबसे कारगर तरीका 48 में से 2 या 24 लंबाई में से 4 का उपयोग करना है। ऐसा करने के लिए मैं एक फ़ंक्शन कैसे लिखूंगा? अलग-अलग परिदृश्यों में lengthArray में अलग-अलग मान होंगे।
मैंने निम्नलिखित की कोशिश की है, लेकिन मैं आधार से दूर हूं:
let materialLengthsArray = [48, 40, 24, 20]
let totalLFSF = 94
let materialToBuy = totalLFSF;
let materialsBreakdown = [];
for (i = 0; i < materialLengthsArray.length; i++) {
if(materialToBuy / materialLengthsArray[i] < 1) {
console.log('dun')
} else {
console.log(materialToBuy)
materialsBreakdown.push({
'length' : materialLengthsArray[i],
'count' : Math.floor(materialToBuy / materialLengthsArray[i])
})
materialToBuy = materialToBuy % materialLengthsArray[i]
}
console.log(materialsBreakdown)
console.log('--')
}
2 जवाब
आप प्रत्येक टुकड़े से आने वाले बचे हुए/अपशिष्ट की मात्रा की गणना कर सकते हैं और इसे न्यूनतम खोजने के लिए सॉर्ट कर सकते हैं। कुछ इस तरह?
// sample data
const lengths = [20, 24, 40, 48];
const piecesNeeded = 8;
const pieceLength = 11.75;
// how many pieces of a given length can we get from each unit of material?
const piecesPerItem = (materialLength, pieceSize) => Math.floor(materialLength / pieceSize);
// compute stats for cutting pieces
// from a given denomination of material
const computeWaste = (materialLength, pieceSize) => {
// pieces per unit of material
const perItem = piecesPerItem(materialLength, pieceSize);
// if the piece size is greater than the material size, bail
if (perItem < 1) {
return null;
}
// how many units of material would we need?
const itemsNeeded = Math.ceil(piecesNeeded / perItem);
// how much would be left over from each?
const wastePerItem = materialLength % pieceSize;
// return what we've learned
return {
perItem,
itemsNeeded,
materialLength,
wastePerItem,
totalWaste: wastePerItem * itemsNeeded
};
}
// compute the waste for each material length
const leftovers = lengths.map(length => computeWaste(length, pieceLength));
// sort by least total waste
leftovers.sort(({totalWaste: a}, {totalWaste: b}) => a - b)
// the first entry has the least total waste
console.log(leftovers[0]);
// emit all the results for inspection
console.log(leftovers);
यह समाधान प्रत्येक संभावित प्रारंभिक लंबाई के लिए एक calculateWaste
फ़ंक्शन को कॉल करता है, परिणामों को समानांतर सरणी में मैप करता है, और इंडेक्स के साथ शुरुआती लंबाई चुनता है जो इंडेक्स से कम से कम अपशिष्ट से मेल खाता है।
calculateWaste
फ़ंक्शन दो नेस्टेड लूप का उपयोग करता है ताकि दी गई शुरुआती लंबाई के बोर्डों से टुकड़ों को काटते रहें, जब तक कि पर्याप्त बोर्ड काटे न जाएं, फिर सभी बोर्डों से संचित अपशिष्ट लौटा दें।
const
// Defines starting data
pieces = 8,
length = 11.75,
availableStartingLengths = [20, 24, 40, 48],
// Makes an array of waste amounts for each starting length
amountsWasted = availableStartingLengths.map( (startLen) =>
calculateWaste(pieces, length, startLen)),
// Gets the minimum waste from the new array
minWaste = Math.min(...amountsWasted);
// Gets starting lengths with minimum waste
const bestStartingLengths = amountsWasted.reduce( (indices, amount, index) =>
// Populates an array with the index values having minimum waste
amount === minWaste
? indices.concat(index)
: indices,
[]
// Makes an array of startingLengths at the selected indices
).map(i => availableStartingLengths[i]);
// Reports results
console.log("best starting lengths:", bestStartingLengths);
// Defines the `calculateWaste` function
function calculateWaste(piecesNeeded, lengthPerPiece, startingLength){
//console.log("\n" + `calculating waste for starting length ${startingLength}`);
let // Defines variables that change in the outer loop
totalPiecesCut = 0,
quantityUsed = 0,
accumulatedScrap = 0;
if(startingLength < lengthPerPiece){ // Board is too short
return Infinity;
}
while(totalPiecesCut < piecesNeeded){
// Starts cutting a new board
quantityUsed++;
let // Defines variables that change in the inner loop
piecesCut = 0,
remaining = startingLength;
while((remaining >= lengthPerPiece) && (totalPiecesCut < piecesNeeded)){
//console.log(` cutting ${lengthPerPiece} from ${remaining}`);
// Cuts a new piece
remaining -= lengthPerPiece;
piecesCut++;
}
totalPiecesCut += piecesCut;
accumulatedScrap += remaining;
//console.log(` ${piecesCut} pieces cut from board #${quantityUsed}, with ${remaining} remaining`);
}
//console.log(` ${totalPiecesCut} pieces cut, ${accumulatedScrap} units wasted`)
return accumulatedScrap;
}