PK PU~Qn n plot_parameter_space.ipynb{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Parameter space\n\nIn this example, we will see the basics of :class:`.ParameterSpace`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from gemseo.algos.parameter_space import ParameterSpace\nfrom gemseo.api import configure_logger\nfrom gemseo.api import create_discipline\nfrom gemseo.api import create_scenario\n\nconfigure_logger()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Firstly, a :class:`.ParameterSpace` does not require any mandatory argument.\n\n## Create a parameter space\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"parameter_space = ParameterSpace()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, we can add either deterministic variables from their lower and upper\nbounds (use :meth:`.DesignSpace.add_variable`)\nor uncertain variables from their distribution names and parameters\n(use :meth:`.add_random_variable`)\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"parameter_space.add_variable(\"x\", l_b=-2.0, u_b=2.0)\nparameter_space.add_random_variable(\"y\", \"SPNormalDistribution\", mu=0.0, sigma=1.0)\nprint(parameter_space)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can check that the deterministic and uncertain variables are implemented\nas deterministic and deterministic variables respectively:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(\"x is deterministic: \", parameter_space.is_deterministic(\"x\"))\nprint(\"y is deterministic: \", parameter_space.is_deterministic(\"y\"))\nprint(\"x is uncertain: \", parameter_space.is_uncertain(\"x\"))\nprint(\"y is uncertain: \", parameter_space.is_uncertain(\"y\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sample from the parameter space\nWe can sample the uncertain variables from the :class:`.ParameterSpace` and\nget values either as an array (default value) or as a dictionary:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sample = parameter_space.compute_samples(n_samples=2, as_dict=True)\nprint(sample)\nsample = parameter_space.compute_samples(n_samples=4)\nprint(sample)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sample a discipline over the parameter space\nWe can also sample a discipline over the parameter space. For simplicity,\nwe instantiate an :class:`.AnalyticDiscipline` from a dictionary of\nexpressions.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"discipline = create_discipline(\"AnalyticDiscipline\", expressions={\"z\": \"x+y\"})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From these parameter space and discipline, we build a :class:`.DOEScenario`\nand execute it with a Latin Hypercube Sampling algorithm and 100 samples.\n\n
Warning
A :class:`.Scenario` deals with all variables available in the\n :class:`.DesignSpace`. By inheritance, a :class:`.DOEScenario` deals\n with all variables available in the :class:`.ParameterSpace`.\n Thus, if we do not filter the uncertain variables, the\n :class:`.DOEScenario` will consider all variables. In particular, the\n deterministic variables will be consider as uniformly distributed.
\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario = create_scenario(\n [discipline], \"DisciplinaryOpt\", \"z\", parameter_space, scenario_type=\"DOE\"\n)\nscenario.execute({\"algo\": \"lhs\", \"n_samples\": 100})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can visualize the result by encapsulating the database in\na :class:`.Dataset`:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"dataset = scenario.export_to_dataset(opt_naming=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This visualization can be tabular for example:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(dataset.export_to_dataframe())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"or graphical by means of a scatter plot matrix for example:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"dataset.plot(\"ScatterMatrix\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sample a discipline over the uncertain space\nIf we want to sample a discipline over the uncertain space,\nwe need to filter the uncertain variables:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"parameter_space.filter(parameter_space.uncertain_variables)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, we create a new scenario from this parameter space\ncontaining only the uncertain variables and execute it.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"scenario = create_scenario(\n [discipline], \"DisciplinaryOpt\", \"z\", parameter_space, scenario_type=\"DOE\"\n)\nscenario.execute({\"algo\": \"lhs\", \"n_samples\": 100})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, we build a dataset from the disciplinary cache and visualize it.\nWe can see that the deterministic variable 'x' is set to its default\nvalue for all evaluations, contrary to the previous case where we were\nconsidering the whole parameter space.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"dataset = scenario.export_to_dataset(opt_naming=False)\nprint(dataset.export_to_dataframe())"
]
}
],
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK PUmD plot_create_design_space.ipynb{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# DesignSpace creation and manipulation\n\nIn this example, we will see how to create and how to manipulate an instance of\n:class:`.DesignSpace`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from gemseo.api import configure_logger\nfrom gemseo.api import create_design_space\nfrom numpy import array\n\nconfigure_logger()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create a design space\n\nThe user can create an instance of the :class:`.DesignSpace` using the API\nand the :func:`.create_design_space` function.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"design_space = create_design_space()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Add design variables\n\nThe user can add new design variables using the :meth:`.DesignSpace.add_variable`. In\nthe following example, we add the `x` variable in the design space. We also\ndefine the lower and upper bound of the variable.\nIt is then possible to plot the :class:`.DesignSpace` instance either using a\nprint statement or by using the logger.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"design_space.add_variable(\"x\", l_b=array([-2.0]), u_b=array([2.0]), value=array([0.0]))\n\nprint(design_space)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The user can also add design variables with dimension greater than one. To do\nthat, the user can use the `size` keyword:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"design_space.add_variable(\n \"y\", l_b=array([-2.0, -1.0]), u_b=array([2.0, 1.0]), value=array([0.0, 0.0]), size=2\n)\nprint(design_space)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By default, each variable infers its type from the given values. One may also\nspecify it with the `var_type` keyword\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"design_space.add_variable(\n \"z\",\n l_b=array([0, -1]),\n u_b=array([3, 1]),\n value=array([0, 1]),\n size=2,\n var_type=\"integer\",\n)\ndesign_space.add_variable(\n \"w\",\n l_b=array([-2, -5]),\n u_b=array([3, 1]),\n value=array([2, -2]),\n size=2,\n var_type=[\"integer\", \"integer\"],\n)\nprint(design_space)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note
Some optimization algorithms may not handle integer variables properly.\n For updated information about the optimization algorithms that handle\n integer variables, refer to `gen_opt_algos`.\n\n For additional information on how |g| handles integer variables, refer to\n `nutshell_design_space`.
\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Remove design variables\n\nThe user can also remove a variable in the design space using the\n:meth:`.DesignSpace.remove_variable` method:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"design_space.remove_variable(\"x\")\nprint(design_space)"
]
}
],
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK PUwUd
plot_load_design_space.ipynb{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# DesignSpace import and export from disk\n\nIn this example, we will see how to read, filter, and export a design space\nfrom the disk.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from gemseo.api import configure_logger\nfrom gemseo.api import export_design_space\nfrom gemseo.api import read_design_space\n\nconfigure_logger()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Read a design space from a file\n\nThe user can read a design space from a file using the\n:func:`.create_design_space` function.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"design_space = read_design_space(\"design_space.txt\")\nprint(design_space)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Filtering the design space\n\nThe user can filter the design space in order to only keep some variables. To\ndo so, the user can use the :meth:`.DesignSpace.filter` method:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"design_space.filter([\"x1\", \"x2\"])\nprint(design_space)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Export the design space\n\nThe user can export a :class:`.DesignSpace` instance by using the\n:func:`.export_design_space` function.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"export_design_space(design_space, \"new_design_space.txt\")"
]
}
],
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK PU~Qn n plot_parameter_space.ipynbPK PUmD plot_create_design_space.ipynbPK PUwUd
3 plot_load_design_space.ipynbPK >