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


Python ConcreteModel.obj方法代码示例

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


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

示例1: test_indexedvar_noindextemplate

# 需要导入模块: from pyomo.core import ConcreteModel [as 别名]
# 或者: from pyomo.core.ConcreteModel import obj [as 别名]
    def test_indexedvar_noindextemplate(self):
        st_model = CreateConcreteTwoStageScenarioTreeModel(1)
        st_model.StageVariables['Stage1'].add("x")
        st_model.StageDerivedVariables['Stage1'].add("y")
        st_model.NodeVariables['RootNode'].add("z")
        st_model.NodeDerivedVariables['RootNode'].add("q")
        st_model.StageCost['Stage1'] = "FirstStageCost"
        st_model.StageCost['Stage2'] = "SecondStageCost"

        scenario_tree = ScenarioTree(scenariotreeinstance=st_model)
        self.assertEqual(len(scenario_tree.stages), 2)
        self.assertEqual(len(scenario_tree.nodes), 2)
        self.assertEqual(len(scenario_tree.scenarios), 1)

        model = ConcreteModel()
        model.s = Set(initialize=[1,2,3])
        model.x = Var(model.s)
        model.y = Var(model.s)
        model.z = Var(model.s)
        model.q = Var(model.s)
        model.FirstStageCost = Expression(expr=0.0)
        model.SecondStageCost = Expression(expr=0.0)
        model.obj = Objective(expr=0.0)

        scenario_tree.linkInInstances({'Scenario1': model})

        root = scenario_tree.findRootNode()
        self.assertEqual(len(root._variable_ids), 12)
        self.assertEqual(len(root._standard_variable_ids), 6)
        self.assertEqual(len(root._derived_variable_ids), 6)
        for name in ("x", "y", "z", "q"):
            for index in model.s:
                self.assertEqual(
                    (name,index) in root._name_index_to_id, True)
开发者ID:SemanticBeeng,项目名称:pyomo,代码行数:36,代码来源:test_scenariotree.py

示例2: test_singletonvar_wildcardtemplate

# 需要导入模块: from pyomo.core import ConcreteModel [as 别名]
# 或者: from pyomo.core.ConcreteModel import obj [as 别名]
    def test_singletonvar_wildcardtemplate(self):
        st_model = CreateConcreteTwoStageScenarioTreeModel(1)
        st_model.StageVariables['Stage1'].add("x[*]")
        st_model.StageDerivedVariables['Stage1'].add("y[*]")
        st_model.NodeVariables['RootNode'].add("z[*]")
        st_model.NodeDerivedVariables['RootNode'].add("q[*]")
        st_model.StageCost['Stage1'] = "FirstStageCost"
        st_model.StageCost['Stage2'] = "SecondStageCost"

        scenario_tree = ScenarioTree(scenariotreeinstance=st_model)
        self.assertEqual(len(scenario_tree.stages), 2)
        self.assertEqual(len(scenario_tree.nodes), 2)
        self.assertEqual(len(scenario_tree.scenarios), 1)

        model = ConcreteModel()
        model.x = Var()
        model.y = Var()
        model.z = Var()
        model.q = Var()
        model.FirstStageCost = Expression(expr=0.0)
        model.SecondStageCost = Expression(expr=0.0)
        model.obj = Objective(expr=0.0)

        scenario_tree.linkInInstances({'Scenario1': model})

        root = scenario_tree.findRootNode()
        self.assertEqual(len(root._variable_ids), 4)
        self.assertEqual(len(root._standard_variable_ids), 2)
        self.assertEqual(len(root._derived_variable_ids), 2)
        for name in ("x", "y", "z", "q"):
            for index in [None]:
                self.assertEqual(
                    (name,index) in root._name_index_to_id, True)
开发者ID:SemanticBeeng,项目名称:pyomo,代码行数:35,代码来源:test_scenariotree.py

示例3: select_tear_mip_model

