Exemplo n.º 1
0
def get_problem():
    # Initialize problem structure.
    p_a = Point('A')
    p_b = Point('B')
    p_c = Point('C')
    p_d = Point('D')

    line_ac = Line('AC', end1=p_a, end2=p_c)
    line_bc = Line('BC', end1=p_b, end2=p_c)
    line_bd = Line('BD', end1=p_b, end2=p_d)
    line_cd = Line('CD', end1=p_c, end2=p_d)

    angle_acb = Angle('ACB', side1=line_ac, side2=line_bc, vertex=p_c)
    angle_acd = Angle('ACD', side1=line_ac, side2=line_cd, vertex=p_c)

    entity = Entity('Basic test4')
    entity.add_entity(p_a, p_b, p_c, p_d)
    entity.add_entity(line_ac, line_bc, line_bd, line_cd)
    entity.add_entity(angle_acb, angle_acd)

    # Initialize conditions.
    conditions = []
    conditions.append(AttributeValue(angle_acb, angle=60))
    sa = SupplementaryAngle('ACB_ACD', angle1=angle_acb, angle2=angle_acd)
    col = Collineation('BCD', points=[p_b, p_c, p_d])
    conditions.append(RelationshipBased(sa))
    conditions.append(RelationshipBased(col))

    # Set target.
    target = Target(angle_acd, 'angle')

    return entity, target, conditions
def get_problem():
    """Test for line sum."""
    # Initialize problem structure.
    p_a = Point('A')
    p_b = Point('B')
    p_c = Point('C')
    p_d = Point('D')
    p_e = Point('E')
    p_f = Point('F')
    p_g = Point('G')
    p_h = Point('H')

    line_ab = Line('AB', end1=p_a, end2=p_b)
    line_af = Line('AF', end1=p_a, end2=p_f)
    line_bf = Line('BF', end1=p_b, end2=p_f)
    line_cd = Line('CD', end1=p_c, end2=p_d)
    line_cg = Line('CG', end1=p_c, end2=p_g)
    line_dg = Line('DG', end1=p_d, end2=p_g)
    line_ef = Line('EF', end1=p_e, end2=p_f)
    line_eg = Line('EG', end1=p_e, end2=p_g)
    line_eh = Line('EH', end1=p_e, end2=p_h)
    line_fg = Line('FG', end1=p_f, end2=p_g)
    line_fh = Line('FH', end1=p_f, end2=p_h)
    line_gh = Line('GH', end1=p_g, end2=p_h)

    angle_afe = Angle('AFE', side1=line_af, side2=line_ef, vertex=p_f)
    angle_bfe = Angle('BFE', side1=line_bf, side2=line_ef, vertex=p_f)
    angle_afh = Angle('AFH', side1=line_af, side2=line_fh, vertex=p_f)
    angle_bfh = Angle('BFH', side1=line_bf, side2=line_fh, vertex=p_f)
    angle_cgh = Angle('CGH', side1=line_cg, side2=line_gh, vertex=p_g)
    angle_dgh = Angle('DGH', side1=line_dg, side2=line_gh, vertex=p_g)
    angle_cge = Angle('CGE', side1=line_cg, side2=line_eg, vertex=p_g)
    angle_dge = Angle('DGE', side1=line_dg, side2=line_eg, vertex=p_g)

    entity = Entity('Basic test14')
    for name, obj in locals().items():
        if name.startswith(tuple(['p_', 'line_', 'angle_'])):
            entity.add_entity(obj)

    # Initialize conditions.
    conditions = []
    conditions.append(AttributeValue(angle_afh, angle=60))
    parallel = Parallel('AB_CD', line1=line_ab, line2=line_cd)
    r = RelationshipBased(parallel)
    conditions.append(r)
    conditions.append(
        RelationshipBased(Collineation('AFB', points=[p_a, p_f, p_b])))
    conditions.append(
        RelationshipBased(Collineation('CGD', points=[p_c, p_g, p_d])))
    conditions.append(
        RelationshipBased(Collineation('EFGH', points=[p_e, p_f, p_g, p_h])))

    # Set target.
    target = Target(angle_cge, 'angle')

    return entity, target, conditions
