项目作者: AndrosovAS

项目描述 :
Interval library in Python using classical interval arithmetic + Kahan division in some functions
高级语言: Python
项目地址: git://github.com/AndrosovAS/intvalpy.git
创建时间: 2020-12-08T15:30:51Z
项目社区:https://github.com/AndrosovAS/intvalpy

开源协议:MIT License

下载


Interval library in Python

The Python module implements an algebraically closed interval arithmetic for interval computations, solving interval systems of both
linear and nonlinear equations, and visualizing solution sets for interval systems of equations.

For details, see the complete documentation on API.

Installation

Ensure you have all the system-wide dependencies installed, then install the module using pip:

  1. pip install intvalpy

Examples

Visualizing solution sets

For a system of linear inequalities of the form A * x >= b or for an interval system of linear algebraic equations A * x = b,
the solution sets are known to be polyhedral sets, convex or non-convex. We can visualize them and display all their vertices:

  1. import intvalpy as ip
  2. ip.precision.extendedPrecisionQ = False
  3. import numpy as np
  4. iplt = ip.IPlot(figsize=(15, 15))
  5. fig, ax = iplt.subplots(nrows=2, ncols=2)
  6. #########################################################################
  7. A, b = ip.Shary(2)
  8. shary_uni = ip.IntLinIncR2(A, b, show=False)
  9. shary_tol = ip.IntLinIncR2(A, b, consistency='tol', show=False)
  10. axindex = (0, 0)
  11. ax[axindex].set_title('United and tolerable solution sets for the Shary interval system')
  12. ax[axindex].title.set_size(15)
  13. iplt.IntLinIncR2(shary_uni, color='gray', alpha=0.5, s=0, axindex=axindex)
  14. iplt.IntLinIncR2(shary_tol, color='blue', alpha=0.3, s=10, axindex=axindex)
  15. #########################################################################
  16. A = ip.Interval([
  17. [[-1, 1], [-1, 1]],
  18. [[-1, -1], [-1, 1]]
  19. ])
  20. b = ip.Interval([[1, 1], [-2, 2]])
  21. unconstrained_set = ip.IntLinIncR2(A, b, show=False)
  22. axindex = (0, 1)
  23. ax[axindex].set_title('Unbounded set')
  24. ax[axindex].title.set_size(15)
  25. iplt.IntLinIncR2(unconstrained_set, color='darkolivegreen', alpha=0.3, s=10, axindex=axindex)
  26. #########################################################################
  27. A = -np.array([[-3, -1],
  28. [-2, -2],
  29. [-1, -3],
  30. [1, -3],
  31. [2, -2],
  32. [3, -1],
  33. [3, 1],
  34. [2, 2],
  35. [1, 3],
  36. [-1, 3],
  37. [-2, 2],
  38. [-3, 1]])
  39. b = -np.array([18,16,18,18,16,18,18,16,18,18,16,18])
  40. duodecagon = ip.lineqs(A, b, show=False)
  41. axindex = (1, 0)
  42. ax[axindex].set_title('Duodecagon')
  43. ax[axindex].title.set_size(15)
  44. iplt.lineqs(duodecagon, color='peru', alpha=0.3, s=10, axindex=axindex)
  45. #########################################################################
  46. x = ip.Interval([[1, 1.2], [1.9, 2.7], [1.7, 1.95], [3.5, 3.5],
  47. [4.5, 5.5], [6, 6], [6.5, 7.5], [7, 7.8]])
  48. y = ip.Interval([[4, 4.3], [4.5, 5.3], [4.6, 4.8], [5.1, 6],
  49. [6, 6.5], [7, 7], [6.7, 7.4], [6.8, 8]])
  50. axindex = (1, 1)
  51. ax[axindex].set_title('Interval scatterplot')
  52. ax[axindex].title.set_size(15)
  53. iplt.scatter(x, y, color='gray', alpha=0.7, s=10, axindex=axindex)

SolSet

It is also possible to create a three-dimensional (or two-dimensional) slice of an N-dimensional figure to visualize the solution set
with fixed N-3 (or N-2) parameters. A specific implementation of this algorithm can be found in the examples.

