Spaces:
Runtime error
Runtime error
Delete ag4masses/alphageometry/ar_test.py
Browse files
ag4masses/alphageometry/ar_test.py
DELETED
|
@@ -1,204 +0,0 @@
|
|
| 1 |
-
# Copyright 2023 DeepMind Technologies Limited
|
| 2 |
-
#
|
| 3 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
-
# you may not use this file except in compliance with the License.
|
| 5 |
-
# You may obtain a copy of the License at
|
| 6 |
-
#
|
| 7 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
-
#
|
| 9 |
-
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
-
# See the License for the specific language governing permissions and
|
| 13 |
-
# limitations under the License.
|
| 14 |
-
# ==============================================================================
|
| 15 |
-
|
| 16 |
-
"""Unit tests for ar.py."""
|
| 17 |
-
import unittest
|
| 18 |
-
|
| 19 |
-
from absl.testing import absltest
|
| 20 |
-
import ar
|
| 21 |
-
import graph as gh
|
| 22 |
-
import problem as pr
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
class ARTest(unittest.TestCase):
|
| 26 |
-
|
| 27 |
-
@classmethod
|
| 28 |
-
def setUpClass(cls):
|
| 29 |
-
super().setUpClass()
|
| 30 |
-
cls.defs = pr.Definition.from_txt_file('defs.txt', to_dict=True)
|
| 31 |
-
cls.rules = pr.Theorem.from_txt_file('rules.txt', to_dict=True)
|
| 32 |
-
|
| 33 |
-
def test_update_groups(self):
|
| 34 |
-
"""Test for update_groups."""
|
| 35 |
-
groups1 = [{1, 2}, {3, 4, 5}, {6, 7}]
|
| 36 |
-
groups2 = [{2, 3, 8}, {9, 10, 11}]
|
| 37 |
-
|
| 38 |
-
_, links, history = ar.update_groups(groups1, groups2)
|
| 39 |
-
self.assertEqual(
|
| 40 |
-
history,
|
| 41 |
-
[
|
| 42 |
-
[{1, 2, 3, 4, 5, 8}, {6, 7}],
|
| 43 |
-
[{1, 2, 3, 4, 5, 8}, {6, 7}, {9, 10, 11}],
|
| 44 |
-
],
|
| 45 |
-
)
|
| 46 |
-
self.assertEqual(links, [(2, 3), (3, 8), (9, 10), (10, 11)])
|
| 47 |
-
|
| 48 |
-
groups1 = [{1, 2}, {3, 4}, {5, 6}, {7, 8}]
|
| 49 |
-
groups2 = [{2, 3, 8, 9, 10}, {3, 6, 11}]
|
| 50 |
-
|
| 51 |
-
_, links, history = ar.update_groups(groups1, groups2)
|
| 52 |
-
self.assertEqual(
|
| 53 |
-
history,
|
| 54 |
-
[
|
| 55 |
-
[{1, 2, 3, 4, 7, 8, 9, 10}, {5, 6}],
|
| 56 |
-
[{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}],
|
| 57 |
-
],
|
| 58 |
-
)
|
| 59 |
-
self.assertEqual(links, [(2, 3), (3, 8), (8, 9), (9, 10), (3, 6), (6, 11)])
|
| 60 |
-
|
| 61 |
-
groups1 = []
|
| 62 |
-
groups2 = [{1, 2}, {3, 4}, {5, 6}, {2, 3}]
|
| 63 |
-
|
| 64 |
-
_, links, history = ar.update_groups(groups1, groups2)
|
| 65 |
-
self.assertEqual(
|
| 66 |
-
history,
|
| 67 |
-
[
|
| 68 |
-
[{1, 2}],
|
| 69 |
-
[{1, 2}, {3, 4}],
|
| 70 |
-
[{1, 2}, {3, 4}, {5, 6}],
|
| 71 |
-
[{1, 2, 3, 4}, {5, 6}],
|
| 72 |
-
],
|
| 73 |
-
)
|
| 74 |
-
self.assertEqual(links, [(1, 2), (3, 4), (5, 6), (2, 3)])
|
| 75 |
-
|
| 76 |
-
def test_generic_table_simple(self):
|
| 77 |
-
tb = ar.Table()
|
| 78 |
-
|
| 79 |
-
# If a-b = b-c & d-a = c-d
|
| 80 |
-
tb.add_eq4('a', 'b', 'b', 'c', 'fact1')
|
| 81 |
-
tb.add_eq4('d', 'a', 'c', 'd', 'fact2')
|
| 82 |
-
tb.add_eq4('x', 'y', 'z', 't', 'fact3') # distractor fact
|
| 83 |
-
|
| 84 |
-
# Then b=d, because {fact1, fact2} but not fact3.
|
| 85 |
-
result = list(tb.get_all_eqs_and_why())
|
| 86 |
-
self.assertIn(('b', 'd', ['fact1', 'fact2']), result)
|
| 87 |
-
|
| 88 |
-
def test_angle_table_inbisector_exbisector(self):
|
| 89 |
-
"""Test that AR can figure out bisector & ex-bisector are perpendicular."""
|
| 90 |
-
# Load the scenario that we have cd is bisector of acb and
|
| 91 |
-
# ce is the ex-bisector of acb.
|
| 92 |
-
p = pr.Problem.from_txt(
|
| 93 |
-
'a b c = triangle a b c; d = incenter d a b c; e = excenter e a b c ?'
|
| 94 |
-
' perp d c c e'
|
| 95 |
-
)
|
| 96 |
-
g, _ = gh.Graph.build_problem(p, ARTest.defs)
|
| 97 |
-
|
| 98 |
-
# Create an external angle table:
|
| 99 |
-
tb = ar.AngleTable('pi')
|
| 100 |
-
|
| 101 |
-
# Add bisector & ex-bisector facts into the table:
|
| 102 |
-
ca, cd, cb, ce = g.names2nodes(['d(ac)', 'd(cd)', 'd(bc)', 'd(ce)'])
|
| 103 |
-
tb.add_eqangle(ca, cd, cd, cb, 'fact1')
|
| 104 |
-
tb.add_eqangle(ce, ca, cb, ce, 'fact2')
|
| 105 |
-
|
| 106 |
-
# Add a distractor fact to make sure traceback does not include this fact
|
| 107 |
-
ab = g.names2nodes(['d(ab)'])[0]
|
| 108 |
-
tb.add_eqangle(ab, cb, cb, ca, 'fact3')
|
| 109 |
-
|
| 110 |
-
# Check for all new equalities
|
| 111 |
-
result = list(tb.get_all_eqs_and_why())
|
| 112 |
-
|
| 113 |
-
# halfpi is represented as a tuple (1, 2)
|
| 114 |
-
halfpi = (1, 2)
|
| 115 |
-
|
| 116 |
-
# check that cd-ce == halfpi and this is because fact1 & fact2, not fact3
|
| 117 |
-
self.assertCountEqual(
|
| 118 |
-
result,
|
| 119 |
-
[
|
| 120 |
-
(cd, ce, halfpi, ['fact1', 'fact2']),
|
| 121 |
-
(ce, cd, halfpi, ['fact1', 'fact2']),
|
| 122 |
-
],
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
def test_angle_table_equilateral_triangle(self):
|
| 126 |
-
"""Test that AR can figure out triangles with 3 equal angles => each is pi/3."""
|
| 127 |
-
# Load an equaliteral scenario
|
| 128 |
-
p = pr.Problem.from_txt('a b c = ieq_triangle ? cong a b a c')
|
| 129 |
-
g, _ = gh.Graph.build_problem(p, ARTest.defs)
|
| 130 |
-
|
| 131 |
-
# Add two eqangles facts because ieq_triangle only add congruent sides
|
| 132 |
-
a, b, c = g.names2nodes('abc')
|
| 133 |
-
g.add_eqangle([a, b, b, c, b, c, c, a], pr.EmptyDependency(0, None))
|
| 134 |
-
g.add_eqangle([b, c, c, a, c, a, a, b], pr.EmptyDependency(0, None))
|
| 135 |
-
|
| 136 |
-
# Create an external angle table:
|
| 137 |
-
tb = ar.AngleTable('pi')
|
| 138 |
-
|
| 139 |
-
# Add the fact that there are three equal angles
|
| 140 |
-
ab, bc, ca = g.names2nodes(['d(ab)', 'd(bc)', 'd(ac)'])
|
| 141 |
-
tb.add_eqangle(ab, bc, bc, ca, 'fact1')
|
| 142 |
-
tb.add_eqangle(bc, ca, ca, ab, 'fact2')
|
| 143 |
-
|
| 144 |
-
# Now check for all new equalities
|
| 145 |
-
result = list(tb.get_all_eqs_and_why())
|
| 146 |
-
result = [(x.name, y.name, z, t) for x, y, z, t in result]
|
| 147 |
-
|
| 148 |
-
# 1/3 pi is represented as a tuple angle_60
|
| 149 |
-
angle_60 = (1, 3)
|
| 150 |
-
angle_120 = (2, 3)
|
| 151 |
-
|
| 152 |
-
# check that angles constants are created and figured out:
|
| 153 |
-
self.assertCountEqual(
|
| 154 |
-
result,
|
| 155 |
-
[
|
| 156 |
-
('d(bc)', 'd(ac)', angle_120, ['fact1', 'fact2']),
|
| 157 |
-
('d(ab)', 'd(bc)', angle_120, ['fact1', 'fact2']),
|
| 158 |
-
('d(ac)', 'd(ab)', angle_120, ['fact1', 'fact2']),
|
| 159 |
-
('d(ac)', 'd(bc)', angle_60, ['fact1', 'fact2']),
|
| 160 |
-
('d(bc)', 'd(ab)', angle_60, ['fact1', 'fact2']),
|
| 161 |
-
('d(ab)', 'd(ac)', angle_60, ['fact1', 'fact2']),
|
| 162 |
-
],
|
| 163 |
-
)
|
| 164 |
-
|
| 165 |
-
def test_incenter_excenter_touchpoints(self):
|
| 166 |
-
"""Test that AR can figure out incenter/excenter touchpoints are equidistant to midpoint."""
|
| 167 |
-
|
| 168 |
-
p = pr.Problem.from_txt(
|
| 169 |
-
'a b c = triangle a b c; d1 d2 d3 d = incenter2 a b c; e1 e2 e3 e ='
|
| 170 |
-
' excenter2 a b c ? perp d c c e',
|
| 171 |
-
translate=False,
|
| 172 |
-
)
|
| 173 |
-
g, _ = gh.Graph.build_problem(p, ARTest.defs)
|
| 174 |
-
|
| 175 |
-
a, b, c, ab, bc, ca, d1, d2, d3, e1, e2, e3 = g.names2nodes(
|
| 176 |
-
['a', 'b', 'c', 'ab', 'bc', 'ac', 'd1', 'd2', 'd3', 'e1', 'e2', 'e3']
|
| 177 |
-
)
|
| 178 |
-
|
| 179 |
-
# Create an external distance table:
|
| 180 |
-
tb = ar.DistanceTable()
|
| 181 |
-
|
| 182 |
-
# DD can figure out the following facts,
|
| 183 |
-
# we manually add them to AR.
|
| 184 |
-
tb.add_cong(ab, ca, a, d3, a, d2, 'fact1')
|
| 185 |
-
tb.add_cong(ab, ca, a, e3, a, e2, 'fact2')
|
| 186 |
-
tb.add_cong(ca, bc, c, d2, c, d1, 'fact5')
|
| 187 |
-
tb.add_cong(ca, bc, c, e2, c, e1, 'fact6')
|
| 188 |
-
tb.add_cong(bc, ab, b, d1, b, d3, 'fact3')
|
| 189 |
-
tb.add_cong(bc, ab, b, e1, b, e3, 'fact4')
|
| 190 |
-
|
| 191 |
-
# Now we check whether tb has figured out that
|
| 192 |
-
# distance(b, d1) == distance(e1, c)
|
| 193 |
-
|
| 194 |
-
# linear comb exprssion of each variables:
|
| 195 |
-
b = tb.v2e['bc:b']
|
| 196 |
-
c = tb.v2e['bc:c']
|
| 197 |
-
d1 = tb.v2e['bc:d1']
|
| 198 |
-
e1 = tb.v2e['bc:e1']
|
| 199 |
-
|
| 200 |
-
self.assertEqual(ar.minus(d1, b), ar.minus(c, e1))
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
if __name__ == '__main__':
|
| 204 |
-
absltest.main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|