Exemplo n.º 3
0
def get_problem():
    # Initialize problem structure.
    p_a = Point('A')
    p_b = Point('B')
    p_c = Point('C')
    p_d = Point('D')

    line_ab = Line('AB', end1=p_a, end2=p_b)
    line_ac = Line('AC', end1=p_a, end2=p_c)
    line_ad = Line('AD', end1=p_a, end2=p_d)

    angle_bac = Angle('BAC', side1=line_ab, side2=line_ac, vertex=p_a)
    angle_cad = Angle('CAD', side1=line_ac, side2=line_ad, vertex=p_a)
    angle_bad = Angle('BAD', side1=line_ab, side2=line_ad, vertex=p_a)

    entity = Entity('Basic test3')
    entity.add_entity(p_a, p_b, p_c, p_d)
    entity.add_entity(line_ab, line_ac, line_ad)
    entity.add_entity(angle_bac, angle_bad, angle_cad)

    # Initialize conditions.
    conditions = []
    conditions.append(AttributeValue(angle_bac, angle=60))
    conditions.append(AttributeValue(angle_cad, angle=30))
    cva = CommonVertexAngle('A_BCD', vertex=p_a, ends=[p_b, p_c, p_d])
    r = RelationshipBased(cva)
    conditions.append(r)

    # Set target.
    target = Target(angle_bad, 'angle')

    return entity, target, conditions
Exemplo n.º 4
0
def get_problem():
    """Test for line sum."""
    # Initialize problem structure.
    p_a = Point('A')
    p_b = Point('B')
    p_c = Point('C')

    line_ab = Line('AB', end1=p_a, end2=p_b)
    line_bc = Line('BC', end1=p_b, end2=p_c)
    line_ac = Line('AC', end1=p_a, end2=p_c)

    entity = Entity('Basic test1')
    entity.add_entity(p_a, p_b, p_c)
    entity.add_entity(line_ab, line_bc, line_ac)

    # Set target.
    target = Target(line_bc, 'length')

    # Initialize conditions.
    conditions = []
    conditions.append(AttributeValue(line_ab, length=2))
    conditions.append(AttributeValue(line_ac, length=5))
    col = Collineation('ABC', [p_a, p_b, p_c])
    r = RelationshipBased(col)
    conditions.append(r)

    return entity, target, conditions
    def index(self, indexer: Indexer):
        ret = []
        triangles = indexer.index_by_type(Triangle)
        exist_relations = set()
        two_sums = indexer.index_by_type(TwoSum)
        for two_sum in two_sums:
            exist_relations.add(two_sum.relationship.id)
        for tri in triangles:
            known_angles = []
            for angle in tri.angles:
                angle_A_cond = indexer.index_value_condition(angle, 'angle')
                if angle_A_cond.attr_value is not None:
                    known_angles.append(angle_A_cond)

            if len(known_angles) >= 2:
                pairs = itertools.product(known_angles, known_angles)
                for angle_B_cond, angle_C_cond in pairs:
                    id_ = f'{angle_B_cond.obj.id}.angle_{angle_C_cond.obj.id}.angle'
                    if id_ in exist_relations:
                        continue
                    two_sum_relation = TwoSum(
                        id_, angle_B_cond.obj, 'angle', angle_C_cond.obj,
                        'angle',
                        angle_B_cond.attr_value + angle_C_cond.attr_value)
                    r = RelationshipBased(two_sum_relation)
                    ret.append([[angle_B_cond, angle_C_cond], r])

        return ret
Exemplo n.º 6
0
def get_problem():
    # Initialize problem structure.
    p_a = Point('A')
    p_b = Point('B')
    p_c = Point('C')
    p_d = Point('D')

    line_ac = Line('AC', end1=p_a, end2=p_c)
    line_bc = Line('BC', end1=p_b, end2=p_c)
    line_bd = Line('BD', end1=p_b, end2=p_d)
    line_cd = Line('CD', end1=p_c, end2=p_d)

    angle_acb = Angle('ACB', side1=line_ac, side2=line_bc, vertex=p_c)
    angle_acd = Angle('ACD', side1=line_ac, side2=line_cd, vertex=p_c)

    entity = Entity('Basic test5')
    entity.add_entity(p_a, p_b, p_c, p_d)
    entity.add_entity(line_ac, line_bc, line_bd, line_cd)
    entity.add_entity(angle_acb, angle_acd)

    # Initialize conditions.
    conditions = []
    p = Perpendicular('AC_BD', line1=line_ac, line2=line_bd, foot_point=p_c)
    conditions.append(RelationshipBased(p))

    # Set target.
    target = Target(angle_acd, 'angle')

    return entity, target, conditions
