{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# specifying the knapsack instance\n", "\n", "import numpy as np\n", "from __future__ import division\n", "import os\n", "import time\n", "\n", "# reading in knapsack instance:\n", "numOfItems = 10\n", "instanceNumber = 3\n", "filename = 'C://users/dimo/desktop/teaching/Saclay2015/introToOptimization/02-dynProgAndBranchBound/knapsackinstances/KP-random-%ditems-%d.txt' % (numOfItems, instanceNumber)\n", "w = []\n", "p = []\n", "with open(filename, \"r\") as text_file:\n", " for line in text_file:\n", " ll = line.split()\n", " if 'W' in ll[0]:\n", " W = int(ll[2])\n", " if 'n' not in ll[0] and 'W' not in ll[0]:\n", " w.append(int(ll[0]))\n", " p.append(int(ll[1]))\n", "\n", "# and its objective and constraint functions:\n", "def f(x): # objective function\n", " return np.sum(p*x)\n", "\n", "def g(x): # constraint violation function\n", " return np.sum(w*x) - W" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }