class Constraint(object): """ Object that stores info for a single constraint. """ def __init__(self, lhs, comparator, rhs, scaler, adder, scope=None): self.lhs = ExprEvaluator(lhs, scope=scope) if not self.lhs.check_resolve(): raise ValueError("Constraint '%s' has an invalid left-hand-side." \ % ' '.join([lhs, comparator, rhs])) self.comparator = comparator self.rhs = ExprEvaluator(rhs, scope=scope) if not self.rhs.check_resolve(): raise ValueError("Constraint '%s' has an invalid right-hand-side." \ % ' '.join([lhs, comparator, rhs])) if not isinstance(scaler, float): raise ValueError("Scaler parameter should be a float") self.scaler = scaler if scaler <= 0.0: raise ValueError("Scaler parameter should be a float > 0") if not isinstance(adder, float): raise ValueError("Adder parameter should be a float") self.adder = adder def evaluate(self, scope): """Returns a tuple of the form (lhs, rhs, comparator, is_violated).""" lhs = (self.lhs.evaluate(scope) + self.adder)*self.scaler rhs = (self.rhs.evaluate(scope) + self.adder)*self.scaler return (lhs, rhs, self.comparator, not _ops[self.comparator](lhs, rhs)) def evaluate_gradient(self, scope, stepsize=1.0e-6, wrt=None): """Returns the gradient of the constraint eq/inep as a tuple of the form (lhs, rhs, comparator, is_violated).""" lhs = self.lhs.evaluate_gradient(scope=scope, stepsize=stepsize, wrt=wrt) for key, value in lhs.iteritems(): lhs[key] = (value + self.adder)*self.scaler rhs = self.rhs.evaluate_gradient(scope=scope, stepsize=stepsize, wrt=wrt) for key, value in rhs.iteritems(): rhs[key] = (value + self.adder)*self.scaler return (lhs, rhs, self.comparator, not _ops[self.comparator](lhs, rhs)) def get_referenced_compnames(self): return self.lhs.get_referenced_compnames().union(self.rhs.get_referenced_compnames()) def __str__(self): return ' '.join([self.lhs.text, self.comparator, self.rhs.text])
def test_eval_gradient_lots_of_vars(self): top = set_as_top(Assembly()) top.add('comp1', B()) #build expr expr = "2*comp1.in1 + 3*comp1.in11" exp = ExprEvaluator(expr, top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp1.in1'], 2.0, 0.00001) assert_rel_error(self, grad['comp1.in11'], 3.0, 0.00001) expr = "asin(comp1.in1)" exp = ExprEvaluator(expr, top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp1.in1'], 1.0, 0.00001)
def test_eval_gradient_lots_of_vars(self): top = set_as_top(Assembly()) top.add('comp1', B()) #build expr expr = "2*comp1.in1 + 3*comp1.in11" exp = ExprEvaluator(expr, top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp1.in1'], 2.0, 0.00001) assert_rel_error(self, grad['comp1.in11'], 3.0, 0.00001) expr = "asin(comp1.in1)" exp = ExprEvaluator(expr, top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp1.in1'], 1.0, 0.00001)
def test_eval_gradient_array(self): top = set_as_top(Assembly()) top.add('comp1', A()) top.run() # Uncomment these when arrays work exp = ExprEvaluator('4.0*comp1.b2d[0][1]*comp1.b2d[1][1]', top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp1.b2d[0][1]'], 12.0, 0.00001) assert_rel_error(self, grad['comp1.b2d[1][1]'], 4.0, 0.00001)
def test_eval_gradient_array(self): top = set_as_top(Assembly()) top.add('comp1', A()) top.run() # Uncomment these when arrays work exp = ExprEvaluator('4.0*comp1.b2d[0][1]*comp1.b2d[1][1]', top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp1.b2d[0][1]'], 12.0, 0.00001) assert_rel_error(self, grad['comp1.b2d[1][1]'], 4.0, 0.00001)
def test_eval_gradient(self): top = set_as_top(Assembly()) top.add('comp1', Simple()) top.run() exp = ExprEvaluator('3.0*comp1.c', top.driver) grad = exp.evaluate_gradient(scope=top) self.assertEqual(top.comp1.c, 7.0) assert_rel_error(self, grad['comp1.c'], 3.0, 0.00001) # Commented out this test, until we find a case that can't be # handled analytically # interface test: step size # (for linear slope, larger stepsize more accurate because of # python's rounding) #grad2 = exp.evaluate_gradient(scope=top, stepsize=0.1) #assert( abs(grad['comp1.c'] - 3.0) > abs(grad2['comp1.c'] - 3.0) ) # More complicated, multiple comps top.add('comp2', Simple()) exp = ExprEvaluator('comp2.b*comp1.c**2', top.driver) grad = exp.evaluate_gradient(scope=top) self.assertEqual(len(grad), 2) assert_rel_error(self, grad['comp1.c'], 70.0, 0.00001) assert_rel_error(self, grad['comp2.b'], 49.0, 0.00001) # test limited varset grad = exp.evaluate_gradient(scope=top, wrt=['comp2.b']) self.assertEqual(len(grad), 1) exp = ExprEvaluator('pow(comp2.b,2)', top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp2.b'], 10.0, 0.00001) exp = ExprEvaluator('pow(comp2.b,3)', top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp2.b'], 75.0, 0.00001) exp = ExprEvaluator('log(comp2.a)', top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp2.a'], 1. / top.comp2.a, 0.00001) exp = ExprEvaluator('sin(cos(comp2.b))+sqrt(comp2.a)/comp1.c', top.driver) grad = exp.evaluate_gradient(scope=top) g1 = -sin(top.comp2.b) * cos(cos( top.comp2.b)) #true gradient components g2 = (2 * sqrt(top.comp2.a) * top.comp1.c)**-1 g3 = -sqrt(top.comp2.a) / top.comp1.c**2 assert_rel_error(self, grad['comp2.b'], g1, 0.00001) assert_rel_error(self, grad['comp2.a'], g2, 0.00001) assert_rel_error(self, grad['comp1.c'], g3, 0.00001)
def test_eval_gradient_array(self): top = set_as_top(Assembly()) top.add('comp1', A()) top.run() # Uncomment these when arrays work exp = ExprEvaluator('4.0*comp1.b2d[0][1]*comp1.b2d[1][1]', top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp1.b2d[0][1]'], 12.0, 0.00001) assert_rel_error(self, grad['comp1.b2d[1][1]'], 4.0, 0.00001) exp = ExprEvaluator('comp1.c2d**2', top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp1.c2d'][0, 0], 0.0, 0.00001) assert_rel_error(self, grad['comp1.c2d'][1, 1], 2.0, 0.00001) assert_rel_error(self, grad['comp1.c2d'][2, 2], 4.0, 0.00001) assert_rel_error(self, grad['comp1.c2d'][3, 3], 6.0, 0.00001) exp = ExprEvaluator('comp1.c1d**2', top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp1.c1d'][0, 0], 0.0, 0.00001) assert_rel_error(self, grad['comp1.c1d'][1, 1], 2.0, 0.00001) assert_rel_error(self, grad['comp1.c1d'][2, 2], 4.0, 0.00001) assert_rel_error(self, grad['comp1.c1d'][3, 3], 6.0, 0.00001) exp = ExprEvaluator('comp1.a2d + comp1.c2d**2', top.driver) grad = exp.evaluate_gradient(scope=top) a2d_grad, c2d_grad = grad['comp1.a2d'], grad['comp1.c2d'] assert_rel_error(self, a2d_grad[0, 0], 1.0, 0.00001) assert_rel_error(self, a2d_grad[1, 1], 1.0, 0.00001) assert_rel_error(self, a2d_grad[2, 2], 1.0, 0.00001) assert_rel_error(self, a2d_grad[3, 3], 1.0, 0.00001) assert_rel_error(self, c2d_grad[0, 0], 0.0, 0.00001) assert_rel_error(self, c2d_grad[1, 1], 2.0, 0.00001) assert_rel_error(self, c2d_grad[2, 2], 4.0, 0.00001) assert_rel_error(self, c2d_grad[3, 3], 6.0, 0.00001)
def test_eval_gradient_array(self): top = set_as_top(Assembly()) top.add('comp1', A()) top.run() # Uncomment these when arrays work exp = ExprEvaluator('4.0*comp1.b2d[0][1]*comp1.b2d[1][1]', top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp1.b2d[0][1]'], 12.0, 0.00001) assert_rel_error(self, grad['comp1.b2d[1][1]'], 4.0, 0.00001) exp = ExprEvaluator('comp1.c2d**2', top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp1.c2d'][0,0], 0.0, 0.00001) assert_rel_error(self, grad['comp1.c2d'][1,1], 2.0, 0.00001) assert_rel_error(self, grad['comp1.c2d'][2,2], 4.0, 0.00001) assert_rel_error(self, grad['comp1.c2d'][3,3], 6.0, 0.00001) exp = ExprEvaluator('comp1.c1d**2', top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp1.c1d'][0,0], 0.0, 0.00001) assert_rel_error(self, grad['comp1.c1d'][1,1], 2.0, 0.00001) assert_rel_error(self, grad['comp1.c1d'][2,2], 4.0, 0.00001) assert_rel_error(self, grad['comp1.c1d'][3,3], 6.0, 0.00001) exp = ExprEvaluator('comp1.a2d + comp1.c2d**2', top.driver) grad = exp.evaluate_gradient(scope=top) a2d_grad, c2d_grad = grad['comp1.a2d'], grad['comp1.c2d'] assert_rel_error(self, a2d_grad[0,0], 1.0, 0.00001) assert_rel_error(self, a2d_grad[1,1], 1.0, 0.00001) assert_rel_error(self, a2d_grad[2,2], 1.0, 0.00001) assert_rel_error(self, a2d_grad[3,3], 1.0, 0.00001) assert_rel_error(self, c2d_grad[0,0], 0.0, 0.00001) assert_rel_error(self, c2d_grad[1,1], 2.0, 0.00001) assert_rel_error(self, c2d_grad[2,2], 4.0, 0.00001) assert_rel_error(self, c2d_grad[3,3], 6.0, 0.00001)
def test_eval_gradient(self): top = set_as_top(Assembly()) top.add('comp1', Simple()) top.run() exp = ExprEvaluator('3.0*comp1.c', top.driver) grad = exp.evaluate_gradient(scope=top) self.assertEqual(top.comp1.c, 7.0) assert_rel_error(self, grad['comp1.c'], 3.0, 0.00001) # Commented out this test, until we find a case that can't be # handled analytically # interface test: step size # (for linear slope, larger stepsize more accurate because of # python's rounding) #grad2 = exp.evaluate_gradient(scope=top, stepsize=0.1) #assert( abs(grad['comp1.c'] - 3.0) > abs(grad2['comp1.c'] - 3.0) ) # More complicated, multiple comps top.add('comp2', Simple()) exp = ExprEvaluator('comp2.b*comp1.c**2', top.driver) grad = exp.evaluate_gradient(scope=top) self.assertEqual(len(grad), 2) assert_rel_error(self, grad['comp1.c'], 70.0, 0.00001) assert_rel_error(self, grad['comp2.b'], 49.0, 0.00001) # test limited varset grad = exp.evaluate_gradient(scope=top, wrt=['comp2.b']) self.assertEqual(len(grad), 1) exp = ExprEvaluator('pow(comp2.b,2)', top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp2.b'], 10.0, 0.00001) exp = ExprEvaluator('pow(comp2.b,3)', top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp2.b'], 75.0, 0.00001) exp = ExprEvaluator('log(comp2.a)', top.driver) grad = exp.evaluate_gradient(scope=top) assert_rel_error(self, grad['comp2.a'], 1./top.comp2.a, 0.00001) exp = ExprEvaluator('sin(cos(comp2.b))+sqrt(comp2.a)/comp1.c', top.driver) grad = exp.evaluate_gradient(scope=top) g1=-sin(top.comp2.b)*cos(cos(top.comp2.b)) #true gradient components g2=(2*sqrt(top.comp2.a)*top.comp1.c)**-1 g3=-sqrt(top.comp2.a)/top.comp1.c**2 assert_rel_error(self, grad['comp2.b'], g1, 0.00001) assert_rel_error(self, grad['comp2.a'], g2, 0.00001) assert_rel_error(self, grad['comp1.c'], g3, 0.00001)
class Constraint(object): """ Object that stores info for a single constraint. """ def __init__(self, lhs, comparator, rhs, scope): self.lhs = ExprEvaluator(lhs, scope=scope) unresolved_vars = self.lhs.get_unresolved() if unresolved_vars: msg = "Left hand side of constraint '{0}' has invalid variables {1}" expression = ' '.join((lhs, comparator, rhs)) raise ExprEvaluator._invalid_expression_error(unresolved_vars, expr=expression, msg=msg) self.rhs = ExprEvaluator(rhs, scope=scope) unresolved_vars = self.rhs.get_unresolved() if unresolved_vars: msg = "Right hand side of constraint '{0}' has invalid variables {1}" expression = ' '.join((lhs, comparator, rhs)) raise ExprEvaluator._invalid_expression_error(unresolved_vars, expr=expression, msg=msg) self.comparator = comparator self.pcomp_name = None self._size = None # Linear flag: constraints are nonlinear by default self.linear = False @property def size(self): """Total scalar items in this constraint.""" if self._size is None: self._size = len(self.evaluate(self.lhs.scope)) return self._size def activate(self): """Make this constraint active by creating the appropriate connections in the dependency graph. """ if self.pcomp_name is None: pseudo = PseudoComponent(self.lhs.scope, self._combined_expr(), pseudo_type='constraint') self.pcomp_name = pseudo.name self.lhs.scope.add(pseudo.name, pseudo) getattr(self.lhs.scope, pseudo.name).make_connections(self.lhs.scope) def deactivate(self): """Remove this constraint from the dependency graph and remove its pseudocomp from the scoping object. """ if self.pcomp_name: scope = self.lhs.scope try: pcomp = getattr(scope, self.pcomp_name) except AttributeError: pass else: scope.remove(pcomp.name) finally: self.pcomp_name = None def _combined_expr(self): """Given a constraint object, take the lhs, operator, and rhs and combine them into a single expression by moving rhs terms over to the lhs. For example, for the constraint 'C1.x < C2.y + 7', return the expression 'C1.x - C2.y - 7'. Depending on the direction of the operator, the sign of the expression may be flipped. The final form of the constraint, when evaluated, will be considered to be satisfied if it evaluates to a value <= 0. """ scope = self.lhs.scope if self.comparator.startswith('>'): first = self.rhs.text second = self.lhs.text else: first = self.lhs.text second = self.rhs.text first_zero = False try: f = float(first) except Exception: pass else: if f == 0: first_zero = True second_zero = False try: f = float(second) except Exception: pass else: if f == 0: second_zero = True if first_zero: newexpr = "-(%s)" % second elif second_zero: newexpr = "%s" % first else: newexpr = '%s-(%s)' % (first, second) return ExprEvaluator(newexpr, scope) def copy(self): """ Returns a copy of our self. """ return Constraint(str(self.lhs), self.comparator, str(self.rhs), scope=self.lhs.scope) def evaluate(self, scope): """Returns the value of the constraint as a sequence.""" pcomp = getattr(scope, self.pcomp_name) val = pcomp.out0 if isinstance(val, ndarray): return val.flatten() else: return [val] def evaluate_gradient(self, scope, stepsize=1.0e-6, wrt=None): """Returns the gradient of the constraint eq/ineq as a tuple of the form (lhs, rhs, comparator, is_violated).""" lhs = self.lhs.evaluate_gradient(scope=scope, stepsize=stepsize, wrt=wrt) if isinstance(self.rhs, float): rhs = 0. else: rhs = self.rhs.evaluate_gradient(scope=scope, stepsize=stepsize, wrt=wrt) return (lhs, rhs, self.comparator, not _ops[self.comparator](lhs, rhs)) def get_referenced_compnames(self): """Returns a set of names of each component referenced by this constraint. """ if isinstance(self.rhs, float): return self.lhs.get_referenced_compnames() else: return self.lhs.get_referenced_compnames().union( self.rhs.get_referenced_compnames()) def get_referenced_varpaths(self, copy=True): """Returns a set of names of each component referenced by this constraint. """ if isinstance(self.rhs, float): return self.lhs.get_referenced_varpaths(copy=copy) else: return self.lhs.get_referenced_varpaths(copy=copy).union( self.rhs.get_referenced_varpaths(copy=copy)) def __str__(self): return ' '.join((str(self.lhs), self.comparator, str(self.rhs))) def __eq__(self, other): if not isinstance(other, Constraint): return False return (self.lhs, self.comparator, self.rhs) == \ (other.lhs, other.comparator, other.rhs)
class Constraint(object): """ Object that stores info for a single constraint. """ def __init__(self, lhs, comparator, rhs, scaler, adder, scope=None): self.lhs = ExprEvaluator(lhs, scope=scope) if not self.lhs.check_resolve(): raise ValueError("Constraint '%s' has an invalid left-hand-side." \ % ' '.join([lhs, comparator, rhs])) self.comparator = comparator self.rhs = ExprEvaluator(rhs, scope=scope) if not self.rhs.check_resolve(): raise ValueError("Constraint '%s' has an invalid right-hand-side." \ % ' '.join([lhs, comparator, rhs])) if not isinstance(scaler, float): raise ValueError("Scaler parameter should be a float") self.scaler = scaler if scaler <= 0.0: raise ValueError("Scaler parameter should be a float > 0") if not isinstance(adder, float): raise ValueError("Adder parameter should be a float") self.adder = adder def copy(self): return Constraint(self.lhs.text, self.comparator, self.rhs.text, self.scaler, self.adder, scope=self.lhs.scope) def evaluate(self, scope): """Returns a tuple of the form (lhs, rhs, comparator, is_violated).""" lhs = (self.lhs.evaluate(scope) + self.adder) * self.scaler rhs = (self.rhs.evaluate(scope) + self.adder) * self.scaler return (lhs, rhs, self.comparator, not _ops[self.comparator](lhs, rhs)) def evaluate_gradient(self, scope, stepsize=1.0e-6, wrt=None): """Returns the gradient of the constraint eq/inep as a tuple of the form (lhs, rhs, comparator, is_violated).""" lhs = self.lhs.evaluate_gradient(scope=scope, stepsize=stepsize, wrt=wrt) for key, value in lhs.iteritems(): lhs[key] = (value + self.adder) * self.scaler rhs = self.rhs.evaluate_gradient(scope=scope, stepsize=stepsize, wrt=wrt) for key, value in rhs.iteritems(): rhs[key] = (value + self.adder) * self.scaler return (lhs, rhs, self.comparator, not _ops[self.comparator](lhs, rhs)) def get_referenced_compnames(self): return self.lhs.get_referenced_compnames().union( self.rhs.get_referenced_compnames()) def __str__(self): return ' '.join([self.lhs.text, self.comparator, self.rhs.text]) def __eq__(self, other): if not isinstance(other, Constraint): return False return (self.lhs,self.comparator,self.rhs,self.scaler,self.adder) == \ (other.lhs,other.comparator,other.rhs,other.scaler,other.adder)
class Constraint(object): """ Object that stores info for a single constraint. """ def __init__(self, lhs, comparator, rhs, scope): self.lhs = ExprEvaluator(lhs, scope=scope) if not self.lhs.check_resolve(): raise ValueError("Constraint '%s' has an invalid left-hand-side." % ' '.join([lhs, comparator, rhs])) self.comparator = comparator self.rhs = ExprEvaluator(rhs, scope=scope) if not self.rhs.check_resolve(): raise ValueError("Constraint '%s' has an invalid right-hand-side." % ' '.join([lhs, comparator, rhs])) self.pcomp_name = None self._size = None @property def size(self): """Total scalar items in this constraint.""" if self._size is None: self._size = len(self.evaluate(self.lhs.scope)) return self._size def activate(self): """Make this constraint active by creating the appropriate connections in the dependency graph. """ if self.pcomp_name is None: pseudo = PseudoComponent(self.lhs.scope, self._combined_expr(), pseudo_type='constraint') self.pcomp_name = pseudo.name self.lhs.scope.add(pseudo.name, pseudo) getattr(self.lhs.scope, pseudo.name).make_connections(self.lhs.scope) def deactivate(self): """Remove this constraint from the dependency graph and remove its pseudocomp from the scoping object. """ if self.pcomp_name: scope = self.lhs.scope try: pcomp = getattr(scope, self.pcomp_name) except AttributeError: pass else: # pcomp.remove_connections(scope) # if hasattr(scope, pcomp.name): scope.remove(pcomp.name) finally: self.pcomp_name = None def _combined_expr(self): """Given a constraint object, take the lhs, operator, and rhs and combine them into a single expression by moving rhs terms over to the lhs. For example, for the constraint 'C1.x < C2.y + 7', return the expression 'C1.x - C2.y - 7'. Depending on the direction of the operator, the sign of the expression may be flipped. The final form of the constraint, when evaluated, will be considered to be satisfied if it evaluates to a value <= 0. """ scope = self.lhs.scope if self.comparator.startswith('>'): first = self.rhs.text second = self.lhs.text else: first = self.lhs.text second = self.rhs.text first_zero = False try: f = float(first) except Exception: pass else: if f == 0: first_zero = True second_zero = False try: f = float(second) except Exception: pass else: if f == 0: second_zero = True if first_zero: newexpr = "-(%s)" % second elif second_zero: newexpr = "%s" % first else: newexpr = '%s-(%s)' % (first, second) return ExprEvaluator(newexpr, scope) def copy(self): return Constraint(str(self.lhs), self.comparator, str(self.rhs), scope=self.lhs.scope) def evaluate(self, scope): """Returns the value of the constraint as a sequence.""" pcomp = getattr(scope, self.pcomp_name) if not pcomp.is_valid(): pcomp.update_outputs(['out0']) val = pcomp.out0 if isinstance(val, ndarray): return val.flatten() else: return [val] def evaluate_gradient(self, scope, stepsize=1.0e-6, wrt=None): """Returns the gradient of the constraint eq/ineq as a tuple of the form (lhs, rhs, comparator, is_violated).""" lhs = self.lhs.evaluate_gradient(scope=scope, stepsize=stepsize, wrt=wrt) if isinstance(self.rhs, float): rhs = 0. else: rhs = self.rhs.evaluate_gradient(scope=scope, stepsize=stepsize, wrt=wrt) return (lhs, rhs, self.comparator, not _ops[self.comparator](lhs, rhs)) def get_referenced_compnames(self): """Returns a set of names of each component referenced by this constraint. """ if isinstance(self.rhs, float): return self.lhs.get_referenced_compnames() else: return self.lhs.get_referenced_compnames().union( self.rhs.get_referenced_compnames()) def get_referenced_varpaths(self, copy=True): """Returns a set of names of each component referenced by this constraint. """ if isinstance(self.rhs, float): return self.lhs.get_referenced_varpaths(copy=copy) else: return self.lhs.get_referenced_varpaths(copy=copy).union( self.rhs.get_referenced_varpaths(copy=copy)) def __str__(self): return ' '.join([str(self.lhs), self.comparator, str(self.rhs)]) def __eq__(self, other): if not isinstance(other, Constraint): return False return (self.lhs, self.comparator, self.rhs) == \ (other.lhs, other.comparator, other.rhs)