how to relate two list?

Viewed 95

I have two lists and some parameters. With the help of these inputs, I am charging and discharging my battery. In the first list, there are 0s and 1s. 0 means discharge the battery and 1 means charge the battery. In the second list, there are values and with the help of them we can charge the battery. Battery parameters are:

Max_capacity = 600MW
initial_capacity = 0MW
Duration = 4hours
battery size = 150MW

there are actually 8760 values in both the list but I am just showing few

signal = [1,1,1,1,0,0,0,0,0,1,1,1,0,0]
excess_energy = [0,100,90,160,20,0,0,0,0,0,50,60,70,0]

So there are few conditions here:

  1. battery can charge only when the signal is 1 and excess_energy is greater than 0.
  2. if the excess_energy is greater than 150MW then the battery will charge with 150MW only or if it is less then it can charge with whatever the value in the excess energy.
  3. battery can discharge only when the signal is 0 with 150MW at a time fixed.
  4. battery cannot discharge below 0 and cannot charge with more than 600MW expected output:

    enter image description here

My approach:

battery_size = 150
duration = 4
initial_capacity = 0
max_capacity = 600
ans_charged = []
ans_discharged = []
discharge = 0
days_counter = 0
signal = [0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1]  # Reversed the polarity
excess_energy = [0, 100, 90, 160, 20, 0, 0, 0, 0, 0, 50, 60, 70, 0]
for decider, ex_energy in zip(signal, excess_energy):
    days_counter += 1  # days counter because every new day all the counters are reset
    if decider == 1 and discharge < duration and max_capacity >= initial_capacity >=0:  # at 0 we charging and at 1 we are discharging
        initial_capacity -= battery_size
        if initial_capacity >= 0:
            ans_charged.append(initial_capacity)
            ans_discharged.append(0)
            discharge += 1
        else:
            ans_charged.append(0)
            ans_discharged.append(0)
            discharge += 1
    elif decider == 1:
        ans_charged.append(0)
        ans_discharged.append(0)
    elif decider == 0 and ex_energy > 0:
        if ex_energy < 150 and max_capacity >= initial_capacity >= 0:
            initial_capacity += ex_energy
            if initial_capacity <= max_capacity:
                ans_discharged.append(initial_capacity)
                ans_charged.append(0)
            else:
                ans_discharged.append(max_capacity)
                ans_charged.append(0)
        else:
            initial_capacity += 150
        if initial_capacity <= max_capacity:
            ans_discharged.append(initial_capacity)
            ans_charged.append(0)
        else:
            ans_discharged.append(max_capacity)
            ans_charged.append(0)
    elif decider == 0:
        ans_discharged.append(0)
        ans_charged.append(0)
        discharge = 0
    if days_counter == 24:
        days_counter = 0
        charge = 0
        discharge = 0
    print(ans_discharged)
    print(ans_charged)

my output:

Charge = [0, 100, 190, 340, 0, 0, 0, 0, 0, 0, 40, 100, 0, 0]
Discharge = [0, 0, 0, 0, 190, 40, 0, 0, 0, 0, 0, 0, 0, 0]

I know its a big question but can someone please help with answer because as you can see I am getting 40 and 100 instead of 50 and 110

1 Answers

Your code is very complicated and it could be simplified:

signal = [1,1,1,1,0,0,0,0,0,1,1,1,0,0]
excess_energy = [0,100,90,160,20,0,0,0,0,0,50,60,70,0]
charge = []
discharge = []

current = 0
for i, value in enumerate(signal):
    if value == 0 : # Discharging
        current = max(current - 150, 0)
        discharge.append(current)
        charge.append(0)
    else:
        current = current + min(excess_energy[i], 150)
        charge.append(current)
        discharge.append(0)

print(charge)
print(discharge)
Related