1

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:

  • How can I improve the balancing logic to avoid generating illegal rail lengths?
  • Are there any alternative approaches I should consider for dividing the total length more effectively?

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.

8
  • How are 1600, 1600, 1600, 1800, 1800 more equal than five times 1730? Commented Oct 14, 2024 at 15:20
  • max rail length of 1600 mm and desired output 1600, 1600, 1600, 1800, 1800 seems contradictory! Commented Oct 14, 2024 at 15:25
  • @Swifty Note the "leeway (buffer)" of 600. Commented Oct 14, 2024 at 15:55
  • 1
    I understand this, but it still doesn't seem logical: if I choose a max length of 1000 and allow a 200 buffer on it, why not choose a max length of 1200? Only the OP can clarify what they mean. Commented Oct 14, 2024 at 16:04
  • 1
    Ok then; please add these clarifications to the body of your question. Commented Oct 15, 2024 at 12:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.