def _register_constraint(self, con): if type(con.expr) == ConditionalExpression: self._register_conditional_constraint(con) return None ccon = self._evaluator.add_constraint() con._c_obj = ccon self._con_ccon_map[con] = ccon leaf_ndx_map = OrderedDict() referenced_vars = OrderedSet() referenced_params = OrderedSet() referenced_floats = OrderedSet() ndx = 0 for v in con.expr.get_vars(): leaf_ndx_map[v] = ndx ndx += 1 cvar = self._increment_var(v) ccon.add_leaf(cvar) referenced_vars.add(v) for p in con.expr.get_params(): leaf_ndx_map[p] = ndx ndx += 1 cparam = self._increment_param(p) ccon.add_leaf(cparam) referenced_params.add(p) for f in con.expr.get_floats(): leaf_ndx_map[f] = ndx ndx += 1 cfloat = self._increment_float(f) ccon.add_leaf(cfloat) referenced_floats.add(f) fn_rpn = con.expr.get_rpn(leaf_ndx_map) for term in fn_rpn: ccon.add_fn_rpn_term(term) jac = con.expr.reverse_sd() for v in con.expr.get_vars(): jac_v = jac[v] if type(jac_v) in native_numeric_types: jac_v = Float(jac_v) for f in jac_v.get_floats(): if f not in leaf_ndx_map: leaf_ndx_map[f] = ndx ndx += 1 cfloat = self._increment_float(f) ccon.add_leaf(cfloat) referenced_floats.add(f) jac_rpn = jac_v.get_rpn(leaf_ndx_map) cvar = self._var_cvar_map[v] for term in jac_rpn: ccon.add_jac_rpn_term(cvar, term) self._vars_referenced_by_con[con] = referenced_vars self._params_referenced_by_con[con] = referenced_params self._floats_referenced_by_con[con] = referenced_floats
def add_usage(self, key, *args): """add args to usage[key]""" if not key: return if not (key in self._usage): self._usage[key] = OrderedSet() for arg in args: self._usage[key].add(arg)
def _collect_leaves(self): self._vars = OrderedSet() self._params = OrderedSet() self._floats = OrderedSet() for oper in self.operators(): for operand in oper.operands(): if operand.is_leaf(): if operand.is_variable_type(): self._vars.add(operand) elif operand.is_parameter_type(): self._params.add(operand) elif operand.is_float_type(): self._floats.add(operand) elif operand.is_expression_type(): self._vars.update(operand.get_vars()) self._params.update(operand.get_params()) self._floats.update(operand.get_floats()) else: raise ValueError('operand type not recognized: ' + str(operand))
def __init__(self): self._observers = OrderedSet()
def get_leaves(self): return OrderedSet([self])
def get_floats(self): return OrderedSet()
def get_params(self): return OrderedSet([self])
def get_vars(self): return OrderedSet()
def _register_conditional_constraint(self, con): ccon = self._evaluator.add_if_else_constraint() con._c_obj = ccon self._con_ccon_map[con] = ccon leaf_ndx_map = OrderedDict() referenced_vars = OrderedSet() referenced_params = OrderedSet() referenced_floats = OrderedSet() ndx = 0 derivs = list() for expr in con.expr._conditions: referenced_vars.update(expr.get_vars()) referenced_params.update(expr.get_params()) referenced_floats.update(expr.get_floats()) for expr in con.expr._exprs: referenced_vars.update(expr.get_vars()) referenced_params.update(expr.get_params()) referenced_floats.update(expr.get_floats()) for expr in con.expr._exprs: _deriv = expr.reverse_sd() derivs.append(_deriv) for v in referenced_vars: if v not in _deriv: _deriv[v] = Float(0) elif type(_deriv[v]) in native_numeric_types: _deriv[v] = Float(_deriv[v]) referenced_floats.update(_deriv[v].get_floats()) for v in referenced_vars: leaf_ndx_map[v] = ndx ndx += 1 cvar = self._increment_var(v) ccon.add_leaf(cvar) for v in referenced_params: leaf_ndx_map[v] = ndx ndx += 1 cvar = self._increment_param(v) ccon.add_leaf(cvar) for v in referenced_floats: leaf_ndx_map[v] = ndx ndx += 1 cvar = self._increment_float(v) ccon.add_leaf(cvar) for i in range(len(con.expr._conditions)): condition_rpn = con.expr._conditions[i].get_rpn(leaf_ndx_map) for term in condition_rpn: ccon.add_condition_rpn_term(term) fn_rpn = con.expr._exprs[i].get_rpn(leaf_ndx_map) for term in fn_rpn: ccon.add_fn_rpn_term(term) for v in referenced_vars: cvar = v._c_obj jac = derivs[i][v] jac_rpn = jac.get_rpn(leaf_ndx_map) for term in jac_rpn: ccon.add_jac_rpn_term(cvar, term) ccon.end_condition() self._vars_referenced_by_con[con] = referenced_vars self._params_referenced_by_con[con] = referenced_params self._floats_referenced_by_con[con] = referenced_floats
def add(self, obj, attr, func): if (obj, attr) not in self.update_functions: self.update_functions[(obj, attr)] = OrderedSet() self.update_functions[(obj, attr)].add(func)