Last active
May 17, 2017 14:15
-
-
Save EthanRosenthal/07776d0a32a061679e9140108eca87cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"prob += (lpSum([1.0 * var for var \n", | |
" in guest_table_ind.values()]),\n", | |
" 'Objective function')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Define basic constraints\n", | |
"\n", | |
"min_table_sz = 8\n", | |
"max_table_sz = 12\n", | |
"\n", | |
"# Every guest must be assigned to a table\n", | |
"for guest in guests:\n", | |
" guests_tables = [guest_table_ind[guest, table] \n", | |
" for table in tables]\n", | |
" prob += (lpSum(guests_tables) == 1, \n", | |
" '{} must have a table'.format(guest))\n", | |
"\n", | |
"# Constrain number of guests per table\n", | |
"for table in tables:\n", | |
" tables_guests = [guest_table_ind[guest, table] \n", | |
" for guest in guests]\n", | |
" prob += (lpSum(tables_guests) >= min_table_sz,\n", | |
" 'Min guests per table {}'.format(table))\n", | |
" prob += (lpSum(tables_guests) <= max_table_sz,\n", | |
" 'Max guests per table {}'.format(table))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Pairing and antipairing constraints\n", | |
"\n", | |
"for guest_a, guest_b in pair_constr:\n", | |
" for table in tables:\n", | |
" pair_a = guest_table_ind[guest_a, table]\n", | |
" pair_b = guest_table_ind[guest_b, table]\n", | |
" prob += (pair_a == pair_b,\n", | |
" 'Pair {} with {} at table {}'.format(\n", | |
" guest_a, guest_b, table))\n", | |
"\n", | |
"for guest_a, guest_b in anti_constr:\n", | |
" for table in tables:\n", | |
" anti_a = guest_table_ind[guest_a, table]\n", | |
" anti_b = guest_table_ind[guest_b, table]\n", | |
" prob += (anti_a != anti_b,\n", | |
" 'Dont pair {} with {} at table {}'.format(\n", | |
" guest_a, guest_b, table))" | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [conda root]", | |
"language": "python", | |
"name": "conda-root-py" | |
}, | |
"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.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment