forked from NikasAl/BattaryTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
battary_combinator.py
102 lines (88 loc) · 2.29 KB
/
battary_combinator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from ast import literal_eval
from operator import itemgetter
import random
parallels_num = 7
series_num = 13
def load_caps():
data = {}
cap = []; r = []; num = []
with open('caps91.txt') as f:
for line in f:
cap.append(int(line.split(",")[1]))
num.append(int(line.split(",")[0]))
r.append(int(line.split(",")[2]))
return { "num":num,"cap":cap,"r":r }
def gen_caps_file():
stuff = [ random.randrange(2450,2790) for x in range(91)]
with open('caps.txt', "w") as fhandle:
for s in stuff:
fhandle.write(f'{s}\n')
def compose(stuff):
global parallels_num, series_num
mystuff = stuff.copy()
series = []
for s_num in range(series_num):
parallels = []
for p_num in range(parallels_num):
parallels.append(mystuff.pop(random.randint(0, len(mystuff)-1)))
series.append(parallels)
return series
def fit(series):
sums = []
for p in series:
sums.append(sum(p))
sums.sort()
d = sums[-1]-sums[0] #максимальная разность емкостей параллелей
return d
def minmax(series):
sums = []
for p in series:
sums.append(sum(p))
mini = sums.index(min(sums))
maxi = sums.index(max(sums))
return mini, maxi, sums[mini], sums[maxi]
def find_optimum(stuff):
m = compose(stuff)
i = 0
while fit(m) > 1:
i += 1;
if i > 5000:
return find_optimum(stuff)
mi, mx, misum, mxsum = minmax(m)
while True:
r7_1 = random.randrange(0,6);
r7_2 = random.randrange(0,6);
x = abs(mxsum - m[mx][r7_1] + m[mi][r7_2])
if x < mxsum:
break
m[mi][r7_2], m[mx][r7_1] = m[mx][r7_1], m[mi][r7_2]
return m
def printer(m, data):
for p in m:
print(p, sum(p))
print("Total delta:",fit(m),"\n")
cap = data.get("cap"); r = data.get("r"); num = data.get("num")
for p in m:
rsum = 0; capsum = 0
for el_of_p in p:
i = 0;
if el_of_p in cap:
i = cap.index(el_of_p)
capsum += cap[i];
if rsum == 0:
rsum = r[i]
else:
rsum = rsum*r[i]/(rsum+r[i])
print(f"{num[i]:02d}",":",el_of_p,':',r[i]," ", end='', sep='')
cap.remove(cap[i]); r.remove(r[i]); num.remove(num[i])
print(" | CSum:",capsum," RTot:",f"{rsum:.2f}",sep='')
print("NumberOfCell:Cap:R")
data = load_caps()
max_cap = 0
for i in range(1000):
m = find_optimum(data.get("cap"))
cur_cap = sum(m[0])
if cur_cap > max_cap:
max_cap = cur_cap
mem = m
printer(mem, data)