Skip to content

Commit

Permalink
Continue implementation of tutorial notebook 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mlojek committed Feb 27, 2025
1 parent 7e7610c commit 646cbe9
Showing 1 changed file with 170 additions and 4 deletions.
174 changes: 170 additions & 4 deletions demo/02_creating_an_optimizer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "bc90b9c6-8955-4c6f-a672-9611db01de77",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -61,15 +61,53 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "dcae81fc-4ca7-42fb-9de8-0fca37cafe99",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"FunctionMetadata(name='custom_function', dim=10, hyperparameters={})"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"func = SimpleObjectiveFunction(10)\n",
"func.get_metadata()"
]
},
{
"cell_type": "markdown",
"id": "8eefdc6a-8d99-4df7-aa9b-51ad6e9602c6",
"metadata": {},
"source": [
"TODO evaluate a point using the function"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "76e26cee-e216-49d7-84d6-3d00385f5f78",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Point(x=[2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3], y=3185.885477972998, is_evaluated=True)\n"
]
}
],
"source": [
"my_point = Point([2.3] * 10)\n",
"print(func(my_point))"
]
},
{
"cell_type": "markdown",
"id": "bf16b403-d659-40ed-92c4-3a113c81a147",
Expand All @@ -80,10 +118,138 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"id": "8106e806-b3b6-4efb-b0f7-e00c7ccecfc0",
"metadata": {},
"outputs": [],
"source": [
"# import for typehint\n",
"from optilab.data_classes import FunctionMetadata\n",
"\n",
"\n",
"class ComplexObjectiveFunction(ObjectiveFunction):\n",
" def __init__(self, dim: int, exponent: float):\n",
" # setting necessary metadata for the function\n",
" super().__init__('custom_function', dim)\n",
" self.exponent = exponent\n",
"\n",
" def get_metadata(self) -> FunctionMetadata:\n",
" metadata = super().get_metadata()\n",
" metadata.hyperparameters['exponent'] = self.exponent\n",
" return metadata\n",
"\n",
" def __call__(self, point: Point) -> Point:\n",
" # incrementing call counter\n",
" super().__call__(point)\n",
"\n",
" # actual function value calculation\n",
" return Point(\n",
" x = point.x,\n",
" y = sum([x_i ** self.exponent for x_i in point.x]),\n",
" is_evaluated = True\n",
" ) "
]
},
{
"cell_type": "markdown",
"id": "9cb613eb-5371-4b79-81dd-2fedbeabdb64",
"metadata": {},
"source": [
"Let's now evaluate out point"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "ca48ed5f-03c9-43b0-a0f5-decf18b11683",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Point(x=[2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3, 2.3], y=121.66999999999999, is_evaluated=True)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"comp = ComplexObjectiveFunction(10, 3)\n",
"comp(my_point)"
]
},
{
"cell_type": "markdown",
"id": "a3fb2415-5d48-4592-a535-f5015c314525",
"metadata": {},
"source": [
"See metadata"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "3b20f2e2-e77f-4538-82f8-4769910b1f7a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"FunctionMetadata(name='custom_function', dim=10, hyperparameters={'exponent': 3})"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"comp.get_metadata()"
]
},
{
"cell_type": "markdown",
"id": "05a2a75a-722f-4dfc-bcbb-478a6fcb1d35",
"metadata": {},
"source": [
"## Creating a custom optimizer\n",
"TODO overload init and optimize, run_optimization is already done"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "383e752d-7f08-4914-9213-79e92659f9a2",
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "expected ':' (1313679344.py, line 3)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[19], line 3\u001b[0;36m\u001b[0m\n\u001b[0;31m class CustomOptimizer\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m expected ':'\n"
]
}
],
"source": [
"from optilab.optimizers import Optimizer\n",
"\n",
"class CustomOptimizer:\n",
" def __init__(self):\n",
" pass\n",
"\n",
" def optimize(self):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "acec8a67-2fd8-48c9-bb39-5870fc465b36",
"metadata": {},
"outputs": [],
"source": []
}
],
Expand Down

0 comments on commit 646cbe9

Please sign in to comment.