{ "cells": [ { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "from __future__ import division\n", "import os\n", "\n", "numOfItems = 10\n", "minweight = 0 # minimum weight of the items\n", "maxweight = 100 # maximum weight of the items\n", "knapsackweight = numOfItems/2*(maxweight-minweight) + minweight\n", "numOfInstances = 3 # number of created random instances\n", "filenamebase = 'instances/KP-random-%ditems-' % numOfItems\n", "if not os.path.isdir('instances'):\n", " os.mkdir('instances')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for k in range(1, numOfInstances+1):\n", " filename = filenamebase + str(k) + '.txt'\n", " mystring = 'n = %d # number of items\\n W = %d # maximum weight of knapsack (capacity)\\n' % (numOfItems, knapsackweight)\n", " A = np.round( (maxweight-minweight) * np.random.rand(numOfItems,2) + minweight )\n", " for i in range(A.shape[0]):\n", " mystring = mystring + '%d %d\\n' % (A[i][0], A[i][1])\n", " with open(filename, \"w\") as text_file:\n", " text_file.write(mystring)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.3.5" } }, "nbformat": 4, "nbformat_minor": 0 }