diff --git a/src/baybe_playground.ipynb b/src/baybe_playground.ipynb index 7bb01e5..7a4b998 100644 --- a/src/baybe_playground.ipynb +++ b/src/baybe_playground.ipynb @@ -9,7 +9,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ @@ -25,7 +25,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 32, "metadata": {}, "outputs": [], "source": [ @@ -33,58 +33,25 @@ "\n", "parameters = [\n", " NumericalContinuousParameter('schwefel1', bounds=(-500,500)),\n", - " NumericalContinuousParameter('schwefel2', bounds=(-500,500))\n", - "\n", "]" ] }, { "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [], - "source": [ - "from baybe.recommenders import SequentialGreedyRecommender\n", - "\n", - "recommender = SequentialGreedyRecommender()" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [], - "source": [ - "from baybe.searchspace import SearchSpace\n", - "\n", - "searchspace = SearchSpace.from_product(parameters)" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [], - "source": [ - "from baybe import Campaign\n", - "\n", - "campaign = Campaign(searchspace, objective, recommender)" - ] - }, - { - "cell_type": "code", - "execution_count": 22, + "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['__annotations__',\n", + "['__abstractmethods__',\n", + " '__annotations__',\n", " '__attrs_attrs__',\n", " '__attrs_own_setattr__',\n", - " '__attrs_types_resolved__',\n", " '__class__',\n", + " '__class_getitem__',\n", " '__delattr__',\n", + " '__dict__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", @@ -101,6 +68,7 @@ " '__module__',\n", " '__ne__',\n", " '__new__',\n", + " '__parameters__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", @@ -111,60 +79,122 @@ " '__str__',\n", " '__subclasshook__',\n", " '__weakref__',\n", - " '_cached_recommendation',\n", - " '_measurements_exp',\n", - " '_measurements_parameters_comp',\n", - " '_measurements_targets_comp',\n", - " '_validate_strategy',\n", - " '_validate_tolerance_flag',\n", - " 'add_measurements',\n", - " 'from_config',\n", - " 'from_dict',\n", - " 'from_json',\n", - " 'measurements',\n", - " 'n_batches_done',\n", - " 'n_fits_done',\n", - " 'numerical_measurements_must_be_within_tolerance',\n", - " 'objective',\n", - " 'parameters',\n", - " 'recommend',\n", - " 'recommender',\n", - " 'searchspace',\n", - " 'strategy',\n", - " 'targets',\n", - " 'to_dict',\n", - " 'to_json',\n", - " 'validate_config']" + " '_abc_impl',\n", + " '_is_protocol',\n", + " '_is_runtime_protocol',\n", + " '_recommend_continuous',\n", + " '_recommend_discrete',\n", + " '_recommend_hybrid',\n", + " '_recommend_with_discrete_parts',\n", + " 'allow_recommending_already_measured',\n", + " 'allow_repeated_recommendations',\n", + " 'compatibility',\n", + " 'recommend']" ] }, - "execution_count": 22, + "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "dir(campaign)" + "import baybe.recommenders\n", + "\n", + "dir(baybe.recommenders.pure.RandomRecommender)" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [], + "source": [ + "from baybe.recommenders import (\n", + " SequentialGreedyRecommender, \n", + " SequentialMetaRecommender, \n", + " RandomRecommender\n", + ")\n", + "\n", + "recommender = RandomRecommender()#SequentialMetaRecommender([SequentialGreedyRecommender(), RandomRecommender()])" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [], + "source": [ + "from baybe.searchspace import SearchSpace\n", + "\n", + "searchspace = SearchSpace.from_product(parameters)" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 88, + "metadata": {}, + "outputs": [], + "source": [ + "from baybe import Campaign\n", + "\n", + "campaign = Campaign(searchspace, objective, recommender)" + ] + }, + { + "cell_type": "code", + "execution_count": 89, "metadata": {}, "outputs": [ { - "ename": "ValueError", - "evalue": "can only convert an array of size 1 to a Python scalar", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[16], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m df \u001b[38;5;241m=\u001b[39m \u001b[43mcampaign\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrecommend\u001b[49m\u001b[43m(\u001b[49m\u001b[43mbatch_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 2\u001b[0m display(df)\n", - "File \u001b[1;32mc:\\Users\\d23895jm\\AppData\\Local\\anaconda3\\envs\\BO\\lib\\site-packages\\baybe\\campaign.py:298\u001b[0m, in \u001b[0;36mCampaign.recommend\u001b[1;34m(self, batch_size, batch_quantity)\u001b[0m\n\u001b[0;32m 295\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_measurements_exp\u001b[38;5;241m.\u001b[39mfillna({\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFitNr\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mn_fits_done}, inplace\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[0;32m 297\u001b[0m \u001b[38;5;66;03m# Get the recommended search space entries\u001b[39;00m\n\u001b[1;32m--> 298\u001b[0m rec \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrecommender\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrecommend\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 299\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msearchspace\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 300\u001b[0m \u001b[43m \u001b[49m\u001b[43mbatch_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 301\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_measurements_parameters_comp\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 302\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_measurements_targets_comp\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 303\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 305\u001b[0m \u001b[38;5;66;03m# Cache the recommendations\u001b[39;00m\n\u001b[0;32m 306\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_cached_recommendation \u001b[38;5;241m=\u001b[39m rec\u001b[38;5;241m.\u001b[39mcopy()\n", - "File \u001b[1;32mc:\\Users\\d23895jm\\AppData\\Local\\anaconda3\\envs\\BO\\lib\\site-packages\\baybe\\recommenders\\pure\\bayesian\\base.py:139\u001b[0m, in \u001b[0;36mBayesianRecommender.recommend\u001b[1;34m(self, searchspace, batch_size, train_x, train_y)\u001b[0m\n\u001b[0;32m 136\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m _ONNX_INSTALLED \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msurrogate_model, CustomONNXSurrogate):\n\u001b[0;32m 137\u001b[0m CustomONNXSurrogate\u001b[38;5;241m.\u001b[39mvalidate_compatibility(searchspace)\n\u001b[1;32m--> 139\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msetup_acquisition_function\u001b[49m\u001b[43m(\u001b[49m\u001b[43msearchspace\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtrain_x\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtrain_y\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 141\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28msuper\u001b[39m()\u001b[38;5;241m.\u001b[39mrecommend(searchspace, batch_size, train_x, train_y)\n", - "File \u001b[1;32mc:\\Users\\d23895jm\\AppData\\Local\\anaconda3\\envs\\BO\\lib\\site-packages\\baybe\\recommenders\\pure\\bayesian\\base.py:94\u001b[0m, in \u001b[0;36mBayesianRecommender.setup_acquisition_function\u001b[1;34m(self, searchspace, train_x, train_y)\u001b[0m\n\u001b[0;32m 89\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m train_x \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mor\u001b[39;00m train_y \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m 90\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m(\n\u001b[0;32m 91\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBayesian recommenders do not support empty training data yet.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 92\u001b[0m )\n\u001b[1;32m---> 94\u001b[0m best_f \u001b[38;5;241m=\u001b[39m \u001b[43mtrain_y\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mitem\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 95\u001b[0m surrogate_model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_fit(searchspace, train_x, train_y)\n\u001b[0;32m 96\u001b[0m acquisition_function_cls \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_acquisition_function_cls()\n", - "File \u001b[1;32mc:\\Users\\d23895jm\\AppData\\Local\\anaconda3\\envs\\BO\\lib\\site-packages\\pandas\\core\\base.py:418\u001b[0m, in \u001b[0;36mIndexOpsMixin.item\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 416\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m 417\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mnext\u001b[39m(\u001b[38;5;28miter\u001b[39m(\u001b[38;5;28mself\u001b[39m))\n\u001b[1;32m--> 418\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcan only convert an array of size 1 to a Python scalar\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", - "\u001b[1;31mValueError\u001b[0m: can only convert an array of size 1 to a Python scalar" - ] + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
schwefel1
0356.991347
1-309.004730
2-55.674007
\n", + "
" + ], + "text/plain": [ + " schwefel1\n", + "0 356.991347\n", + "1 -309.004730\n", + "2 -55.674007" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ @@ -174,10 +204,151 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 91, + "metadata": {}, + "outputs": [], + "source": [ + "from schwefel_functions import schwefel_1d\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
schwefel1Yield
0356.991347403.045423
1-309.004730123.758482
2-55.674007470.423643
\n", + "
" + ], + "text/plain": [ + " schwefel1 Yield\n", + "0 356.991347 403.045423\n", + "1 -309.004730 123.758482\n", + "2 -55.674007 470.423643" + ] + }, + "execution_count": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['Yield'] = df['schwefel1'].apply(schwefel_1d)\n", + "df" + ] + }, + { + "cell_type": "code", + "execution_count": 93, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "campaign.add_measurements(df)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
schwefel1
0-428.432368
1-347.173847
2395.345514
\n", + "
" + ], + "text/plain": [ + " schwefel1\n", + "0 -428.432368\n", + "1 -347.173847\n", + "2 395.345514" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "campaign.recommend(batch_size=3)" + ] } ], "metadata": {