当前位置: 首页>>代码示例>>Python>>正文


Python ExprEvaluator.get_unresolved方法代码示例

本文整理汇总了Python中openmdao.main.expreval.ExprEvaluator.get_unresolved方法的典型用法代码示例。如果您正苦于以下问题:Python ExprEvaluator.get_unresolved方法的具体用法?Python ExprEvaluator.get_unresolved怎么用?Python ExprEvaluator.get_unresolved使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在openmdao.main.expreval.ExprEvaluator的用法示例。


在下文中一共展示了ExprEvaluator.get_unresolved方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Constraint2Sided

# 需要导入模块: from openmdao.main.expreval import ExprEvaluator [as 别名]
# 或者: from openmdao.main.expreval.ExprEvaluator import get_unresolved [as 别名]
class Constraint2Sided(Constraint):
    """ Object that stores info for a double-sided constraint. """

    def __init__(self, lhs, center, rhs, comparator, scope, jacs=None):
        self.lhs = ExprEvaluator(lhs, scope=scope)
        unresolved_vars = self.lhs.get_unresolved()

        self._pseudo = None
        self.pcomp_name = None

        if unresolved_vars:
            msg = "Left hand side of constraint '{0}' has invalid variables {1}"
            expression = ' '.join((lhs, comparator, center, comparator,
                                   rhs))

            raise ExprEvaluator._invalid_expression_error(unresolved_vars,
                                                          expr=expression,
                                                          msg=msg)
        self.center = ExprEvaluator(center, scope=scope)
        unresolved_vars = self.center.get_unresolved()

        if unresolved_vars:
            msg = "Center of constraint '{0}' has invalid variables {1}"
            expression = ' '.join((lhs, comparator, center, 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, center, comparator,
                                   rhs))

            raise ExprEvaluator._invalid_expression_error(unresolved_vars,
                                                          expr=expression,
                                                          msg=msg)
        self.comparator = comparator
        self._size = None

        # Linear flag: constraints are nonlinear by default
        self.linear = False

        self.low = self.lhs.evaluate()
        self.high = self.rhs.evaluate()

        # User-defined jacobian function
        self.jacs = jacs

        self._create_pseudo()

    def _create_pseudo(self):
        """Create our pseudo component."""
        scope = self.lhs.scope
        refs = list(self.center.ordered_refs())
        pseudo_class = PseudoComponent

        # look for a<var1<b
        if len(refs) == 1 and self.center.text == refs[0]:
            pseudo_class = SimpleEQ0PComp

        self._pseudo = pseudo_class(scope,
                                    self.center,
                                    pseudo_type='constraint',
                                    subtype='inequality',
                                    exprobject=self)

        self.pcomp_name = self._pseudo.name

    def _combined_expr(self):
        """Only need the center expression
        """
        return self.center

    def copy(self):
        """ Returns a copy of our self. """
        return Constraint2Sided(str(self.lhs), str(self.center), str(self.rhs),
                          self.comparator, scope=self.lhs.scope,
                          jacs=self.jacs)

    def get_referenced_compnames(self):
        """Returns a set of names of each component referenced by this
        constraint.
        """
        return self.center.get_referenced_compnames()

    def get_referenced_varpaths(self, copy=True, refs=False):
        """Returns a set of names of each component referenced by this
        constraint.
        """
        return self.center.get_referenced_varpaths(copy=copy, refs=refs)

    def name_changed(self, old, new):
        """Update expressions if necessary when an object is renamed."""
        self.rhs.name_changed(old, new)
        self.lhs.name_changed(old, new)
        self.center.name_changed(old, new)
#.........这里部分代码省略.........
开发者ID:ChanChiChoi,项目名称:OpenMDAO-Framework,代码行数:103,代码来源:hasconstraints.py

示例2: Constraint

# 需要导入模块: from openmdao.main.expreval import ExprEvaluator [as 别名]
# 或者: from openmdao.main.expreval.ExprEvaluator import get_unresolved [as 别名]
class Constraint(object):
    """ Object that stores info for a single constraint. """

    def __init__(self, lhs, comparator, rhs, scope, jacs=None):
        self.lhs = ExprEvaluator(lhs, scope=scope)
        self._pseudo = None
        self.pcomp_name = None
        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._size = None

        # Linear flag: constraints are nonlinear by default
        self.linear = False

        # User-defined jacobian function
        self.jacs = jacs

        self._create_pseudo()

    @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 _create_pseudo(self):
        """Create our pseudo component."""
        if self.comparator == '=':
            subtype = 'equality'
        else:
            subtype = 'inequality'

        # check for simple structure of equality constraint,
        # either
        #     var1 = var2
        #  OR
        #     var1 - var2 = 0
        #  OR
        #     var1 = 0
        lrefs = list(self.lhs.ordered_refs())
        rrefs = list(self.rhs.ordered_refs())

        try:
            leftval = float(self.lhs.text)
        except ValueError:
            leftval = None

        try:
            rightval = float(self.rhs.text)
        except ValueError:
            rightval = None

        pseudo_class = PseudoComponent

        if self.comparator == '=':
            # look for var1-var2=0
            if len(lrefs) == 2 and len(rrefs) == 0:
                if rightval == 0. and \
                        _remove_spaces(self.lhs.text) == \
                            lrefs[0]+'-'+lrefs[1]:
                    pseudo_class = SimpleEQConPComp
            # look for 0=var1-var2
            elif len(lrefs) == 0 and len(rrefs) == 2:
                if leftval==0. and \
                       _remove_spaces(self.rhs.text) == \
                            rrefs[0]+'-'+rrefs[1]:
                    pseudo_class = SimpleEQConPComp
            # look for var1=var2
            elif len(lrefs) == 1 and len(rrefs) == 1:
                if lrefs[0] == self.lhs.text and \
                           rrefs[0] == self.rhs.text:
                    pseudo_class = SimpleEQConPComp
            # look for var1=0
            elif len(lrefs) == 1 and len(rrefs) == 0 and rightval is not None:
                pseudo_class = SimpleEQ0PComp

        self._pseudo = pseudo_class(self.lhs.scope,
                                    self._combined_expr(),
                                    pseudo_type='constraint',
                                    subtype=subtype,
                                    exprobject=self)
#.........这里部分代码省略.........
开发者ID:ChanChiChoi,项目名称:OpenMDAO-Framework,代码行数:103,代码来源:hasconstraints.py

示例3: Constraint

# 需要导入模块: from openmdao.main.expreval import ExprEvaluator [as 别名]
# 或者: from openmdao.main.expreval.ExprEvaluator import get_unresolved [as 别名]
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
#.........这里部分代码省略.........
开发者ID:FashtimeDotCom,项目名称:OpenMDAO-Framework,代码行数:103,代码来源:hasconstraints.py


注:本文中的openmdao.main.expreval.ExprEvaluator.get_unresolved方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。