def main(): pesos = [22, 14, 16, 23, 12, 15, 22, 6, 19, 20, 40, 8, 16, 6, 15, 21, 16] valores = [ 55, 34, 28, 30, 80, 3, 28, 24, 21, 43, 54, 12, 21, 11, 6, 21, 28 ] mochila = knapsack.Knapsack(pesos, valores, 100) #q = queens.Queens(4, 12) ag = AG.AG(18, 17, 1, 400, 0.1, mochila) #ag = AG.AG(32, 48, 4, 10000, 0.01, q) ag.run()
def main(): valores = [ 1, 20, 5, 1, 2, 5, 5, 1, 5, 2, 2, 1, 10, 5, 10, 5, 20, 20, 20, 5, 1, 1, 20, 20, 1, 10, 2, 10, 5, 2, 10, 1, 20, 1, 20, 10, 5, 5, 20, 2, 10, 1, 2, 5, 10, 20, 10, 2, 5, 5, 20, 1, 1, 5, 10, 10, 10, 1, 5, 2, 1, 2, 10, 20, 2, 10, 10, 20, 5, 10, 1, 2, 1, 5, 20, 2, 5, 1, 5, 10, 2, 5, 10, 2, 1, 1, 1, 10, 20, 10, 20, 2, 2, 10, 20, 10, 1, 1, 5, 2 ] carry = knapsack.Knapsack(valores) ag = AG.AG(110, 100, 1, 1200, 0.01, carry) ag.run()
def result(): data = request.get_json() totalWeight = data["totalWeight"] weights = data["weights"] values = data["values"] itemIndexes = data["indexes"] time1 = time.time() sol = knapsack.Knapsack(totalWeight, weights, values, itemIndexes) res = sol.run() spenTime = time.time() - time1 items = sol.showItems() items = " ".join("#"+str(i)+"," for i in items) items = items[:-1] res = {"res":int(res), "items": items, "time":spenTime} print(sol.matrix) return jsonify(res)
def post(self): #This will be the calculate method #Everything must be done through the user id user = users.get_current_user() if user: #Get the total income and calculate the expenses cb = checkbook.CheckBook() cbInfo = cb.getStats() #Get the bills bt = billTracker.BillTracker() btInfo = bt.getStats() #Get wishlist wl = wishList.WishList() wlInfo = wl.getStats() #First knap sack limit = cbInfo['totalIncome'] - abs(btInfo['totalBills']) #If we can't even afford our fixed bills, we're having a bad time. if limit <= 0: passback_values = { 'leftOver': round(limit, 2), 'expensesCovered': [], 'expensesNotCovered': [], 'spentOnExpenses': 0, 'wishListCovered': [], 'wishListNotCovered': [], 'spentOnwishList': 0, 'totalBills': abs(btInfo['totalBills']), } self.response.set_status(200, 'Computation Complete') self.response.write(json.dumps(passback_values)) else: #Solve the knapsack problem using the limit and the expenses k = knapsack.Knapsack(round(abs(limit) + .5), cbInfo['expenses']) results = k.solve() logging.info(results) limit = limit - results['used'] #Now that we have that, we have a new limit how much money is leftover for wishlist items? #We should really only advise people to purchase 'want' items from their wishlist when they've #managed to buy all their expenses, so if not results['unusedItems']: #unused items is empty k2 = knapsack.Knapsack(round(abs(limit) + .5), wlInfo['wishes']) wishResults = k2.solve() logging.info(wishResults) #Send back the results including the wishList passback_values = { 'leftOver': round(limit - wishResults['used'], 2), 'expensesCovered': results['usedItems'], 'expensesNotCovered': results['unusedItems'], 'spentOnExpenses': results['used'], 'wishListCovered': wishResults['usedItems'], 'wishListNotCovered': wishResults['unusedItems'], 'spentOnwishList': wishResults['used'], 'totalBills': abs(btInfo['totalBills']), } else: #Just send back the results passback_values = { 'leftOver': round(limit, 2), 'expensesCovered': results['usedItems'], 'expensesNotCovered': results['unusedItems'], 'spentOnExpenses': results['used'], 'wishListCovered': [], 'wishListNotCovered': [], 'spentOnwishList': 0, 'totalBills': abs(btInfo['totalBills']), } self.response.set_status(200, 'Computation Complete') self.response.write(json.dumps(passback_values)) else: self.redirect(users.create_login_url(self.request.uri))
def setUp(self): self.len = 5 self.knapsack = knapsack.Knapsack(self.len)
def view(knap): print(f"* Knapsack capacity: {knap.w()}") print(f"* Number of items in knapsack: {knap.n()}") print(f"* Weights for each item: {knap.weights()}") print(f"* Values for each item: {knap.values()}") print(f"* Optimal profit: {knap.val()}") print(f"* Mask of items: {knap.items()}\n\n") if __name__ == '__main__': print("\nKNAPSACK TESTS\n") print("Ex.1") w1 = 26 weights1 = [12, 7, 11, 8, 9] values1 = [24, 13, 23, 15, 16] knap1 = knapsack.Knapsack(w1, weights1, values1) view(knap1) print("Ex.2") w2 = 190 weights2 = [56, 59, 80, 64, 75, 17] values2 = [50, 50, 64, 46, 50, 5] knap2 = knapsack.Knapsack(w2, weights2, values2) view(knap2) print("Ex.3") w3 = 170 weights3 = [41, 50, 49, 59, 55, 57, 60] values3 = [442, 525, 511, 593, 546, 564, 617] knap3 = knapsack.Knapsack(w3, weights3, values3) view(knap3)
import knapsack, time # totalWeight = 6404180 # weights = [382745,799601,909247,729069,467902,44328,34610,698150,823460,903959,853665,551830,610856,670702,488960,951111,323046,446298,931161,31385,496951,264724,224916,169684] # values = [ 825594,1677009,1676628,1523970,943972,97426,69666,1296457,1679693,1902996,1844992,1049289,1252836,1319836,953277,2067538,675367,853655,1826027,65731,901489,577243,466257,369261] # totalWeight = 7 # weights = [1,3,4,5] # values = [1,4,5,7] # itemIndexes = [0,1,2,3] totalWeight = 5 weights = [4, 6, 8] values = [5, 4, 4] itemIndexes = [0, 1, 2] sol = knapsack.Knapsack(totalWeight, weights, values, itemIndexes) time1 = time.time() res = sol.run() print(str(time.time() - time1) + " seconds") print(res) items = sol.showItems() print(items) print(" ".join("#" + str(i) + "," for i in items))
color = r.getBrickColor() weight, value = config.itemDataForColor(color) item = [weight, value] r.dropBrick() r.enclosed_brick_count += 1 # output solution of knapsack problem if optimal_subset.count(item) > output.count(item): r.throwBrick(trash=False) output.append(item) else: r.throwBrick(trash=True) r.enclosed_brick_count -= 1 leftBlockCount -= 1 # output possibly enclosed bricks r.outputBricks(r.enclosed_brick_count) if __name__ == "__main__": # load config, init robot and knapsack config = settings.Settings() r = robot.Robot(debug=config.debug()) ks = knapsack.Knapsack(cap=config.knapsack_cap) # start demonstration if initialization of objects succeeded demonstration(r, ks, config) r.say("Success!")
def main(): # setup libknapsack object capacity = 5 ks = knapsack.Knapsack(capacity) # add items to knapsack ks.addItem(3,10) ks.addItem(1,2) ks.addItem(2,2) ks.addItem(1,3) # benchmark first small example ks.setCapacity(5); ks.addItem(3,10); ks.addItem(1,2); ks.addItem(2,2); ks.addItem(1,3); start = time.clock() result = ks.solve() duration_small_one = (time.clock()-start) print "cap: 5, items: 4. took: ", duration_small_one, " seconds." # benchmark second small example ks.setCapacity(7); ks.addItem(3,10) ks.addItem(2,1); start = time.clock() result = ks.solve() duration_small_two = (time.clock()-start) print "cap: 7, items: 6. took: ", duration_small_two, " seconds." # benchmark first medium example ks.setCapacity(89); ks.addItem(3,14); ks.addItem(2,1); ks.addItem(3,30); ks.addItem(2,14); ks.addItem(3,10); ks.addItem(5,6); ks.addItem(42,10); ks.addItem(7,23); ks.addItem(9,10); ks.addItem(1,3); ks.addItem(4,10); ks.addItem(32,7); ks.addItem(3,10); ks.addItem(1,2); ks.addItem(6,10); ks.addItem(3,54); ks.addItem(3,12); ks.addItem(5,13); ks.addItem(51,11); ks.addItem(4,42); ks.addItem(9,52); ks.addItem(24,2); ks.addItem(4,10); ks.addItem(63,17); start = time.clock() result = ks.solve() duration_medium_two = (time.clock()-start) print "cap: 89, items: 30. took: ", duration_medium_two, " seconds." # benchmark second medium example ks.setCapacity(102); ks.addItem(3,13); ks.addItem(53,10); ks.addItem(4,42); ks.addItem(23,62); ks.addItem(1,19); start = time.clock() result = ks.solve() duration_medium_two = (time.clock()-start) print "cap: 102, items: 35. took: ", duration_medium_two, " seconds." # benchmark first big example ks.setCapacity(130); ks.addItem(3,14); ks.addItem(2,1); ks.addItem(3,30); ks.addItem(2,12); ks.addItem(3,10); ks.addItem(5,6); ks.addItem(42,10); ks.addItem(7,24); ks.addItem(9,53); ks.addItem(6,65); ks.addItem(4,4); ks.addItem(32,12); ks.addItem(3,53); ks.addItem(2,3); ks.addItem(6,53); ks.addItem(3,2); ks.addItem(12,1); ks.addItem(5,43); ks.addItem(51,11); ks.addItem(4,53); ks.addItem(23,3); ks.addItem(24,6); ks.addItem(1,2); ks.addItem(6,10); ks.addItem(7,5); ks.addItem(41,23); ks.addItem(4,13); ks.addItem(51,11); ks.addItem(2,45); ks.addItem(9,53); ks.addItem(8,1); ks.addItem(23,13); ks.addItem(42,10); ks.addItem(7,24); ks.addItem(8,32); ks.addItem(3,65); ks.addItem(4,4); ks.addItem(2,2); ks.addItem(41,53); ks.addItem(7,33); ks.addItem(2,53); start = time.clock() result = ks.solve() duration_big_two = (time.clock()-start) print "cap: 130, items: 76. took: ", duration_big_two, " seconds." # benchmark second big example ks.setCapacity(150); ks.addItem(2,13); ks.addItem(53,154); ks.addItem(14,442); ks.addItem(23,2); ks.addItem(2,42); ks.addItem(53,123); ks.addItem(2,12); start = time.clock() result = ks.solve() duration_big_two = (time.clock()-start) print "cap: 150, items: 83. took: ", duration_big_two, " seconds."
def setUp(self): """ Initiallize knapsack object for testing """ self.knapsack = knapsack.Knapsack(cap=KNAPSACK_CAP)
import socket as factory import functions import sys import json sys.path.append('../encryption') import knapsack import solitaire clientId = 33302 HOST = 'localhost' PORT = 30001 knap = knapsack.Knapsack(8) print('Client started') # Connecting the client to the server socket = factory.socket(factory.AF_INET, factory.SOCK_STREAM) server_address = (HOST, PORT) socket.connect(server_address) print('Connected to the keyserver') functions.login(socket, clientId, knap.publicKey) print('Logged in to keyserver') # Creating a new socket by which we can speak to the other client clientSocket = factory.socket(factory.AF_INET, factory.SOCK_STREAM) clientSocket.bind((HOST, clientId)) print('Listening for the client that wants to chat')