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


Python ConcreteModel.S方法代码示例

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


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

示例1: test_get_check_units_on_all_expressions

# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import S [as 别名]
    def test_get_check_units_on_all_expressions(self):
        # this method is going to test all the expression types that should work
        # to be defensive, we will also test that we actually have the expected expression type
        # therefore, if the expression system changes and we get a different expression type,
        # we will know we need to change these tests

        uc = units
        kg = uc.kg
        m = uc.m

        model = ConcreteModel()
        model.x = Var()
        model.y = Var()
        model.z = Var()
        model.p = Param(initialize=42.0, mutable=True)

        # test equality
        self._get_check_units_ok(3.0*kg == 1.0*kg, uc, 'kg', expr.EqualityExpression)
        self._get_check_units_fail(3.0*kg == 2.0*m, uc, expr.EqualityExpression)

        # test inequality
        self._get_check_units_ok(3.0*kg <= 1.0*kg, uc, 'kg', expr.InequalityExpression)
        self._get_check_units_fail(3.0*kg <= 2.0*m, uc, expr.InequalityExpression)
        self._get_check_units_ok(3.0*kg >= 1.0*kg, uc, 'kg', expr.InequalityExpression)
        self._get_check_units_fail(3.0*kg >= 2.0*m, uc, expr.InequalityExpression)

        # test RangedExpression
        self._get_check_units_ok(inequality(3.0*kg, 4.0*kg, 5.0*kg), uc, 'kg', expr.RangedExpression)
        self._get_check_units_fail(inequality(3.0*m, 4.0*kg, 5.0*kg), uc, expr.RangedExpression)
        self._get_check_units_fail(inequality(3.0*kg, 4.0*m, 5.0*kg), uc, expr.RangedExpression)
        self._get_check_units_fail(inequality(3.0*kg, 4.0*kg, 5.0*m), uc, expr.RangedExpression)

        # test SumExpression, NPV_SumExpression
        self._get_check_units_ok(3.0*model.x*kg + 1.0*model.y*kg + 3.65*model.z*kg, uc, 'kg', expr.SumExpression)
        self._get_check_units_fail(3.0*model.x*kg + 1.0*model.y*m + 3.65*model.z*kg, uc, expr.SumExpression)

        self._get_check_units_ok(3.0*kg + 1.0*kg + 2.0*kg, uc, 'kg', expr.NPV_SumExpression)
        self._get_check_units_fail(3.0*kg + 1.0*kg + 2.0*m, uc, expr.NPV_SumExpression)

        # test ProductExpression, NPV_ProductExpression
        self._get_check_units_ok(model.x*kg * model.y*m, uc, 'kg * m', expr.ProductExpression)
        self._get_check_units_ok(3.0*kg * 1.0*m, uc, 'kg * m', expr.NPV_ProductExpression)
        self._get_check_units_ok(3.0*kg*m, uc, 'kg * m', expr.NPV_ProductExpression)
        # I don't think that there are combinations that can "fail" for products

        # test MonomialTermExpression
        self._get_check_units_ok(model.x*kg, uc, 'kg', expr.MonomialTermExpression)

        # test ReciprocalExpression, NPV_ReciprocalExpression
        self._get_check_units_ok(1.0/(model.x*kg), uc, '1 / kg', expr.ReciprocalExpression)
        self._get_check_units_ok(1.0/kg, uc, '1 / kg', expr.NPV_ReciprocalExpression)
        # I don't think that there are combinations that can "fail" for products

        # test PowExpression, NPV_PowExpression
        # ToDo: fix the str representation to combine the powers or the expression system
        self._get_check_units_ok((model.x*kg**2)**3, uc, 'kg ** 6', expr.PowExpression) # would want this to be kg**6
        self._get_check_units_fail(kg**model.x, uc, expr.PowExpression, UnitsError)
        self._get_check_units_fail(model.x**kg, uc, expr.PowExpression, UnitsError)
        self._get_check_units_ok(kg**2, uc, 'kg ** 2', expr.NPV_PowExpression)
        self._get_check_units_fail(3.0**kg, uc, expr.NPV_PowExpression, UnitsError)

        # test NegationExpression, NPV_NegationExpression
        self._get_check_units_ok(-(kg*model.x*model.y), uc, 'kg', expr.NegationExpression)
        self._get_check_units_ok(-kg, uc, 'kg', expr.NPV_NegationExpression)
        # don't think there are combinations that fan "fail" for negation

        # test AbsExpression, NPV_AbsExpression
        self._get_check_units_ok(abs(kg*model.x), uc, 'kg', expr.AbsExpression)
        self._get_check_units_ok(abs(kg), uc, 'kg', expr.NPV_AbsExpression)
        # don't think there are combinations that fan "fail" for abs

        # test the different UnaryFunctionExpression / NPV_UnaryFunctionExpression types
        # log
        self._get_check_units_ok(log(3.0*model.x), uc, None, expr.UnaryFunctionExpression)
        self._get_check_units_fail(log(3.0*kg*model.x), uc, expr.UnaryFunctionExpression, UnitsError)
        self._get_check_units_ok(log(3.0*model.p), uc, None, expr.NPV_UnaryFunctionExpression)
        self._get_check_units_fail(log(3.0*kg), uc, expr.NPV_UnaryFunctionExpression, UnitsError)
        # log10
        self._get_check_units_ok(log10(3.0*model.x), uc, None, expr.UnaryFunctionExpression)
        self._get_check_units_fail(log10(3.0*kg*model.x), uc, expr.UnaryFunctionExpression, UnitsError)
        self._get_check_units_ok(log10(3.0*model.p), uc, None, expr.NPV_UnaryFunctionExpression)
        self._get_check_units_fail(log10(3.0*kg), uc, expr.NPV_UnaryFunctionExpression, UnitsError)
        # sin
        self._get_check_units_ok(sin(3.0*model.x*uc.radians), uc, None, expr.UnaryFunctionExpression)
        self._get_check_units_fail(sin(3.0*kg*model.x), uc, expr.UnaryFunctionExpression, UnitsError)
        self._get_check_units_fail(sin(3.0*kg*model.x*uc.kg), uc, expr.UnaryFunctionExpression, UnitsError)
        self._get_check_units_ok(sin(3.0*model.p*uc.radians), uc, None, expr.NPV_UnaryFunctionExpression)
        self._get_check_units_fail(sin(3.0*kg), uc, expr.NPV_UnaryFunctionExpression, UnitsError)
        # cos
        self._get_check_units_ok(cos(3.0*model.x*uc.radians), uc, None, expr.UnaryFunctionExpression)
        self._get_check_units_fail(cos(3.0*kg*model.x), uc, expr.UnaryFunctionExpression, UnitsError)
        self._get_check_units_fail(cos(3.0*kg*model.x*uc.kg), uc, expr.UnaryFunctionExpression, UnitsError)
        self._get_check_units_ok(cos(3.0*model.p*uc.radians), uc, None, expr.NPV_UnaryFunctionExpression)
        self._get_check_units_fail(cos(3.0*kg), uc, expr.NPV_UnaryFunctionExpression, UnitsError)
        # tan
        self._get_check_units_ok(tan(3.0*model.x*uc.radians), uc, None, expr.UnaryFunctionExpression)
        self._get_check_units_fail(tan(3.0*kg*model.x), uc, expr.UnaryFunctionExpression, UnitsError)
        self._get_check_units_fail(tan(3.0*kg*model.x*uc.kg), uc, expr.UnaryFunctionExpression, UnitsError)
        self._get_check_units_ok(tan(3.0*model.p*uc.radians), uc, None, expr.NPV_UnaryFunctionExpression)
        self._get_check_units_fail(tan(3.0*kg), uc, expr.NPV_UnaryFunctionExpression, UnitsError)
