def knapsackSimple(tree, func, bounds, n, d, limit, h): # добавление и инициализация дочерних узлов 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) print('\n----------------------------\n' 'Классический метод решения |\n' '----------------------------') methods.printAnswer(func, bounds, limit, list(range(0, d)), bestChild.elementPath, bestChild.elementValue)
import data import calc import matplotlib.pyplot as plot import sys import numpy as np x = data.dyn.radius y = data.dyn.time x_plot = np.linspace(0, 80000, 500) plot.plot(x, y, 'rx', label='Messwerte') plot.xlabel('Radius $a^2 \:/\:\mathrm{mm^2}$') plot.ylabel('Periodendauer $T^2\:/\:\mathrm{s^2}$') # plot.ylim(5e3,2e5) plot.grid() plot.legend(loc='best') plot.tight_layout(pad=0, h_pad=1.08, w_pad=1.08) plot.plot(x_plot, calc.f(x_plot, *calc.params), label='linearer Fit', linewidth=1) if len(sys.argv) > 1 and sys.argv[1] == "save": plot.savefig('plots/low_press.pdf') else: plot.show()
import data import matplotlib.pyplot as plot import numpy as np import calc x_plot = np.linspace(0, 20) plot.plot(data.temp.time, data.temp.temp1, "rx", label="T1") plot.plot(data.temp.time, data.temp.temp2, "bx", label="T2") plot.plot(x_plot, calc.f(x_plot, *calc.params1), "r-", linewidth=1) plot.plot(x_plot, calc.f(x_plot, *calc.params2), "b-", linewidth=0.8) plot.xlabel("$t$ in $\mathrm{min}$") plot.ylabel("$T$ in $\mathrm{K}$") plot.tight_layout(pad=1.08, h_pad=1.08, w_pad=1.08) plot.grid() plot.legend(loc="best") plot.show()
def knapsackDynamic(tree, func, bounds, n, d, limit, h): # добавление и инициализация дочерних узлов 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) print('\n-------------------------------------\n' 'Метод динамического програмирования |\n' '-------------------------------------') methods.printAnswer(func, bounds, limit, list(range(0, d)), bestChild.elementPath, bestChild.elementValue)