PK VWB" " # plot_sobieski_bilevel_example.ipynb{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# BiLevel-based MDO on the Sobieski SSBJ test case\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import annotations\n\nfrom copy import deepcopy\nfrom logging import WARNING\n\nfrom gemseo import configure_logger\nfrom gemseo import create_discipline\nfrom gemseo import create_scenario\nfrom gemseo import execute_post\nfrom gemseo.problems.sobieski.core.design_space import SobieskiDesignSpace\n\nconfigure_logger()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instantiate the disciplines\nFirst, we instantiate the four disciplines of the use case:\n:class:`.SobieskiPropulsion`,\n:class:`.SobieskiAerodynamics`,\n:class:`.SobieskiMission`\nand :class:`.SobieskiStructure`.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"propu, aero, mission, struct = create_discipline([\n \"SobieskiPropulsion\",\n \"SobieskiAerodynamics\",\n \"SobieskiMission\",\n \"SobieskiStructure\",\n])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Build, execute and post-process the scenario\nThen, we build the scenario which links the disciplines\nwith the formulation and the optimization algorithm. Here, we use the\n:class:`.BiLevel` formulation. We tell the scenario to minimize -y_4\ninstead of minimizing y_4 (range), which is the default option.\n\nWe need to define the design space.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"design_space = SobieskiDesignSpace()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, we build a sub-scenario for each strongly coupled disciplines,\nusing the following algorithm, maximum number of iterations and\nalgorithm options:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"algo_options = {\n \"xtol_rel\": 1e-7,\n \"xtol_abs\": 1e-7,\n \"ftol_rel\": 1e-7,\n \"ftol_abs\": 1e-7,\n \"ineq_tolerance\": 1e-4,\n}\nsub_sc_opts = {\"max_iter\": 30, \"algo\": \"SLSQP\", \"algo_options\": algo_options}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Build a sub-scenario for Propulsion\nThis sub-scenario will minimize SFC.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sc_prop = create_scenario(\n propu,\n \"DisciplinaryOpt\",\n \"y_34\",\n design_space=deepcopy(design_space).filter(\"x_3\"),\n name=\"PropulsionScenario\",\n)\nsc_prop.default_inputs = sub_sc_opts\nsc_prop.add_constraint(\"g_3\", constraint_type=\"ineq\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Build a sub-scenario for Aerodynamics\nThis sub-scenario will minimize L/D.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sc_aero = create_scenario(\n aero,\n \"DisciplinaryOpt\",\n \"y_24\",\n deepcopy(design_space).filter(\"x_2\"),\n name=\"AerodynamicsScenario\",\n maximize_objective=True,\n)\nsc_aero.default_inputs = sub_sc_opts\nsc_aero.add_constraint(\"g_2\", constraint_type=\"ineq\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Build a sub-scenario for Structure\nThis sub-scenario will maximize\nlog(aircraft total weight / (aircraft total weight - fuel weight)).\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sc_str = create_scenario(\n struct,\n \"DisciplinaryOpt\",\n \"y_11\",\n deepcopy(design_space).filter(\"x_1\"),\n name=\"StructureScenario\",\n maximize_objective=True,\n)\nsc_str.add_constraint(\"g_1\", constraint_type=\"ineq\")\nsc_str.default_inputs = sub_sc_opts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Build a scenario for Mission\nThis scenario is based on the three previous sub-scenarios and on the\nMission and aims to maximize the range (Breguet).\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sub_disciplines = [sc_prop, sc_aero, sc_str, mission]\ndesign_space = deepcopy(design_space).filter(\"x_shared\")\nsystem_scenario = create_scenario(\n sub_disciplines,\n \"BiLevel\",\n \"y_4\",\n design_space,\n apply_cstr_tosub_scenarios=False,\n parallel_scenarios=False,\n multithread_scenarios=True,\n tolerance=1e-14,\n max_mda_iter=30,\n maximize_objective=True,\n sub_scenarios_log_level=WARNING,\n)\nsystem_scenario.add_constraint([\"g_1\", \"g_2\", \"g_3\"], \"ineq\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Visualize the XDSM\nGenerate the XDSM on the fly:\n\n- ``log_workflow_status=True`` will log the status of the workflow in the console,\n- ``save_html`` (default ``True``) will generate a self-contained HTML file,\n that can be automatically opened using ``show_html=True``.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"system_scenario.xdsmize(save_html=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Execute the main scenario\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"system_scenario.execute({\n \"max_iter\": 50,\n \"algo\": \"NLOPT_COBYLA\",\n \"algo_options\": algo_options,\n})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the history of the MDA residuals\nFor the first MDA:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"system_scenario.formulation.mda1.plot_residual_history(save=False, show=True)\n# For the second MDA:\nsystem_scenario.formulation.mda2.plot_residual_history(save=False, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the system optimization history view\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"system_scenario.post_process(\"OptHistoryView\", save=False, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the structure optimization histories of the 2 first iterations\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"struct_databases = system_scenario.formulation.scenario_adapters[2].databases\nfor database in struct_databases[:2]:\n opt_problem = deepcopy(sc_str.formulation.opt_problem)\n opt_problem.database = database\n execute_post(opt_problem, \"OptHistoryView\", save=False, show=True)\n\nfor disc in [propu, aero, mission, struct]:\n print(f\"{disc.name}: {disc.n_calls} calls.\")"
]
}
],
"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.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK VW
, , ' plot_doe_sobieski_bilevel_example.ipynb{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# BiLevel-based DOE on the Sobieski SSBJ test case\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import annotations\n\nfrom copy import deepcopy\nfrom os import name as os_name\n\nfrom gemseo import configure_logger\nfrom gemseo import create_discipline\nfrom gemseo import create_scenario\nfrom gemseo.problems.sobieski.core.design_space import SobieskiDesignSpace\n\nconfigure_logger()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instantiate the disciplines\nFirst, we instantiate the four disciplines of the use case:\n:class:`.SobieskiPropulsion`,\n:class:`.SobieskiAerodynamics`,\n:class:`.SobieskiMission`\nand :class:`.SobieskiStructure`.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"propu, aero, mission, struct = create_discipline([\n \"SobieskiPropulsion\",\n \"SobieskiAerodynamics\",\n \"SobieskiMission\",\n \"SobieskiStructure\",\n])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Build, execute and post-process the scenario\nThen, we build the scenario which links the disciplines\nwith the formulation and the optimization algorithm. Here, we use the\n:class:`.BiLevel` formulation. We tell the scenario to minimize -y_4\ninstead of minimizing y_4 (range), which is the default option.\n\nWe need to define the design space.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"design_space = SobieskiDesignSpace()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, we build a sub-scenario for each strongly coupled disciplines,\nusing the following algorithm, maximum number of iterations and\nalgorithm options:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"algo_options = {\n \"xtol_rel\": 1e-7,\n \"xtol_abs\": 1e-7,\n \"ftol_rel\": 1e-7,\n \"ftol_abs\": 1e-7,\n \"ineq_tolerance\": 1e-4,\n}\nsub_sc_opts = {\"max_iter\": 30, \"algo\": \"SLSQP\", \"algo_options\": algo_options}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Build a sub-scenario for Propulsion\nThis sub-scenario will minimize SFC.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sc_prop = create_scenario(\n propu,\n \"DisciplinaryOpt\",\n \"y_34\",\n design_space=deepcopy(design_space).filter(\"x_3\"),\n name=\"PropulsionScenario\",\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Build a sub-scenario for Aerodynamics\nThis sub-scenario will minimize L/D.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sc_aero = create_scenario(\n aero,\n \"DisciplinaryOpt\",\n \"y_24\",\n deepcopy(design_space).filter(\"x_2\"),\n name=\"AerodynamicsScenario\",\n maximize_objective=True,\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Build a sub-scenario for Structure\nThis sub-scenario will maximize\nlog(aircraft total weight / (aircraft total weight - fuel weight)).\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sc_str = create_scenario(\n struct,\n \"DisciplinaryOpt\",\n \"y_11\",\n deepcopy(design_space).filter(\"x_1\"),\n name=\"StructureScenario\",\n maximize_objective=True,\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Build a scenario for Mission\nThis scenario is based on the three previous sub-scenarios and on the\nMission and aims to maximize the range (Breguet).\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sub_disciplines = [sc_prop, sc_aero, sc_str, mission]\ndesign_space = deepcopy(design_space).filter(\"x_shared\")\nsystem_scenario = create_scenario(\n sub_disciplines,\n \"BiLevel\",\n \"y_4\",\n design_space,\n parallel_scenarios=False,\n reset_x0_before_opt=True,\n scenario_type=\"DOE\",\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Note
Setting ``reset_x0_before_opt=True`` is mandatory when doing a DOE\n in parallel. If we want reproducible results, don't reuse previous xopt.
\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"system_scenario.formulation.mda1.warm_start = False\nsystem_scenario.formulation.mda2.warm_start = False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note
This is mandatory when doing a DOE in parallel if we want always exactly\n the same results, don't warm start mda1 to have exactly the same\n process whatever the execution order and process dispatch.
\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for sub_sc in sub_disciplines[0:3]:\n sub_sc.default_inputs = {\"max_iter\": 20, \"algo\": \"L-BFGS-B\"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Visualize the XDSM\nGenerate the XDSM on the fly:\n\n- ``log_workflow_status=True`` will log the status of the workflow in the console,\n- ``save_html`` (default ``True``) will generate a self-contained HTML file,\n that can be automatically opened using ``show_html=True``.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"system_scenario.xdsmize(save_html=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Multiprocessing\nIt is possible to run a DOE in parallel using multiprocessing, in order to do\nthis, we specify the number of processes to be used for the computation of\nthe samples.\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Warning
The multiprocessing option has some limitations on Windows.\n Due to problems with sphinx, we disable it in this example.\n The features :class:`.MemoryFullCache` and :class:`.HDF5Cache` are not\n available for multiprocessing on Windows.\n As an alternative, we recommend the method\n :meth:`.DOEScenario.set_optimization_history_backup`.
\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"system_scenario.execute({\n \"n_samples\": 30,\n \"algo\": \"lhs\",\n \"algo_options\": {\"n_processes\": 1 if os_name == \"nt\" else 4},\n})\n\nsystem_scenario.print_execution_metrics()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Warning
On Windows, the progress bar may show duplicated instances during the\n initialization of each subprocess. In some cases it may also print the\n conclusion of an iteration ahead of another one that was concluded first.\n This is a consequence of the pickling process and does not affect the\n computations of the scenario.
\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exporting the problem data.\nAfter the execution of the scenario, you may want to export your data to use it\nelsewhere. The method :meth:`.Scenario.to_dataset` will allow you to export\nyour results to a :class:`.Dataset`, the basic |g| class to store data.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"dataset = system_scenario.to_dataset(\"a_name_for_my_dataset\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the optimization history view\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"system_scenario.post_process(\"OptHistoryView\", save=False, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the scatter matrix\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"system_scenario.post_process(\n \"ScatterPlotMatrix\",\n variable_names=[\"y_4\", \"x_shared\"],\n save=False,\n show=True,\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot parallel coordinates\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"system_scenario.post_process(\"ParallelCoordinates\", save=False, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot correlations\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"system_scenario.post_process(\"Correlations\", save=False, show=True)"
]
}
],
"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.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK VW4x plot_sobieski_idf_example.ipynb{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# IDF-based MDO on the Sobieski SSBJ test case\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import annotations\n\nfrom gemseo import configure_logger\nfrom gemseo import create_discipline\nfrom gemseo import create_scenario\nfrom gemseo import generate_n2_plot\nfrom gemseo.problems.sobieski.core.design_space import SobieskiDesignSpace\n\nconfigure_logger()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instantiate the disciplines\nFirst, we instantiate the four disciplines of the use case:\n:class:`.SobieskiPropulsion`,\n:class:`.SobieskiAerodynamics`,\n:class:`.SobieskiMission`\nand :class:`.SobieskiStructure`.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"disciplines = create_discipline([\n \"SobieskiPropulsion\",\n \"SobieskiAerodynamics\",\n \"SobieskiMission\",\n \"SobieskiStructure\",\n])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can quickly access the most relevant information of any discipline (name, inputs,\nand outputs) with Python's ``print()`` function. Moreover, we can get the default\ninput values of a discipline with the attribute :attr:`.MDODiscipline.default_inputs`\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for discipline in disciplines:\n print(discipline)\n print(f\"Default inputs: {discipline.default_inputs}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You may also be interested in plotting the couplings of your disciplines.\nA quick way of getting this information is the API function\n:func:`.generate_n2_plot`. A much more detailed explanation of coupling\nvisualization is available `here `.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"generate_n2_plot(disciplines, save=False, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Build, execute and post-process the scenario\nThen, we build the scenario which links the disciplines\nwith the formulation and the optimization algorithm. Here, we use the\n:class:`.IDF` formulation. We tell the scenario to minimize -y_4 instead of\nminimizing y_4 (range), which is the default option.\n\n### Instantiate the scenario\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"design_space = SobieskiDesignSpace()\ndesign_space"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario = create_scenario(\n disciplines,\n \"IDF\",\n objective_name=\"y_4\",\n design_space=design_space,\n maximize_objective=True,\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Set the design constraints\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for c_name in [\"g_1\", \"g_2\", \"g_3\"]:\n scenario.add_constraint(c_name, \"ineq\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Visualize the XDSM\nGenerate the XDSM file on the fly:\n\n- ``log_workflow_status=True`` will log the status of the workflow in the console,\n- ``save_html`` (default ``True``) will generate a self-contained HTML file,\n that can be automatically opened using ``show_html=True``.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.xdsmize(save_html=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define the algorithm inputs\nWe set the maximum number of iterations, the optimizer\nand the optimizer options\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"algo_options = {\n \"ftol_rel\": 1e-10,\n \"ineq_tolerance\": 1e-3,\n \"eq_tolerance\": 1e-3,\n \"normalize_design_space\": True,\n}\nscn_inputs = {\"max_iter\": 20, \"algo\": \"SLSQP\", \"algo_options\": algo_options}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Execute the scenario\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.execute(scn_inputs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Save the optimization history\nWe can save the whole optimization problem and its history for further post\nprocessing:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.save_optimization_history(\"idf_history.h5\", file_format=\"hdf5\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also save only calls to functions and design variables history:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.save_optimization_history(\"idf_history.xml\", file_format=\"ggobi\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Print optimization metrics\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.print_execution_metrics()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the optimization history view\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\"OptHistoryView\", save=True, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the quadratic approximation of the objective\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\"QuadApprox\", function=\"-y_4\", save=False, show=True)"
]
}
],
"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.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK VWO%&' &' # plot_doe_sobieski_mdf_example.ipynb{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# MDF-based DOE on the Sobieski SSBJ test case\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import annotations\n\nfrom os import name as os_name\n\nfrom gemseo import configure_logger\nfrom gemseo import create_discipline\nfrom gemseo import create_scenario\nfrom gemseo import generate_n2_plot\nfrom gemseo.problems.sobieski.core.design_space import SobieskiDesignSpace\n\nconfigure_logger()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instantiate the disciplines\nFirst, we instantiate the four disciplines of the use case:\n:class:`.SobieskiPropulsion`,\n:class:`.SobieskiAerodynamics`,\n:class:`.SobieskiMission`\nand :class:`.SobieskiStructure`.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"disciplines = create_discipline([\n \"SobieskiPropulsion\",\n \"SobieskiAerodynamics\",\n \"SobieskiMission\",\n \"SobieskiStructure\",\n])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can quickly access the most relevant information of any discipline (name, inputs,\nand outputs) with Python's ``print()`` function. Moreover, we can get the default\ninput values of a discipline with the attribute :attr:`.MDODiscipline.default_inputs`\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for discipline in disciplines:\n print(discipline)\n print(f\"Default inputs: {discipline.default_inputs}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You may also be interested in plotting the couplings of your disciplines.\nA quick way of getting this information is the high-level function\n:func:`.generate_n2_plot`. A much more detailed explanation of coupling\nvisualization is available `here `.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"generate_n2_plot(disciplines, save=False, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Build, execute and post-process the scenario\nThen, we build the scenario which links the disciplines\nwith the formulation and the optimization algorithm. Here, we use the\n:class:`.BiLevel` formulation. We tell the scenario to minimize -y_4\ninstead of minimizing y_4 (range), which is the default option.\n\nWe need to define the design space.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"design_space = SobieskiDesignSpace()\ndesign_space"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Instantiate the scenario\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario = create_scenario(\n disciplines,\n formulation=\"MDF\",\n objective_name=\"y_4\",\n design_space=design_space,\n maximize_objective=True,\n scenario_type=\"DOE\",\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Set the design constraints\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for constraint in [\"g_1\", \"g_2\", \"g_3\"]:\n scenario.add_constraint(constraint, \"ineq\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Visualize the XDSM\nGenerate the XDSM on the fly:\n\n- ``log_workflow_status=True`` will log the status of the workflow in the console,\n- ``save_html`` (default ``True``) will generate a self-contained HTML file,\n that can be automatically opened using ``show_html=True``.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.xdsmize(save_html=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Execute the scenario\nUse provided analytic derivatives\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.set_differentiation_method()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Multiprocessing\nIt is possible to run a DOE in parallel using multiprocessing, in order to do\nthis, we specify the number of processes to be used for the computation of\nthe samples.\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Warning
The multiprocessing option has some limitations on Windows.\n Due to problems with sphinx, we disable it in this example.\n The features :class:`.MemoryFullCache` and :class:`.HDF5Cache` are not\n available for multiprocessing on Windows.\n As an alternative, we recommend the method\n :meth:`.DOEScenario.set_optimization_history_backup`.
\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We define the algorithm options. Here the criterion = center option of pyDOE\ncenters the points within the sampling intervals.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"algo_options = {\n \"criterion\": \"center\",\n # Evaluate gradient of the MDA\n # with coupled adjoint\n \"eval_jac\": True,\n # Run in parallel on 1 or 4 processors\n \"n_processes\": 1 if os_name == \"nt\" else 4,\n}\n\nscenario.execute({\"n_samples\": 30, \"algo\": \"lhs\", \"algo_options\": algo_options})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Warning
On Windows, the progress bar may show duplicated instances during the\n initialization of each subprocess. In some cases it may also print the\n conclusion of an iteration ahead of another one that was concluded first.\n This is a consequence of the pickling process and does not affect the\n computations of the scenario.
\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exporting the problem data.\nAfter the execution of the scenario, you may want to export your data to use it\nelsewhere. The method :meth:`.Scenario.to_dataset` will allow you to export\nyour results to a :class:`.Dataset`, the basic |g| class to store data.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"dataset = scenario.to_dataset(\"a_name_for_my_dataset\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the optimization history view\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\"OptHistoryView\", save=False, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. tip::\n\n Each post-processing method requires different inputs and offers a variety\n of customization options. Use the high-level function\n :func:`.get_post_processing_options_schema` to print a table with\n the attributes for any post-processing algo. Or refer to our dedicated page:\n `gen_post_algos`.\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the scatter matrix\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\n \"ScatterPlotMatrix\",\n save=False,\n show=True,\n variable_names=[\"y_4\", \"x_shared\"],\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot correlations\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\"Correlations\", save=False, show=True)"
]
}
],
"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.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK VWie7 e7 plot_sobieski_mdf_example.ipynb{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# MDF-based MDO on the Sobieski SSBJ test case\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import annotations\n\nfrom gemseo import configure_logger\nfrom gemseo import create_discipline\nfrom gemseo import create_scenario\nfrom gemseo import generate_n2_plot\nfrom gemseo.problems.sobieski.core.design_space import SobieskiDesignSpace\n\nconfigure_logger()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instantiate the disciplines\nFirst, we instantiate the four disciplines of the use case:\n:class:`.SobieskiPropulsion`,\n:class:`.SobieskiAerodynamics`,\n:class:`.SobieskiMission`\nand :class:`.SobieskiStructure`.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"disciplines = create_discipline([\n \"SobieskiPropulsion\",\n \"SobieskiAerodynamics\",\n \"SobieskiMission\",\n \"SobieskiStructure\",\n])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can quickly access the most relevant information of any discipline (name, inputs,\nand outputs) with Python's ``print()`` function. Moreover, we can get the default\ninput values of a discipline with the attribute :attr:`.MDODiscipline.default_inputs`\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for discipline in disciplines:\n print(discipline)\n print(f\"Default inputs: {discipline.default_inputs}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You may also be interested in plotting the couplings of your disciplines.\nA quick way of getting this information is the high-level function\n:func:`.generate_n2_plot`. A much more detailed explanation of coupling\nvisualization is available `here `.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"generate_n2_plot(disciplines, save=False, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Build, execute and post-process the scenario\nThen, we build the scenario which links the disciplines\nwith the formulation and the optimization algorithm. Here, we use the\n:class:`.MDF` formulation. We tell the scenario to minimize -y_4 instead of\nminimizing y_4 (range), which is the default option.\n\n### Instantiate the scenario\nDuring the instantiation of the scenario, we provide some options for the\nMDF formulations:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"formulation_options = {\n \"tolerance\": 1e-14,\n \"max_mda_iter\": 50,\n \"warm_start\": True,\n \"use_lu_fact\": False,\n \"linear_solver_tolerance\": 1e-14,\n}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- ``'warm_start``: warm starts MDA,\n- ``'warm_start``: optimize the adjoints resolution by storing\n the Jacobian matrix LU factorization for the multiple RHS\n (objective + constraints). This saves CPU time if you can pay for\n the memory and have the full Jacobians available, not just matrix vector\n products.\n- ``'linear_solver_tolerance'``: set the linear solver tolerance,\n idem we need full convergence\n\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"design_space = SobieskiDesignSpace()\ndesign_space"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario = create_scenario(\n disciplines,\n \"MDF\",\n objective_name=\"y_4\",\n design_space=design_space,\n maximize_objective=True,\n **formulation_options,\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Set the design constraints\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"for c_name in [\"g_1\", \"g_2\", \"g_3\"]:\n scenario.add_constraint(c_name, \"ineq\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### XDSMIZE the scenario\nGenerate the XDSM file on the fly:\n\n- ``log_workflow_status=True`` will log the status of the workflow in the console,\n- ``save_html`` (default ``True``) will generate a self-contained HTML file,\n that can be automatically opened using ``show_html=True``.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.xdsmize(save_html=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define the algorithm inputs\nWe set the maximum number of iterations, the optimizer\nand the optimizer options. Algorithm specific options are passed there.\nUse the high-level function :func:`.get_algorithm_options_schema`\nfor more information or read the documentation.\n\nHere ftol_rel option is a stop criteria based on the relative difference\nin the objective between two iterates ineq_tolerance the tolerance\ndetermination of the optimum; this is specific to the |g| wrapping and not\nin the solver.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"algo_options = {\n \"ftol_rel\": 1e-10,\n \"ineq_tolerance\": 2e-3,\n \"normalize_design_space\": True,\n}\nscn_inputs = {\"max_iter\": 15, \"algo\": \"SLSQP\", \"algo_options\": algo_options}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. seealso::\n\n We can also generate a backup file for the optimization,\n as well as plots on the fly of the optimization history if option\n ``generate_opt_plot`` is ``True``.\n This slows down a lot the process, here since SSBJ is very light\n\n .. code::\n\n scenario.set_optimization_history_backup(file_path=\"mdf_backup.h5\",\n each_new_iter=True,\n each_store=False, erase=True,\n pre_load=False,\n generate_opt_plot=True)\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Execute the scenario\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.execute(scn_inputs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Save the optimization history\nWe can save the whole optimization problem and its history for further post\nprocessing:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.save_optimization_history(\"mdf_history.h5\", file_format=\"hdf5\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also save only calls to functions and design variables history:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.save_optimization_history(\"mdf_history.xml\", file_format=\"ggobi\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Print optimization metrics\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.print_execution_metrics()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Post-process the results\n\n### Plot the optimization history view\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\"OptHistoryView\", save=False, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the basic history view\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\n \"BasicHistory\", variable_names=[\"x_shared\"], save=False, show=True\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the constraints and objective history\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\"ObjConstrHist\", save=False, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the constraints history\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\n \"ConstraintsHistory\",\n constraint_names=[\"g_1\", \"g_2\", \"g_3\"],\n save=False,\n show=True,\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the constraints history using a radar chart\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\n \"RadarChart\",\n constraint_names=[\"g_1\", \"g_2\", \"g_3\"],\n save=False,\n show=True,\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the quadratic approximation of the objective\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\"QuadApprox\", function=\"-y_4\", save=False, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the functions using a SOM\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\"SOM\", save=False, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the scatter matrix of variables of interest\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\n \"ScatterPlotMatrix\",\n variable_names=[\"-y_4\", \"g_1\"],\n save=False,\n show=True,\n fig_size=(14, 14),\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the variables using the parallel coordinates\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\"ParallelCoordinates\", save=False, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the robustness of the solution\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\"Robustness\", save=True, show=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plot the influence of the design variables\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario.post_process(\"VariableInfluence\", fig_size=(14, 14), save=False, show=True)"
]
}
],
"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.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK VWB" " # plot_sobieski_bilevel_example.ipynbPK VW
, , ' !# plot_doe_sobieski_bilevel_example.ipynbPK VW4x EP plot_sobieski_idf_example.ipynbPK VWO%&' &' # mp plot_doe_sobieski_mdf_example.ipynbPK VWie7 e7 ԗ plot_sobieski_mdf_example.ipynbPK v