예제 #1
0
    def test_unsat_system_from_paper3(self):
        """ Test an unsatisfiable system of inequalities from Smaragdakis
            (Fig 9c). """
        sys = IneqSys.InequalitySystem()

        # Two object types related by two binary fact types
        obj1 = IneqSys.Variable("obj1", upper=20)
        obj2 = IneqSys.Variable("obj2", upper=20)
        rol11 = IneqSys.Variable("rol11", upper=20)
        rol12 = IneqSys.Variable("rol12", upper=20)
        rol21 = IneqSys.Variable("rol21", upper=20)
        rol22 = IneqSys.Variable("rol22", upper=20)
        fact1 = IneqSys.Variable("fact1", upper=20)
        fact2 = IneqSys.Variable("fact2", upper=20)

        # Constant
        con2 = IneqSys.Variable("con2", lower=2, upper=2)

        # Semantics of roles
        sys.add(IneqSys.Inequality(lhs=rol11, rhs=IneqSys.Sum([obj1])))
        sys.add(IneqSys.Inequality(lhs=rol11, rhs=IneqSys.Sum([fact1])))
        sys.add(IneqSys.Inequality(lhs=rol21, rhs=IneqSys.Sum([fact1])))
        sys.add(IneqSys.Inequality(lhs=rol21, rhs=IneqSys.Sum([obj2])))

        sys.add(IneqSys.Inequality(lhs=rol12, rhs=IneqSys.Sum([obj1])))
        sys.add(IneqSys.Inequality(lhs=rol12, rhs=IneqSys.Sum([fact2])))
        sys.add(IneqSys.Inequality(lhs=rol22, rhs=IneqSys.Sum([fact2])))
        sys.add(IneqSys.Inequality(lhs=rol22, rhs=IneqSys.Sum([obj2])))

        # Uniqueness of fact types
        sys.add(
            IneqSys.Inequality(lhs=fact1, rhs=IneqSys.Product([rol11, rol21])))
        sys.add(
            IneqSys.Inequality(lhs=fact2, rhs=IneqSys.Product([rol12, rol22])))

        # Disjunctive mandatory
        sys.add(IneqSys.Inequality(lhs=obj1, rhs=IneqSys.Sum([rol11, rol12])))
        sys.add(IneqSys.Inequality(lhs=obj2, rhs=IneqSys.Sum([rol21, rol22])))

        # Simple uniqueness
        sys.add(IneqSys.Inequality(lhs=fact1, rhs=IneqSys.Sum([rol21])))
        sys.add(IneqSys.Inequality(lhs=fact2, rhs=IneqSys.Sum([rol12])))

        # Simple mandatory
        sys.add(IneqSys.Inequality(lhs=obj1, rhs=IneqSys.Sum([rol11])))
        sys.add(IneqSys.Inequality(lhs=obj2, rhs=IneqSys.Sum([rol22])))

        # Frequency
        sys.add(
            IneqSys.Inequality(lhs=fact1, rhs=IneqSys.Product([con2, rol11])))
        sys.add(
            IneqSys.Inequality(coeff=2, lhs=rol11, rhs=IneqSys.Sum([fact1])))

        solution = sys.solve()
        self.assertEquals(solution, None)
        self.assertEquals(rol11.upper, 0)
예제 #2
0
    def test_simple_unsat_system(self):
        """ Test a simple unsatisfiable system. """
        sys = IneqSys.InequalitySystem()

        a = IneqSys.Variable('a')
        b = IneqSys.Variable('b')

        p = IneqSys.Inequality(coeff=2, lhs=a, rhs=IneqSys.Sum([b]))
        q = IneqSys.Inequality(lhs=b, rhs=IneqSys.Sum([a]))

        sys.add(p)
        sys.add(q)
        solution = sys.solve(debug=True)  # Debug on to achieve coverage

        self.assertEquals(solution, None)
        self.assertEquals(a.upper, 0)
        self.assertEquals(a.lower, 1)
예제 #3
0
    def test_simple_sat_system(self):
        """ Test a simple satisfiable system. """
        sys = IneqSys.InequalitySystem()

        a = IneqSys.Variable('a', upper=50)
        b = IneqSys.Variable('b', lower=20)
        c = IneqSys.Variable('c')

        p = IneqSys.Inequality(lhs=b, rhs=IneqSys.Sum([a]))
        q = IneqSys.Inequality(lhs=c, rhs=IneqSys.Product([a, b]))

        sys.add(p)
        sys.add(q)
        solution = sys.solve(debug=True)  # Debug on to achieve coverage

        self.assertEquals(solution['a'], 50)
        self.assertEquals(solution['b'], 50)
        self.assertEquals(solution['c'], 2500)
