Читать книгу Mathematical Programming for Power Systems Operation - Alejandro Garcés Ruiz - Страница 39

Example 2.4

Оглавление

Consider the following optimization problem:

(2.31)

The gradient of this function is presented below:

(2.32)

We require to find a value (x, y) such that this gradient is zero. Therefore, we use the gradient method. The algorithm starts from an initial point (for example x = 10, y = 10) and calculate new points as follows:

(2.33)

(2.34)

This step can be implemented in a script in Python, as presented below:

import numpy as np x = 10 y = 10 t = 0.03 for k in range(50): dx = 20*x + np.exp(x+y) dy = 30*y + np.exp(x+y) x += -t*dx y += -t*dy print('grad:',np.abs([dx,dy])) print('argmin:',x,y)

In the first line, we import the module NumPy with the alias np. This module contains mathematical functions such as sin, cos, exp, ln among others. The gradient introduces two components dx and dy, which are evaluated in each iteration and added to the previous point (x,y). We repeat the process 50 times and print the value of the gradient each iteration. Notice that all the indented statements belong to the for-statement, and hence the gradient is printed in each iteration. In contrast, the argmin is printed only at the end of the process.

Mathematical Programming for Power Systems Operation

Подняться наверх