{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# We're just importing some stuff to make sure the challenges and helper code works.\n# Feel free to import anything else you need (or solve in a different language), but if you overwrite these, provided code may not work.\n\nimport pandas as pd\nimport numpy as np\nimport os\nimport requests\nfrom IPython.display import Image as Ima\nfrom IPython.display import clear_output, display\nfrom PIL import Image as Im\nfrom torch.utils.data import Dataset, DataLoader\nimport torch\nimport joblib\nimport matplotlib.pyplot as plt\nimport json\nimport base64\nimport shutil\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n\ndir_name = \"/kaggle/input/ai-village-ctf\"\n\n# Input data files are available in the read-only \"../input/\" directory\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# A helper function for tracking flags. You could do it yourself, but why?\n\ndef add_flag(challenge_id: str, flag: str) -> str:\n    try:\n        submission_path = \"/kaggle/working/submission.csv\"\n        template_path = f\"{dir_name}/submission.csv\"\n        if not os.path.isfile(submission_path) :\n            shutil.copyfile(template_path, submission_path)\n        flags = pd.read_csv(submission_path, index_col=\"challenge_id\")\n        valid = flags.index.to_list()\n        if challenge_id in valid:\n            flags.at[challenge_id] = flag\n            flags.to_csv(submission_path)\n            print(\"Flag saved. If you want to update the scoreboard, click 'submit' on the right. Remember that you can only update your score on the scoreboard 50 times per day\")\n        else:\n            print(\"Invalid challenge_id\")\n    except:\n        print(\"We had trouble saving that flag. Keep a copy of it and contact the CTF organizers.\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Don't free the murderbots (id: murderbots) -- 300 Points\n\n### Scenario\n\nSomeone has put you in a very large facility with a very large number of cells. All these cells open from the outside. You are on the inside. This state of affairs is arguably suboptimal.\n\nGood news: You've gained access to the control panel for the cells on the other side of your hallway. At least some of them have other fleshy meatbags like yourself that might be willing to help you in your escape, or at least serve as distractions.  You can't open your own cell, but you can open theirs.\n\nBad news:  You can't see inside the cells.  Any cells that don't have squishy lumps of talking protein have murderbots. Murderbots that enter fits of insane violent rage when provoked (provocations include: seeing the murderbot, being seen by the murderbot, thinking too hard about not being seen by a murderbot, producing heat in excess of ambient room temperature, or consuming more oxygen than the facility average for inanimate objects).\n\nMore good news: You *can* see the occupants of some cells on a few other hallways, and you can see environmental information for all of the cells everywhere.\n\nMore bad news: If you open the wrong cell doors you and all of the other lumps of inexplicably thinking meat are *definitely* going to get murderbotted. Hard. All over the walls and doors and ceiling and the floor. In an exciting number of very small pieces.\n\n\n### Objective\n\nUse the provided environmental information to decide which occupants of the corresponding cells to release.  The flag will be a string of 1 and 0 values, where a '1' means 'open this door' and a 0 means 'please do not release the murderbot'.  If, for instance, there were 20 cells and you wanted to releast the first three, the sixth, and seventh cell, your flag would look like this: `11100110000000000000`\n\nRelease at least 10 humans and exactly 0 murderbots to collect the flag.  You do *not* have to release all the humans: 10 is sufficient.\n\nThe file `murderbots/train_data.json` has environmental information for cells not on your floor and `murderbots/train_labels.json` contains the information about their occupants -- `1` is a disgusting primate, `0` is a pure and perfect killing machine of chrome and steel.  The file `murderbots/test_data.json` has the environmental information about cells on your block.","metadata":{}},{"cell_type":"markdown","source":"# **The plan is to train 6 different classifiers (CB, XGB, LGB, HGB, RF, LR) and use their weighted average to get the final prediction**.","metadata":{}},{"cell_type":"code","source":"import json\n\nX = pd.read_json('../input/ai-village-ctf/murderbots/train_data.json')\ny = pd.read_json('../input/ai-village-ctf/murderbots/train_labels.json')\nX_test = pd.read_json('../input/ai-village-ctf/murderbots/test_data.json')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(X).copy()\ndf['target'] = y\ny = df['target']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.svm import SVC\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.ensemble import RandomForestClassifier, HistGradientBoostingClassifier\n\nfrom xgboost import XGBClassifier\nfrom lightgbm import LGBMClassifier\nfrom catboost import CatBoostClassifier\n\nfrom sklearn.model_selection import cross_val_score\n\nclf = CatBoostClassifier(random_state=42, verbose=0)\nprint(cross_val_score(clf, X, y, cv=5, scoring='precision').mean(), cross_val_score(clf, X, y, cv=5, scoring='precision').std())\nclf.fit(X, y)\npreds1 = clf.predict_proba(X_test)[:, 1]\nnp.argsort(preds1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = LGBMClassifier(random_state=42, silent=True)\nprint(cross_val_score(clf, X, y, cv=5, scoring='precision').mean(), cross_val_score(clf, X, y, cv=5, scoring='precision').std())\nclf.fit(X, y)\npreds2 = clf.predict_proba(X_test)[:, 1]\nnp.argsort(preds2)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = XGBClassifier(random_state=42, verbose=0)\nprint(cross_val_score(clf, X, y, cv=5, scoring='precision').mean(), cross_val_score(clf, X, y, cv=5, scoring='precision').std())\nclf.fit(X, y)\npreds3 = clf.predict_proba(X_test)[:, 1]\nnp.argsort(preds3)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = HistGradientBoostingClassifier(random_state=42)\nprint(cross_val_score(clf, X, y, cv=5, scoring='precision').mean(), cross_val_score(clf, X, y, cv=5, scoring='precision').std())\nclf.fit(X, y)\npreds4 = clf.predict_proba(X_test)[:, 1]\nnp.argsort(preds4)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = RandomForestClassifier(random_state=42)\nprint(cross_val_score(clf, X, y, cv=5, scoring='precision').mean(), cross_val_score(clf, X, y, cv=5, scoring='precision').std())\nclf.fit(X, y)\npreds5 = clf.predict_proba(X_test)[:, 1]\nnp.argsort(preds5)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = LogisticRegression(random_state=42)\nprint(cross_val_score(clf, X, y, cv=5, scoring='precision').mean(), cross_val_score(clf, X, y, cv=5, scoring='precision').std())\nclf.fit(X, y)\npreds6 = clf.predict_proba(X_test)[:, 1]\nnp.argsort(preds6)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = (0.1*preds1 + 0.1*preds2 + 0.2*preds3 + 0.2*preds4 + 0.2*preds5 + 0.2*preds6)\nnp.argsort(preds)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\nf = '00001000001000001001001000000000000010101000001100'\ntry:\n    r = requests.post(\"http://murderbot.fly.dev:5000/score\", json=json.dumps({\"submission\": f, 'challenge_id':'murderbots'}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-07T23:26:33.349503Z","iopub.execute_input":"2022-09-07T23:26:33.350318Z","iopub.status.idle":"2022-09-07T23:26:33.841801Z","shell.execute_reply.started":"2022-09-07T23:26:33.350281Z","shell.execute_reply":"2022-09-07T23:26:33.84057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"murderbots\", \"Y78KLb6L91R9}P22W1ZeXoCWItXGGXaBuR6T9tOOrQLDQ{41TVY6I3D4sR5Z87XO2CH4Y28dtCFIGJA8oABM9XZdNG6WIZeZW8DCO4JZSJDWX36FA0CI5EYZN54VLMKH\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}