Ejemplo n.º 1
0
def ddd_problem():
    problem = GeometricProblem(dimension=2)
    problem.add_point('v1', vector([random.random() for i in [1,2]]))
    problem.add_point('v2', vector([random.random() for i in [1,2]]))
    problem.add_point('v3', vector([random.random() for i in [1,2]]))
    problem.add_constraint(DistanceConstraint('v1','v2',distance_2p(problem.get_point('v1'), problem.get_point('v2'))))
    problem.add_constraint(DistanceConstraint('v1','v3',distance_2p(problem.get_point('v1'), problem.get_point('v3'))))
    problem.add_constraint(DistanceConstraint('v2','v3',distance_2p(problem.get_point('v2'), problem.get_point('v3')))) 
    return problem
Ejemplo n.º 2
0
    def solve_shape(self, layers):
        for layer in layers:
            print("Solving shape ", layer.id)

            pt0 = layer.named_pairs[0][0]
            pt1 = layer.named_pairs[1][0]
            pt2 = layer.named_pairs[2][0]
            for i, point in enumerate(layer.named_pairs):
                # Add initial points
                self.problem.add_variable(point[0],
                                          vector([point[1], point[2], 0]))

                # Lock them together
                if i > 0:
                    self.problem.add_constraint(
                        DistanceConstraint(
                            pt0, point[0],
                            distance_2p(self.problem.get_point(pt0),
                                        self.problem.get_point(point[0]))))
                if i > 1:
                    self.problem.add_constraint(
                        AngleConstraint(
                            pt0, pt1, point[0],
                            angle_3p(self.problem.get_point(pt0),
                                     self.problem.get_point(pt1),
                                     self.problem.get_point(point[0]))))
                if i > 2:
                    self.problem.add_constraint(
                        AngleConstraint(
                            pt0, pt2, point[0],
                            angle_3p(self.problem.get_point(pt0),
                                     self.problem.get_point(pt2),
                                     self.problem.get_point(point[0]))))
Ejemplo n.º 3
0
def add_random_constraint(problem, ratio):
    """add a random constraint to a problem, with a given ratio angles/distances"""
    if random.random() < ratio:
        # add angle
        pointvars = list(problem.cg.variables())
        random.shuffle(pointvars)
        v1 = pointvars[0]
        v2 = pointvars[1]
        v3 = pointvars[2]
        p1 = problem.get_point(v1)
        p2 = problem.get_point(v2)
        p3 = problem.get_point(v3)
        angle = angle_3p(p1, p2, p3)
        con = AngleConstraint(v1, v2, v3, angle)
        diag_print("**Add constraint:" + str(con), "drplan")
        problem.add_constraint(con)
    else:
        # add distance
        pointvars = list(problem.cg.variables())
        random.shuffle(pointvars)
        v1 = pointvars[0]
        v2 = pointvars[1]
        p1 = problem.get_point(v1)
        p2 = problem.get_point(v2)
        dist = distance_2p(p1, p2)
        con = DistanceConstraint(v1, v2, dist)
        diag_print("**Add constraint:" + str(con), "drplan")
        problem.add_constraint(con)
    return
Ejemplo n.º 4
0
 def satisfied(self, mapping):
     """return True iff mapping from variable names to points
     satisfies constraint"""
     a = mapping[self._variables[0]]
     b = mapping[self._variables[1]]
     result = tol_eq(distance_2p(a, b), self._value)
     return result
Ejemplo n.º 5
0
def propagation_problem():
    problem = GeometricProblem(dimension=2)
    problem.add_point('v1', vector([random.random() for i in [1, 2]]))
    problem.add_point('v2', vector([random.random() for i in [1, 2]]))
    problem.add_point('v3', vector([random.random() for i in [1, 2]]))
    problem.add_point('v4', vector([random.random() for i in [1, 2]]))
    problem.add_point('v5', vector([random.random() for i in [1, 2]]))

    problem.add_constraint(
        DistanceConstraint(
            'v1', 'v2',
            distance_2p(problem.get_point('v1'), problem.get_point('v2'))))

    problem.add_constraint(
        DistanceConstraint(
            'v1', 'v3',
            distance_2p(problem.get_point('v1'), problem.get_point('v3'))))

    problem.add_constraint(
        DistanceConstraint(
            'v2', 'v3',
            distance_2p(problem.get_point('v2'), problem.get_point('v3'))))

    problem.add_constraint(
        DistanceConstraint(
            'v2', 'v4',
            distance_2p(problem.get_point('v2'), problem.get_point('v4'))))

    problem.add_constraint(
        DistanceConstraint(
            'v1', 'v5',
            distance_2p(problem.get_point('v1'), problem.get_point('v5'))))

    problem.add_constraint(
        AngleConstraint(
            'v3', 'v1', 'v4',
            angle_3p(problem.get_point('v3'), problem.get_point('v1'),
                     problem.get_point('v4'))))

    problem.add_constraint(
        AngleConstraint(
            'v3', 'v2', 'v5',
            angle_3p(problem.get_point('v3'), problem.get_point('v2'),
                     problem.get_point('v5'))))

    return problem
