InstallationΒΆ
To install the METOD Algorithm from source:
$ git clone https://github.com/Megscammell/METOD-Algorithm.git
$ cd METOD-Algorithm
$ python setup.py install
To ensure all tests are working, create an environment and run the tests using pytest:
$ conda env create -f environment.yml
$ conda activate metod_algorithm
$ pytest
An example of applying the METOD Algorithm with an objective function and gradient is presented below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | >>> import numpy as np
>>> import math
>>> import metod_alg as mt
>>>
>>> np.random.seed(90)
>>> d = 2
>>> A = np.array([[1, 0],[0, 10]])
>>> theta = np.random.uniform(0, 2 * math.pi)
>>> rotation = np.array([[math.cos(theta), -math.sin(theta)],
... [math.sin(theta), math.cos(theta)]])
>>> x0 = np.array([0.5, 0.2])
>>>
>>> def f(x, x0, A, rotation):
... return 0.5 * (x - x0).T @ rotation.T @ A @ rotation @ (x - x0)
...
>>> def g(x, x0, A, rotation):
... return rotation.T @ A @ rotation @ (x - x0)
...
>>> args = (x0, A, rotation)
>>> (discovered_minimizers,
... number_minimizers,
... func_vals_of_minimizers,
... excessive_no_descents,
... starting_points,
... no_grad_evals) = mt.metod(f, g, args, d, num_points=10)
>>> assert(np.all(np.round(discovered_minimizers[0], 3) == np.array([0.500, 0.200])))
>>> assert(number_minimizers == 1)
>>> assert(np.round(func_vals_of_minimizers, 3) == 0)
>>> assert(excessive_no_descents == 0)
>>> assert(np.array(starting_points).shape == (10, d))
|
The purpose of each line of code within the example is discussed in the following table.
Line number |
Purpose of each line of code within the example |
|---|---|
1 - 3 |
Import the required libraries. |
5 |
Initialize the pseudo-random number generator seed. |
6 |
Set the dimension as |
7 |
Create the variable |
8 |
Create the variable |
9 - 10 |
Create the variable |
11 |
Create the variable |
13 - 14 |
Define a function |
16-17 |
Define the gradient |
19 |
Set |
20 - 25 |
Run the METOD Algorithm with |
26 - 30 |
Check outputs of the METOD Algorithm. |