0

I'm doing some optimization problem and defining many constrains which are very close in names and contents. The code follows:

Battery_Capacity = 2000
SOC_INIT = 0.0
soc = []    
for t in range(48):
    soc.append(random.random())

# defining 48 constraints
def constraint0(soc):
    return (soc[0] - SOC_INIT) * Battery_Capacity 

def constraint1(soc):
    return (soc[1] - soc[0]) * Battery_Capacity 

def constraint2(soc):
    return (soc[2] - soc[1]) * Battery_Capacity 

def constraint3(soc):
    return (soc[3] - soc[2]) * Battery_Capacity 

def constraint4(soc):
    return (soc[4] - soc[3]) * Battery_Capacity 

def constraint5(soc):
    return (soc[5] - soc[4]) * Battery_Capacity 

def constraint6(soc):
    return (soc[6] - soc[5]) * Battery_Capacity 

def constraint7(soc):
    return (soc[7] - soc[6]) * Battery_Capacity 

def constraint8(soc):
    return (soc[8] - soc[7]) * Battery_Capacity 

def constraint9(soc):
    return (soc[9] - soc[8]) * Battery_Capacity 

def constraint10(soc):
    return (soc[10] - soc[9]) * Battery_Capacity 

def constraint11(soc):
    return (soc[11] - soc[10]) * Battery_Capacity 

def constraint12(soc):
    return (soc[12] - soc[11]) * Battery_Capacity 

def constraint13(soc):
    return (soc[13] - soc[12]) * Battery_Capacity 

def constraint14(soc):
    return (soc[14] - soc[13]) * Battery_Capacity 

def constraint15(soc):
    return (soc[15] - soc[14]) * Battery_Capacity 

def constraint16(soc):
    return (soc[16] - soc[15]) * Battery_Capacity 

def constraint17(soc):
    return (soc[17] - soc[16]) * Battery_Capacity 

def constraint18(soc):
    return (soc[18] - soc[17]) * Battery_Capacity 

def constraint19(soc):
    return (soc[19] - soc[18]) * Battery_Capacity 

def constraint20(soc):
    return (soc[20] - soc[19]) * Battery_Capacity 

def constraint21(soc):
    return (soc[21] - soc[20]) * Battery_Capacity 

def constraint22(soc):
    return (soc[22] - soc[21]) * Battery_Capacity 

def constraint23(soc):
    return (soc[23] - soc[22]) * Battery_Capacity 

def constraint24(soc):
    return (soc[24] - soc[23]) * Battery_Capacity 

def constraint25(soc):
    return (soc[25] - soc[24]) * Battery_Capacity 

def constraint26(soc):
    return (soc[26] - soc[25]) * Battery_Capacity 

def constraint27(soc):
    return (soc[27] - soc[26]) * Battery_Capacity 

def constraint28(soc):
    return (soc[28] - soc[27]) * Battery_Capacity 

def constraint29(soc):
    return (soc[29] - soc[28]) * Battery_Capacity 

def constraint30(soc):
    return (soc[30] - soc[29]) * Battery_Capacity 

def constraint31(soc):
    return (soc[31] - soc[30]) * Battery_Capacity 

def constraint32(soc):
    return (soc[32] - soc[31]) * Battery_Capacity 

def constraint33(soc):
    return (soc[33] - soc[32]) * Battery_Capacity 

def constraint34(soc):
    return (soc[34] - soc[33]) * Battery_Capacity 

def constraint35(soc):
    return (soc[35] - soc[34]) * Battery_Capacity 

def constraint36(soc):
    return (soc[36] - soc[35]) * Battery_Capacity 

def constraint37(soc):
    return (soc[37] - soc[36]) * Battery_Capacity 

def constraint38(soc):
    return (soc[38] - soc[37]) * Battery_Capacity 

def constraint39(soc):
    return (soc[39] - soc[38]) * Battery_Capacity 

def constraint40(soc):
    return (soc[40] - soc[39]) * Battery_Capacity 

def constraint41(soc):
    return (soc[41] - soc[40]) * Battery_Capacity 

def constraint42(soc):
    return (soc[42] - soc[41]) * Battery_Capacity 

def constraint43(soc):
    return (soc[43] - soc[42]) * Battery_Capacity 

def constraint44(soc):
    return (soc[44] - soc[43]) * Battery_Capacity 

def constraint45(soc):
    return (soc[45] - soc[44]) * Battery_Capacity 

def constraint46(soc):
    return (soc[46] - soc[45]) * Battery_Capacity 

def constraint47(soc):
    return (soc[47] - soc[46]) * Battery_Capacity 

Is there a short and efficient way for defining those functions, especially if I wanna define thousands of constraints? Can I use for loop for automatically naming the 'number part' after 'constraint'?

Ganaini
  • 1
  • 1
  • Whatever solver you're using probably has a better way to specify constraints than using thousands of constraint functions. – user2357112 Dec 12 '20 at 12:22
  • If you actually need those functions to have the same name, consider defining a custom class and then calling `setattr(custom_class_instance, "constraint" + i, lambda soc: return (soc[i] - soc[i-1]) * Battery_Capacity` forall i in your target range. – Captain Trojan Dec 12 '20 at 12:25

0 Answers0