Skip to content

Commit c3e7d76

Browse files
committed
Add Matrix Inversion Example to Documentation
1 parent 02ce5bf commit c3e7d76

File tree

1 file changed

+224
-0
lines changed

1 file changed

+224
-0
lines changed
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "raw",
5+
"metadata": {
6+
"editable": true,
7+
"raw_mimetype": "text/restructuredtext",
8+
"slideshow": {
9+
"slide_type": ""
10+
},
11+
"tags": []
12+
},
13+
"source": [
14+
".. _nb_matrix_inversion:"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {
20+
"editable": true,
21+
"slideshow": {
22+
"slide_type": ""
23+
},
24+
"tags": []
25+
},
26+
"source": [
27+
"## Matrix Inversion\n",
28+
"\n",
29+
"In this case study, the optimization of a matrix shall be illustrated. Of course, we all know that there are very efficient algorithms for calculating an inverse of a matrix. However, for the sake of illustration, a small example shall show that pymoo can also be used to optimize matrices or even tensors.\n",
30+
"\n",
31+
"Assuming matrix `A` has a size of `n x n`, the problem can be defined by optimizing a vector consisting of `n**2` variables. During evaluation the vector `x`, is reshaped to inversion of the matrix to be found (and also stored as the attribute `A_inv` to be retrieved later)."
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 1,
37+
"metadata": {
38+
"editable": true,
39+
"slideshow": {
40+
"slide_type": ""
41+
},
42+
"tags": []
43+
},
44+
"outputs": [],
45+
"source": [
46+
"from pymoo.core.problem import ElementwiseProblem\n",
47+
"\n",
48+
"\n",
49+
"class MatrixInversionProblem(ElementwiseProblem):\n",
50+
"\n",
51+
" def __init__(self, A, **kwargs):\n",
52+
" self.A = A\n",
53+
" self.n = len(A)\n",
54+
" super().__init__(n_var=self.n**2, n_obj=1, xl=-100.0, xu=+100.0, **kwargs)\n",
55+
"\n",
56+
"\n",
57+
" def _evaluate(self, x, out, *args, **kwargs):\n",
58+
" A_inv = x.reshape((self.n, self.n))\n",
59+
" out[\"A_inv\"] = A_inv\n",
60+
"\n",
61+
" I = np.eye(self.n)\n",
62+
" out[\"F\"] = ((I - (A @ A_inv)) ** 2).sum()"
63+
]
64+
},
65+
{
66+
"cell_type": "markdown",
67+
"metadata": {
68+
"editable": true,
69+
"slideshow": {
70+
"slide_type": ""
71+
},
72+
"tags": []
73+
},
74+
"source": [
75+
"Now let us see what solution is found to be optimal"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 2,
81+
"metadata": {
82+
"editable": true,
83+
"slideshow": {
84+
"slide_type": ""
85+
},
86+
"tags": []
87+
},
88+
"outputs": [],
89+
"source": [
90+
"import numpy as np\n",
91+
"from pymoo.algorithms.soo.nonconvex.de import DE\n",
92+
"from pymoo.optimize import minimize\n",
93+
"\n",
94+
"np.random.seed(1)\n",
95+
"A = np.random.random((2, 2))\n",
96+
"\n",
97+
"problem = MatrixInversionProblem(A)\n",
98+
"\n",
99+
"algorithm = DE()\n",
100+
"\n",
101+
"res = minimize(problem,\n",
102+
" algorithm,\n",
103+
" seed=1,\n",
104+
" verbose=False)\n",
105+
"\n",
106+
"opt = res.opt[0]"
107+
]
108+
},
109+
{
110+
"cell_type": "markdown",
111+
"metadata": {
112+
"editable": true,
113+
"slideshow": {
114+
"slide_type": ""
115+
},
116+
"tags": []
117+
},
118+
"source": [
119+
"In this case the true optimum is actually known. It is"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": 3,
125+
"metadata": {
126+
"editable": true,
127+
"slideshow": {
128+
"slide_type": ""
129+
},
130+
"tags": []
131+
},
132+
"outputs": [
133+
{
134+
"data": {
135+
"text/plain": [
136+
"array([[ 2.39952297e+00, -5.71699951e+00],\n",
137+
" [-9.07758630e-04, 3.30977861e+00]])"
138+
]
139+
},
140+
"execution_count": 3,
141+
"metadata": {},
142+
"output_type": "execute_result"
143+
}
144+
],
145+
"source": [
146+
"np.linalg.inv(A)"
147+
]
148+
},
149+
{
150+
"cell_type": "markdown",
151+
"metadata": {
152+
"editable": true,
153+
"slideshow": {
154+
"slide_type": ""
155+
},
156+
"tags": []
157+
},
158+
"source": [
159+
"Let us see if the black-box optimization algorithm has found something similar"
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": 4,
165+
"metadata": {
166+
"editable": true,
167+
"slideshow": {
168+
"slide_type": ""
169+
},
170+
"tags": []
171+
},
172+
"outputs": [
173+
{
174+
"data": {
175+
"text/plain": [
176+
"array([[ 2.39916052e+00, -5.71656622e+00],\n",
177+
" [-8.41267527e-04, 3.30978797e+00]])"
178+
]
179+
},
180+
"execution_count": 4,
181+
"metadata": {},
182+
"output_type": "execute_result"
183+
}
184+
],
185+
"source": [
186+
"opt.get(\"A_inv\")"
187+
]
188+
},
189+
{
190+
"cell_type": "markdown",
191+
"metadata": {
192+
"editable": true,
193+
"slideshow": {
194+
"slide_type": ""
195+
},
196+
"tags": []
197+
},
198+
"source": [
199+
"This small example shall have illustrated how a matrix can be optimized. In fact, this is implemented by optimizing a vector of variables that are reshaped during evaluation."
200+
]
201+
}
202+
],
203+
"metadata": {
204+
"kernelspec": {
205+
"display_name": "Python 3 (ipykernel)",
206+
"language": "python",
207+
"name": "python3"
208+
},
209+
"language_info": {
210+
"codemirror_mode": {
211+
"name": "ipython",
212+
"version": 3
213+
},
214+
"file_extension": ".py",
215+
"mimetype": "text/x-python",
216+
"name": "python",
217+
"nbconvert_exporter": "python",
218+
"pygments_lexer": "ipython3",
219+
"version": "3.12.1"
220+
}
221+
},
222+
"nbformat": 4,
223+
"nbformat_minor": 4
224+
}

0 commit comments

Comments
 (0)