# coding: utf-8 # In[ ]: # specifying the knapsack instance from __future__ import division import numpy as np import os import time numOfItems = 10 instanceNumber = 3 # change path to your needs: filename = 'C://users/dimo/desktop/teaching/Saclay2015/introToOptimization/02-dynProgAndBranchBound/knapsackinstances/KP-random-%ditems-%d.txt' % (numOfItems, instanceNumber) w = [] p = [] with open(filename, "r") as text_file: for line in text_file: ll = line.split() if 'W' in ll[0]: W = int(ll[2]) if 'n' not in ll[0] and 'W' not in ll[0]: w.append(int(ll[0])) p.append(int(ll[1])) # and its objective and constraint functions: def f(x): # objective function return np.sum(p*x) def g(x): # constraint violation function return np.sum(w*x) - W