# 需要导入模块: from pyomo.core import ConcreteModel [as 别名]
# 或者: from pyomo.core.ConcreteModel import obj [as 别名]
    def select_tear_mip_model(self, G):
        """
        Generate a model for selecting tears from the given graph

        Returns
        -------
            model
            bin_list
                A list of the binary variables representing each edge,
                indexed by the edge index of the graph
        """
        model = ConcreteModel()

        bin_list = []
        for i in range(G.number_of_edges()):
            # add a binary "torn" variable for every edge
            vname = "edge%s" % i
            var = Var(domain=Binary)
            bin_list.append(var)
            model.add_component(vname, var)

        # var containing the maximum number of times any cycle is torn
        mct = model.max_cycle_tears = Var()

        _, cycleEdges = self.all_cycles(G)

        for i in range(len(cycleEdges)):
            ecyc = cycleEdges[i]

            # expression containing sum of tears for each cycle
            ename = "cycle_sum%s" % i
            expr = Expression(expr=sum(bin_list[i] for i in ecyc))
            model.add_component(ename, expr)

            # every cycle must have at least 1 tear
            cname_min = "cycle_min%s" % i
            con_min = Constraint(expr=expr >= 1)
            model.add_component(cname_min, con_min)

            # mct >= cycle_sum for all cycles, thus it becomes the max
            cname_mct = mct.name + "_geq%s" % i
            con_mct = Constraint(expr=mct >= expr)
            model.add_component(cname_mct, con_mct)

        # weigh the primary objective much greater than the secondary
        obj_expr = 1000 * mct + sum(var for var in bin_list)
        model.obj = Objective(expr=obj_expr, sense=minimize)

        return model, bin_list
开发者ID:Pyomo,项目名称:pyomo,代码行数:51,代码来源:decomposition.py

示例4: makeNestedDisjunctions_NestedDisjuncts

# 需要导入模块: from pyomo.core import ConcreteModel [as 别名]
# 或者: from pyomo.core.ConcreteModel import obj [as 别名]
def makeNestedDisjunctions_NestedDisjuncts():
    m = ConcreteModel()
    m.x = Var(bounds=(0, 2))
    m.obj = Objective(expr=m.x)
    m.d1 = Disjunct()
    m.d1.c = Constraint(expr=m.x >= 1)
    m.d2 = Disjunct()
    m.d2.c = Constraint(expr=m.x >= 1.1)
    m.d1.d3 = Disjunct()
    m.d1.d3.c = Constraint(expr=m.x >= 1.2)
    m.d1.d4 = Disjunct()
    m.d1.d4.c = Constraint(expr=m.x >= 1.3)
    m.disj = Disjunction(expr=[m.d1, m.d2])
    m.d1.disj2 = Disjunction(expr=[m.d1.d3, m.d1.d4])
    return m
开发者ID:Pyomo,项目名称:pyomo,代码行数:17,代码来源:models.py

示例5: _get_block_model

# 需要导入模块: from pyomo.core import ConcreteModel [as 别名]
# 或者: from pyomo.core.ConcreteModel import obj [as 别名]
 def _get_block_model(self):
     model = ConcreteModel()
     model.s = Set(initialize=[1,2])
     b = Block(concrete=True)
     b.s = Set(initialize=[1,2])
     b.x = Var()
     b.X = Var(model.s)
     model.b1 = b.clone()
     model.b2 = b.clone()
     model.b3 = b.clone()
     model.b4 = b.clone()
     model.B1 = Block(model.s, rule=lambda _,i: b.clone())
     model.B2 = Block(model.s, rule=lambda _,i: b.clone())
     model.B3 = Block(model.s, rule=lambda _,i: b.clone())
     model.B4 = Block(model.s, rule=lambda _,i: b.clone())
     model.FirstStageCost = Expression(expr=0.0)
     model.SecondStageCost = Expression(expr=0.0)
     model.obj = Objective(expr=0.0)
     return model
开发者ID:SemanticBeeng,项目名称:pyomo,代码行数:21,代码来源:test_scenariotree.py

示例6: build_nonexclusive_model

# 需要导入模块: from pyomo.core import ConcreteModel [as 别名]
# 或者: from pyomo.core.ConcreteModel import obj [as 别名]

