Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #+TITLE: ОТЧЕТ ПО ПРАКТИЧЕСКОЙ РАБОТЕ № 7
- #+AUTHOR: Чащинский Н.И.
- #+MACRO: GROUP ИВБО-03-16
- #+MACRO: DEPARTMENT Вычислительной Техники
- #+MACRO: SUBJECT Теория принятия решений
- #+MACRO: THEMETITLE Тема практической работы:
- #+MACRO: THEME Методы безусловной оптимизации 1-го порядка: Метод наискорейшего спуска
- #+MACRO: TIMETITLE Практическая работа выполнена \today
- #+MACRO: TEACHER доцент Сорокин А.Б.
- #+INCLUDE: ~/BORG/common/header.org
- * Задание
- Вариант №9
- \begin{equation}
- f(\vec{x}) = 1.8x_1^2 + 1.8x_1x_2 + 2x_2^2 - 1.1x_1 + 2.3x_2 \rightarrow \text{min}
- \end{equation}
- \begin{align*}
- \vec{x}^{(0)} &= (2, 3)\\
- \vec{x}^* &= (0.7652, -0.9194)\\
- f^* &= -1.478\\
- \epsilon &= 0.0001\\
- \end{align*}
- * Формулы
- \begin{equation*}
- f_k = f(\vec{x}^{(k)})
- \end{equation}
- \begin{equation}
- \vec{x}^{(k+1)} = \vec{x}^{(k)} - h_k \nabla f_k
- \end{equation}
- \begin{equation}
- h_k = \frac{(\nabla f_k, \nabla f_k)}{(H\vec{x}^{(k)} \nabla f_k, \nabla f_k)}
- \end{equation}
- * Решение
- #+call: shortest_descent()
- #+RESULTS:
- | k | x1 | x2 | f | grad norm |
- |----+---------+---------+---------+-----------|
- | 0 | 2.0 | 3.0 | 40.7 | 21.2758 |
- | 1 | -1.2135 | -2.0018 | 11.7682 | 12.0239 |
- | 2 | 1.2592 | 0.1492 | 2.1946 | 6.353 |
- | 3 | 0.2414 | -1.2705 | -0.4064 | 3.4426 |
- | 4 | 0.924 | -0.6341 | -1.1884 | 1.7927 |
- | 5 | 0.6276 | -1.0238 | -1.3964 | 0.9539 |
- | 6 | 0.8125 | -0.8438 | -1.4563 | 0.4937 |
- | 7 | 0.7292 | -0.9492 | -1.4721 | 0.2601 |
- | 8 | 0.7788 | -0.8994 | -1.4765 | 0.1343 |
- | 9 | 0.7558 | -0.9277 | -1.4777 | 0.0703 |
- | 10 | 0.769 | -0.9141 | -1.478 | 0.0362 |
- | 11 | 0.7628 | -0.9217 | -1.4781 | 0.0189 |
- | 12 | 0.7663 | -0.918 | -1.4781 | 0.0097 |
- | 13 | 0.7646 | -0.92 | -1.4781 | 0.0051 |
- | 14 | 0.7655 | -0.919 | -1.4781 | 0.0026 |
- | 15 | 0.7651 | -0.9195 | -1.4781 | 0.0014 |
- | 16 | 0.7653 | -0.9193 | -1.4781 | 0.0007 |
- | 17 | 0.7652 | -0.9194 | -1.4781 | 0.0004 |
- | 18 | 0.7653 | -0.9193 | -1.4781 | 0.0002 |
- | 19 | 0.7652 | -0.9194 | -1.4781 | 0.0001 |
- * Код
- #+NAME: shortest_descent
- #+BEGIN_SRC python
- import numpy as np
- coefs = [1.8, 1.8, 2, -1.1, 2.3]
- def f(x):
- c = coefs
- c0 = c[0]*x[0]**2
- c1 = c[1]*x[0]*x[1]
- c2 = c[2]*x[1]**2
- c3 = c[3]*x[0]
- c4 = c[4]*x[1]
- return c0 + c1 + c2 + c3 + c4
- def grad(x):
- c = coefs
- x0 = 2*c[0]*x[0] + c[1]*x[1] + 0*c[2] + c[3] + 0*c[4]
- x1 = 0*c[0] + c[1]*x[0] + 2*c[2]*x[1] + 0*c[3] + c[4]
- return np.array([x0, x1])
- def hesse():
- c = coefs
- row0 = [c[0], c[1]]
- row1 = [c[1], c[2]]
- return np.array([row0, row1])
- def h_k(x):
- g = grad(x)
- H = hesse()
- return (np.dot(g, g) / np.dot(H.dot(g), g))
- def step(x):
- h = h_k(x)
- return x - h*grad(x)
- def optimize(x, epsilon):
- report = []
- g = grad(x)
- grad_norm = np.sqrt(g.dot(g))
- k = 0
- row = [k, x[0], x[1], f(x), grad_norm]
- row = [round(c, 4) for c in row]
- report.append(row)
- while grad_norm > epsilon:
- h = h_k(x)
- x -= h * g
- g = grad(x)
- grad_norm = np.sqrt(g.dot(g))
- k += 1
- row = [k, x[0], x[1], f(x), grad_norm]
- row = [round(c, 4) for c in row]
- report.append(row)
- header = ['k', 'x1', 'x2', 'f', 'grad norm']
- report.insert(0, header)
- report.insert(1, None)
- return report
- x = np.array([2.0, 3.0])
- epsilon = 0.0001
- report = optimize(x, epsilon)
- return report
- #+END_SRC
- #+RESULTS: shortest_descent
- | k | x1 | x2 | f | grad norm |
- |----+---------+---------+---------+-----------|
- | 0 | 2.0 | 3.0 | 40.7 | 21.2758 |
- | 1 | -1.2135 | -2.0018 | 11.7682 | 12.0239 |
- | 2 | 1.2592 | 0.1492 | 2.1946 | 6.353 |
- | 3 | 0.2414 | -1.2705 | -0.4064 | 3.4426 |
- | 4 | 0.924 | -0.6341 | -1.1884 | 1.7927 |
- | 5 | 0.6276 | -1.0238 | -1.3964 | 0.9539 |
- | 6 | 0.8125 | -0.8438 | -1.4563 | 0.4937 |
- | 7 | 0.7292 | -0.9492 | -1.4721 | 0.2601 |
- | 8 | 0.7788 | -0.8994 | -1.4765 | 0.1343 |
- | 9 | 0.7558 | -0.9277 | -1.4777 | 0.0703 |
- | 10 | 0.769 | -0.9141 | -1.478 | 0.0362 |
- | 11 | 0.7628 | -0.9217 | -1.4781 | 0.0189 |
- | 12 | 0.7663 | -0.918 | -1.4781 | 0.0097 |
- | 13 | 0.7646 | -0.92 | -1.4781 | 0.0051 |
- | 14 | 0.7655 | -0.919 | -1.4781 | 0.0026 |
- | 15 | 0.7651 | -0.9195 | -1.4781 | 0.0014 |
- | 16 | 0.7653 | -0.9193 | -1.4781 | 0.0007 |
- | 17 | 0.7652 | -0.9194 | -1.4781 | 0.0004 |
- | 18 | 0.7653 | -0.9193 | -1.4781 | 0.0002 |
- | 19 | 0.7652 | -0.9194 | -1.4781 | 0.0001 |
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement