# For global optimization the user can use the function globopt # # In this file we will look at just one function and an example of global optimization # # # You can read the theory used to algorithmize the process in the following sources: # [1] http://www.nsc.ru/interval/Education/Manuals/Bazhenov-InteAnalBasics.pdf (ru version) from intvalpy import Interval from intvalpy.nonlinear import globopt import numpy as np # Consider the Branin function def f(x): return (x[1] - 5.1/(4*np.pi**2)*x[0]**2 + 5/np.pi*x[0] - 6)**2 + \ 10*(1 - 1/(8*np.pi))*np.cos(x[0]) + 10 print(globopt(f, Interval([-5, 0], [10, 15]), tol=1e-14)) # +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ # # globopt(f, ip.Interval([-5,0],[10,15])) # # (interval(['[9.424778, 9.424778]', '[2.475000, 2.475000]']), [0.397887, 0.397887]) # # +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ # # As an example of how to solve another problem, you can watch the video on the website below: # http://www.nsc.ru/interval/?page=Education/Manuals
# In this file, we consider two recognizing functionals. One for the united set of # solutions, and one for the tolerance set. # # # You can read the theory used to algorithmize the process in the following sources: # [1] http://www.nsc.ru/interval/shary/Papers/SharyAiT.pdf (ru version) # [2] http://www.nsc.ru/interval/shary/Papers/Sharys-JCT2013.pdf (ru version) # [3] http://www.nsc.ru/interval/shary/Papers/SShary-JCT-2017.pdf (ru version) # [4] http://www.nsc.ru/interval/shary/Slides/SShary-WeakStrong.pdf (en version) from intvalpy import Interval from intvalpy.linear import Uni, Tol import numpy as np # As an example, consider the Barth-Nuding system: A = Interval([[2, -2], [-1, 2]], [[4, 1], [2, 4]]) b = Interval([-2, -2], [2, 2]) # Take a random point in space and see if it is a solution x = np.random.uniform(0, 1, 2) print('Uni: ', Uni(A, b, x)) print('Tol: ', Tol(A, b, x)) # However, we are interested not just in a random point, but in whether the system # as a whole is solvable. To do this, we maximize the functionals. # Since the system is linear, the functionals are concave and any method that does # not use a gradient can be used as an optimization method. # This code uses the Nelder-Mead method. print('Uni: ', Uni(A, b, maxQ=True)) print('Tol: ', Tol(A, b, maxQ=True))
# dendritic deposits under the galvanostatic electrolysis conditions / T. # N. Ostanina, V. M. Rudoi, A. V. Patrushev, A. B. Darintseva, A. S. # Farlenkov // J. Electroanal. Chem. – 2015. – Vol. 750. – P. 9-18. # # [4] Ostanina, T. N. Determination of the surface of dendritic electrolytic # zinc powders and evaluation of its fractal dimension / T.N. Ostanina, # V.M. Rudoy, V.S. Nikitin, A.B. Darintseva, O.L. Zalesova, N.M. # Porotnikova // Russ. J. Non-Ferr. Met. – 2016. – Vol. 57 – P. 47–51. # DOI: 10.3103/S1067821216010120. from intvalpy import Interval, zeros from intvalpy.linear import Gauss, Gauss_Seidel, Rohn, PSS import numpy as np # First, consider the Gauss and Gauss-Seidel methods for solving quadratic systems: A = Interval([[2, -2], [-1, 2]], [[4, 1], [2, 4]]) b = Interval([-2, -2], [2, 2]) print('Gauss: ', Gauss(A, b)) print('Gauss_Seidel: ', Gauss_Seidel(A, b, P=True)) # +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ # # Gauss(A, b) # # interval(['[-5.000000, 5.000000]', '[-4.000000, 4.000000]']) # # # # Gauss-Seidel(A, b, P=True) # # interval(['[-14.000000, 14.000000]', '[-14.000000, 14.000000]']) # # +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ # A = Interval([[0.5, -0.456], [-0.438, 0.624]], [[1.176, 0.448], [0.596, 1.36]]) b = Interval([0.316, 0.27], [0.632, 0.624])
from intvalpy import Interval, lineqs, IntLinIncR2 import numpy as np import matplotlib.pyplot as plt # To begin with, consider a point system: A = -np.array([[-3, -1], [-2, -2], [-1, -3], [1, -3], [2, -2], [3, -1], [3, 1], [2, 2], [1, 3], [-1, 3], [-2, 2], [-3, 1]]) b = -np.array([18, 16, 18, 18, 16, 18, 18, 16, 18, 18, 16, 18]) vertices = lineqs(A, b, color='blue', alpha=0.2, size=(10, 12)) plt.show() # As the following example, consider the bounded set of solutions that is obtained # from the Barth-Nuding system: A = Interval([[2, -2], [-1, 2]], [[4, 1], [2, 4]]) b = Interval([-2, -2], [2, 2]) vertices = IntLinIncR2(A, b, title='Barth-Nuding', size=(10, 12)) plt.show() # In the previous examples the set of solutions was always finite, now consider a # system that gives an infinite solution: A = Interval([[-1, -1], [-1, -1]], [[1, 1], [-1, 1]]) b = Interval([1, -2], [1, 2]) vertices = IntLinIncR2(A, b, title='Infinite solution', size=(10, 12)) plt.show() # Finally, consider a set of solutions degenerate into a single point: A = Interval([[1, 3], [2, 5]], [[1, 4], [2, 5]])
def f(x): return asinterval([x[0]**2 + x[1]**2 - 1 - Interval(-epsilon, epsilon), x[0] - x[1]**2])
# [1] http://www-sbras.nsc.ru/interval/Library/InteBooks/SharyBook.pdf (ru version) # [2] http://www.nsc.ru/interval/Education/Manuals/Bazhenov-InteAnalBasics.pdf (ru version) from intvalpy import Interval, asinterval from intvalpy.nonlinear import Krawczyk, HansenSengupta import numpy as np # First, let's look at the one-dimensional equation: def f(x): return np.sin(x)**2 - x/5 - 1 def df(x): return 2*np.sin(x)*np.cos(x) - 1/5 x = Interval(-1.5, -1) print('Krawczyk: ', Krawczyk(f, df, x)) # +-----+-----+-----+-----+-----+-----+-----+ # # Krawczyk(f, df, x) # # [-1.085983, -1.085983] # # +-----+-----+-----+-----+-----+-----+-----+ # # For the multidimensional case, consider the system from the textbook [2]: epsilon = 0 def f(x): return asinterval([x[0]**2 + x[1]**2 - 1 - Interval(-epsilon, epsilon), x[0] - x[1]**2]) def J(x): result = [[2*x[0], 2*x[1]],