#.........这里部分代码省略.........
            # No Unit 1
            m.x[2] == 0, m.x[3] == 0
        ]
    ])
    m.unit2 = Disjunction(expr=[
        [
            # Unit 2
            m.x[5] == log(m.x[4] + 1),
        ],
        [
            # No Unit 2
            m.x[4] == 0, m.x[5] == 0
        ]
    ])
    m.stage1_mix = Constraint(expr=m.x[3] + m.x[5] == m.x[6])
    m.stage2_split = Constraint(expr=m.x[6] == sum(m.x[i] for i in (7, 9, 11, 13)))
    m.unit3 = Disjunction(expr=[
        [
            # Unit 3
            m.x[8] == 2 * log(m.x[7]) + 3,
            m.x[7] >= 0.2,
        ],
        [
            # No Unit 3
            m.x[7] == 0, m.x[8] == 0
        ]
    ])
    m.unit4 = Disjunction(expr=[
        [
            # Unit 4
            m.x[10] == 1.8 * log(m.x[9] + 4),
        ],
        [
            # No Unit 4
            m.x[9] == 0, m.x[10] == 0
        ]
    ])
    m.unit5 = Disjunction(expr=[
        [
            # Unit 5
            m.x[12] == 1.2 * log(m.x[11]) + 2,
            m.x[11] >= 0.001,
        ],
        [
            # No Unit 5
            m.x[11] == 0, m.x[12] == 0
        ]
    ])
    m.unit6 = Disjunction(expr=[
        [
            # Unit 6
            m.x[15] == sqrt(m.x[14] - 3) * m.x[23] + 1,
            m.x[14] >= 5, m.x[14] <= 20,
        ],
        [
            # No Unit 6
            m.x[14] == 0, m.x[15] == 0
        ]
    ])
    m.stage2_special_mix = Constraint(expr=m.x[14] == m.x[13] + m.x[23])
    m.stage2_mix = Constraint(expr=sum(m.x[i] for i in (8, 10, 12, 15)) == m.x[16])
    m.stage3_split = Constraint(expr=m.x[16] == sum(m.x[i] for i in (17, 19, 21)))
    m.unit7 = Disjunction(expr=[
        [
            # Unit 7
            m.x[18] == m.x[17] * 0.9,
        ],
        [
            # No Unit 7
            m.x[17] == 0, m.x[18] == 0
        ]
    ])
    m.unit8 = Disjunction(expr=[
        [
            # Unit 8
            m.x[20] == log(m.x[19] ** 1.5) + 2,
            m.x[19] >= 1,
        ],
        [
            # No Unit 8
            m.x[19] == 0, m.x[20] == 0
        ]
    ])
    m.unit9 = Disjunction(expr=[
        [
            # Unit 9
            m.x[22] == log(m.x[21] + sqrt(m.x[21])) + 1,
            m.x[21] >= 4,
        ],
        [
            # No Unit 9
            m.x[21] == 0, m.x[22] == 0
        ]
    ])
    m.stage3_special_split = Constraint(expr=m.x[22] == m.x[23] + m.x[24])
    m.stage3_mix = Constraint(expr=m.x[25] == sum(m.x[i] for i in (18, 20, 24)))

    m.obj = Objective(expr=-10 * m.x[25] + m.x[1])

    return m
开发者ID:Pyomo,项目名称:pyomo,代码行数:104,代码来源:small_process.py

示例7: build_model

# 需要导入模块: from pyomo.core import ConcreteModel [as 别名]
# 或者: from pyomo.core.ConcreteModel import obj [as 别名]
def build_model():
    """
    Base Model

    Optimal solution:
    Select units 1, 3, 8
    Objective value -36.62
    """
    m = ConcreteModel()
    m.streams = RangeSet(25)
    m.x = Var(m.streams, bounds=(0, 50), initialize=5)

    m.stage1_split = Constraint(expr=m.x[1] == m.x[2] + m.x[4])
    m.first_stage = Disjunction(expr=[
        [
            # Unit 1
            m.x[2] == exp(m.x[3]) - 1,
            m.x[4] == 0, m.x[5] == 0
        ],
        [
            # Unit 2
            m.x[5] == log(m.x[4] + 1),
            m.x[2] == 0, m.x[3] == 0
        ]
    ])
    m.stage1_mix = Constraint(expr=m.x[3] + m.x[5] == m.x[6])
    m.stage2_split = Constraint(expr=m.x[6] == sum(m.x[i] for i in (7, 9, 11, 13)))
    m.second_stage = Disjunction(expr=[
        [
            # Unit 3
            m.x[8] == 2 * log(m.x[7]) + 3,
            m.x[7] >= 0.2,
        ] + [m.x[i] == 0 for i in (9, 10, 11, 12, 14, 15)],
        [
            # Unit 4
            m.x[10] == 1.8 * log(m.x[9] + 4),
        ] + [m.x[i] == 0 for i in (7, 8, 11, 12, 14, 15)],
        [
            # Unit 5
            m.x[12] == 1.2 * log(m.x[11]) + 2,
            m.x[11] >= 0.001,
        ] + [m.x[i] == 0 for i in (7, 8, 9, 10, 14, 15)],
        [
            # Unit 6
            m.x[15] == sqrt(m.x[14] - 3) * m.x[23] + 1,
            m.x[14] >= 5, m.x[14] <= 20,
        ] + [m.x[i] == 0 for i in (7, 8, 9, 10, 11, 12)]
    ])
    m.stage2_special_mix = Constraint(expr=m.x[14] == m.x[13] + m.x[23])
    m.stage2_mix = Constraint(expr=sum(m.x[i] for i in (8, 10, 12, 15)) == m.x[16])
    m.stage3_split = Constraint(expr=m.x[16] == sum(m.x[i] for i in (17, 19, 21)))
    m.third_stage = Disjunction(expr=[
        [
            # Unit 7
            m.x[18] == m.x[17] * 0.9,
        ] + [m.x[i] == 0 for i in (19, 20, 21, 22)],
        [
            # Unit 8
            m.x[20] == log(m.x[19] ** 1.5) + 2,
            m.x[19] >= 1,
        ] + [m.x[i] == 0 for i in (17, 18, 21, 22)],
        [
            # Unit 9
            m.x[22] == log(m.x[21] + sqrt(m.x[21])) + 1,
            m.x[21] >= 4,
        ] + [m.x[i] == 0 for i in (17, 18, 19, 20)]
    ])
    m.stage3_special_split = Constraint(expr=m.x[22] == m.x[23] + m.x[24])
    m.stage3_mix = Constraint(expr=m.x[25] == sum(m.x[i] for i in (18, 20, 24)))

    m.obj = Objective(expr=-10 * m.x[25] + m.x[1])

    return m
