From 9e8c5ac5d685aef9224289a5bac530250aee7298 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 23 Apr 2011 22:23:07 -0400 Subject: [PATCH] Sketch: only attempt to solve if #vars == #equs --- Sketch.py | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/Sketch.py b/Sketch.py index f6fc82a..3b7a4c2 100644 --- a/Sketch.py +++ b/Sketch.py @@ -6,6 +6,8 @@ class Sketch(object): def __init__(self): self.shapes = [] self.constraints = [] + self.num_variables = 0 + self.num_constraints = 0 def __iter__(self): return self.shapes.__iter__() @@ -28,26 +30,28 @@ class Sketch(object): shape_vars[shape_var] = varid varid += 1 varid_to_shape_var.append(shape_var) - dim = len(equations) - if len(shape_vars) > dim: - dim = len(shape_vars) - equs = [] - variables = oovars(dim) - for e in equations: - coeffs = e[0] - equ = -e[1] - for c in coeffs: - equ = c[0] * variables[shape_vars[c[1]]] + equ - equs.append(equ) - if len(equs) < dim: - print 'TODO: pad equation list: %d equations, %d vars' % \ - (len(equs), len(variables)) - s = sle(equs) - r = s.solve() - if r.ff != float('inf'): - print "SOLVED: ", r(variables) - for i in range(len(variables)): - varid_to_shape_var[i][0].setVar(varid_to_shape_var[i][1], - r(variables[i])) - for s in self.shapes: - print 'shape:', repr(s.vars) + self.num_constraints = len(equations) + self.num_variables = len(shape_vars) + dim = self.num_constraints + if self.num_variables > dim: + dim = self.num_variables + if self.num_variables == self.num_constraints: + sle_equs = [] + variables = oovars(dim) + for e in equations: + coeffs = e[0] + equ = -e[1] + for c in coeffs: + equ = c[0] * variables[shape_vars[c[1]]] + equ + sle_equs.append(equ) + s = sle(sle_equs) + r = s.solve() + if r.ff != float('inf'): + print "SOLVED: ", r(variables) + for i in range(len(variables)): + varid_to_shape_var[i][0].setVar(varid_to_shape_var[i][1], + r(variables[i])) + for s in self.shapes: + print 'shape:', repr(s.vars) + return True + return False