Initial commit
This commit is contained in:
174
.gitignore
vendored
Normal file
174
.gitignore
vendored
Normal file
@@ -0,0 +1,174 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# UV
|
||||
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
#uv.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.lock
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
#pdm.lock
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
||||
.pdm.toml
|
||||
.pdm-python
|
||||
.pdm-build/
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
#.idea/
|
||||
|
||||
# Ruff stuff:
|
||||
.ruff_cache/
|
||||
|
||||
# PyPI configuration file
|
||||
.pypirc
|
||||
1
README.md
Normal file
1
README.md
Normal file
@@ -0,0 +1 @@
|
||||
# Репозиторий для работ по вычислительной математике
|
||||
141
lab1/main.py
Normal file
141
lab1/main.py
Normal file
@@ -0,0 +1,141 @@
|
||||
import numpy as np
|
||||
|
||||
|
||||
def point_info(x, f):
|
||||
return f'x = {x}, f(x) = {f(x)}'
|
||||
|
||||
|
||||
def hooke_jeeves(f, x0, delta0, epsilon, alpha):
|
||||
x = np.array(x0)
|
||||
delta = np.array(delta0)
|
||||
|
||||
iteration = 0
|
||||
|
||||
while np.linalg.norm(delta) > epsilon:
|
||||
iteration += 1
|
||||
|
||||
print()
|
||||
print('=' * 40)
|
||||
print('Итерация', iteration)
|
||||
|
||||
print('Текущая базовая точка', point_info(x, f))
|
||||
|
||||
sample_x = exploratory_search(f, x, delta)
|
||||
|
||||
if f(sample_x) < f(x):
|
||||
print('Исследующий поиск УДАЧНЫЙ')
|
||||
|
||||
x_p = sample_search(f, x, sample_x)
|
||||
|
||||
if f(x_p) < f(x):
|
||||
print('Поиск по образцу УДАЧНЫЙ')
|
||||
x = x_p
|
||||
else:
|
||||
print('Поиск по образцу ПРОВАЛЕН')
|
||||
x = sample_x
|
||||
|
||||
print()
|
||||
print('Новая базовая точка', point_info(x, f))
|
||||
|
||||
else:
|
||||
print('Исследующий поиск ПРОВАЛЕН')
|
||||
|
||||
print('Уменьшаем шаг', delta, '->', delta / alpha)
|
||||
delta = delta / alpha
|
||||
print('ε =', np.linalg.norm(delta))
|
||||
|
||||
return x, f(x)
|
||||
|
||||
|
||||
def exploratory_search(f, x, delta):
|
||||
print()
|
||||
print('Выполняем исследующий поиск')
|
||||
|
||||
x_new = np.array(x)
|
||||
f_x_new = f(x_new)
|
||||
|
||||
for i in range(len(x)):
|
||||
x_up = x_new.copy()
|
||||
x_down = x_new.copy()
|
||||
|
||||
x_up[i] += delta[i]
|
||||
x_down[i] -= delta[i]
|
||||
|
||||
f_x_up = f(x_up)
|
||||
f_x_down = f(x_down)
|
||||
|
||||
if (f_x_up < f_x_new and f_x_up < f_x_down):
|
||||
x_new = x_up
|
||||
f_x_new = f_x_up
|
||||
elif (f_x_down < f_x_new):
|
||||
x_new = x_down
|
||||
f_x_new = f_x_down
|
||||
|
||||
if (any([x_new[i] != x[i] for i in range(len(x))])):
|
||||
print('Найдена точка', x_new, f(x_new))
|
||||
return x_new
|
||||
|
||||
|
||||
def sample_search(f, x1, x2):
|
||||
print()
|
||||
print('Выполняем поиск по образцу')
|
||||
while f(x2) < f(x1):
|
||||
x2, x1 = x2 + (x2 - x1), x2
|
||||
print('Найдена точка', x2, f(x2))
|
||||
return x2
|
||||
|
||||
|
||||
def f1():
|
||||
x0 = [1, 0, 0]
|
||||
delta0 = [1e+6, 1e+6, 1e+6]
|
||||
alpha = 2
|
||||
epsilon = 1e-4
|
||||
def f(x): return x[0] ** 2 + 2 * x[1] ** 2 + 5 * \
|
||||
x[2] ** 2 - 2 * x[0] * x[1] - 4 * x[0] * x[2] - 2 * x[2]
|
||||
|
||||
|
||||
def f2():
|
||||
x0 = [1, 1]
|
||||
delta0 = [1, 1]
|
||||
alpha = 2
|
||||
epsilon = 1e-6
|
||||
|
||||
def f(x): return x[0] ** 4 + x[1] ** 2 - 4 * x[0] * x[1]
|
||||
|
||||
x, val = hooke_jeeves(f, x0, delta0, epsilon, alpha)
|
||||
return x, val
|
||||
|
||||
|
||||
def f3():
|
||||
x0 = [1, 0]
|
||||
delta0 = [1, 1]
|
||||
alpha = 2
|
||||
epsilon = 1e-5
|
||||
|
||||
def f(x): return x[0] * np.exp(x[0]) - (1 + np.exp(x[0])) * np.sin(x[1])
|
||||
|
||||
x, val = hooke_jeeves(f, x0, delta0, epsilon, alpha)
|
||||
return x, val
|
||||
|
||||
|
||||
def example():
|
||||
x0 = [-4, -4]
|
||||
delta0 = [1, 1]
|
||||
alpha = 2
|
||||
epsilon = 1e-4
|
||||
|
||||
def f(x): return 8 * (x[0] ** 2) + 4 * x[0] * x[1] + 5 * (x[1] ** 2)
|
||||
|
||||
x, val = hooke_jeeves(f, x0, delta0, epsilon, alpha)
|
||||
return x, val
|
||||
|
||||
|
||||
# x, val = f1()
|
||||
# x, val = f2()
|
||||
# x, val = f3()
|
||||
x, val = example()
|
||||
|
||||
print()
|
||||
print('=' * 40)
|
||||
print("Точка экстремумв:", x)
|
||||
print("Минимальное значение функции:", val)
|
||||
1
lab1/requirements.txt
Normal file
1
lab1/requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
numpy==2.2.3
|
||||
Reference in New Issue
Block a user