[Python] How can I divide a list of electrical loads into 3 groups with near-equal total sum...

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    I'm an electrician working toward becoming an electrical engineer. I have some basic knowledge of Python and am trying to automate a tedious process we often face in the field.

    In 3-phase electrical installations (phases R, S, and T), we need to distribute loads across the phases such that the total power in each phase is as balanced as possible. For example, given the following list of loads (in watts):

    loads = [2000, 2500, 1500, 1700, 5500, 3000, 1200, 1300, 1600, 2700]


    The total load is 23,000 W, and the ideal distribution would be roughly 7666.67 W per phase. Of course, exact balance is often impossible, but I want to get as close as possible.

    I wrote this basic Python script to assign each load to the phase with the lowest current total:

    loads = [2000, 2500, 1500, 1700, 5500, 3000, 1200, 1300, 1600, 2700]
    loads.sort(reverse=True)

    phase_R, phase_S, phase_T = [], [], []
    sum_R = sum_S = sum_T = 0

    for load in loads:
    if sum_R <= sum_S and sum_R <= sum_T:
    phase_R.append(load)
    sum_R += load
    elif sum_S <= sum_T:
    phase_S.append(load)
    sum_S += load
    else:
    phase_T.append(load)
    sum_T += load

    total = sum_R + sum_S + sum_T
    imbalance = (max(sum_R, sum_S, sum_T) - min(sum_R, sum_S, sum_T)) / max(sum_R, sum_S, sum_T) * 100

    print("Phase R:", phase_R, "->", sum_R, "W")
    print("Phase S:", phase_S, "->", sum_S, "W")
    print("Phase T:", phase_T, "->", sum_T, "W")
    print(f"Imbalance: {imbalance:.2f}%")


    This greedy approach works decently for many inputs but performs poorly in some cases, especially when the number of loads is not a multiple of 3 or when certain values dominate the list.

    My question: Is there an existing algorithm, Python package, or method that can help distribute a list of numbers into 3 groups such that the sums of the groups are as close as possible?

    Ideally, the solution should be able to handle lists of arbitrary length and not assume the number of elements is divisible by 3.

    Thanks for your time!

    Continue reading...

Compartilhe esta Página