{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Optimization with CSDL_alpha models\n", "\n", "*Problem:*\n", "\n", "$$\n", "\\begin{align}\n", "\\underset{x, y \\in \\mathbb{R}}{\\text{minimize}} & \\quad x^4 + y^4 \\\\\n", "\\text{subject to} & \\quad x\\geq0, \\\\\n", " & \\quad x+y=1, \\\\\n", " & \\quad x-y\\geq1.\n", "\\end{align}\n", "$$" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/modopt/modopt/external_libraries/snopt/snoptc.py:6: UserWarning: snoptc from 'optimize' could not be imported\n", " warnings.warn(\"snoptc from 'optimize' could not be imported\")\n", "/Users/modopt/modopt/external_libraries/snopt/snoptc.py:10: UserWarning: snopt7_python from 'optimize.solvers' could not be imported\n", " warnings.warn(\"snopt7_python from 'optimize.solvers' could not be imported\")\n", "/Users/modopt/modopt/external_libraries/snopt/snopt_optimizer.py:6: UserWarning: SNOPT_options from 'optimize' could not be imported\n", " warnings.warn(\"SNOPT_options from 'optimize' could not be imported\")\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Directory quartic_outputs already exists\n", "Computing gradient >>>>>>>>>>\n", "---------Computed gradient---------\n", "Computing Jacobian >>>>>>>>>>\n", "---------Computed Jacobian---------\n", "Computing objective >>>>>>>>>>\n", "---------Computed objective---------\n", "Computing constraints >>>>>>>>>>\n", "---------Computed constraints---------\n", "Computing objective >>>>>>>>>>\n", "---------Computed objective---------\n", "Computing constraints >>>>>>>>>>\n", "---------Computed constraints---------\n", "Computing objective >>>>>>>>>>\n", "---------Computed objective---------\n", "Computing constraints >>>>>>>>>>\n", "---------Computed constraints---------\n", "\n", "----------------------------------------------------------------------------\n", "Derivative type | Calc norm | FD norm | Abs error norm | Rel error norm \n", "----------------------------------------------------------------------------\n", "\n", "Gradient | 2.0100e+01 | 2.0100e+01 | 3.0000e-05 | 1.4925e-06 \n", "Jacobian | 1.4213e+00 | 1.4213e+00 | 2.8262e-10 | 1.9885e-10 \n", "----------------------------------------------------------------------------\n", "\n", "Computing constraints >>>>>>>>>>\n", "---------Computed constraints---------\n", "Computing constraints >>>>>>>>>>\n", "---------Computed constraints---------\n", "Computing objective >>>>>>>>>>\n", "---------Computed objective---------\n", "Computing gradient >>>>>>>>>>\n", "---------Computed gradient---------\n", "Computing constraints >>>>>>>>>>\n", "---------Computed constraints---------\n", "Computing constraints >>>>>>>>>>\n", "---------Computed constraints---------\n", "Computing Jacobian >>>>>>>>>>\n", "---------Computed Jacobian---------\n", "Computing Jacobian >>>>>>>>>>\n", "---------Computed Jacobian---------\n", "Computing objective >>>>>>>>>>\n", "---------Computed objective---------\n", "Computing constraints >>>>>>>>>>\n", "---------Computed constraints---------\n", "Computing constraints >>>>>>>>>>\n", "---------Computed constraints---------\n", "Computing gradient >>>>>>>>>>\n", "---------Computed gradient---------\n", "Computing Jacobian >>>>>>>>>>\n", "---------Computed Jacobian---------\n", "Computing Jacobian >>>>>>>>>>\n", "---------Computed Jacobian---------\n", "\n", " \t ==============\n", "\t Scipy summary:\n", "\t ==============\n", "\t Problem : quartic\n", "\t Solver : scipy_slsqp\n", "\t Success : True\n", "\t Message : Optimization terminated successfully\n", "\t Objective : 5.0000000000003375\n", "\t Gradient norm : 20.000000000001013\n", "\t Total time : 0.004601955413818359\n", "\t Major iterations : 2\n", "\t Total function evals : 2\n", "\t Total gradient evals : 2\n", "\t ==================================================\n" ] } ], "source": [ "import csdl_alpha as csdl\n", "\n", "# minimize x^4 + y^4 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=1.)\n", "y = csdl.Variable(name = 'y', value=1.)\n", "x.set_as_design_variable(lower = 0.0)\n", "y.set_as_design_variable(scaler=10.)\n", "\n", "# add objective\n", "z = x**4 + y**4\n", "z.add_name('z')\n", "z.set_as_objective(scaler=5.0)\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()\n", "\n", "# Create a Simulator object from the Recorder object\n", "sim = csdl.experimental.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(problem_name='quartic',simulator=sim)\n", "\n", "# Import your preferred optimizer from modopt\n", "from modopt import SQP, SLSQP, SNOPT\n", "\n", "# Setup your preferred optimizer (here, SLSQP) with the Problem object \n", "# Pass in the options for your chosen optimizer\n", "optimizer = SLSQP(prob, solver_options={'maxiter':20, 'ftol':1e-6})\n", "# optimizer = SQP(prob, maxiter=20)\n", "snopt_options = {\n", " 'Infinite bound': 1.0e20, \n", " 'Verify level': 3,\n", " 'Verbose': True\n", " }\n", "# optimizer = SNOPT(prob, solver_options=snopt_options)\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 the results of optimization\n", "optimizer.print_results()" ] } ], "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 }