예제 #4
0
    def test_unsat_system_from_paper(self):
        """ Test an unsatisfiable system of inequalities from Smaragdakis
            (Fig 9a). """
        sys = IneqSys.InequalitySystem()

        # Two object types related by a binary fact type
        obj1 = IneqSys.Variable("obj1")
        obj2 = IneqSys.Variable("obj2")
        rol1 = IneqSys.Variable("rol1")
        rol2 = IneqSys.Variable("rol2")
        fact = IneqSys.Variable("fact")

        # Constants
        cons1 = IneqSys.Variable("con1", lower=1000000, upper=1000000)
        cons2 = IneqSys.Variable("con2", lower=2, upper=2)

        # Inequalities that represent Smaragdakis' rules

        # Cardinality: # >= 1000000 for obj1, # <= 2 for obj2
        sys.add(IneqSys.Inequality(lhs=cons1, rhs=IneqSys.Sum([obj1])))
        sys.add(IneqSys.Inequality(lhs=obj2, rhs=IneqSys.Sum([cons2])))

        # Role 1 is mandatory
        sys.add(IneqSys.Inequality(lhs=obj1, rhs=IneqSys.Sum([rol1])))

        # Simple uniqueness constraints
        sys.add(IneqSys.Inequality(lhs=fact, rhs=IneqSys.Sum([rol1])))
        sys.add(IneqSys.Inequality(lhs=fact, rhs=IneqSys.Sum([rol2])))

        # Semantics of roles
        sys.add(IneqSys.Inequality(lhs=rol1, rhs=IneqSys.Sum([obj1])))
        sys.add(IneqSys.Inequality(lhs=rol1, rhs=IneqSys.Sum([fact])))
        sys.add(IneqSys.Inequality(lhs=rol2, rhs=IneqSys.Sum([fact])))
        sys.add(IneqSys.Inequality(lhs=rol2, rhs=IneqSys.Sum([obj2])))

        # Solve the system and assert that there is no solution
        solution = sys.solve()
        self.assertEquals(solution, None)
        self.assertEquals(cons1.lower, 1000000)
        self.assertEquals(cons1.upper, 2)
예제 #5
0
    def test_unsat_system_from_paper2(self):
        """ Test an unsatisfiable system of inequalities from Smaragdakis
            (Fig 9b). """
        sys = IneqSys.InequalitySystem()

        # Two object types related by a binary fact type
        obj1 = IneqSys.Variable("obj1", upper=50)
        obj2 = IneqSys.Variable("obj2", upper=2)
        rol1 = IneqSys.Variable("rol1", upper=50)
        rol2 = IneqSys.Variable("rol2", upper=50)
        fact = IneqSys.Variable("fact", upper=50)

        # Constant
        con3 = IneqSys.Variable("con3", lower=3, upper=3)

        # Semantics of roles
        sys.add(IneqSys.Inequality(lhs=rol1, rhs=IneqSys.Sum([obj1])))
        sys.add(IneqSys.Inequality(lhs=rol1, rhs=IneqSys.Sum([fact])))
        sys.add(IneqSys.Inequality(lhs=rol2, rhs=IneqSys.Sum([fact])))
        sys.add(IneqSys.Inequality(lhs=rol2, rhs=IneqSys.Sum([obj2])))

        # Frequency constraint
        sys.add(IneqSys.Inequality(lhs=fact, rhs=IneqSys.Product([con3,
                                                                  rol1])))
        sys.add(IneqSys.Inequality(coeff=3, lhs=rol1, rhs=IneqSys.Sum([fact])))

        # Uniqueness
        sys.add(IneqSys.Inequality(lhs=fact, rhs=IneqSys.Product([rol1,
                                                                  rol2])))

        # Disjunctive mandatory
        sys.add(IneqSys.Inequality(lhs=obj1, rhs=IneqSys.Sum([rol1])))
        sys.add(IneqSys.Inequality(lhs=obj2, rhs=IneqSys.Sum([rol2])))

        # Solve the system
        solution = sys.solve()
        self.assertEquals(solution, None)
        self.assertEquals(rol1.upper, 0)
