.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/workflow_creation/02_3_oscillator_optimization_on_EA.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_workflow_creation_02_3_oscillator_optimization_on_EA.py: .. _ref_oscillator_optimization_on_EA_create_workflow: Oscillator optimization using EA flow ------------------------------------- This example demonstrates how to create and run a direct optimization. It creates a direct optimization workflow for an oscillator with the Nature Inspired Optimization (Evolutionary Algorithm) using pyOptiSLang. It then explains how you can optionally save the project to a desired location. .. GENERATED FROM PYTHON SOURCE LINES 37-40 Perform required imports ~~~~~~~~~~~~~~~~~~~~~~~~ Perform the required imports. .. GENERATED FROM PYTHON SOURCE LINES 40-58 .. code-block:: Python import os from pathlib import Path from ansys.optislang.core import Optislang import ansys.optislang.core.node_types as node_types from ansys.optislang.core.nodes import DesignFlow, IntegrationNode, ParametricSystem from ansys.optislang.core.project_parametric import ( ComparisonType, ConstraintCriterion, DistributionType, MixedParameter, ObjectiveCriterion, StochasticParameter, ) example_files_path = Path(os.environ["OSL_EXAMPLES"]) / "00_run_script" / "oscillator" / "files" .. GENERATED FROM PYTHON SOURCE LINES 59-62 Create optiSLang instance ~~~~~~~~~~~~~~~~~~~~~~~~~ Create the optiSLang instance. .. GENERATED FROM PYTHON SOURCE LINES 62-66 .. code-block:: Python osl = Optislang(ini_timeout=60) print(osl) .. GENERATED FROM PYTHON SOURCE LINES 67-69 Create workflow ~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 69-160 .. code-block:: Python root_system = osl.application.project.root_system # Create EA optimizer system and postprocessing noa2: ParametricSystem = root_system.create_node(type_=node_types.NOA2) omdb_path_slot = noa2.get_output_slots("OMDBPath")[0] post_processing = root_system.create_node(type_=node_types.Postprocessing) imdb_path_slot = post_processing.get_input_slots("IMDBPath")[0] omdb_path_slot.connect_to(imdb_path_slot) # Create oscillator python_node: IntegrationNode = noa2.create_node( type_=node_types.Python2, design_flow=DesignFlow.RECEIVE_SEND ) python_node.set_property( name="Path", value={ "path": { "base_path_mode": {"value": "ABSOLUTE_PATH"}, "split_path": {"head": "", "tail": str(example_files_path / "oscillator.py")}, } }, ) python_node.set_property(name="MaxParallel", value=4) python_node.register_location_as_parameter( location="m", reference_value={ "kind": {"value": "scalar"}, "scalar": {"real": 2.0, "imag": 0.0}, }, ) python_node.register_location_as_parameter(location="k", reference_value=20) python_node.register_location_as_parameter( location="D", reference_value={ "kind": {"value": "scalar"}, "scalar": {"real": 0.02, "imag": 0.0}, }, ) python_node.register_location_as_parameter(location="Ekin", reference_value=10) python_node.register_location_as_response(location="omega_damped", reference_value=4.47124) python_node.register_location_as_response(location="x_max", reference_value=0.62342) # Modify registered parameters noa2.parameter_manager.modify_parameter( MixedParameter( name="k", reference_value=20.0, range=(10, 50), distribution_type=DistributionType.NORMAL, distribution_parameters=(20.0, 1.0), ) ) noa2.parameter_manager.modify_parameter( StochasticParameter( name="Ekin", reference_value=10.0, distribution_type=DistributionType.NORMAL, distribution_parameters=(0.02, 0.002), ) ) # Setup EA noa2.criteria_manager.add_criterion( ObjectiveCriterion(name="obj", expression="x_max", criterion=ComparisonType.MIN) ) noa2.criteria_manager.add_criterion( ConstraintCriterion( name="constr", expression="omega_damped", criterion=ComparisonType.LESSEQUAL, limit_expression="8.0", ) ) optimizer_settings = noa2.get_property("OptimizerSettings") optimizer_settings["settings"]["MaxGenerations"] = 20 noa2.set_property("OptimizerSettings", optimizer_settings) # Setup postprocessing post_processing.set_property("PostprocessingMode", {"value": "automatic"}) post_processing.set_property("ShowPostProcessingDuringRun", False) post_processing.set_property("WaitForPostprocessingToFinish", True) .. GENERATED FROM PYTHON SOURCE LINES 161-171 Optionally save project ~~~~~~~~~~~~~~~~~~~~~~~ If you want to save the project to some desired location, uncomment and edit these lines: .. code:: python dir_path = Path(r"") project_name = "oscillator_optimization_workflow.opf" osl.application.save_as(dir_path / project_name) .. GENERATED FROM PYTHON SOURCE LINES 174-177 Run workflow ~~~~~~~~~~~~ Run the workflow created by the preceding scripts. .. GENERATED FROM PYTHON SOURCE LINES 177-181 .. code-block:: Python osl.application.project.start() osl.application.save() .. GENERATED FROM PYTHON SOURCE LINES 182-185 Stop and cancel project ~~~~~~~~~~~~~~~~~~~~~~~ Stop and cancel the project. .. GENERATED FROM PYTHON SOURCE LINES 185-188 .. code-block:: Python osl.dispose() .. GENERATED FROM PYTHON SOURCE LINES 189-197 View generated workflow ~~~~~~~~~~~~~~~~~~~~~~~ This image shows the generated workflow. .. image:: ../../_static/02_3_optimization_on_EA_pyOSL_workflow.png :width: 400 :alt: Result of script. .. _sphx_glr_download_examples_workflow_creation_02_3_oscillator_optimization_on_EA.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 02_3_oscillator_optimization_on_EA.ipynb <02_3_oscillator_optimization_on_EA.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 02_3_oscillator_optimization_on_EA.py <02_3_oscillator_optimization_on_EA.py>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_