def knapsackSimple(window, tree, func, bounds, limit, n, d, h): startTime = timeit.default_timer() # добавление и инициализация дочерних узлов hangTops = [tree] # фронт висячих вершин for i in range(0, n, h): if h > n - i: h = n - i for hh in range(0, h): newCh = hangTops.copy() topsCnt = len(hangTops) hangTops.clear() for cnt in range(0, topsCnt): for ch in range(0, d): if newCh[cnt] == None: continue newCh[cnt].addChild(ch) fnc = calc.f(func, newCh[cnt].elementChilds[ch].elementPath, d) bnd = calc.bound(bounds, newCh[cnt].elementChilds[ch].elementPath, limit) if bnd < 0: newCh[cnt].elementChilds[ch] = None hangTops.append(None) else: newCh[cnt].elementChilds[ch].elementValue = fnc newCh[cnt].elementChilds[ch].elementResurseValue = bnd hangTops.append(newCh[cnt].elementChilds[ch]) bestChildVal = 0 for element in hangTops: if element == None: continue maxVal = element.elementValue if maxVal > bestChildVal: bestChildVal = maxVal bestChild = element hangTops.clear() hangTops.append(bestChild) endTime = timeit.default_timer() - startTime window.children.get('!text').insert(1.0, 'Классический метод\n') window.children.get('!text').insert( 2.0, 'Zопт-' + str(bestChild.elementPath) + '\n') window.children.get('!text').insert( 3.0, 'Fопт - ' + str(bestChild.elementValue) + '\n') window.children.get('!text').insert( 4.0, 'Время - ' + str(round(endTime, 6)) + '\n')
def knapsackSimple(tree, func, bounds, limit, n, d, h): startTime = timeit.default_timer() # добавление и инициализация дочерних узлов hangTops = [tree] # фронт висячих вершин for i in range(0, n, h): fh = h if h > n - i: h = n - i for hh in range(0, h): newCh = hangTops.copy() topsCnt = len(hangTops) hangTops.clear() for cnt in range(0, topsCnt): for ch in range(0, d): if newCh[cnt] == None: continue newCh[cnt].addChild(ch) fnc = calc.f(func, newCh[cnt].elementChilds[ch].elementPath, d) bnd = calc.bound(bounds, newCh[cnt].elementChilds[ch].elementPath, limit) if bnd < 0: newCh[cnt].elementChilds[ch] = None hangTops.append(None) else: newCh[cnt].elementChilds[ch].elementValue = fnc newCh[cnt].elementChilds[ch].elementResurseValue = bnd hangTops.append(newCh[cnt].elementChilds[ch]) bestChildVal = 0 for element in hangTops: if element == None: continue maxVal = element.elementValue if maxVal > bestChildVal: bestChildVal = maxVal bestChild = element hangTops.clear() hangTops.append(bestChild) endTime = timeit.default_timer() - startTime print('\n----------------------------\n' 'Классический метод решения |\n' '----------------------------') printAnswer(func, bounds, limit, bestChild.elementPath, bestChild.elementValue, endTime, fh)
def knapsackDynamic(window, tree, func, bounds, limit, n, d, h): startTime = timeit.default_timer() # добавление и инициализация дочерних узлов hangTops = [tree] # фронт висячих вершин for st in range(0, n, h): if h > n - st: h = n - st for hh in range(0, h): newCh = hangTops.copy() topsCnt = len(hangTops) hangTops.clear() for cnt in range(0, topsCnt): for ch in range(0, d): if newCh[cnt] == None: continue newCh[cnt].addChild(ch) fnc = calc.f(func, newCh[cnt].elementChilds[ch].elementPath, d) bnd = calc.bound(bounds, newCh[cnt].elementChilds[ch].elementPath, limit) if bnd < 0: newCh[cnt].elementChilds[ch] = None # hangTops.append(None) else: newCh[cnt].elementChilds[ch].elementValue = fnc newCh[cnt].elementChilds[ch].elementResurseValue = bnd hangTops.append(newCh[cnt].elementChilds[ch]) i = 0 while i < len(hangTops): flag = True currentValue = hangTops[i].elementValue curentResurse = hangTops[i].elementResurseValue for j in range(0, len(hangTops)): nextValue = hangTops[j].elementValue nextResurse = hangTops[j].elementResurseValue if currentValue <= nextValue and curentResurse < nextResurse: hangTops.pop(i) flag = False break if flag: i += 1 bestChildVal = 0 for element in hangTops: if element == None: continue maxVal = element.elementValue if maxVal > bestChildVal: bestChildVal = maxVal bestChild = element hangTops.clear() hangTops.append(bestChild) endTime = timeit.default_timer() - startTime window.children.get('!text').insert(7.0, '\nКомпозитный метод\n') window.children.get('!text').insert( 8.0, 'Zопт-' + str(bestChild.elementPath) + '\n') window.children.get('!text').insert( 9.0, 'Fопт - ' + str(bestChild.elementValue) + '\n') window.children.get('!text').insert( 10.0, 'Время - ' + str(round(endTime, 6)) + '\n')
def knapsackDynamic(tree, func, bounds, limit, n, d, h): startTime = timeit.default_timer() # добавление и инициализация дочерних узлов hangTops = [tree] # фронт висячих вершин for st in range(0, n, h): if h > n - st: h = n - st for hh in range(0, h): newCh = hangTops.copy() topsCnt = len(hangTops) hangTops.clear() for cnt in range(0, topsCnt): for ch in range(0, d): if newCh[cnt] == None: continue newCh[cnt].addChild(ch) fnc = calc.f(func, newCh[cnt].elementChilds[ch].elementPath, d) bnd = calc.bound(bounds, newCh[cnt].elementChilds[ch].elementPath, limit) if bnd < 0: newCh[cnt].elementChilds[ch] = None # hangTops.append(None) else: newCh[cnt].elementChilds[ch].elementValue = fnc newCh[cnt].elementChilds[ch].elementResurseValue = bnd hangTops.append(newCh[cnt].elementChilds[ch]) i = 0 while i < len(hangTops): flag = True currentValue = hangTops[i].elementValue curentResurse = hangTops[i].elementResurseValue for j in range(0, len(hangTops)): nextValue = hangTops[j].elementValue nextResurse = hangTops[j].elementResurseValue if currentValue <= nextValue and curentResurse < nextResurse: hangTops.pop(i) flag = False break if flag: i += 1 bestChildVal = 0 for element in hangTops: if element == None: continue maxVal = element.elementValue if maxVal > bestChildVal: bestChildVal = maxVal bestChild = element hangTops.clear() hangTops.append(bestChild) endTime = timeit.default_timer() - startTime print('\n---------------------------\n' 'Композитный метод решения |\n' '---------------------------') printAnswer(func, bounds, limit, list(range(0, d)), bestChild.elementPath, bestChild.elementValue, endTime)