예제 #6
0
class Solvespace():
    def __init__(self):
        self.Script = """# -*- coding: utf-8 -*-
'''This Code is Generate by Pyslvs.'''

from slvs import *
import matplotlib.pyplot as plt

#Please Choose Point number.
Point_num = 2
wx = Point_num*2+5
wy = Point_num*2+6
"""
    
    def table_process(self, table_point, table_line, table_chain, table_shaft, table_slider, table_rod, table_parameter):
        table_point_l = []
        table_line_l = []
        table_chain_l = []
        table_shaft_l = []
        table_slider_l = []
        table_rod_l = []
        table_parameter_l = []
        for i in range(table_parameter.rowCount()):
            try: table_parameter_l += [float(table_parameter.item(i, 1).text())]
            except: pass
        for i in range(table_point.rowCount()):
            k = []
            for j in range(1, 3):
                #float
                table_item = table_point.item(i, j).text()
                table_val = table_item if not 'n' in table_item else table_parameter_l[int(table_item.replace("n", ""))]
                k += [float(table_val)]
            #bool
            k += [bool(table_point.item(i, 3).checkState())]
            #XY
            try:
                k += [float(table_point.item(i, 4).text().replace("(", "").replace(")", "").split(", ")[0])]
                k += [float(table_point.item(i, 4).text().replace("(", "").replace(")", "").split(", ")[1])]
            except: pass
            table_point_l += [k]
        for i in range(table_line.rowCount()):
            k = []
            for j in range(1, 3):
                #int Point
                table_item = table_line.item(i, j).text().replace("Point", "")
                k += [int(table_item)]
            #float
            table_item = table_line.item(i, 3).text()
            table_val = table_item if not 'n' in table_item else table_parameter_l[int(table_item.replace("n", ""))]
            k += [float(table_val)]
            table_line_l += [k]
        for i in range(table_chain.rowCount()):
            k = []
            for j in range(1, 4):
                #int Point
                table_item = table_chain.item(i, j).text().replace("Point", "")
                k += [int(table_item)]
            for j in range(4, 7):
                #float
                table_item = table_chain.item(i, j).text()
                table_val = table_item if not 'n' in table_item else table_parameter_l[int(table_item.replace("n", ""))]
                k += [float(table_val)]
            table_chain_l += [k]
        for i in range(table_shaft.rowCount()):
            k = []
            for j in range(1, 3):
                #int Point
                table_item = table_shaft.item(i, j).text().replace("Point", "")
                k += [int(table_item)]
            for j in range(3, 5):
                #float angle
                table_item = table_shaft.item(i, j).text().replace("°", "")
                table_val = table_item if not 'n' in table_item else table_parameter_l[int(table_item.replace("n", ""))]
                k += [float(table_val)]
            table_item = table_shaft.item(i, 5).text().replace("°", "")
            k += [float(table_item if table_shaft.item(i, 5) else False)]
            table_shaft_l += [k]
        for i in range(table_slider.rowCount()):
            k = []
            #int Point
            k += [int(table_slider.item(i, 1).text().replace("Point", ""))]
            #int Line
            k += [int(table_slider.item(i, 2).text().replace("Line", ""))]
            table_slider_l += [k]
        for i in range(table_rod.rowCount()):
            k = []
            for j in range(1, 4):
                #int Point
                table_item = table_rod.item(i, j).text().replace("Point", "")
                k += [int(table_item)]
            #float
            table_item = table_rod.item(i, 4).text()
            table_val = table_item if not 'n' in table_item else table_parameter_l[int(table_item.replace("n", ""))]
            k += [float(table_val)]
            table_slider_l += [k]
        return table_point_l, table_line_l, table_chain_l, table_shaft_l, table_slider_l, table_rod_l
    
    def static_process(self, table_point, table_line, table_chain, table_shaft, table_slider, table_rod, filename, table_parameter):
        table_point, table_line, table_chain, table_shaft, table_slider, table_rod = self.table_process(table_point, table_line, table_chain, table_shaft, table_slider, table_rod, table_parameter)
        sys = System(1000)
        #Pre-oder
        p0 = sys.add_param(0.0)
        p1 = sys.add_param(0.0)
        p2 = sys.add_param(0.0)
        Point0 = Point3d(p0, p1, p2)
        qw, qx, qy, qz = Slvs_MakeQuaternion(1, 0, 0, 0, 1, 0)
        p3 = sys.add_param(qw)
        p4 = sys.add_param(qx)
        p5 = sys.add_param(qy)
        p6 = sys.add_param(qz)
        Normal1 = Normal3d(p3, p4, p5, p6)
        Workplane1 = Workplane(Point0, Normal1)
        p7 = sys.add_param(0.0)
        p8 = sys.add_param(0.0)
        Point1 = Point2d(Workplane1, p7, p8)
        Constraint.dragged(Workplane1, Point1)
        self.Script += """
def """+'_'.join(e for e in filename if e.isalnum())+"""(degree):
    sys = System(1000)
    p0 = sys.add_param(0.0)
    p1 = sys.add_param(0.0)
    p2 = sys.add_param(0.0)
    Point0 = Point3d(p0, p1, p2)
    qw, qx, qy, qz = Slvs_MakeQuaternion(1, 0, 0, 0, 1, 0)
    p3 = sys.add_param(qw)
    p4 = sys.add_param(qx)
    p5 = sys.add_param(qy)
    p6 = sys.add_param(qz)
    Normal1 = Normal3d(p3, p4, p5, p6)
    Workplane1 = Workplane(Point0, Normal1)
    other = -1 if degree >= 180 else 1

    p7 = sys.add_param(0.0)
    p8 = sys.add_param(0.0)
    Point1 = Point2d(Workplane1, p7, p8)
    Constraint.dragged(Workplane1, Point1)
"""
        Point = [Point1]
        #Load tables to constraint
        for i in range(1, len(table_point) if len(table_point)>=1 else 1):
            x_val = 0
            if not(len(table_shaft)>=1):
                x = sys.add_param(table_point[i][0])
                y = sys.add_param(table_point[i][1])
            else:
                for j in range(len(table_shaft)):
                    case = table_shaft[j][1]==i and table_shaft[j][4]
                    if case:
                        angle = table_shaft[j][4]
                        other = -1 if angle >= 180 else 1
                        a = table_shaft[j][0]
                        x = sys.add_param(table_point[a][0])
                        x_val = table_point[a][0]
                        y = sys.add_param(table_point[i][1]*other)
                    else:
                        x_val = table_point[i][0]
                        x = sys.add_param(table_point[i][0])
                        y = sys.add_param(table_point[i][1])
            p = Point2d(Workplane1, x, y)
            Point += [p]
            self.Script += """
    p"""+str(i*2+7)+""" = sys.add_param("""+str(x_val)+""")
    p"""+str(i*2+8)+""" = sys.add_param("""+str(table_point[i][1])+""")
    Point"""+str(i+1)+""" = Point2d(Workplane1, p"""+str(i*2+7)+""", p"""+str(i*2+8)+""")
"""
            if table_point[i][2]:
                Constraint.dragged(Workplane1, p)
                self.Script += """    Constraint.dragged(Workplane1, Point"""+str(i+1)+""")
"""
        for i in range(len(table_chain)):
            pa = table_chain[i][0]
            pb = table_chain[i][1]
            pc = table_chain[i][2]
            lengab = table_chain[i][3]
            lengbc = table_chain[i][4]
            lengac = table_chain[i][5]
            Constraint.distance(lengab, Workplane1, Point[pa], Point[pb])
            Constraint.distance(lengbc, Workplane1, Point[pb], Point[pc])
            Constraint.distance(lengac, Workplane1, Point[pa], Point[pc])
            self.Script += """    Constraint.distance("""+str(lengab)+""", Workplane1, Point"""+str(pa+1)+""", Point"""+str(pb+1)+""")
    Constraint.distance("""+str(lengbc)+""", Workplane1, Point"""+str(pb+1)+""", Point"""+str(pc+1)+""")
    Constraint.distance("""+str(lengac)+""", Workplane1, Point"""+str(pa+1)+""", Point"""+str(pc+1)+""")
"""
        for i in range(len(table_line)):
            start = table_line[i][0]
            end = table_line[i][1]
            leng = table_line[i][2]
            Constraint.distance(leng, Workplane1, Point[start], Point[end])
            self.Script += """    Constraint.distance("""+str(leng)+""", Workplane1, Point"""+str(start+1)+""", Point"""+str(end+1)+""")
"""
        for i in range(len(table_slider)):
            pt = table_slider[i][0]
            start = table_line[table_slider[i][1]][0]
            end = table_line[table_slider[i][1]][1]
            line = LineSegment2d(Workplane1, Point[start], Point[end])
            Constraint.on(Workplane1, Point[pt], line)
            self.Script += """    Constraint.on(Workplane1, Point"""+str(pt+1)+""", LineSegment2d(Workplane1, Point"""+str(start+1)+""", Point"""+str(end+1)+""")
"""
        for i in range(len(table_rod)):
            pt = table_rod[i][0]
            start = table_rod[i][1]
            end = table_rod[i][2]
            leng = table_rod[i][3]
            line = LineSegment2d(Workplane1, Point[start], Point[end])
            Constraint.on(Workplane1, Point[pt], line)
            Constraint.distance(leng, Workplane1, Point[start], Point[pt])
            self.Script += """    Constraint.on(Workplane1, Point"""+str(pt+1)+""", LineSegment2d(Workplane1, Point"""+str(start+1)+""", Point"""+str(end+1)+""")
    Constraint.distance("""+str(leng)+""", Workplane1, Point"""+str(start+1)+""", Point"""+str(pt+1)+""")
"""
        if len(table_shaft) >= 1:
            pN = sys.add_param(10)
            pNN = sys.add_param(0.0)
            PointN = Point2d(Workplane1, pN, pNN)
            Point += [PointN]
            Constraint.dragged(Workplane1, Point[-1])
            Line0 = LineSegment2d(Workplane1, Point[0], Point[-1])
            self.Script += """    px = sys.add_param(10)
    py = sys.add_param(0.0)
    PointN = Point2d(Workplane1, px, py)
    Constraint.dragged(Workplane1, PointN)
    Line0 = LineSegment2d(Workplane1, Point1, PointN)
"""
            for i in range(len(table_shaft)):
                center = table_shaft[i][0]
                reference = table_shaft[i][1]
                line = LineSegment2d(Workplane1, Point[center], Point[reference])
                try:
                    angle = table_shaft[i][4]
                    Constraint.angle(Workplane1, angle, line, Line0, False)
                except: pass
                self.Script += """    Line1 = LineSegment2d(Workplane1, Point"""+str(center+1)+""", Point"""+str(reference+1)+""")
    Constraint.angle(Workplane1, degree, Line1, Line0, False)
"""
        self.Script += """
    sys.solve()
    if (sys.result == SLVS_RESULT_OKAY):
        x = sys.get_param(wx).val
        y = sys.get_param(wy).val
        return x, y

if __name__=="__main__":
    Xval  = []
    Yval  = []
    for i in range(0, 361, 1):
        x, y = """+filename.replace(" ", "_")+"""(i)
        Xval += [x]
        Yval += [y]
    print("Solve Completed")
    plt.plot(Xval, Yval)
    plt.show()
"""
        sys.solve()
        result = []
        if (sys.result == SLVS_RESULT_OKAY):
            for i in range(len(table_point)*2):
                result += [sys.get_param(i+7).val]
        elif (sys.result == SLVS_RESULT_INCONSISTENT): print ("SLVS_RESULT_INCONSISTENT")
        elif (sys.result == SLVS_RESULT_DIDNT_CONVERGE): print ("SLVS_RESULT_DIDNT_CONVERGE")
        elif (sys.result == SLVS_RESULT_TOO_MANY_UNKNOWNS): print ("SLVS_RESULT_TOO_MANY_UNKNOWNS")
        return result, sys.dof
