{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Objective Maximisation\n", "\n", "ENTMOOT supports both minimisation and maximisation of objective functions. This notebook defines a concave function, that has a maximum at (1, 1)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from entmoot import Enting, ProblemConfig, PyomoOptimizer\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# define a maximisation problem\n", "def eval_simple_max_testfunc(X):\n", " x = np.array(X)\n", " y = - np.sum((x - np.ones_like(x)) ** 2, axis=1)\n", " return y.reshape(-1, 1)\n", "\n", "def build_simple_max_problem(problem_config: ProblemConfig):\n", " problem_config.add_feature(\"real\", (0.0, 2.0), name=\"x1\")\n", " problem_config.add_feature(\"real\", (0.0, 2.0), name=\"x2\")\n", " problem_config.add_max_objective()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# define problem\n", "problem_config = ProblemConfig(rnd_seed=73)\n", "# number of objectives\n", "build_simple_max_problem(problem_config)\n", "# sample data\n", "rnd_sample = problem_config.get_rnd_sample_list(num_samples=200)\n", "testfunc_evals = eval_simple_max_testfunc(rnd_sample)\n", "\n", "params = {\"unc_params\": {\"dist_metric\": \"l1\", \"acq_sense\": \"penalty\"}}\n", "enting = Enting(problem_config, params=params)\n", "enting.fit(rnd_sample, testfunc_evals)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "WARNING: The gurobipy module (an optional Pyomo dependency) failed to import:\n", "NameError: name 'GurobiDirect' is not defined\n" ] }, { "ename": "ApplicationError", "evalue": "No Python bindings available for solver plugin", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mApplicationError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[10], line 4\u001b[0m\n\u001b[0;32m 1\u001b[0m params_pyomo \u001b[38;5;241m=\u001b[39m {\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msolver_name\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgurobi_direct\u001b[39m\u001b[38;5;124m\"\u001b[39m}\n\u001b[0;32m 2\u001b[0m opt_pyo \u001b[38;5;241m=\u001b[39m PyomoOptimizer(problem_config, params\u001b[38;5;241m=\u001b[39mparams_pyomo)\n\u001b[1;32m----> 4\u001b[0m res_pyo \u001b[38;5;241m=\u001b[39m \u001b[43mopt_pyo\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msolve\u001b[49m\u001b[43m(\u001b[49m\u001b[43menting\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 5\u001b[0m res_pyo\n", "File \u001b[1;32m~\\phd\\entmoot\\entmoot\\optimizers\\pyomo_opt.py:104\u001b[0m, in \u001b[0;36mPyomoOptimizer.solve\u001b[1;34m(self, tree_model, model_core, weights)\u001b[0m\n\u001b[0;32m 102\u001b[0m \u001b[38;5;66;03m# Solve optimization model\u001b[39;00m\n\u001b[0;32m 103\u001b[0m verbose \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_params\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mverbose\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m--> 104\u001b[0m \u001b[43mopt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msolve\u001b[49m\u001b[43m(\u001b[49m\u001b[43mopt_model\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtee\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mverbose\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 106\u001b[0m \u001b[38;5;66;03m# update current solution\u001b[39;00m\n\u001b[0;32m 107\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_curr_sol, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_active_leaves \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_sol(opt_model)\n", "File \u001b[1;32mc:\\Users\\tobyb\\phd\\phdvenv\\lib\\site-packages\\pyomo\\solvers\\plugins\\solvers\\direct_solver.py:75\u001b[0m, in \u001b[0;36mDirectSolver.solve\u001b[1;34m(self, *args, **kwds)\u001b[0m\n\u001b[0;32m 72\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21msolve\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwds):\n\u001b[0;32m 73\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Solve the problem\"\"\"\u001b[39;00m\n\u001b[1;32m---> 75\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mavailable\u001b[49m\u001b[43m(\u001b[49m\u001b[43mexception_flag\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[0;32m 76\u001b[0m \u001b[38;5;66;03m#\u001b[39;00m\n\u001b[0;32m 77\u001b[0m \u001b[38;5;66;03m# If the inputs are models, then validate that they have been\u001b[39;00m\n\u001b[0;32m 78\u001b[0m \u001b[38;5;66;03m# constructed! Collect suffix names to try and import from solution.\u001b[39;00m\n\u001b[0;32m 79\u001b[0m \u001b[38;5;66;03m#\u001b[39;00m\n\u001b[0;32m 80\u001b[0m _model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n", "File \u001b[1;32mc:\\Users\\tobyb\\phd\\phdvenv\\lib\\site-packages\\pyomo\\solvers\\plugins\\solvers\\gurobi_direct.py:215\u001b[0m, in \u001b[0;36mGurobiDirect.available\u001b[1;34m(self, exception_flag)\u001b[0m\n\u001b[0;32m 213\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m exception_flag:\n\u001b[0;32m 214\u001b[0m gurobipy\u001b[38;5;241m.\u001b[39mlog_import_warning(logger\u001b[38;5;241m=\u001b[39m\u001b[38;5;18m__name__\u001b[39m)\n\u001b[1;32m--> 215\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m ApplicationError(\n\u001b[0;32m 216\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo Python bindings available for \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m solver plugin\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m (\u001b[38;5;28mtype\u001b[39m(\u001b[38;5;28mself\u001b[39m),)\n\u001b[0;32m 217\u001b[0m )\n\u001b[0;32m 218\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[0;32m 220\u001b[0m \u001b[38;5;66;03m# Ensure environment is started to check for a valid license\u001b[39;00m\n", "\u001b[1;31mApplicationError\u001b[0m: No Python bindings available for solver plugin" ] } ], "source": [ "params_pyomo = {\"solver_name\": \"gurobi_direct\"}\n", "opt_pyo = PyomoOptimizer(problem_config, params=params_pyomo)\n", "\n", "res_pyo = opt_pyo.solve(enting)\n", "res_pyo" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.11" } }, "nbformat": 4, "nbformat_minor": 4 }