|
15 | 15 | # You should have received a copy of the GNU General Public License
|
16 | 16 | # along with ODL. If not, see <http://www.gnu.org/licenses/>.
|
17 | 17 |
|
18 |
| -"""Basic examples on how to use the fucntional class. |
19 |
| -
|
20 |
| -This file contains two basic examples on how to use the functional class. It |
21 |
| -contains one example of how to use the default functionals, and how they can be |
22 |
| -used in connection to solvers. It also contains one example on how to |
23 |
| -implement a new functional. |
| 18 | +"""Basic examples on how to use the fucntional class together with solvers. |
| 19 | +
|
| 20 | +This file shows an example of how to set up and solve an optimization problem |
| 21 | +using the default functionals. The problem we will solve is to minimize |
| 22 | +1/2 * ||x - g||_2^2 + lam*||x||_1, for some vector g and some constant lam, |
| 23 | +subject to that all components in x are greater than or equal to 0. The |
| 24 | +theoretical optimal solution to this problem is x = (g - lam)_+, where ( )+ |
| 25 | +denotes the positive part of (i.e., (z)_+ = z if z >= 0, 0 otherwise). |
24 | 26 | """
|
25 | 27 |
|
26 | 28 | import numpy as np
|
27 | 29 | import odl
|
28 | 30 |
|
29 |
| -# First we show how the default functionals can be used in order to set up and |
30 |
| -# solve an optimization problem. The problem we will solve is to minimize |
31 |
| -# 1/2 * ||x - g||_2^2 + lam*||x||_1, for some vector g and some constant lam, |
32 |
| -# subject to that all components in x are greater than or equal to 0. |
33 |
| - |
34 |
| -# Create a space with dimensiona n. |
| 31 | +# Create a space with dimensiona n=10. |
35 | 32 | n = 10
|
36 | 33 | space = odl.rn(n)
|
37 | 34 |
|
38 | 35 | # Create parameters. First half of g are ones, second half are minus ones.
|
39 | 36 | g = space.element(np.hstack((np.ones(n/2), -np.ones(n/2))))
|
40 | 37 | lam = 0.5
|
41 | 38 |
|
| 39 | +# Note that with the values above, the optimal solution is given by a vector |
| 40 | +# with fires half of the elements equal to 0.5, the second half equal to 0. |
| 41 | + |
42 | 42 | # Create the L1-norm functional and multiplyit with the constant lam.
|
43 | 43 | lam_l1_func = lam * odl.solvers.L1Norm(space)
|
44 | 44 |
|
|
81 | 81 | op, x, tau=tau, sigma=sigma, proximal_primal=proximal_primal,
|
82 | 82 | proximal_dual=proximal_dual, niter=niter, callback=callback)
|
83 | 83 |
|
84 |
| -# The theoretical optimal solution to this problem is x = (g - lam)_+, where |
85 |
| -# ( )+ denotes the positive part of (i.e., (z)_+ = z if z >= 0, 0 otherwise). |
86 | 84 | print(x.asarray())
|
0 commit comments