Exemplo n.º 7
0
def get_problem():
    # Initialize problem structure.
    p_a = Point('A')
    p_b = Point('B')
    p_c = Point('C')
    p_d = Point('D')

    line_ab = Line('AB', end1=p_a, end2=p_b)
    line_ac = Line('AC', end1=p_a, end2=p_c)
    line_ad = Line('AD', end1=p_a, end2=p_d)

    angle_bac = Angle('BAC', side1=line_ab, side2=line_ac, vertex=p_a)
    angle_bad = Angle('BAD', side1=line_ab, side2=line_ad, vertex=p_a)
    angle_cad = Angle('CAD', side1=line_ac, side2=line_ad, vertex=p_a)

    entity = Entity('Basic test20')
    entity.add_entity(p_a, p_b, p_c, p_d)
    entity.add_entity(line_ab, line_ac, line_ad)
    entity.add_entity(angle_bac, angle_bad, angle_cad)

    # Set target.
    target = Target(angle_bad, 'angle')

    # Initialize conditions.
    conditions = []
    conditions.append(AttributeValue(angle_bac, angle=30))
    r = NAngleSector('BAD_AC',
                     angle=angle_bad,
                     split_line=line_ac,
                     near_line=line_ab)
    conditions.append(RelationshipBased(r))
    conditions.append(AttributeValue(r, ratio=0.25))
    return entity, target, conditions
Exemplo n.º 8
0
def get_problem():
    """Test for equality of opposite vertical angle."""
    # Initialize problem structure.
    p_a = Point('A')
    p_b = Point('B')
    p_c = Point('C')
    p_d = Point('D')
    p_o = Point('O')

    line_ao = Line('AO', end1=p_a, end2=p_o)
    line_ad = Line('AD', end1=p_a, end2=p_d)
    line_do = Line('DO', end1=p_d, end2=p_o)
    line_bo = Line('BO', end1=p_b, end2=p_o)
    line_bc = Line('BC', end1=p_b, end2=p_c)
    line_co = Line('CO', end1=p_c, end2=p_o)

    angle_aob = Angle('AOB', side1=line_ao, side2=line_bo, vertex=p_o)
    angle_aoc = Angle('AOC', side1=line_ao, side2=line_co, vertex=p_o)
    angle_cod = Angle('COD', side1=line_co, side2=line_do, vertex=p_o)
    angle_bod = Angle('BOD', side1=line_bo, side2=line_do, vertex=p_o)

    entity = Entity('Basic test6')
    entity.add_entity(p_a, p_b, p_c, p_d, p_o)
    entity.add_entity(line_ao, line_ad, line_do, line_bo, line_bc, line_co)
    entity.add_entity(angle_aob, angle_aoc, angle_cod, angle_bod)

    # Initialize conditions.
    conditions = []
    ova1 = OppositeVerticalAngle('AOB_COD',
                                 angle1=angle_aob,
                                 angle2=angle_cod,
                                 vertex=p_o)
    ova2 = OppositeVerticalAngle('AOC_BOD',
                                 angle1=angle_aoc,
                                 angle2=angle_bod,
                                 vertex=p_o)
    conditions.append(RelationshipBased(ova1))
    conditions.append(RelationshipBased(ova2))
    conditions.append(AttributeValue(angle_aob, angle=60))

    # Set target.
    target = Target(angle_cod, 'angle')

    return entity, target, conditions
    def index(self, indexer: Indexer):
        ret = []
        r_conds = indexer.index_by_type(IsRightTriangle)
        triangles = indexer.index_by_type(Triangle)
        right_triangles = [cond.relationship.triangle for cond in r_conds]

        for th in triangles:
            if th in right_triangles:
                continue
            for angle in [th.angle1, th.angle2, th.angle3]:
                a_cond = indexer.index_value_condition(angle, 'angle')
                if a_cond.attr_value is not None and a_cond.attr_value == 90:
                    rt = IsRightTriangle(th.id, th, angle)
                    rt_cond = RelationshipBased(rt)
                    ret.append([[a_cond], rt_cond])
                    break
        return ret