开发者ID:Pyomo,项目名称:pyomo,代码行数:75,代码来源:small_process.py

示例8: criticalityCheck

# 需要导入模块: from pyomo.core import ConcreteModel [as 别名]
# 或者: from pyomo.core.ConcreteModel import obj [as 别名]
    def criticalityCheck(self, x, y, z, rom_params, worstcase=False, M=[0.0]):

        model = self.model

        self.setVarValue(x=x,y=y,z=z)
        self.setBound(x, y, z, 1e10)
        self.deactiveExtraConObj()
        self.activateRomCons(x, rom_params)

        optGJH = SolverFactory('contrib.gjh')
        optGJH.solve(model, tee=False, symbolic_solver_labels=True)
        g, J, varlist, conlist = model._gjh_info

        l = ConcreteModel()
        l.v = Var(varlist, domain=Reals)
        for i in varlist:
            #dummy = model.find_component(i)
            l.v[i] = 0.0
            l.v[i].setlb(-1.0)
            l.v[i].setub(1.0)
        if worstcase:
            if M.all() == 0.0:
                print('WARNING: worstcase criticality was requested but Jacobian error bound is zero')
            l.t = Var(range(0, self.ly), domain=Reals)
            for i in range(0, self.ly):
                l.t[i].setlb(-M[i])
                l.t[i].setub(M[i])

        def linConMaker(l, i):
            # i should be range(len(conlist) - 1)
            # because last element of conlist is the objective
            con_i = model.find_component(conlist[i])

            isEquality = con_i.equality

            isROM = False

            if conlist[i][:7] == '.' + self.TRF.name + '.rom':
                isROM = True
                romIndex = int(filter(str.isdigit, conlist[i]))

            # This is very inefficient
            # Fix this later if these problems get big
            # This is the ith row of J times v
            Jv = sum(x[2] * l.v[varlist[x[1]]] for x in J if x[0] == i)

            if isEquality:
                if worstcase and isROM:
                    return Jv + l.t[romIndex] == 0
                else:
                    return Jv == 0
            else:
                lo = con_i.lower
                up = con_i.upper
                if lo is not None:
                    level = lo.value - con_i.lslack()
                    if up is not None:
                        return (lo.value <= level + Jv <= up.value)
                    else:
                        return (lo.value <= level + Jv)
                elif up is not None:
                    level = up.value - con_i.uslack()
                    return (level + Jv <= up.value)
                else:
                    raise Exception(
                        "This constraint seems to be neither equality or inequality: " + conlist(i))


        l.lincons = Constraint(range(len(conlist)-1), rule=linConMaker)

        l.obj = Objective(expr=sum(x[1] * l.v[varlist[x[0]]] for x in g))

        # Calculate gradient norm for scaling purposes
        gfnorm = sqrt(sum(x[1]**2 for x in g))


        opt = SolverFactory(self.config.solver)
        opt.options.update(self.config.solver_options)
        #opt.options['halt_on_ampl_error'] = 'yes'
        #opt.options['max_iter'] = 5000
        results = opt.solve(
            l, keepfiles=self.keepfiles, tee=self.stream_solver)

        if ((results.solver.status == SolverStatus.ok)
                and (results.solver.termination_condition == TerminationCondition.optimal)):
            l.solutions.load_from(results)
            if gfnorm > 1:
                return True, abs(l.obj())/gfnorm
            else:
                return True, abs(l.obj())
        else:
            print("Waring: Crticality check fails with solver Status: " + str(results.solver.status))
            print("And Termination Conditions: " + str(results.solver.termination_condition))
            return False, infinity
开发者ID:Pyomo,项目名称:pyomo,代码行数:96,代码来源:PyomoInterface.py


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