#.........这里部分代码省略.........
开发者ID:Pyomo,项目名称:pyomo,代码行数:103,代码来源:test_units.py

示例2: create_model

# 需要导入模块: from pyomo.environ import ConcreteModel [as 别名]
# 或者: from pyomo.environ.ConcreteModel import S [as 别名]
def create_model(model_name, nodes, links, type_nodes, type_links, timesteps, params):

    m = ConcreteModel(name=model_name)

    # SETS

    # basic sets
    m.Nodes = Set(initialize=nodes)  # nodes
    m.Links = Set(initialize=links)  # links
    m.TS = Set(initialize=timesteps, ordered=True)  # time steps

    # all nodes directly upstream from a node
    def NodesIn_init(m, node):
        retval = []
        for (i, j) in m.Links:
            if j == node:
                retval.append(i)
        return retval

    m.NodesIn = Set(m.Nodes, initialize=NodesIn_init)

    # all nodes directly downstream from a node
    def NodesOut_init(m, node):
        retval = []
        for (j, k) in m.Links:
            if j == node:
                retval.append(k)
        return retval

    m.NodesOut = Set(m.Nodes, initialize=NodesOut_init)

    # sets (nodes or links) for each template type
    for k, v in type_nodes.items():
        exec("m.{} = Set(within=m.Nodes, initialize={})".format(k.replace(" ", "_"), v))
    for k, v in type_links.items():
        exec("m.{} = Set(within=m.Links, initialize={})".format(k.replace(" ", "_"), v))

    # sets for non-storage nodes
    m.NonReservoir = m.Nodes - m.Reservoir
    m.DemandNodes = m.NonReservoir - m.Junction

    # these are collected to initialize the node-block/link-block sets
    demand_node_blocks = []
    reservoir_blocks = []
    link_blocks = []

    # set - all blocks in each demand or reservoir node, and identify node-blocks
    def NodeBlockLookup_init(m, node):
        if "Priority" in params["node"] and node in params["node"]["Priority"]:
            blocks = params["node"]["Priority"][node].columns
        else:
            blocks = [0]  # every node should have a priority
        node_blocks = [(node, b) for b in blocks]
        if node in m.DemandNodes:
            demand_node_blocks.extend(node_blocks)
        elif node in m.Reservoir:
            reservoir_blocks.extend(node_blocks)
        return blocks

    m.DemandNodeBlockLookup = Set(m.DemandNodes, initialize=NodeBlockLookup_init)
    m.ReservoirBlockLookup = Set(m.Reservoir, initialize=NodeBlockLookup_init)

    # set - all blocks in each link
    def LinkBlockLookup_init(m, i, j):
        if "Priority" in params["link"] and (i, j) in params["node"]["Priority"]:
            blocks = params["link"]["Priority"][(i, j)].columns
        else:
            blocks = [0]  # every link should have a priority
            # return Set.End
        link_blocks.extend([(i, j, b) for b in blocks])
        return blocks

    m.LinkBlockLookup = Set(m.Links, initialize=LinkBlockLookup_init)

    # create node-block and link-block sets
    m.DemandNodeBlocks = Set(initialize=demand_node_blocks)
    m.ReservoirBlocks = Set(initialize=reservoir_blocks)
    m.LinkBlocks = Set(initialize=link_blocks)

    # VARIABLES

    m.D = Var(m.DemandNodes * m.TS, domain=NonNegativeReals)  # delivery to demand nodes
    m.D_DB = Var(m.DemandNodeBlocks * m.TS, domain=NonNegativeReals)  # delivery to demand nodes
    m.D_surplus = Var(m.DemandNodes * m.TS, domain=NonNegativeReals)  # delivery to demand nodes
    m.S = Var(m.Reservoir * m.TS, domain=NonNegativeReals)  # storage
    m.S_RB = Var(m.ReservoirBlocks * m.TS, domain=NonNegativeReals)  # storage
    m.S_surplus = Var(m.Reservoir * m.TS, domain=NonNegativeReals)  # storage

    m.G = Var(m.Nodes * m.TS, domain=NonNegativeReals)  # gain (local inflow)
    m.L = Var(m.Nodes * m.TS, domain=NonNegativeReals)  # loss (local outflow)
    m.I = Var(m.Nodes * m.TS, domain=NonNegativeReals)  # total inflow to a node
    m.O = Var(m.Nodes * m.TS, domain=NonNegativeReals)  # total outflow from a node

    m.Q = Var(m.Links * m.TS, domain=NonNegativeReals)  # flow in links
    m.Q_LB = Var(m.LinkBlocks * m.TS, domain=NonNegativeReals)  # flow in links
    m.Q_surplus = Var(m.Links * m.TS, domain=NonNegativeReals)  # flow in links

    # PARAMETERS

    # IMPORTANT: Defaults should not be set here
#.........这里部分代码省略.........
开发者ID:CentroDelAgua-Decisiones,项目名称:OpenAguaDSS,代码行数:103,代码来源:model.py


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