Скачать книгу

alt="min blank f left parenthesis x right parenthesis"/> (2.29)

      where the objective function f :

n
is differentiable. The gradient ∇f(x) represents the direction of greatest increase of f. Thus, minimizing f implies to move in the direction opposite to the gradient. Therefore, we use the following iteration:

      The gradient method consists in applying this iteration until the gradient is small enough, i.e., until ‖∇f(x)‖ ≥ ϵ. It is easier to understand the algorithm by considering concrete problems and their implementation in Python, as given in the next examples.

      Example 2.4

      Consider the following optimization problem:

       min blank f left parenthesis x comma y right parenthesis equals 10 x squared plus 15 y squared plus exp invisible function application left parenthesis x plus y right parenthesis (2.31)

      The gradient of this function is presented below:

       nabla f left parenthesis x comma y right parenthesis equals left parenthesis table attributes columnspacing 1em end attributes row cell 20 x plus exp invisible function application left parenthesis x plus y right parenthesis end cell row cell 30 y plus exp invisible function application left parenthesis x plus y right parenthesis end cell end table right parenthesis nabla f left parenthesis x comma y right parenthesis equals left parenthesis table attributes columnspacing 1em end attributes row cell 20 x plus exp invisible function application left parenthesis x plus y right parenthesis end cell row cell 30 y plus exp invisible function application left parenthesis x plus y right parenthesis end cell end table right parenthesis (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:

       table attributes columnalign right left right left right left right left right left right left columnspacing 0em 2em 0em 2em 0em 2em 0em 2em 0em 2em 0em end attributes row cell x not stretchy leftwards arrow x minus t fraction numerator straight partial differential f over denominator straight partial differential x end fraction end cell end table (2.33)

       table attributes columnalign right left right left right left right left right left right left columnspacing 0em 2em 0em 2em 0em 2em 0em 2em 0em 2em 0em end attributes row cell y not stretchy leftwards arrow y minus t fraction numerator straight partial differential f over denominator straight partial differential y end fraction end cell end table (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)

      Example 2.5

      Python allows calculating the gradient automatically using the module AutoGrad. It is quite intuitive to use. Consider the following script, which solves the same problem presented in the previous example:

      import autograd.numpy as np from autograd import grad # gradient calculation def f(x): z = 10.0*x[0]**2 + 15*x[1]**2 + np.exp(x[0]+x[1]) return z g = grad(f) # create a funtion g that returns the gradient x = np.array([10.0,10.0]) t = 0.03 for k in range(50): dx = g(x) x = x -t*dx print('argmin:',x)

      In this case, we defined a function f and its gradient g where (x, y) was replaced by a vector (x0, x1). The module NumPy was loaded using autograd.numpy to obtain a gradient function automatically. The code executes the same 50 iterations, obtaining the same result. The reader should execute and compare the two codes in terms of time calculation and results.

      Example 2.6

begin inline style D equals left parenthesis 100 comma 50 right parenthesis end style

      Figure 2.6 A small photovoltaic system with three solar panels.

       min space blank f space equals cost subscript stack A E with bar on top end subscript open double vertical bar top enclose A E end enclose close double vertical bar space plus space cost subscript stack B E with bar on top end subscript open double vertical bar stack C E with bar on top close double vertical bar space cost subscript stack D E with bar on top end subscript open double vertical bar top enclose D E end enclose close double vertical bar (2.35)

      where costij¯ is the unitary cost of the cable that connects the point i and j, and ij¯ is the corresponding length.

      The costs of the cables are costAE¯ = 12, costBE¯ = 13, costCE¯=11 and costDE¯=18 The distance between any two points U = (u0, u1) and V = (v0, v1) is given by the following expression:

       dist equals square root of left parenthesis straight u subscript 0 minus straight v subscript 0 right parenthesis squared plus left parenthesis straight u subscript 1 minus straight v subscript 1 right parenthesis squared end root (2.36)

      This equation is required several times; thus, it is useful to define a function, as presented below:

      import numpy as np A = (0,40) B = (20,70) C = (30,0) D = (100,50) def dist(U,V): return

Скачать книгу