Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-van-Woudenberg committed May 22, 2024
1 parent 81e390a commit e7a577e
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 12 deletions.
1 change: 0 additions & 1 deletion book/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ parts:
- file: pages/metaheuristics
sections:
- file: pages/metaheuristics_example
- file: more_will_follow
1 change: 0 additions & 1 deletion book/more_will_follow.md

This file was deleted.

142 changes: 132 additions & 10 deletions book/pages/metaheuristics_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "d4f3fe4b",
"metadata": {
"tags": [
Expand Down Expand Up @@ -196,18 +196,86 @@
"id": "c1e8ff92",
"metadata": {},
"source": [
"#### Define variables\n",
"As before, we don't need to specify our variable $x$ itself as defined in {eq}`meta_x`. "
"### Define variables\n",
"As before, we don't need to specify our variable $x$ itself as defined in {eq}`meta_x`. However, we do need to specify that we have integers (specifically only $0$ and $1$ which will be covered by the bounds)\n",
"\n",
"#### SciPy\n",
"In SciPy we use an array of booleans to specify which design variables are integers: "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c46446d",
"metadata": {},
"outputs": [],
"source": [
"integers=np.array([True,True,True,True,True,True,True,True,True,True])"
]
},
{
"cell_type": "markdown",
"id": "d10375a7",
"metadata": {},
"source": [
"#### pymoo\n",
"In pymoo we don't have to specify that have integers, but we have to adapt all of our functions later on so that the outputs are integers.\n",
"\n",
"### Define bounds\n",
"Now let's continue with the bounds, specified by {eq}`meta_x` too:\n",
"\n",
"#### SciPy\n",
"The bounds are defined as before:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f422f0d1",
"metadata": {},
"outputs": [],
"source": [
"bounds = [[0,1],\n",
" [0,1],\n",
" [0,1],\n",
" [0,1],\n",
" [0,1],\n",
" [0,1],\n",
" [0,1],\n",
" [0,1],\n",
" [0,1],\n",
" [0,1]]"
]
},
{
"cell_type": "markdown",
"id": "b444641c",
"metadata": {},
"source": [
"#### pymoo\n",
"\n",
"In pymoo, the bounds are defined as separate arrays:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f740b252",
"metadata": {},
"outputs": [],
"source": [
"xl = np.array([0,0,0,0,0,0,0,0,0,0])\n",
"xu = np.array([1,1,1,1,1,1,1,1,1,1])"
]
},
{
"cell_type": "markdown",
"id": "1333c1bb",
"metadata": {},
"source": [
"#### Define objective function\n",
"### Define objective function\n",
"\n",
"Let's define the objective function as defined in {eq}`meta_c`:"
"Let's define the objective function as defined in {eq}`meta_c`."
]
},
{
Expand All @@ -221,6 +289,59 @@
" return np.array([500, 800, 1000, 1500, 4000, 5700, 6500, 5400, 5500, 6800])@x"
]
},
{
"cell_type": "markdown",
"id": "630cf95d",
"metadata": {},
"source": [
"### Define constraint function\n",
"\n",
"Let's define the constraint function as defined in {eq}`meta_g_matrix` and {eq}`meta_h`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4173993",
"metadata": {},
"outputs": [],
"source": [
"g = np.array([-200, -240, -265, -330, -470, -700, -800, -640, -730, -775])\n",
"\n",
"def nonlinconfun(x):\n",
" return max(x[4:7]) * max(x[7:])"
]
},
{
"cell_type": "markdown",
"id": "a8500036",
"metadata": {},
"source": [
"#### SciPy\n",
"\n",
"In SciPy we need to store the functions in a scipy-object including the bounds"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e35b955a",
"metadata": {},
"outputs": [],
"source": [
"lincon = sp.optimize.LinearConstraint(Ei, lb=np.inf, ub=2700)\n",
"nonlincon = sp.optimize.NonlinearConstraint(nonlinconfun, 0, 0)"
]
},
{
"cell_type": "markdown",
"id": "7635e24b",
"metadata": {},
"source": [
"#### pymoo\n",
"In pymoo the functions can be inserted directly in the problem object later on."
]
},
{
"cell_type": "markdown",
"id": "76ddef0f",
Expand All @@ -234,9 +355,9 @@
"id": "18dba09c",
"metadata": {},
"source": [
"Now let's solve the problem for each of the three models. We're gonna time them to see how long the analysis takes. Please note that when running this in the browser, the `CPU times` gives invalid values.\n",
"Now let's solve the problem using both SciPy and pymoo.\n",
"\n",
"#### Weighted objective function"
"#### SciPy"
]
},
{
Expand Down Expand Up @@ -266,8 +387,9 @@
],
"source": [
"%%time\n",
"result = sp.optimize.minimize(fun = weighted_obj, x0 = x0, bounds = bounds)\n",
"print(result)"
"result = sp.optimize.differential_evolution(func = objective,bounds = bounds,constraints=[lincon,nonlincon], integrality = integers)\n",
"print(result)\n",
"print([email protected])"
]
},
{
Expand Down Expand Up @@ -1865,7 +1987,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
"version": "3.11.8"
}
},
"nbformat": 4,
Expand Down

0 comments on commit e7a577e

Please sign in to comment.