{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CSDL_alpha\n", "\n", "## Define a problem in `CSDL_alpha`\n", "\n", "This example does not intend to cover all the features of *CSDL_alpha*.\n", "For more details and tutorials on *CSDL_alpha*, please refer to **[CSDL_alpha documentation](https://csdl-alpha.readthedocs.io)**.\n", "In this example, we solve a constrained problem given by\n", "\n", "$$\n", "\\underset{x_1, x_2 \\in \\mathbb{R}}{\\text{minimize}} \\quad x_1^2 + x_2^2\n", "\n", "\\newline\n", "\\text{subject to} \\quad x_1 \\geq 0\n", "\\newline\n", "\\quad \\quad \\quad \\quad x_1 + x_2 = 1\n", "\\newline\n", "\\quad \\quad \\quad \\quad x_1 - x_2 \\geq 1\n", "$$\n", "\n", "We know the solution of this problem is $x_1=1$, and $x_2=0$.\n", "However, we start from an initial guess of $x_1=500.0$, and $x_2=5.0$ for the purposes of this tutorial.\n", "\n", "The problem model is written in *CSDL_alpha* as follows:\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import csdl_alpha as csdl\n", "\n", "# minimize x^2 + y^2 subject to x>=0, x+y=1, x-y>=1.\n", "\n", "rec = csdl.Recorder()\n", "rec.start()\n", "\n", "# add design variables\n", "x = csdl.Variable(name = 'x', value=500.)\n", "y = csdl.Variable(name = 'y', value=5.)\n", "x.set_as_design_variable(lower = 0.0)\n", "y.set_as_design_variable()\n", "\n", "# add objective\n", "z = x**2 + y**2\n", "z.add_name('z')\n", "z.set_as_objective()\n", "\n", "# add constraints\n", "constraint_1 = x + y\n", "constraint_2 = x - y\n", "constraint_1.add_name('constraint_1')\n", "constraint_2.add_name('constraint_2')\n", "constraint_1.set_as_constraint(lower=1., upper=1.)\n", "constraint_2.set_as_constraint(lower=1.)\n", "\n", "rec.stop()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once your model is defined within *CSDL_alpha*'s `Recorder` object, \n", "create a `Simulator` object from it. \n", "Lastly, translate the `Simulator` object to a `CSDLAlphaProblem` object \n", "for use in modOpt." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from csdl_alpha.experimental import PySimulator, JaxSimulator\n", "\n", "# Create a Simulator object from the Recorder object\n", "sim = PySimulator(rec)\n", "\n", "# Import CSDLAlphaProblem from modopt\n", "from modopt import CSDLAlphaProblem\n", "\n", "# Instantiate your problem using the csdl Simulator object and name your problem\n", "prob = CSDLAlphaProblem(\n", " problem_name='quadratic',\n", " simulator=sim,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solve your problem using an optimizer\n", "\n", "Once your problem model is wrapped for modOpt, import your preferred optimizer\n", "from modOpt and solve it, following the standard procedure.\n", "Here we will use the `SLSQP` optimizer from the SciPy library." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "----------------------------------------------------------------------------\n", "Derivative type | Calc norm | FD norm | Abs error norm | Rel error norm \n", "----------------------------------------------------------------------------\n", "\n", "Gradient | 1.0000e+03 | 1.0000e+03 | 1.5473e-05 | 1.5472e-08 \n", "Jacobian | 2.0000e+00 | 2.0000e+00 | 5.0495e-09 | 2.5248e-09 \n", "----------------------------------------------------------------------------\n", "\n", "\n", "\tSolution from Scipy SLSQP:\n", "\t----------------------------------------------------------------------------------------------------\n", "\tProblem : quadratic\n", "\tSolver : scipy-slsqp\n", "\tSuccess : True\n", "\tMessage : Optimization terminated successfully\n", "\tStatus : 0\n", "\tTotal time : 0.006938934326171875\n", "\tObjective : 1.0000000068019972\n", "\tGradient norm : 2.000000006801997\n", "\tTotal function evals : 2\n", "\tTotal gradient evals : 2\n", "\tMajor iterations : 2\n", "\tTotal callbacks : 17\n", "\tReused callbacks : 0\n", "\tobj callbacks : 5\n", "\tgrad callbacks : 3\n", "\thess callbacks : 0\n", "\tcon callbacks : 6\n", "\tjac callbacks : 3\n", "\t----------------------------------------------------------------------------------------------------\n" ] } ], "source": [ "from modopt import SLSQP\n", "\n", "# Setup your preferred optimizer (SLSQP) with the Problem object \n", "# Pass in the options for your chosen optimizer\n", "optimizer = SLSQP(prob, solver_options={'maxiter':20})\n", "\n", "# Check first derivatives at the initial guess, if needed\n", "optimizer.check_first_derivatives(prob.x0)\n", "\n", "# Solve your optimization problem\n", "optimizer.solve()\n", "\n", "# Print results of optimization\n", "optimizer.print_results()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Scaling API\n", "\n", "Please refer to the code snippet below as a guide for scaling \n", "the design variables, objective, and constraints independent \n", "of their definitions.\n", "```{warning}\n", "The results provided by the optimizer will always be scaled,\n", "while the values from the models will remain unscaled.\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# add design variables\n", "x.set_as_design_variable(lower = 0.0, scaler=2.)\n", "y.set_as_design_variable(scaler=2.)\n", "\n", "# add objective\n", "z.set_as_objective(scaler=5.)\n", "\n", "# add constraints\n", "constraint_1.set_as_constraint(lower=1., upper=1., scaler=10.)\n", "constraint_2.set_as_constraint(lower=1., scaler=100.)" ] } ], "metadata": { "kernelspec": { "display_name": "venv", "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.13" } }, "nbformat": 4, "nbformat_minor": 2 }