Исправил ошибку в коде Python
Было:
Код:
+ n4*bld30[0])
+ n4*bld30[0])
+ n4*bld30[0])
+ n4*bld30[0])
+ n4*bld30[0])
Надо:
Код:
+ n4*bld30[0])
+ n4*bld30[1])
+ n4*bld30[2])
+ n4*bld30[3])
+ n4*bld30[4])
Исправленный код
Код:
#-*-coding:cp1251-*-
from itertools import groupby
total_parts = [962, 1343, 607, 771, 595]
bld10 = [60, 90, 30, 50, 21]
bld15 = [95, 131, 20, 60, 28]
bld20 = [110, 172, 60, 45, 41]
bld25 = [134, 195, 70, 75, 58]
bld30 = [157, 212, 84, 92, 76]
div = lambda a, b: a // b
fs = lambda x: 10*x[0] + 15*x[1] + 20*x[2] + 25*x[3] + 30*x[4]
d1 = lambda n0, n1, n2, n3, n4: total_parts[0] - (n0*bld10[0] + n1*bld15[0] + n2*bld20[0] + n3*bld25[0] + n4*bld30[0])
d2 = lambda n0, n1, n2, n3, n4: total_parts[1] - (n0*bld10[1] + n1*bld15[1] + n2*bld20[1] + n3*bld25[1] + n4*bld30[1])
d3 = lambda n0, n1, n2, n3, n4: total_parts[2] - (n0*bld10[2] + n1*bld15[2] + n2*bld20[2] + n3*bld25[2] + n4*bld30[2])
d4 = lambda n0, n1, n2, n3, n4: total_parts[3] - (n0*bld10[3] + n1*bld15[3] + n2*bld20[3] + n3*bld25[3] + n4*bld30[3])
d5 = lambda n0, n1, n2, n3, n4: total_parts[4] - (n0*bld10[4] + n1*bld15[4] + n2*bld20[4] + n3*bld25[4] + n4*bld30[4])
m10 = min(map(div, total_parts, bld10))
m15 = min(map(div, total_parts, bld15))
m20 = min(map(div, total_parts, bld20))
m25 = min(map(div, total_parts, bld25))
m30 = min(map(div, total_parts, bld30))
lst = [(n10, n15, n20, n25, n30) for n10 in range(m10 + 1)
for n15 in range(m15 + 1)
for n20 in range(m20 + 1)
for n25 in range(m25 + 1)
for n30 in range(m30 + 1)
if d1(n10, n15, n20, n25, n30) >= 0 and
d2(n10, n15, n20, n25, n30) >= 0 and
d3(n10, n15, n20, n25, n30) >= 0 and
d4(n10, n15, n20, n25, n30) >= 0 and
d5(n10, n15, n20, n25, n30) >= 0]
flst = zip(map(fs, lst), lst)
fmax = max(map(fs, lst))
res = [it for it in flst if it[0] == fmax]
print(res)
if len(res) > 1:
rests = lambda x: d1(*x[1]) + d2(*x[1]) + d3(*x[1]) + d4(*x[1]) + d5(*x[1])
# оптимальное решение
key, grp = next(groupby(sorted(res, key=rests, reverse=True), key=rests))
for it in grp:
fs, rs = it
print('{}, {} material_rests {}, ({}, {}, {}, {}, {})'.format(
fs, rs, key, d1(*rs), d2(*rs), d3(*rs), d4(*rs), d5(*rs)))
# # все решения
print('-'*60)
for x in sorted(res, key=rests, reverse=True):
print('{}, {} material_rests {}, ({}, {}, {}, {}, {})'.format(
x[0], x[1], rests(x), d1(*x[1]), d2(*x[1]), d3(*x[1]), d4(*x[1]), d5(*x[1])))
Правильный ответ, учитывающий экономию по остаткам по всем вариантам:
Код:
*** Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit (Intel)] on win32. ***
>>>
[(180, (0, 0, 0, 0, 6)), (180, (1, 0, 0, 2, 4)), (180, (1, 0, 1, 0, 5))]
180, (0, 0, 0, 0, 6) material_rests 552, (20, 71, 103, 219, 139)
------------------------------------------------------------
180, (0, 0, 0, 0, 6) material_rests 552, (20, 71, 103, 219, 139)
180, (1, 0, 1, 0, 5) material_rests 494, (7, 21, 97, 216, 153)
180, (1, 0, 0, 2, 4) material_rests 479, (6, 15, 101, 203, 154)
>>>
Вложения файлов с расширением *.py, даже архивом — запрещены. Ничего не могу сделать.