예제 #7
0
         Constraint.on(Workplane1, Point[pt], line)
     #TODO:
     for i in range(len(table_shaft)):
         center = table_shaft[i][0]
         reference = table_shaft[i][1]
         line = LineSegment2d(Workplane1, Point[center], Point[reference])
         Constraint.angle(Workplane1, angle, line, Line0, False)
     for i in range(len(table_rod)):
         pt = table_rod[i][0]
         start = table_rod[i][1]
         end = table_rod[i][2]
         leng = table_rod[i][3]
         line = LineSegment2d(Workplane1, Point[start], Point[end])
         Constraint.on(Workplane1, Point[pt], line)
         Constraint.distance(leng, Workplane1, Point[start], Point[pt])
     sys.solve()
     x = 0
     y = 0
     if (sys.result == SLVS_RESULT_OKAY):
         x = sys.get_param((point_int+2)*2+5).val
         y = sys.get_param((point_int+2)*2+6).val
     elif (sys.result == SLVS_RESULT_INCONSISTENT): print ("SLVS_RESULT_INCONSISTENT")
     elif (sys.result == SLVS_RESULT_DIDNT_CONVERGE): print ("SLVS_RESULT_DIDNT_CONVERGE")
     elif (sys.result == SLVS_RESULT_TOO_MANY_UNKNOWNS): print ("SLVS_RESULT_TOO_MANY_UNKNOWNS")
     return x, y
 
 def slvs_formate(self, table_point, table_line, table_chain, table_shaft, table_slider, table_rod, table_parameter):
     table_point, table_line, table_chain, table_shaft, table_slider, table_rod = self.table_process(table_point, table_line, table_chain, table_shaft, table_slider, table_rod, table_parameter)
     code = SLVS_Code()
     
     return code.output()