Ejemplo n.º 6
0
def random_distance_problem_3D(npoints, radius, roundoff):
    """creates a 3D problem with random distances"""
    problem = GeometricProblem(dimension=3)
    for i in range(npoints):
        # add point
        newvar = 'v' + str(i)
        newpoint = vector([
            _round(random.uniform(-radius, radius), roundoff),
            _round(random.uniform(-radius, radius), roundoff),
            _round(random.uniform(-radius, radius), roundoff)
        ])
        sellist = list(problem.cg.variables())
        problem.add_point(newvar, newpoint)
        # add distance constraints
        for j in range(min(3, len(sellist))):
            index = random.randint(0, len(sellist) - 1)
            var = sellist.pop(index)
            point = problem.get_point(var)
            dist = distance_2p(point, newpoint)
            problem.add_constraint(DistanceConstraint(var, newvar, dist))
    return problem
Ejemplo n.º 7
0
def _constraint_group(problem, group, dependend, angleratio):
    """Add constraints to problem to constrain given group of points. 
       Group may be optionally dependend on pair of points.
       Creates angle constraints with a given chance."""

    diag_print(
        "_constraint_group(group=" + str(group.keys()) + ",dep=" +
        str(dependend) + ")", "geometric._constraint_group")
    if len(group) == 2:
        if dependend == None:
            v1 = group.keys()[0]
            v2 = group.keys()[1]
            p1 = group[v1]
            p2 = group[v2]
            dist = distance_2p(p1, p2)
            con = DistanceConstraint(v1, v2, dist)
            diag_print("**Add constraint:" + str(con),
                       "geometric._constraint_group")
            problem.add_constraint(con)
    elif len(group) >= 3:
        # pick three points
        keys = group.keys()
        if dependend == None:
            v1 = random.choice(keys)
        else:
            v1 = dependend[0]
        keys.remove(v1)
        if dependend == None:
            v2 = random.choice(keys)
        else:
            v2 = dependend[1]
        keys.remove(v2)
        v3 = random.choice(keys)
        keys.remove(v3)
        # create three groups
        g = [{}, {}, {}]
        g[0][v1] = group[v1]
        g[0][v2] = group[v2]
        g[1][v1] = group[v1]
        g[1][v3] = group[v3]
        g[2][v2] = group[v2]
        g[2][v3] = group[v3]
        # distribute remaining points over groups
        while (len(keys) > 0):
            k = keys.pop()
            if dependend == None:
                i = random.randint(0, 2)
            else:
                i = random.randint(1, 2)
            g[i][k] = group[k]
        # compute facts from prototype
        p1 = group[v1]
        p2 = group[v2]
        p3 = group[v3]
        # group 0: if independend, add at least one independent group
        if dependend == None:
            _constraint_group(problem, g[0], None, angleratio)
        # group 1: random: angle constraint or independend group
        if random.random() > angleratio:
            _constraint_group(problem, g[1], None, angleratio)
        else:
            angle = angle_3p(p1, p2, p3)
            con = AngleConstraint(v1, v2, v3, angle)
            diag_print("**Add constraint:" + str(con),
                       "geometric._constraint_group")
            problem.add_constraint(con)
            _constraint_group(problem, g[1], [v1, v3], angleratio)
        # group 2: random: angle constraint, two configuratins, or independend group
        if random.random() > angleratio:
            _constraint_group(problem, g[2], None, angleratio)
        elif random.random() < 0.5:
            angle = angle_3p(p2, p1, p3)
            con = AngleConstraint(v2, v1, v3, angle)
            diag_print("**Add constraint:" + str(con),
                       "geometric._constraint_group")
            problem.add_constraint(con)
            _constraint_group(problem, g[2], [v2, v3], angleratio)
        else:
            angle = angle_3p(p2, p3, p1)
            con = AngleConstraint(v2, v3, v1, angle)
            diag_print("**Add constraint:" + str(con),
                       "geometric._constraint_group")
            problem.add_constraint(con)
            _constraint_group(problem, g[2], [v2, v3], angleratio)