Reputation: 11
I've been working on a fence calculation tool that breaks down a total length, as equally as possible, into components (rails and posts) using a predefined set of rail lengths. The user can choose a maximum rail length with some leeway (buffer).
Issue: The tool works for most inputs, but it occasionally produces incorrect rail lengths. For example, for small values or certain remaining buffer lengths, it outputs rails that are not allowed (e.g., lengths smaller than the allowed minimum).
Desired output examples (current): Given an input max rail length of 1600 mm, a buffer of 600 mm, and a total length of 8650 mm, the correct output should be: {1600, 1600, 1600, 1800, 1800}
For a total length of 4650 mm, the correct output should be: {1400, 1400, 1600} (These values, with the end post buffer added, sum to the total length, using only allowed rail lengths and ensuring the set is balanced.)
Incorrect Output examples (current): For example, when the remaining length is 1050 mm, the code produces an illegal rail of {800}. It sometimes overshoots or undershoots legal rail lengths.
remainingLength = 27250
remainingBufferLength = 600
maxLengthIndex = 3
standardLengths = [1000, 1200, 1400, 1600, 1800, 2000]
standardLengths.sort()
standardStep = 200
odCCDifference = 250
endPostCount = 2
railSets = []
remainingLength -= odCCDifference
while remainingLength >= standardLengths[maxLengthIndex]:
remainingLength -= standardLengths[maxLengthIndex]
railSets.append(standardLengths[maxLengthIndex])
if remainingBufferLength >= remainingLength:
while remainingLength > 0:
remainingLength -= standardStep
railSets.sort()
railSets[0] += standardStep
else:
remainingLength -= standardLengths[maxLengthIndex]
railSets.append(standardLengths[maxLengthIndex])
while remainingLength < 0:
remainingLength += standardStep
railSets.sort(reverse=True)
railSets[0] -= standardStep
midPostCount = len(railSets)-1
railSetsList = []
for i in set(railSets):
railSetsList.append([i, railSets.count(i)])
print("Endposts: " + str(endPostCount))
print("Midposts: " + str(midPostCount))
for i in railSetsList:
print(str(i[0]) + " Railsets: " + str(i[1]))
Questions:
Second restructured question upload, in case this is the wrong platform for this question, please point me to the correct place to ask this.
Edit: I'll clarify the buffer and standard lengths: we have standard lengths available for purchase, 1730 would be a custom, non standard length which is something I will add to the code later. In industry standard lengths are common with regards to pricing & stock. In general we want to work with our desired maximum lengths (for example 1600), but if this was a hard cap, for only a small excess length of 200 we would require an extra midpost and railset. The buffer is supposed to catch that and allow for some leeway (one longer railset of 1800) in case this happens.
Upvotes: 1
Views: 63