Exemplo n.º 10
0
 def index(self, indexer: Indexer):
     ret = []
     conds = indexer.index_by_type(SupplementaryAngle)
     exist_relations = set()
     two_sums = indexer.index_by_type(TwoSum)
     for two_sum in two_sums:
         exist_relations.add(two_sum.relationship.id)
     for cond in conds:
         r = cond.relationship
         angle1_cond = indexer.index_value_condition(r.angle1, 'angle')
         angle2_cond = indexer.index_value_condition(r.angle2, 'angle')
         id_ = f'{angle1_cond.obj.id}.angle_{angle2_cond.obj.id}.angle'
         if id_ in exist_relations:
             continue
         two_sum_relation = TwoSum(id_, angle1_cond.obj, 'angle',
                                   angle2_cond.obj, 'angle', 180)
         r = RelationshipBased(two_sum_relation)
         ret.append([[cond], r])
     return ret
 def index(self, indexer: Indexer):
     ret = []
     triangles = indexer.index_by_type(Triangle)
     pattern = TrianglePattern(angle_A=AttributeState.KNOWN,
                               angle_B=AttributeState.KNOWN,
                               angle_C=AttributeState.KNOWN)
     
     st_conds = indexer.index_by_type(SimilarTriangle)
     all_st = [cond.relationship for cond in st_conds]
     
     def get_corresp():
         th1_conds = [indexer.index_value_condition(th1_angle, 'angle') \
                 for th1_angle in th1.angles]
         th2_conds = [indexer.index_value_condition(th2_angle, 'angle') \
                 for th2_angle in th2.angles]
         cor_angle = []
         for cond1 in th1_conds:
             for cond2 in th2_conds:
                 if cond1.attr_value == cond2.attr_value:
                     cor_angle.append((cond1.obj, cond2.obj))
                     break
         return cor_angle, th1_conds, th2_conds
     
     replaced, entities = indexer.index_by_pattern(pattern, True)
     size = len(entities)
     for i in range(size):
         for j in range(i + 1, size):
             i_angles = set([replaced[i].angle_A, 
                             replaced[i].angle_B, 
                             replaced[i].angle_C])
             j_angles = set([replaced[j].angle_A, 
                             replaced[j].angle_B, 
                             replaced[j].angle_C])
             if i_angles == j_angles:
                 th1 = entities[i]
                 th2 = entities[j]
                 cor_angle, conds1, conds2 = get_corresp()
                 st = SimilarTriangle('_'.join([th1.id, th2.id]),
                                 th1, th2, cor_angle)
                 if st not in all_st:
                     ret.append([conds1+conds2, RelationshipBased(st)])
     return ret
def get_problem():
    # Initialize problem structure.
    p_a = Point('A')
    p_b = Point('B')
    p_c = Point('C')

    line_ab = Line('AB', end1=p_a, end2=p_b)
    line_bc = Line('BC', end1=p_b, end2=p_c)
    line_ac = Line('AC', end1=p_a, end2=p_c)

    angle_a = Angle('BAC', side1=line_ab, side2=line_ac, vertex=p_a)
    angle_b = Angle('ABC', side1=line_ab, side2=line_bc, vertex=p_b)
    angle_c = Angle('ACB', side1=line_ac, side2=line_bc, vertex=p_c)

    triangle = Triangle('ABC',
                        vertex1=p_c,
                        vertex2=p_a,
                        vertex3=p_b,
                        side1=line_ab,
                        side2=line_bc,
                        side3=line_ac,
                        angle1=angle_c,
                        angle2=angle_a,
                        angle3=angle_b)

    entity = Entity('Basic test23')
    entity.add_entity(triangle)

    # Set target.
    target = Target(angle_c, 'angle')

    # Initialize conditions.
    conditions = []
    conditions.append(AttributeValue(angle_b, angle=30))
    r = ValueEquivalence('AB_AC',
                         obj_list=[line_ab, line_ac],
                         attr_list=['length', 'length'])
    conditions.append(RelationshipBased(r))

    return entity, target, conditions
    def index(self, indexer: Indexer):
        ret = []
        triangles = indexer.index_by_type(Triangle)
        exist_relations = set()
        two_sums = indexer.index_by_type(TwoSum)
        for two_sum in two_sums:
            exist_relations.add(two_sum.relationship.id)
        for tri in triangles:
            for angle in tri.angles:
                angle_A_cond = indexer.index_value_condition(angle, 'angle')
                if angle_A_cond.attr_value is not None:
                    angle_B, angle_C = [a for a in tri.angles if a != angle]
                    id_ = f'{angle_B.id}.angle_{angle_C.id}.angle'
                    if id_ in exist_relations:
                        continue
                    two_sum_relation = TwoSum(id_, angle_B, 'angle', angle_C,
                                              'angle',
                                              180 - angle_A_cond.attr_value)
                    r = RelationshipBased(two_sum_relation)
                    ret.append([[angle_A_cond], r])

        return ret