Recognizing functionals:

Before we start solving a system of equations with interval data it is necessary to understand whether it is solvable or not.
To do this we consider the problem of decidability recognition, i.e. non-emptiness of the set of solutions.
In the case of an interval linear (m x n)-system of equations, we will need to solve no more than 2\ :sup:n
linear inequalities of size 2m+n. This follows from the fact of convexity and polyhedra of the intersection of the sets of solutions
interval system of linear algebraic equations (ISLAE) with each of the orthants of R\ :sup:n space.
Reducing the number of inequalities is fundamentally impossible, which follows from the fact that the problem is intractable,
i.e. its NP-hardness. It is clear that the above described method is applicable only for small dimensionality of the problem,
that is why the recognizing functional method was proposed.

After global optimization, if the value of the functional is non-negative, then the system is solvable. If the value is negative,
then the set of parameters consistent with the data is empty, but the argument delivering the maximum of the functional minimizes this inconsistency.

As an example, it is proposed to investigate the Bart-Nuding system for the emptiness/non-emptiness of the tolerance set of solutions:

  1. import intvalpy as ip
  2. # transition from extend precision (type mpf) to double precision (type float)
  3. # ip.precision.extendedPrecisionQ = False
  4. A = ip.Interval([
  5. [[2, 4], [-2, 1]],
  6. [[-1, 2], [2, 4]]
  7. ])
  8. b = ip.Interval([[-2, 2], [-2, 2]])
  9. tol = ip.linear.Tol.maximize(
  10. A = A,
  11. b = b,
  12. x0 = None,
  13. weight = None,
  14. linear_constraint = None
  15. )
  16. print(tol)

External decision evaluation:

To obtain an optimal external estimate of the united set of solutions of an interval system linear of algebraic equations (ISLAE),
a hybrid method of splitting PSS solutions is implemented. Since the task is NP-hard, the process can be stopped by the number of iterations completed.
PSS methods are consistently guaranteeing, i.e. when the process is interrupted at any number of iterations, an approximate estimate of the solution satisfies the required estimation method.

  1. import intvalpy as ip
  2. # transition from extend precision (type mpf) to double precision (type float)
  3. # ip.precision.extendedPrecisionQ = False
  4. A, b = ip.Shary(12, N=12, alpha=0.23, beta=0.35)
  5. pss = ip.linear.PSS(A, b)
  6. print('pss: ', pss)

Interval system of nonlinear equations:

For nonlinear systems, the simplest multidimensional interval methods of Krawczyk and Hansen-Sengupta are implemented for solving nonlinear systems:

  1. import intvalpy as ip
  2. # transition from extend precision (type mpf) to double precision (type float)
  3. # ip.precision.extendedPrecisionQ = False
  4. epsilon = 0.1
  5. def f(x):
  6. return ip.asinterval([x[0]**2 + x[1]**2 - 1 - ip.Interval(-epsilon, epsilon),
  7. x[0] - x[1]**2])
  8. def J(x):
  9. result = [[2*x[0], 2*x[1]],
  10. [1, -2*x[1]]]
  11. return ip.asinterval(result)
  12. ip.nonlinear.HansenSengupta(f, J, ip.Interval([0.5,0.5],[1,1]))

The library also provides the simplest interval global optimization:

  1. import intvalpy as ip
  2. # transition from extend precision (type mpf) to double precision (type float)
  3. # ip.precision.extendedPrecisionQ = False
  4. def levy(x):
  5. z = 1 + (x - 1) / 4
  6. t1 = np.sin( np.pi * z[0] )**2
  7. t2 = sum(((x - 1) ** 2 * (1 + 10 * np.sin(np.pi * x + 1) ** 2))[:-1])
  8. t3 = (z[-1] - 1) ** 2 * (1 + np.sin(2*np.pi * z[-1]) ** 2)
  9. return t1 + t2 + t3
  10. N = 2
  11. x = ip.Interval([-5]*N, [5]*N)
  12. ip.nonlinear.globopt(levy, x, tol=1e-14)