def search(knapsack: Knapsack, max_evals: int) -> Tuple[float, int]: """Use the hill climber best improvement to find a solution.""" base_solution = random_solution(knapsack) base_score = knapsack.evaluate(base_solution) nb_eval = 1 while True: best_score = -math.inf index_solution = -1 for i in range(knapsack.items): base_solution[i] = not base_solution[i] score = knapsack.evaluate(base_solution) nb_eval += 1 if score > best_score: best_score = score index_solution = i base_solution[i] = not base_solution[i] if nb_eval >= max_evals: break if best_score > base_score: base_solution[index_solution] = not base_solution[index_solution] base_score = best_score else: break # Optimum local return base_score, nb_eval
def search(knapsack: Knapsack, max_nb_eval: int) -> Tuple[float, int]: base_solution = random_solution(knapsack) base_score = knapsack.evaluate(base_solution) nb_eval = 1 voisins_possibles = list(range(knapsack.items)) while True: shuffle(voisins_possibles) current_score = base_score index_voisin = -1 while (index_voisin + 1 < len(voisins_possibles) and current_score <= base_score and nb_eval <= max_nb_eval): index_voisin += 1 index = voisins_possibles[index_voisin] base_solution[index] = not base_solution[index] current_score = knapsack.evaluate(base_solution) nb_eval += 1 base_solution[index] = not base_solution[index] if current_score > base_score: base_score = current_score index = voisins_possibles[index_voisin] base_solution[index] = not base_solution[index] elif index_voisin + 1 == len(voisins_possibles): break # optimum local if nb_eval >= max_nb_eval: break return base_score, nb_eval
def test_input_raises_value_error_if_wrong_len_of_data(self): self.input_data.pop(0) with self.assertRaises(ValueError) as e: Knapsack.process_input_data(self.input_data) self.assertTrue( Knapsack._DATA_LENGTH_ERROR.format(len(self.input_data)) in str(e.exception))
def test_knapsack_empty(self): bag = () size = 50 ks = Knapsack(bag, size) result = ks.solve() self.assertEqual((), result)
def head(self, triangles): LIMIT = 180 knapsack = Knapsack(triangles, LIMIT) head = knapsack.main() for h in head: triangles.remove(h) head = self.sortHead(head) return [head, triangles]
def rest(self, triangles): remainig = [] while True: ks = Knapsack(triangles, 180) seq = ks.main() for s in seq: triangles.remove(s) remainig.append(seq) if triangles == []: break return remainig
def optimize_values(data_frame, capacity, label): data_frame = data_frame.drop_duplicates(subset='Keyword') cost = data_frame.daily_cost_average.copy(deep=True) cost[cost == 0] = minimum / 10 values = (data_frame.daily_impressions_average + data_frame.daily_clicks_average) * (1 / cost) opt = Knapsack(items_names=data_frame.Keyword.to_list(), values=values.to_list(), capacity=capacity, weights=data_frame.daily_cost_average.tolist(), solve_type=5, name='Branch_n_bound_%s' % label) opt.get_results(print_it=True) df = data_frame[data_frame.Keyword.isin(opt.packed_items)] return df
def __init__(self, mu: int, knapsack: Knapsack): self.knapsack = knapsack parents = [random_solution(knapsack) for i in range(mu)] self._population = [ Individu(parent, knapsack.evaluate(parent)) for parent in parents ] self.mu = mu
class KnapsackTest(unittest.TestCase): def setUp(self): self.weight_available = 7 weight_per_item = [2, 3, 4, 5] value_per_item = [3, 4, 5, 5] self.input_data = ['7', '2 3 4 5', '3 4 5 5'] self.obj = Knapsack(self.weight_available, weight_per_item, value_per_item) def test_rec_knap_returns_zero_if_iterator_equals_zero(self): res = self.obj.rec_knap(0, self.weight_available) self.assertEqual(res, 0) def test_rec_knap_returns_proper_value(self): res = self.obj.rec_knap( self.obj.number_of_items, self.weight_available) self.assertEqual(res, 9) def test_get_values_returns_proper_items(self): res = self.obj.get_values() self.assertEqual(len(res), 2) self.assertEqual(res[1], [(3, 4), (4, 5)]) def test_input_processes_and_returns_list_succeeds(self): res = Knapsack.process_input_data(self.input_data) self.assertEqual(res, [7, [2, 3, 4, 5], [3, 4, 5, 5]]) def test_input_raises_value_error_if_wrong_len_of_data(self): self.input_data.pop(0) with self.assertRaises(ValueError) as e: Knapsack.process_input_data(self.input_data) self.assertTrue( Knapsack._DATA_LENGTH_ERROR.format(len(self.input_data)) in str(e.exception)) def test_input_raises_value_error_if_wrong_input(self): self.input_data[2] = '3 4 5' with self.assertRaises(ValueError) as e: Knapsack.process_input_data(self.input_data) self.assertTrue(Knapsack._DATA_INPUT_ERROR in str(e.exception)) def test_input_raises_value_error_if_wrong_input_type(self): self.input_data[1] = '3 4 5 A' with self.assertRaises(ValueError) as e: Knapsack.process_input_data(self.input_data) self.assertTrue('invalid literal' in str(e.exception))
def test_knapsack_values(self): bag = ( (10, 60), (20, 100), (30, 195), (40, 120), (50, 120), (5, 120), (60, 120), (10, 50), (10, 200), (20, 120), ) size = 50 ks = Knapsack(bag, size) result = ks.solve() self.assertEqual(((30, 195), (5, 120), (10, 200)), result)
def read_input(): print("Enter items knapsack volume and items number") volume, items_number = map(int, input().split()) costs = [] weights = [] print("Enter item weight and item cost") for i in range(0, items_number): item_weight, item_cost = map(int, input().split()) costs.append(item_cost) weights.append(item_weight) return Knapsack(volume, costs, weights), items_number
def loadInstances(files): ''' Load instances from files. ''' # if there are more than 1 available files if len(files) > 1: instances = validateChoices('file', files) # if there is only 1 available file else: instances = files # queue for threads thread_queue = Queue(len(instances)) # list of threads for reading files reading = list() for index, file_name in enumerate(instances): name = 'r%d' % index # new thread to read a file read = Thread(target=lambda q, arg: q.put(Knapsack.fromFile(arg)), args=(thread_queue, file_name), name=name, daemon=True) # start new thread and add it to list read.start() reading.append(read) heuristics = validateChoices('heuristic') # number of instances to solve size = len(instances) index = 1 load_str = ' Loading instance...' # while there exist unsolved instances while size: # loop over list of threads for read in reading: # if thread is still running if read.is_alive(): print('%s\r' % load_str, end='') # check next thread continue read.join() # get Knapsack object knapsack = thread_queue.get() if knapsack is not None: print('%s\r' % (' ' * len(load_str)), end='') solveInstance(knapsack, index, heuristics) size -= 1 index += 1 # if all instances have been solved if size <= 0: # break for break
def read(self): C = self.file.readline() print("Number of test cases is C:", C) lines = self.file.readlines() items_num_flag = True knapsack = Knapsack(0, 0) template_counter = 0 for line in lines: if line[0] == '\n': continue if len((line.rstrip()).split()) == 1 and items_num_flag: items_num_flag = False if knapsack.S != 0: self.knapsacks.append(knapsack) del knapsack knapsack = Knapsack(0, 0) knapsack.N = int(line.rstrip()) continue if len((line.rstrip()).split()) == 1 and not items_num_flag: items_num_flag = True knapsack.S = int(line.rstrip()) continue # Initialize knapsack items line.rstrip() weight, value = line.split(' ') item = Item(int(weight), int(value)) knapsack.items.append(item) self.knapsacks.append(knapsack)
def read(self): C = int(input("Enter the number of test case: ")) # Loop through the test cases for i in range(C): print("Test case: " + str(i + 1)) N = int(input("Enter the number of items: ")) S = int(input("Enter the size of the knapsack: ")) print("Enter the item and its value in pairs: ") knapsack = Knapsack(N, S) item = Item(0, 0) for n in range(N): weight, value = input("Pair" + str(n + 1) + ":").split() item = Item(weight, value) knapsack.items.append(item) self.knapsacks.append(knapsack)
def generateInstances(): ''' Generate instances from prompt. ''' knapsacks = list() # list of threads for writing to files writing = list() index = 1 another = True while another: print('\n === {}° instance ==='.format(index)) answers = prompt(createInstanceQuestions()) k = Knapsack.random(answers['n'], answers['min w'], answers['max w'], answers['min v'], answers['max v'], answers['p']) knapsacks.append(k) # new thread's name name = 'w%d' % index # new thread to write last instance and start it write = Thread(target=knapsacks[-1].toFile, name=name) write.start() writing.append(write) another = confirm('Do you want to add another instance?').ask() index += 1 heuristics = validateChoices('heuristic') save_str = ' Saving instances to files...' size = len(writing) while size: # loop over threads list for write, k in zip(writing, knapsacks): index = int(write.name[-1]) # if a thread is still running if write.is_alive(): print('%s\r' % save_str, end='') continue else: # wait until the thread finishes write.join() print('%s\r' % (' ' * len(save_str)), end='') solveInstance(k, index, heuristics) size -= 1 print('\n All instances have been saved to files.')
def buildKnapsacks(commandSets): """ The buildKnapsacks function takes a 2d array of sets of commands to execute and runs through the array interpreting the commands. Args: commandSets A list of the sets of commands to execute for the problem. The builder first goes through the commands and splits them into spice and knapsack commands, it then makes them more easily interpreted, and finally it iterates through each set of commands and carries out their instructions. """ spiceCommands = [] knapsackCommands = [] commentCount = 0 spiceCount = 0 knapsackCount = 0 commandSet = [] spices = [] knapsacks = [] for commandSet in commandSets: for command in commandSet: if command[0] == '--': continue elif command[0] == 'spice': spiceCommands.append( ['spice', command[3][:-1], float(command[6][:-1]), float(command[9][:-1])]) elif command[0] == 'knapsack': knapsackCommands.append(['knapsack', float(command[3][:-1])]) print("Spice commands:", len(spiceCommands)) print("Knapsack commands:", len(knapsackCommands), '\n') for command in spiceCommands: if command[0] == 'spice': spices.append(Spice(command[1], command[2], command[3])) for command in knapsackCommands: if command[0] == 'knapsack': knapsacks.append(Knapsack(command[1])) spices.sort(key=lambda spice: spice.price, reverse=True) return spices, knapsacks
def pickItems(knapsack: Knapsack, heuristic): ''' Use the heuristic specified in the argument to get a solution for the knapsack problem. Returns a generator containing the items that were picked. ''' items_sorted = knapsack.sortItems(heuristic) W = knapsack.capacity for item in items_sorted: weight = item.weight if weight <= W: # add item to generator yield item W -= weight # if no more items fit in the knapsack if W == 0: break # if heuristic by weight is used, the rest of the items won't fit elif heuristic == 2: break
def test_knapsack(self): # Problems to test problems = [ 'dynamic', 'bipercube', 'hypercube2', 'hypercube4', 'hypercube6', 'hypercube8' ] naive = 0 dynamic = 0 parallel = 0 bipercube = 0 hypercube2 = 0 hypercube4 = 0 hypercube6 = 0 hypercube8 = 0 # Variables capacity = 100 elementCount = 100 profitsRange = 100 weigthsRange = 100 values = [] weigths = [] # Construct the Knapsack for i in range(elementCount): values.append(random.randint(1, profitsRange)) weigths.append(random.randint(1, weigthsRange)) knap = Knapsack(capacity, weigths, values) # Resolve # Dynamic start = time.time() dynamic = knap.dynamic() end = time.time() print "Dynamic time: " + str(end - start) # Naive if 'naive' in problems: start = time.time() naive = knap.naive() end = time.time() print "Naive time: " + str(end - start) # Parallel if 'parallel' in problems: start = time.time() parallel = knap.parallel() end = time.time() print "Parallel time: " + str(end - start) # Bipercube if 'bipercube' in problems: start = time.time() bipercube = knap.bipercube() end = time.time() print "Bipercube time: " + str(end - start) # Hypercube # 2 Processors if 'hypercube2' in problems: start = time.time() hypercube2 = knap.hypercube(2) end = time.time() print "Hypercube 2 time: " + str(end - start) # 4 Processors if 'hypercube4' in problems: start = time.time() hypercube4 = knap.hypercube(4) end = time.time() print "Hypercube 4 time: " + str(end - start) # 6 Processors if 'hypercube6' in problems: start = time.time() hypercube6 = knap.hypercube(6) end = time.time() print "Hypercube 6 time: " + str(end - start) # 8 Processors if 'hypercube8' in problems: start = time.time() hypercube8 = knap.hypercube(8) end = time.time() print "Hypercube 8 time: " + str(end - start) # Results # Naive if 'naive' in problems: self.assertEqual(naive, dynamic) # Parallel if 'parallel' in problems: self.assertEqual(parallel, dynamic) # Bipercube if 'bipercube' in problems: self.assertEqual(bipercube, dynamic) # 2 Processors if 'hypercube2' in problems: self.assertEqual(hypercube2, dynamic) # 4 Processors if 'hypercube4' in problems: self.assertEqual(hypercube4, dynamic) # 6 Processors if 'hypercube6' in problems: self.assertEqual(hypercube6, dynamic) # 8 Processors if 'hypercube8' in problems: self.assertEqual(hypercube8, dynamic)
from knapsack import Curve, Knapsack if __name__ == '__main__': # initializing solver (Knapsack class) instance with maximum money for all media budget = Knapsack(70874156) # maximal budget for every media maxb = 70 * 10**6 # initializing media as Curve instances # every Curve is some media like TV, OOH, etc. media1 = Curve(397650, 0.5085217, 0.92, 9.168728e-06 / 87.98509) media2 = Curve(1336580, 0.941772, 0.9964167, 7.202334e-07) media3 = Curve(5.509022, 5000, 0.99, 0.0002982394 / 219) media4 = Curve(3191.663, 10000, 0.75, 0.001353697 / 65) media5 = Curve(237349.3, 0.9954354, 0.7, 9.501362e-06 / 12.47645) media6 = Curve(2961.2209, 100, 0.5, 0.003669801 / 87.98509) media7 = Curve(664196.7, 0.662257, 0.92, 1.275716e-06 / 87.98509) # adding media to budget # in second argument we pass minimum and maximum allowed for media budget.add_curve(media1, 10600000, maxb) budget.add_curve(media2, 5700000, maxb) budget.add_curve(media3, 1923077, maxb) budget.add_curve(media4, 8307692, maxb) budget.add_curve(media5, 3246154, maxb) budget.add_curve(media6, 9791667, maxb) budget.add_curve(media7, 15786389, maxb) # call to optimize spends between media # last line represents optimal budget for every media, e.g. first number
from knapsack import Knapsack from extract_objects import Objects if (__name__ == '__main__'): total_individual = 400 total_chromossomes = 20 total_generation = 800 mutation_rate = 0.1 crossover_rate = 0.9 elite = 20 pack_weight_limit = 400 objects = Objects() knapsack = Knapsack(total_individual, total_chromossomes, total_generation, mutation_rate, crossover_rate, elite, pack_weight_limit, objects) knapsack.start()
def test_input_processes_and_returns_list_succeeds(self): res = Knapsack.process_input_data(self.input_data) self.assertEqual(res, [7, [2, 3, 4, 5], [3, 4, 5, 5]])
def test_input_raises_value_error_if_wrong_input(self): self.input_data[2] = '3 4 5' with self.assertRaises(ValueError) as e: Knapsack.process_input_data(self.input_data) self.assertTrue(Knapsack._DATA_INPUT_ERROR in str(e.exception))
default='result.json', type=str, help='Set the output filename(json).') args = parser.parse_args() # Check argument by standard output print(args) # Set arguments to variables filename = args.filename crossover_prob = args.crossover_prob mutation_prob = args.mutation_prob popSize = args.population_size generation = args.generation # Dataset read and parsing knapsack = Knapsack(filename) knapsack.reader() gene_size = len(knapsack.dataset) # Generate each GA instance roulette = GeneticAlgorithm(gene_size, popSize, crossover_prob, mutation_prob) tournament = GeneticAlgorithm(gene_size, popSize, crossover_prob, mutation_prob) # Roulette Wheel selection GA Initialization print('Roulette', end=' ') roulette.initialization() roulette.calculateFitness(knapsack) # Tournament selection GA Initialization print('Tournament', end=' ')
def test_input_raises_value_error_if_wrong_input_type(self): self.input_data[1] = '3 4 5 A' with self.assertRaises(ValueError) as e: Knapsack.process_input_data(self.input_data) self.assertTrue('invalid literal' in str(e.exception))
def setUp(self): self.weight_available = 7 weight_per_item = [2, 3, 4, 5] value_per_item = [3, 4, 5, 5] self.input_data = ['7', '2 3 4 5', '3 4 5 5'] self.obj = Knapsack(self.weight_available, weight_per_item, value_per_item)
def main(): knapsack = Knapsack("./ks_1000.dat") # Hill-climbing best improvement iterations = [ (100, 64), (500, 32), (1_000, 16), (5_000, 8), (10_000, 4), (50_000, 2), (100_000, 1), ] average_score = [0] average_evaluations = [0] for iters, repeats in iterations: print( f"Testing hill-climbing best improvement {repeats} time(s) with {iters} iterations." ) total_score = 0 total_eval = 0 for _ in range(repeats): score, evaluations = hc_best(knapsack, iters) total_score += score total_eval += evaluations average_score.append(total_score / repeats) average_evaluations.append(total_eval / repeats) plt.plot(average_evaluations, average_score, label="Hill-climbing best improvement") # Hill-climbing first improvement iterations = [ (100, 64), (500, 32), (1_000, 16), (5_000, 8), (10_000, 4), (50_000, 2), (100_000, 1), ] average_score = [0] average_evaluations = [0] for iters, repeats in iterations: print( f"Testing hill-climbing first improvement {repeats} time(s) with {iters} iterations." ) total_score = 0 total_eval = 0 for _ in range(repeats): score, evaluations = hc_first(knapsack, iters) total_score += score total_eval += evaluations average_score.append(total_score / repeats) average_evaluations.append(total_eval / repeats) plt.plot(average_evaluations, average_score, label="Hill-climbing first improvement") # print(score, evals) # score, evals = recuit_simule(knapsack) # Evolution iterations = [(100, 50), (500, 10), (1_000, 5), (5_000, 1), (10_000, 1)] average_score = [0] average_evaluations = [0] for iters, repeats in iterations: print(f"Testing evolution {repeats} time(s) with {iters} iterations.") total_score = 0 total_eval = 0 for _ in range(repeats): pop = Population(10, knapsack) evaluations, score = pop.run(iters) total_score += score total_eval += evaluations average_score.append(total_score / repeats) average_evaluations.append(total_eval / repeats) plt.plot(average_evaluations, average_score, label="Evolution mu=10") plt.grid() plt.xlabel("Evaluations") plt.ylabel("Average performance") plt.figlegend() plt.show()
from knapsack import Knapsack import time import sys sys.setrecursionlimit(10000) knapsack = Knapsack() startTime = time.time() result = knapsack.findOptimum(knapsack.itemCount, knapsack.knapCapacity) print(time.strftime("%H:%M:%S", time.gmtime(time.time() - startTime))) print(result)
@author: josep """ from Pair import Pair from knapsack import Knapsack as Knapsack pair1=Pair(4,1) pair2=Pair(7,7) pair3=Pair(1,22) pair4=Pair(2,23) pair5=Pair(3,6) items=[] items.extend([pair1,pair2,pair3,pair4,pair5]) knapsackObject=Knapsack(14,5,items) #print(knapsackObject.algorithm().fitness) filePath = "input_example.txt" f = open("result.txt", "a") with open(filePath, "r") as file: text = file.read() testCases = text.split("\n\n") start=1 while start <= int(testCases[0]): case = testCases[start].strip().split("\n")
def __init__(self, knapsacks): self.knapsacks = [Knapsack(knapsack) for knapsack in knapsacks]
def knapsackPage(): x = Knapsack(6, 1000) return render_template('knapsack.html', maxRevenue=x.getMaxRevenue(), totalBudget=x.getBudget(), observations=x.getObs(), Title="Knapsack Problem", GitHubLink="https://github.com/deacons2016/SimulationModels/blob/master/Exam2/Problem4/index.py")
def test_knapsack(self): # Initial variables naive = 0 dynamic = 0 parallel = 0 bipercube = 0 hypercube2 = 0 hypercube4 = 0 hypercube6 = 0 hypercube8 = 0 # Problems to test problems = [ 'dynamic', 'hypercube2', 'hypercube4', 'hypercube6', 'hypercube8' ] # File out = open("out.csv", 'w') # csv output out.write("Capacidad" + "\t" + "Elementos" + "\t" + "Naive" + "\t" + "Programacion dinamica" + "\t" + "2 procesadores" + "\t" + "4 procesadores" + "\t" + "6 procesadores" + "\t" + "8 procesadores" + "\t" + "Peters and Rudolph" + "\t" + "\n") capacities = [100, 1000] elements = [100, 1000] for capacity in capacities: for element in elements: print print 'Capacity', capacity print 'Elements', element # Times naiveTime = '-' dynamicTime = '-' hypercube2Time = '-' hypercube4Time = '-' hypercube6Time = '-' hypercube8Time = '-' parallelTime = '-' # Variables values = [] weigths = [] # Construct the Knapsack for i in range(element): values.append(random.randint(1, int(element / 2))) weigths.append(random.randint(1, int(element / 2))) knap = Knapsack(capacity, weigths, values) # Resolve # Dynamic if 'dynamic' in problems: start = time.time() dynamic = knap.dynamic() end = time.time() dynamicTime = str(end - start) print "Dynamic time: " + str(end - start) # Naive if 'naive' in problems: start = time.time() naive = knap.naive() end = time.time() naiveTime = str(end - start) print "Naive time: " + str(end - start) # Parallel if 'parallel' in problems: start = time.time() parallel = knap.parallel() end = time.time() parallel = str(end - start) print "Parallel time: " + str(end - start) # Bipercube if 'bipercube' in problems: start = time.time() bipercube = knap.bipercube() end = time.time() print "Bipercube time: " + str(end - start) # Hypercube # 2 Processors if 'hypercube2' in problems: start = time.time() hypercube2 = knap.hypercube(2) end = time.time() hypercube2Time = str(end - start) print "Hypercube 2 time: " + str(end - start) # 4 Processors if 'hypercube4' in problems: start = time.time() hypercube4 = knap.hypercube(4) end = time.time() hypercube4Time = str(end - start) print "Hypercube 4 time: " + str(end - start) # 6 Processors if 'hypercube6' in problems: start = time.time() hypercube6 = knap.hypercube(6) end = time.time() hypercube6Time = str(end - start) print "Hypercube 6 time: " + str(end - start) # 8 Processors if 'hypercube8' in problems: start = time.time() hypercube8 = knap.hypercube(8) end = time.time() hypercube8Time = str(end - start) print "Hypercube 8 time: " + str(end - start) # File out.write( str(capacity) + "\t" + str(element) + "\t" + str(naiveTime) + "\t" + str(dynamicTime) + "\t" + str(hypercube2Time) + "\t" + str(hypercube4Time) + "\t" + str(hypercube6Time) + "\t" + str(hypercube8Time) + "\t" + str(parallelTime) + "\t") out.write("\n")