Exemplo n.º 14
0
    def index(self, indexer: Indexer):
        ret = []
        i_conds = indexer.index_by_type(IsIsoscelesTriangle)
        triangles = indexer.index_by_type(Triangle)
        isosceles_triangles = [cond.relationship.triangle for cond in i_conds]

        for th in triangles:
            if th in isosceles_triangles:
                continue
            permutaton = [(th.angle1, th.angle2, th.vertex3), \
                          (th.angle1, th.angle3, th.vertex2), \
                          (th.angle2, th.angle3, th.vertex1)]
            for a1, a2, v in permutaton:
                a1_cond = indexer.index_value_condition(a1, 'angle')
                a2_cond = indexer.index_value_condition(a2, 'angle')
                if a1_cond.attr_value is None or a2_cond.attr_value is None:
                    continue
                if a1_cond.attr_value == a2_cond.attr_value:
                    it = IsIsoscelesTriangle(th.id, th, v)
                    it_cond = RelationshipBased(it)
                    ret.append([[a1_cond, a2_cond], it_cond])
                    break
        return ret
Exemplo n.º 15
0
    def index(self, indexer: Indexer):
        ret = []
        i_conds = indexer.index_by_type(IsIsoscelesTriangle)
        triangles = indexer.index_by_type(Triangle)
        isosceles_triangles = [cond.relationship.triangle for cond in i_conds]

        for th in triangles:
            if th in isosceles_triangles:
                continue
            permutaton = [(th.side1, th.side2, th.vertex3), \
                          (th.side1, th.side3, th.vertex2), \
                          (th.side2, th.side3, th.vertex1)]
            for s1, s2, v in permutaton:
                s1_cond = indexer.index_value_condition(s1, 'length')
                s2_cond = indexer.index_value_condition(s2, 'length')
                if s1_cond.attr_value is None or s2_cond.attr_value is None:
                    continue
                if s1_cond.attr_value == s2_cond.attr_value:
                    it = IsIsoscelesTriangle(th.id, th, v)
                    it_cond = RelationshipBased(it)
                    ret.append([[s1_cond, s2_cond], it_cond])
                    break
        return ret
    def index(self, indexer: Indexer):
        ret = []
        i_conds = indexer.index_by_type(IsIsoscelesTriangle)
        triangles = indexer.index_by_type(Triangle)
        isosceles_triangles = [cond.relationship.triangle for cond in i_conds]

        for th in triangles:
            if th in isosceles_triangles:
                continue
            permutaton = [(th.side1, th.side2, th.vertex3), \
                          (th.side1, th.side3, th.vertex2), \
                          (th.side2, th.side3, th.vertex1)]
            for s1, s2, v in permutaton:
                eq_cond = index_equivalent_value(indexer,
                                                 obj1=s1,
                                                 attr1='length',
                                                 obj2=s2,
                                                 attr2='length')
                if eq_cond is not None:
                    it = IsIsoscelesTriangle(th.id, th, v)
                    it_cond = RelationshipBased(it)
                    ret.append([[eq_cond], it_cond])
        return ret
