The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

99 lines
5.0 KiB

2 months ago
  1. import pytest
  2. import pycarl
  3. from configurations import PackageSelector
  4. parameters = [(pycarl.gmp, pycarl.convert.gmp_converter)]
  5. names = ["gmp_converter"]
  6. if pycarl.has_cln():
  7. parameters.append((pycarl.cln, pycarl.convert.cln_converter))
  8. names.append("cln_converter")
  9. @pytest.mark.parametrize("convert_package,converter", parameters, ids=names)
  10. class TestConvertExplicit(PackageSelector):
  11. def test_convert_integer(self, package, convert_package, converter):
  12. original = package.Integer(-4)
  13. assert isinstance(original, package.Integer)
  14. converted = converter.convert_integer(original)
  15. assert isinstance(converted, convert_package.Integer)
  16. def test_convert_rational(self, package, convert_package, converter):
  17. original = package.Rational(package.Integer(-1), package.Integer(2))
  18. assert isinstance(original, package.Rational)
  19. converted = converter.convert_rational(original)
  20. assert isinstance(converted, convert_package.Rational)
  21. def test_convert_term(self, package, convert_package, converter):
  22. pycarl.clear_pools()
  23. var = pycarl.Variable("n")
  24. rational = package.Integer(2) / package.Integer(5)
  25. monomial = pycarl.create_monomial(var, 2)
  26. original = package.Term(rational, monomial)
  27. assert isinstance(original, package.Term)
  28. converted = converter.convert_term(original)
  29. assert isinstance(converted, convert_package.Term)
  30. def test_convert_polynomial(self, package, convert_package, converter):
  31. pycarl.clear_pools()
  32. var1 = pycarl.Variable("n")
  33. var2 = pycarl.Variable("o")
  34. original = package.Polynomial(4) * var1 * var2 + var1 * var1 + package.Integer(2)
  35. assert isinstance(original, package.Polynomial)
  36. converted = converter.convert_polynomial(original)
  37. assert isinstance(converted, convert_package.Polynomial)
  38. def test_convert_rational_function(self, package, convert_package, converter):
  39. pycarl.clear_pools()
  40. var1 = pycarl.Variable("x")
  41. var2 = pycarl.Variable("y")
  42. pol1 = var1 * var1 + package.Integer(2)
  43. pol2 = var2 + package.Integer(1)
  44. original = package.RationalFunction(pol1, pol2)
  45. assert isinstance(original, package.RationalFunction)
  46. converted = converter.convert_rational_function(original)
  47. assert isinstance(converted, convert_package.RationalFunction)
  48. def test_convert_constant_factorized_polynomial(self, package, convert_package, converter):
  49. original = package.FactorizedPolynomial(3)
  50. assert isinstance(original, package.FactorizedPolynomial)
  51. converted = converter.convert_factorized_polynomial(original)
  52. assert isinstance(converted, convert_package.FactorizedPolynomial)
  53. def test_convert_factorized_polynomial(self, package, convert_package, converter):
  54. pycarl.clear_pools()
  55. var1 = pycarl.Variable("a")
  56. var2 = pycarl.Variable("b")
  57. pol1 = package.create_factorized_polynomial(package.Polynomial(4) * (var2 + package.Integer(-2)))
  58. pol2 = package.create_factorized_polynomial(package.Polynomial(var2) - 2)
  59. pol3 = package.create_factorized_polynomial(package.Polynomial(2) * var1)
  60. original = pol1 * pol2 * pol3
  61. assert isinstance(original, package.FactorizedPolynomial)
  62. converted = converter.convert_factorized_polynomial(original)
  63. assert isinstance(converted, convert_package.FactorizedPolynomial)
  64. assert len(converted.factorization()) == len(original.factorization())
  65. def test_convert_factorized_rational_function(self, package, convert_package, converter):
  66. pycarl.clear_pools()
  67. var1 = pycarl.Variable("a")
  68. var2 = pycarl.Variable("b")
  69. pol1 = package.create_factorized_polynomial(package.Polynomial(4) * (var2 + package.Integer(-2)))
  70. pol2 = package.create_factorized_polynomial(package.Polynomial(var2) - 2)
  71. pol3 = package.create_factorized_polynomial(package.Polynomial(2) * var1)
  72. original = package.FactorizedRationalFunction(pol1 * pol2, pol2 * pol3)
  73. assert isinstance(original, package.FactorizedRationalFunction)
  74. converted = converter.convert_factorized_rational_function(original)
  75. assert isinstance(converted, convert_package.FactorizedRationalFunction)
  76. assert len(converted.numerator.factorization()) == len(original.numerator.factorization())
  77. assert len(converted.denominator.factorization()) == len(original.denominator.factorization())
  78. def test_convert_constraint(self, package, convert_package, converter):
  79. pycarl.clear_pools()
  80. var1 = pycarl.Variable("a")
  81. pol1 = package.Polynomial(2) * var1 * var1 + var1 + package.Integer(4)
  82. original = package.formula.Constraint(pol1, pycarl.formula.Relation.GREATER)
  83. assert isinstance(original, package.formula.Constraint)
  84. converted = converter.convert_constraint(original)
  85. assert isinstance(converted, convert_package.formula.Constraint)
  86. assert converted.relation == original.relation