#!/usr/bin/env python3
import numpy as np
import csv
import itertools
import math

column_label=0
column_capacity=3
column_resistance=4


config_parallel=2
config_cells=12

#config_parallel*config_cells*config_packs needed
batteriesneeded=config_parallel*config_cells


batteries=[]
batterylabels=[]

with open('12S_LiIon_Akkupack_selected.csv', 'r') as csvfile:
    csvreader = csv.reader(csvfile, delimiter=';')
    firstrow=True
    for row in csvreader:
        label=row[column_label]
        capacity=row[column_capacity]
        resistance=row[column_resistance]
        if not firstrow:
            capacity=float(capacity)
            resistance=float(resistance)

            batteries.append([label,capacity,resistance])
            batterylabels.append(label)
        firstrow=False

print(str(len(batteries))+" Batteries found")
if len(batteries)==batteriesneeded:
    print("You have just enough batteries")
elif len(batteries)>batteriesneeded:
    print("You have "+str(len(batteries)-batteriesneeded)+" batteries spare")
elif len(batteries)<batteriesneeded:
    print("You need "+str(batteriesneeded-len(batteries))+" more batteries!")
    exit()

#batteries=[["a",1],["b",2],["c",3],["d",4],["e",5],["f",6]]

bestcombination=[]
bestdist=100000000

combinations=[]


count=0

for comb_parallel1 in itertools.combinations(batteries,config_parallel):
    rest1=[x for x in batteries if x not in comb_parallel1]
    count+=1
    print(count)
    for comb_parallel2 in itertools.combinations(rest1,config_parallel):
        rest2=[x for x in rest1 if x not in comb_parallel2]
        for comb_parallel3 in itertools.combinations(rest2,config_parallel):
            rest3=[x for x in rest2 if x not in comb_parallel3]
            for comb_parallel4 in itertools.combinations(rest3,config_parallel):
                rest4=[x for x in rest3 if x not in comb_parallel4]
                for comb_parallel5 in itertools.combinations(rest4,config_parallel):
                    rest5=[x for x in rest4 if x not in comb_parallel5]
                    for comb_parallel6 in itertools.combinations(rest5,config_parallel):
                        rest6=[x for x in rest5 if x not in comb_parallel6]
                        for comb_parallel7 in itertools.combinations(rest6,config_parallel):
                            rest7=[x for x in rest6 if x not in comb_parallel7]
                            for comb_parallel8 in itertools.combinations(rest7,config_parallel):
                                rest8=[x for x in rest7 if x not in comb_parallel8]
                                for comb_parallel9 in itertools.combinations(rest8,config_parallel):
                                    rest9=[x for x in rest8 if x not in comb_parallel9]
                                    for comb_parallel10 in itertools.combinations(rest9,config_parallel):
                                        rest10=[x for x in rest9 if x not in comb_parallel10]
                                        for comb_parallel11 in itertools.combinations(rest10,config_parallel):
                                            rest11=[x for x in rest10 if x not in comb_parallel11]
                                            for comb_parallel12 in itertools.combinations(rest11,config_parallel):
                                                combinations.append([comb_parallel1,comb_parallel2,comb_parallel3,comb_parallel4,comb_parallel5,comb_parallel6,comb_parallel7,comb_parallel8,comb_parallel9,comb_parallel10,comb_parallel11,comb_parallel12])



    #evaluate current combination of comb_pack1 and comb_pack2
print(str(len(combinations))+" Combinations")
for i in combinations: #every combinations

    seriescapacities=[]
    for s in i: #all series
        parallelcapacity=0
        for p in s: #all parallels
            parallelcapacity+=p[1]
        seriescapacities.append(parallelcapacity)

    meancapacity=np.mean(seriescapacities)
    dist=0
    for c in seriescapacities:
        dist+=math.pow(abs(c-meancapacity),2)

    if dist<bestdist:
        print(str(dist)+" "+str(seriescapacities))
        bestdist=dist
        bestcombination=i

print("best combination with "+str(bestdist)+" distance:")
#print(bestcombination)
for i in bestcombination:
    print(i)
