From 39f0757c2eb8becd2c50100e78b90964a1fac6bb Mon Sep 17 00:00:00 2001 From: cyfraeviolae Date: Sun, 4 Jul 2021 12:22:00 -0400 Subject: init --- Untitled.ipynb | 465 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 465 insertions(+) create mode 100644 Untitled.ipynb (limited to 'Untitled.ipynb') diff --git a/Untitled.ipynb b/Untitled.ipynb new file mode 100644 index 0000000..eca45d1 --- /dev/null +++ b/Untitled.ipynb @@ -0,0 +1,465 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 185, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Dark rum', 'Dry white wine', 'Créme de Menthe', 'Peach bitters', 'Maraschino', 'Galliano', 'Raspberry syrup', 'Campari', 'Tequila', 'Créme de Cassis', 'DOM Bénédictine', 'Lillet Blonde', 'Sweet vermouth', 'Kirsch', 'Cream liqueur', 'Grenadine', 'Cherry liqueur', 'Calvados', 'Bourbon', 'Aperol', 'Créme de Cacao', 'Triple Sec', 'Absinthe', 'DiSaronno', 'Peach schnapps', 'Rye', 'Coffee liqueur', 'Drambuie', 'Strawberry syrup', 'Irish whiskey', \"Peychaud's bitters\", 'Raspberry liqueur', 'Scotch', 'Dry vermouth', 'Peach puree', 'Apricot brandy', 'White rum', 'Pisco', 'Orange flower water', 'Prosecco', 'Orange bitters', 'Cognac', 'Angostura bitters', 'Gin', 'Vodka', 'Orgeat', 'Cachaca', 'Blackberry liqueur', 'Champagne', 'Red Port']\n" + ] + } + ], + "source": [ + "import json\n", + "skip = [\n", + " 'Lime juice',\n", + " 'Syrup',\n", + " 'Lemon juice',\n", + " 'Orange juice',\n", + " 'Cream',\n", + " 'Soda water',\n", + " 'Cola',\n", + " 'Hot coffee',\n", + " 'Egg yolk',\n", + " 'Coconut milk',\n", + " 'Ginger beer',\n", + " 'Pineapple juice',\n", + " 'Grapefruit juice',\n", + " 'Cranberry juice',\n", + " 'Tomato juice',\n", + " 'Olive juice',\n", + " 'Ginger Ale',\n", + "]\n", + "recipesj = json.load(open('recipes-fix.json'))\n", + "ingredients = set()\n", + "for recipe in recipesj:\n", + " for ingredient in recipe['ingredients']:\n", + " if 'special' in ingredient:\n", + " continue\n", + " if ingredient['ingredient'] in skip:\n", + " continue\n", + " ingredients.add(ingredient['ingredient'])\n", + "ingredientsl = list(ingredients)\n", + "ingredientsm = dict(zip(ingredientsl, range(len(ingredientsl))))\n", + "recipes = []\n", + "for recipe in recipesj:\n", + " x = set()\n", + " for ingredient in recipe['ingredients']:\n", + " if 'special' in ingredient:\n", + " continue\n", + " if ingredient['ingredient'] in skip:\n", + " continue\n", + " y = ingredientsm[ingredient['ingredient']]\n", + " x.add(y)\n", + " recipes.append(x)\n", + "n_ingredients = len(ingredientsl)\n", + "print(ingredientsl)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 186, + "metadata": {}, + "outputs": [], + "source": [ + "makeStepR = lambda step, r: 'r' + str(step) + '_' + str(r)\n", + "makeStepY = lambda step, y: 'y' + str(step) + '_' + str(y)\n", + "\n", + "f = open('lp.2', 'w')\n", + "f.write('Minimize\\n')\n", + "for step in range(n_ingredients):\n", + " for r, recipe in enumerate(recipes):\n", + " f.write(' - ' + makeStepR(step, r))\n", + "f.write('\\nSubject To\\n')\n", + "for step in range(n_ingredients):\n", + " f.write(' + '.join(makeStepY(step, y) for y in range(n_ingredients)) + ' = ' + str(step + 1) + '\\n')\n", + "for y in range(n_ingredients):\n", + " for step in range(n_ingredients - 1):\n", + " f.write(makeStepY(step+1, y) + ' - ' + makeStepY(step, y) + ' >= 0\\n')\n", + "for r, recipe in enumerate(recipes):\n", + " for step in range(n_ingredients):\n", + " tot = ' - '.join(makeStepY(step, y) for y in recipe)\n", + " f.write(makeStepR(step, r) + ' - ' + tot + ' >= -' + str(len(recipe) - 1) + '\\n')\n", + " for y in recipe:\n", + " f.write(makeStepR(step, r) + ' - ' + makeStepY(step, y) + ' <= 0\\n')\n", + "f.write('Binary\\n')\n", + "f.write(' '.join(makeStepY(step, y) \n", + " for step in range(n_ingredients) for y in range(n_ingredients)))\n", + "f.write(' ')\n", + "f.write(' '.join(makeStepR(step, r) \n", + " for step in range(n_ingredients) for r in range(len(recipes))))\n", + "f.write('\\nEnd\\n')\n", + "f.close()" + ] + }, + { + "cell_type": "code", + "execution_count": 164, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(50, 50)" + ] + }, + "execution_count": 164, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "linesf = open('sol.2')\n", + "lines = linesf.read().splitlines()\n", + "linesf.close()\n", + "sol = []\n", + "seen = set()\n", + "batch = None\n", + "batches = []\n", + "oldstep = None\n", + "for line in lines:\n", + " if 'y' in line:\n", + " y = line.split()[1].split('_')[-1]\n", + " if int(y) in sol:\n", + " continue\n", + " sol.append(int(y))\n", + " if 'r' in line:\n", + " step, r = line.split()[1][1:].split('_')\n", + " step = int(step)\n", + " r = int(r)\n", + " if step != oldstep:\n", + " if batch is not None:\n", + " batches.append((oldstep, batch))\n", + " batch = []\n", + " oldstep = step\n", + " if r not in seen:\n", + " batch.append(r)\n", + " seen.add(r)\n", + "batches.append((oldstep, batch))\n", + "len(batches), len(sol)" + ] + }, + { + "cell_type": "code", + "execution_count": 166, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1. Vodka | Sea Breeze, Screwdriver, Bloody Mary, Moscow Mule\n", + "2. White rum | Mojito, Cuba Libre, Pina Colada, Daiquiri\n", + "3. Triple Sec | Lemon Drop Martini, Cosmopolitan, Kamikaze\n", + "4. Tequila | Margarita, Tommy's Margarita, Vampiro\n", + "5. Gin | Long Island Iced Tea, Gin Fizz, John Collins, White Lady\n", + "6. Cognac | Horse's Neck, Sidecar, Between the Sheets\n", + "7. Galliano | Yellow Bird, Golden Dream, Harvey Wallbanger\n", + "8. Cherry liqueur | Aviation, Hemingway Special\n", + "9. Grenadine | Bacardi, Tequila Sunrise, Mary Pickford\n", + "10. Bourbon | Whiskey Sour, Mint Julep\n", + "11. Champagne | Mimosa, French 75\n", + "12. Angostura bitters | Old Fashioned, Champagne Cocktail\n", + "13. DiSaronno | God Mother, French Connection\n", + "14. Dark rum | Planter's Punch, Dark 'n' Stormy\n", + "15. Dry vermouth | Dirty Martini, Dry Martini\n", + "16. Coffee liqueur | Espresso Martini, Black Russian\n", + "17. Créme de Cacao | Alexander\n", + "18. Créme de Menthe | Grasshopper, Stinger\n", + "19. Irish whiskey | Irish Coffee\n", + "20. Peach bitters | Derby\n", + "21. Créme de Cassis | Russian Spring Punch\n", + "22. Cachaca | Caipirinha\n", + "23. Pisco | Pisco Sour\n", + "24. Blackberry liqueur | Bramble\n", + "25. Peach schnapps | Sex on the Beach\n", + "26. Prosecco | Barracuda\n", + "27. Red Port | Porto Flip\n", + "28. Raspberry syrup | Clover Club\n", + "29. Dry White Wine | Kir\n", + "30. Sweet vermouth | \n", + "31. Campari | Negroni, Americano\n", + "32. Absinthe | Monkey Gland\n", + "33. Peychaud's bitters | Sazerac\n", + "34. Aperol | Spritz Veneziano\n", + "35. Scotch | God Father\n", + "36. Peach puree | Bellini\n", + "37. Orange Bitters | Casino\n", + "38. Lillet Blonde | Vesper\n", + "39. Drambuie | Rusty Nail\n", + "40. DOM Bénédictine | Singapore Sling\n", + "41. Cream liqueur | B52\n", + "42. Apricot brandy | Paradise\n", + "43. Calvados | Angel Face\n", + "44. Raspberry liqueur | French Martini\n", + "45. Rye | Manhattan\n", + "46. Maraschino | Tuxedo\n", + "47. Orgeat | Mai-tai\n", + "48. Orange flower water | Ramos Fizz\n", + "49. Strawberry syrup | \n", + "50. Kirsch | Rose\n" + ] + } + ], + "source": [ + "def pad(s, n):\n", + " return s + (n - len(s))*' '\n", + "for i, (y, (step, batch)) in enumerate(zip(sol, batches)):\n", + " drinks = [recipesj[r]['name'] for r in batch]\n", + " print(pad(str(i+1) + '. ' + ingredientsl[y], 23) + ' | ' + ', '.join(drinks))" + ] + }, + { + "cell_type": "code", + "execution_count": 169, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "White rum\n", + "Vodka\n", + "Triple Sec\n", + "Cognac\n", + "Gin\n", + "Tequila\n", + "Galliano\n", + "Grenadine\n", + "Cherry liqueur\n", + "Champagne\n", + "Bourbon\n", + "Angostura bitters\n", + "Dark rum\n", + "Coffee liqueur\n", + "DiSaronno\n", + "Dry vermouth\n", + "Lillet Blonde\n", + "Pisco\n", + "Raspberry syrup\n", + "Red Port\n", + "Cream liqueur\n", + "Créme de Menthe\n", + "Créme de Cacao\n", + "Blackberry liqueur\n", + "Raspberry liqueur\n", + "Orgeat\n", + "Prosecco\n", + "Peach puree\n", + "Aperol\n", + "Peach schnapps\n", + "Absinthe\n", + "Peach bitters\n", + "Irish whiskey\n", + "Peychaud's bitters\n", + "DOM Bénédictine\n", + "Apricot brandy\n", + "Calvados\n", + "Orange flower water\n", + "Créme de Cassis\n", + "Dry White Wine\n", + "Scotch\n", + "Drambuie\n", + "Orange Bitters\n", + "Maraschino\n", + "Cachaca\n", + "Campari\n", + "Sweet vermouth\n", + "Rye\n", + "Kirsch\n", + "Strawberry syrup\n" + ] + } + ], + "source": [ + "from collections import Counter\n", + "recipesc = recipes.copy()\n", + "greedysol = []\n", + "\n", + "for step in range(n_ingredients):\n", + " c = Counter()\n", + " for recipe in recipesc:\n", + " if len(recipe) == 1:\n", + " c.update([list(recipe)[0]])\n", + " ingrs = c.most_common()\n", + " if len(ingrs) == 0:\n", + " ingr = list(set.union(*recipesc))[0]\n", + " else:\n", + " ingr = ingrs[0][0]\n", + " greedysol.append(ingr)\n", + " for i, recipe in enumerate(recipesc):\n", + " recipesc[i] = recipe - {ingr}\n", + "for y in greedysol:\n", + " print(ingredientsl[y])" + ] + }, + { + "cell_type": "code", + "execution_count": 174, + "metadata": {}, + "outputs": [], + "source": [ + "def score(recipes, sol):\n", + " t = 0\n", + " recipesc = recipes.copy()\n", + " for i, y in enumerate(sol):\n", + " for recipe in recipesc:\n", + " if recipe == {y}:\n", + " t += (len(ingredientsl)-i)\n", + " for i, recipe in enumerate(recipesc):\n", + " recipesc[i] = recipe - {y}\n", + " return t" + ] + }, + { + "cell_type": "code", + "execution_count": 175, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2501, 2495)" + ] + }, + "execution_count": 175, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "score(recipes, sol), score(recipes, greedysol)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "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.9.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} -- cgit v1.2.3