def get_problem():
    # Initialize problem structure.
    p_a = Point('A')
    p_b = Point('B')
    p_c = Point('C')

    line_ab = Line('AB', end1=p_a, end2=p_b)
    line_ac = Line('AC', end1=p_a, end2=p_c)
    line_bc = Line('BC', end1=p_b, end2=p_c)

    entity = Entity('Basic test21')
    entity.add_entity(line_ab, line_ac, line_bc)

    # Set target.
    target = Target(line_ac, 'length')

    # Initialize conditions.
    conditions = []
    conditions.append(AttributeValue(line_ab, length=1))
    r = NLineSector('AC_B', line=line_ac, split_point=p_b, nearer_point=p_a)
    conditions.append(RelationshipBased(r))
    conditions.append(AttributeValue(r, ratio=0.5))

    return entity, target, conditions
    def index(self, indexer: Indexer):
        ret = []
        e_conds = indexer.index_by_type(IsEquilateralTriangle)
        triangles = indexer.index_by_type(Triangle)
        e_triangles = [cond.relationship.triangle for cond in e_conds]

        def f(th):
            for i in range(3):
                a_cond = indexer.index_value_condition(th.angles[i], 'angle')
                if a_cond.attr_value is None or a_cond.attr_value != 60:
                    yield False
                else:
                    yield True

        for th in triangles:
            if th in e_triangles:
                continue
            if all(f(th)):
                pre = [indexer.index_value_condition(th.angles[i], 'angle') \
                         for i in range(3)]
                r = IsEquilateralTriangle(th.id, th)
                tg = RelationshipBased(r)
                ret.append([pre, tg])
        return ret
    def parse_problem(self):
        """This function is used to generate entities automatically."""

        entity = Entity('Entity container')
        target = None
        conditions = []

        if self.problem is not None:
            return self.problem

        if self._shown:
            print('Using intelligent parser...')

        # Generate points.
        self.points = {pid: Point(pid) for pid in self._points}

        # Generate lines.
        self.lines = {}
        for lid in self._lines:
            lid = Parser._sort_string(lid)
            ends = [self.points[pid] for pid in lid]
            line = Line(lid, end1=ends[0], end2=ends[1])
            length = self._look_up_length(lid)
            if length is not None:
                cond = AttributeValue(line, length=length)
                conditions.append(cond)
            self.lines[lid] = line

        # Generate angles.
        self.angles = {}
        for vertex, adj_nodes in self._adj_table.items():
            n_adj = len(adj_nodes)
            for i in range(n_adj):
                for j in range(i + 1, n_adj):
                    node1, node2 = adj_nodes[i], adj_nodes[j]
                    node1 = self._line_alias[node1 + vertex][0]
                    node2 = self._line_alias[vertex + node2][1]
                    # Angle with zero degree
                    if node1 == node2:
                        continue
                    # Flat angle
                    if self._is_collineation(node1, vertex, node2):
                        continue
                    node1, node2 = sorted([node1, node2])
                    aid = ''.join([node1, vertex, node2])
                    line1 = self.find_line_by_ends(node1, vertex)
                    line2 = self.find_line_by_ends(node2, vertex)
                    sides = [line1, line2]
                    angle = Angle(aid,
                                  side1=sides[0],
                                  side2=sides[1],
                                  vertex=self.points[vertex])
                    self.angles[aid] = angle
                    degree = self._look_up_degree(aid)
                    if degree is not None:
                        cond = AttributeValue(angle, angle=degree)
                        conditions.append(cond)

        # Generate triangle.
        self.triangles = {}
        triangle_ids = self._analyse_triangle()
        for tid in triangle_ids:
            t_vertexes = [self.points[v] for v in tid]
            t_side1 = self.find_line_by_ends(tid[0], tid[1])
            t_side2 = self.find_line_by_ends(tid[1], tid[2])
            t_side3 = self.find_line_by_ends(tid[0], tid[2])
            t_sides = [t_side1, t_side2, t_side3]
            t_angle1 = self.find_angle_by_points(tid[0], tid[2], tid[1])
            t_angle2 = self.find_angle_by_points(tid[1], tid[0], tid[2])
            t_angle3 = self.find_angle_by_points(tid[0], tid[1], tid[2])
            t_angles = [t_angle1, t_angle2, t_angle3]
            triangle = Triangle(tid,
                                vertex1=t_vertexes[0],
                                vertex2=t_vertexes[1],
                                vertex3=t_vertexes[2],
                                side1=t_sides[0],
                                side2=t_sides[1],
                                side3=t_sides[2],
                                angle1=t_angles[0],
                                angle2=t_angles[1],
                                angle3=t_angles[2])
            self.triangles[tid] = triangle

        if self._shown:
            print('points: ', sorted(self.points.keys()))
            print('lines', sorted(self.lines.keys()))
            print('angles: ', sorted(self.angles.keys()))
            print('triangles: ', sorted(self.triangles.keys()))

        # Generate relationships.
        collineations = {}
        for col in self._collineation_list:
            col_id = 'Collineation ' + ''.join([p for p in col])
            ps = [self.points[pid] for pid in col]
            r = Collineation(col_id, points=ps)
            collineations[col_id] = r

        # Generate opposite vertival angles.
        opposite_angles = {}
        n_cols = len(self._collineation_list)
        for i in range(n_cols):
            for j in range(i + 1, n_cols):
                col1 = self._collineation_list[i]
                col2 = self._collineation_list[j]
                common = list(set(col1) & set(col2))
                if len(common) != 1:
                    continue
                vertex = common[0]
                if vertex in [col1[0], col1[-1], col2[0], col2[-1]]:
                    continue

                angle1_1 = self.find_angle_by_points(col1[0], vertex, col2[0])
                angle1_2 = self.find_angle_by_points(col1[-1], vertex,
                                                     col2[-1])
                rid = ' '.join(['OppositeAngle', angle1_1.id, angle1_2.id])
                opposite_angles[rid] = OppositeVerticalAngle(rid,
                                                             angle1=angle1_1,
                                                             angle2=angle1_2,
                                                             vertex=vertex)

                angle2_1 = self.find_angle_by_points(col1[0], vertex, col2[-1])
                angle2_2 = self.find_angle_by_points(col1[-1], vertex, col2[0])
                rid = ' '.join(['OppositeAngle', angle2_1.id, angle2_2.id])
                opposite_angles[rid] = OppositeVerticalAngle(rid,
                                                             angle1=angle2_1,
                                                             angle2=angle2_2,
                                                             vertex=vertex)

        # Generate supplementary angles.
        supplementary_angles = {}
        for col in self._collineation_list:
            for p in col[1:-1]:
                for adj_p in self._adj_table[p]:
                    if adj_p in col:
                        continue
                    angle1 = self.find_angle_by_points(col[0], p, adj_p)
                    angle2 = self.find_angle_by_points(col[-1], p, adj_p)
                    rid = ' '.join(
                        ['SupplementaryAngle', angle1.id, angle2.id])
                    supplementary_angles[rid] = \
                        SupplementaryAngle(rid, angle1=angle1, angle2=angle2)

        # Generate common vertex angles.
        common_vertex_angles = {}
        for v, arounds in self._common_vertex:
            vertex = self.points[v]
            ends = [self.points[pid] for pid in arounds]
            rid = ' '.join(['CommonVertexAngle', v, ''.join(arounds)])
            r = CommonVertexAngle(rid, vertex=vertex, ends=ends)
            common_vertex_angles[rid] = r

        # Generate n angles sector.
        n_angles_sector = {}
        for aid, lid, ratio in self._angle_split:
            near_line = self.find_line_by_ends(*aid[:2])
            angle_ = self.find_angle_by_points(*aid)
            line_ = self.find_line_by_ends(*lid)
            rid = ' '.join([angle_.id, line_.id, str(ratio), near_line.id])
            r = NAngleSector(rid,
                             angle=angle_,
                             split_line=line_,
                             near_line=near_line)
            cond = AttributeValue(r, ratio=ratio)
            conditions.append(cond)
            n_angles_sector[rid] = r

        # Generate n line sector.
        n_line_sector = {}
        for lid, pid, ratio in self._line_split:
            rid = ' '.join(['NLineSector', lid, pid, str(ratio)])
            r = NLineSector(rid,
                            line=self.find_line_by_ends(*lid),
                            split_point=self.points[pid],
                            near_point=self.points[lid[0]])
            cond = AttributeValue(r, ratio=ratio)
            conditions.append(cond)
            n_line_sector[rid] = r

        # Generate perpendicular relationship.
        perpendiculars = {}
        for lid1, lid2 in self._perpendicular_pairs:
            rid = ' '.join(['Perpendicular', lid1, lid2])
            r = Perpendicular(rid,
                              line1=self.find_line_by_ends(*lid1),
                              line2=self.find_line_by_ends(*lid2),
                              foot_point=None)
            perpendiculars[rid] = r

        # Generate parallel relationship.
        parallels = {}
        for line_ids_tp in self._parallel_sets:
            line_ids = list(line_ids_tp)
            line_num = len(line_ids)
            for i in range(line_num):
                for j in range(i + 1, line_num):
                    reverse = False
                    if line_ids[i][0] > line_ids[i][1]:
                        reverse = not reverse
                        line_ids[i] = line_ids[i][::-1]
                    if line_ids[j][0] > line_ids[j][1]:
                        reverse = not reverse
                        line_ids[j] = line_ids[j][::-1]
                    line1 = self.lines[line_ids[i]]
                    line2 = self.lines[line_ids[j]]
                    rid = ' '.join(['Parallel', line_ids[i], line_ids[j]])
                    parallels[rid] = Parallel(rid,
                                              line1,
                                              line2,
                                              reverse=reverse)

        # Generate ValueEquivalence relationship.
        value_equivalence = {}
        for obj_list in self._angle_equivalent:
            obj_list = [
                self.find_angle_by_points(*obj_id) for obj_id in obj_list
            ]
            rid = '='.join([obj.id for obj in obj_list])
            r = ValueEquivalence(rid,
                                 obj_list=obj_list,
                                 attr_list=['angle'] * len(obj_list))
            value_equivalence[rid] = r
        for obj_list in self._line_equivalent:
            obj_list = [self.find_line_by_ends(*obj_id) for obj_id in obj_list]
            rid = '='.join([obj.id for obj in obj_list])
            r = ValueEquivalence(rid,
                                 obj_list=obj_list,
                                 attr_list=['length'] * len(obj_list))
            value_equivalence[rid] = r

        # Generate equilateral triangle
        equilateral_triangles = {}
        for th_id in self._equilateral_triangle:
            std_th_id = ''.join(sorted(th_id))
            th = self.triangles[std_th_id]
            r = IsEquilateralTriangle(std_th_id, th)
            equilateral_triangles[std_th_id] = r

        # Generate right triangle
        right_triangles = {}
        for th_id, aid in self._right_triangle:
            std_th_id = ''.join(sorted(th_id))
            right_angle = self.find_angle_by_points(*aid)
            th = self.triangles[std_th_id]
            r = IsRightTriangle(std_th_id, th, right_angle)
            right_triangles[std_th_id] = r

        if self._shown:
            print('collineations: ', sorted(collineations.keys()))
            print('opposite angles: ', sorted(opposite_angles.keys()))
            print('supplementary angles: ',
                  sorted(supplementary_angles.keys()))
            print('common vertex angles: ',
                  sorted(common_vertex_angles.keys()))
            print('n angles sector: ', sorted(n_angles_sector.keys()))
            print('n line sector: ', sorted(n_line_sector.keys()))
            print('perpendiculars: ', sorted(perpendiculars.keys()))
            print('parallels: ', sorted(parallels.keys()))

        relationships = []
        relationships += collineations.values()
        relationships += opposite_angles.values()
        relationships += supplementary_angles.values()
        relationships += common_vertex_angles.values()
        relationships += n_angles_sector.values()
        relationships += perpendiculars.values()
        relationships += n_line_sector.values()
        relationships += parallels.values()
        relationships += value_equivalence.values()
        relationships += equilateral_triangles.values()
        relationships += right_triangles.values()
        for r in relationships:
            conditions.append(RelationshipBased(r))

        entity.add_entity(*(self.points.values()))
        entity.add_entity(*(self.lines.values()))
        entity.add_entity(*(self.angles.values()))
        entity.add_entity(*(self.triangles.values()))

        # Generate target.
        if self._target_triplet is None:
            raise NotImplementedError("Target not setted.")
        target_id = self._target_triplet[0]
        target_type = self._target_triplet[1]
        target_attr = self._target_triplet[2]
        target_obj = entity.find_child(target_id, target_type)
        target = Target(target_obj, target_attr)

        problem = Problem(entity, conditions, target)
        return problem
