{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Scalable study\n\nWe want to compare :class:`.IDF` and :class:`.MDF` formulations\nwith respect to the problem dimension for the aerostructure problem.\nFor that,\nwe use the :class:`.ScalabilityStudy` and :class:`.PostScalabilityStudy` classes.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import absolute_import, division, unicode_literals\n\nfrom future import standard_library\n\nfrom gemseo.api import configure_logger, create_discipline, create_scenario\nfrom gemseo.problems.aerostructure.aerostructure_design_space import (\n AerostructureDesignSpace,\n)\nfrom gemseo.problems.scalable.data_driven.api import (\n create_scalability_study,\n plot_scalability_results,\n)\n\nconfigure_logger()\n\nstandard_library.install_aliases()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create the disciplinary datasets\nFirst of all, we create the disciplinary :class:`.AbstractFullCache` datasets\nbased on a :class:`.DiagonalDOE`.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"disciplines = create_discipline([\"Aerodynamics\", \"Structure\", \"Mission\"])\nfor discipline in disciplines:\n discipline.set_cache_policy(\"HDF5Cache\", cache_hdf_file=\"dataset.hdf5\")\n design_space = AerostructureDesignSpace()\n design_space.filter(discipline.get_input_data_names())\n output = next(iter(discipline.get_output_data_names()))\n scenario = create_scenario(\n discipline, \"DisciplinaryOpt\", output, design_space, scenario_type=\"DOE\"\n )\n scenario.execute({\"algo\": \"DiagonalDOE\", \"n_samples\": 10})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the design problem\nThen, we instantiate a :class:`.ScalabilityStudy`\nfrom the definition of the design problem, expressed in terms of\nobjective function (to maximize or minimize),\ndesign variables (local and global)\nand constraints (equality and inequality).\nWe can also specify the coupling variables that we could scale.\nNote that this information is only required by the scaling stage.\nIndeed, MDO formulations know perfectly\nhow to automatically recognize the coupling variables.\nLastly, we can specify some properties of the scalable methodology\nsuch as the fill factor\ndescribing the level of dependence between inputs and outputs.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"study = ScalabilityStudy(\n objective=\"range\",\n design_variables=[\"thick_airfoils\", \"thick_panels\", \"sweep\"],\n eq_constraints=[\"c_rf\"],\n ineq_constraints=[\"c_lift\"],\n maximize_objective=True,\n coupling_variables=[\"forces\", \"displ\"],\n fill_factor=-1,\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Add the disciplinary datasets\nAfter instantiation, we add the disciplinary datasets\nused for the construction of the different scalable disciplines.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"study.add_discipline(\"Aerodynamics\", HDF5Cache(\"dataset.hdf5\", \"Aerodynamics\"))\nstudy.add_discipline(\"Structure\", HDF5Cache(\"dataset.hdf5\", \"Structure\"))\nstudy.add_discipline(\"Mission\", HDF5Cache(\"dataset.hdf5\", \"Mission\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Add the optimization strategies\nThen, we define the different optimization strategies we want to compare:\nIn this case, the strategies are:\n\n- :class:`.MDF` formulation with the :code:`\"NLOPT_SLSQP\"` optimization algorithm\n and no more than 100 iterations,\n- :class:`.IDF` formulation with the :code:`\"NLOPT_SLSQP\"` optimization algorithm\n and no more than 100 iterations,\n\nNote that in this case, we compare MDO formulations\nbut we could easily compare optimization algorithms.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"study.add_optimization_strategy(\"NLOPT_SLSQP\", 100, \"MDF\")\nstudy.add_optimization_strategy(\"NLOPT_SLSQP\", 100, \"IDF\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Add the scaling strategy\nAfter that, we define the different scaling strategies\nfor which we want to compare the optimization strategies.\nIn this case, the strategies are:\n\n1. All design parameters have a size equal to 1,\n2. All design parameters have a size equal to 10,\n3. All design parameters have a size equal to 20.\n\nTo do that, we pass :code:`design_size=[1, 10, 20]`\nto the :meth:`.ScalabilityStudy.add_scaling_strategies` method.\n:code:`design_size` expects either:\n\n- a list of integer where the ith component is the size for the ith scaling strategy,\n- an integer changing the fixed size (if :code:`None`, use the original size).\n\nNote that we could also compare the optimization strategies while\n\n- varying the size of the different coupling variables (use :code:`coupling_size`),\n- varying the size of the different equality constraints (use :code:`eq_size`),\n- varying the size of the different inequality constraints (use :code:`ineq_size`),\n- varying the size of any variable (use :code:`variables`),\n\nwhere the corresponding arguments works in the same way as :code:`design_size`,\nexcept for :code:`variables` which expects a list of dictionary\nwhose keys are variables names and values are variables sizes.\nIn this way, we can use this argument to fine-tune a scaling strategy\nto very specific variables, e.g. local variables.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"study.add_scaling_strategies(design_size=[1, 10, 20])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Execute the scalable study\nThen, we execute the scalability study,\ni.e. to build and execute a :class:`.ScalableProblem`\nfor each optimization strategy and each scaling strategy,\nand repeat it 10 times in order to get statistics on the results\n(because the :class:`.ScalableDiagonalModel` relies on stochastic features.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"study.execute(n_replicates=10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Look at the dependency matrices\nHere are the dependency matrices obtained with the 1st replicate when\n:code:`design_size=10`.\n\n### Aerodynamics\n\n\n### Structure\n\n\n### Mission\n\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Look at optimization histories\nHere are the optimization histories obtained with the 1st replicate when\n:code:`design_size=10`, where the left side represents the :class:`.MDF` formulation\nwhile the right one represents the :class:`.IDF` formulation.\n\n### Objective function\n\n\n\n\n### Design variables\n\n\n\n\n### Equality constraints\n\n\n\n\n### Inequality constraints\n\n\n\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Post-process the results\nLastly, we plot the results.\nBecause of the replicates,\nthe latter are not displayed as one line per optimization strategy w.r.t. scaling strategy,\nbut as one series of boxplots per optimization strategy w.r.t. scaling strategy,\nwhere the boxplots represents the variability due to the 10 replicates.\nIn this case, it seems that\nthe :class:`.MDF` formulation is more expensive than the :class:`.IDF` one\nwhen the design space dimension increases\nwhile they seems to be the same when each design parameter has a size equal to 1.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"post = PostScalabilityStudy(\"study\")\npost.labelize_scaling_strategy(\"Number of design parameters per type.\")\npost.plot(\n xmargin=3.0, xticks=[1.0, 10.0, 20.0], xticks_labels=[\"1\", \"10\", \"20\"], widths=1.0\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}