Exemplo n.º 20
0
def get_problem():
    # Initialize problem structure.
    p_a = Point('A')
    p_b = Point('B')
    p_c = Point('C')

    line_ab = Line('AB', end1=p_a, end2=p_b)
    line_bc = Line('BC', end1=p_b, end2=p_c)
    line_ac = Line('AC', end1=p_a, end2=p_c)

    angle_a = Angle('BAC', side1=line_ab, side2=line_ac, vertex=p_a)
    angle_b = Angle('ABC', side1=line_ab, side2=line_bc, vertex=p_b)
    angle_c = Angle('ACB', side1=line_ac, side2=line_bc, vertex=p_c)

    triangle_abc = Triangle('ABC',
                            vertex1=p_c,
                            vertex2=p_a,
                            vertex3=p_b,
                            side1=line_ab,
                            side2=line_bc,
                            side3=line_ac,
                            angle1=angle_c,
                            angle2=angle_a,
                            angle3=angle_b)

    p_d = Point('D')
    p_e = Point('E')
    p_f = Point('F')

    line_de = Line('DE', end1=p_d, end2=p_e)
    line_ef = Line('EF', end1=p_e, end2=p_f)
    line_df = Line('DF', end1=p_d, end2=p_f)

    angle_d = Angle('EDF', side1=line_de, side2=line_df, vertex=p_d)
    angle_e = Angle('DEF', side1=line_de, side2=line_ef, vertex=p_e)
    angle_f = Angle('DFE', side1=line_df, side2=line_ef, vertex=p_f)

    triangle_def = Triangle('DEF',
                            vertex1=p_f,
                            vertex2=p_d,
                            vertex3=p_e,
                            side1=line_de,
                            side2=line_ef,
                            side3=line_df,
                            angle1=angle_f,
                            angle2=angle_d,
                            angle3=angle_e)

    entity = Entity('Basic test17')
    entity.add_entity(triangle_abc)
    entity.add_entity(triangle_def)

    # Set target.
    target = Target(angle_f, 'angle')

    # Initialize conditions.
    conditions = []
    conditions.append(AttributeValue(angle_a, angle=60))
    conditions.append(AttributeValue(angle_b, angle=90))
    st = SimilarTriangle('ABC_DEF',
                         triangle_abc,
                         triangle_def,
                         cor_angle=[(angle_a, angle_d), (angle_b, angle_e),
                                    (angle_c, angle_f)])
    conditions.append(RelationshipBased(st))
    return entity, target, conditions