Example #1
0
def test_costing_FH_solve():
    m = pyo.ConcreteModel()
    m.fs = FlowsheetBlock(default={"dynamic": False})
    m.fs.get_costing()
    m.fs.costing.CE_index = 550  # for testing only
    m.fs.unit = pyo.Block()
    m.fs.unit.heat_duty = pyo.Var(initialize=1e6,
                                  units=pyo.units.BTU / pyo.units.hr)
    m.fs.unit.pressure = pyo.Var(initialize=1e5, units=pyo.units.psi)
    m.fs.unit.costing = pyo.Block()
    m.fs.unit.heat_duty.fix(18390000)  # Btu/hr
    m.fs.unit.pressure.fix(700)  # psig

    cs.fired_heater_costing(m.fs.unit.costing,
                            fired_type='fuel',
                            Mat_factor='stain_steel',
                            ref_parameter_pressure=m.fs.unit.pressure,
                            ref_parameter_heat_duty=m.fs.unit.heat_duty)

    assert degrees_of_freedom(m) == 0
    # Check unit config arguments
    assert isinstance(m.fs.unit.costing.purchase_cost, pyo.Var)
    assert isinstance(m.fs.unit.costing.base_cost_per_unit, pyo.Var)
    # initialize costing block
    costing.initialize(m.fs.unit.costing)
    assert (pytest.approx(pyo.value(m.fs.unit.costing.purchase_cost),
                          abs=1e-2) == 962795.521)
    results = solver.solve(m, tee=False)
    # Check for optimal solution
    assert results.solver.termination_condition == \
        pyo.TerminationCondition.optimal
    assert results.solver.status == pyo.SolverStatus.ok
    assert (pytest.approx(pyo.value(m.fs.unit.costing.purchase_cost),
                          abs=1e-2) == 962795.521)  # Example 22.1 Ref Book
Example #2
0
    def test_stochastic_objective(self):
        m = aml.ConcreteModel()
        self._populate_block_with_objectives(m)
        m.b = aml.Block()
        self._populate_block_with_objectives(m.b)
        m.b_inactive = aml.Block()
        self._populate_block_with_objectives(m.b_inactive)
        m.b_inactive.deactivate()
        m.B = aml.Block([1],
                        rule=lambda b: \
            self._populate_block_with_objectives(b))

        a = StochasticObjectiveAnnotation()
        self.assertEqual(a.default, (None, True))
        self.assertEqual(a.has_declarations, False)
        a.declare(m.o)
        self.assertEqual(a.has_declarations, True)
        a.declare(m.O1)
        a.declare(m.O2[1])
        a.declare(m.b)
        a.declare(m.b_inactive)
        a.declare(m.B, variables=1, include_constant=False)
        with self.assertRaises(TypeError):
            a.declare(None, 1)
        self.assertEqual(
            set([(v[0].name, v[1]) for v in a.expand_entries()]),
            set([('o', (None,True)), ('O1[1]', (None,True)), ('O2[1]', (None,True)),
                 ('b.o', (None,True)), ('b.O1[1]', (None,True)), ('b.O2[1]', (None,True)),
                 ('B[1].o', (1,False)), ('B[1].O1[1]', (1,False)), ('B[1].O2[1]', (1,False))]))
        self.assertEqual(
            set([(v[0].name, v[1]) for v in a.expand_entries(expand_containers=False)]),
            set([('o', (None,True)), ('O1', (None,True)), ('O2[1]', (None,True)),
                 ('b.o', (None,True)), ('b.O1', (None,True)), ('b.O2', (None,True)),
                 ('B[1].o', (1,False)), ('B[1].O1', (1,False)), ('B[1].O2', (1,False))]))
Example #3
0
    def setUp(self):
        m = pe.ConcreteModel()
        m.x = pe.Var(bounds=(0.5, 1.5))
        m.y = pe.Var()
        m.c1 = pe.Constraint(expr=m.y == m.x)
        m.r1 = coramin.relaxations.PWUnivariateRelaxation()
        m.r1.set_input(x=m.x,
                       aux_var=m.y,
                       shape=coramin.utils.FunctionShape.CONCAVE,
                       f_x_expr=pe.log(m.x))
        m.r1.add_partition_point(value=1)
        m.r1.rebuild()
        m.b1 = pe.Block()
        m.b1.x = pe.Var(bounds=(0.5, 1.5))
        m.b1.y = pe.Var()
        m.b1.c1 = pe.Constraint(expr=m.b1.y == m.b1.x)
        m.b1.r1 = coramin.relaxations.PWUnivariateRelaxation()
        m.b1.r1.set_input(x=m.b1.x,
                          aux_var=m.b1.y,
                          shape=coramin.utils.FunctionShape.CONCAVE,
                          f_x_expr=pe.log(m.b1.x))
        m.b1.r1.add_partition_point(value=1)
        m.b1.r1.rebuild()
        m.b1.b1 = pe.Block()

        self.m = m
Example #4
0
    def add_blocks(self, TIMEs, TIMEp, decisionrule="continuous", **kwargs):
        stn = self.stn
        m = self.model
        available_rules = ["continuous", "integer"]

        assert decisionrule in available_rules, ("Unknown decision rule %s" %
                                                 decisionrule)

        # scheduling and planning block
        m.sb = pyomo.Block()
        m.pb = pyomo.Block()
        self.sb = blockSchedulingRobust(stn,
                                        np.array([t for t in TIMEs]),
                                        self.Demand,
                                        decisionrule=decisionrule,
                                        prfx=self.prfx,
                                        **kwargs)
        self.sb.define_block(m.sb, decisionrule=decisionrule, **kwargs)
        self.pb = blockPlanningRobust(stn,
                                      np.array([t for t in TIMEp]),
                                      self.Demand,
                                      decisionrule=decisionrule,
                                      prfx=self.prfx,
                                      **kwargs)
        self.pb.define_block(m.pb, decisionrule=decisionrule, **kwargs)
Example #5
0
File: ld.py Project: whart222/pao
    def _create_pyomo_model(self, repn):
        #
        # Create Pyomo model
        #
        M = pe.ConcreteModel()
        M.U = pe.Block()
        M.L = pe.Block()
        M.dual = pe.Block()

        # upper- and lower-level variables
        pyomo_util._create_variables(repn.U, M.U)
        pyomo_util._create_variables(repn.L, M.L)
        M.dual.z = pe.Var()

        # upper- and lower-level constraints
        pyomo_util.add_linear_constraints(M.U, repn.U.A, repn.U, repn.L,
                                          repn.U.b, M.U.inequalities)
        pyomo_util.add_linear_constraints(M.L, repn.L.A, repn.U, repn.L,
                                          repn.L.b, M.L.inequalities)

        # objective
        e = pyomo_util.dot(repn.U.c.U, repn.U, num=1) + pyomo_util.dot(
            repn.U.c.L, repn.L, num=1) + repn.U.d
        M.o = pe.Objective(expr=e)

        # dual variables for primal constraints
        M.Dual.dual_c = Var(range(len(M.L.c)))

        # duality gap
        e = pyomo_util._linear_expression(1, repn.c.L, repn.L)
        M.lower_gap = Constraint(expr=e[0] == M.z)

        return M
Example #6
0
def test_costing_distillation_solve():
    m = pyo.ConcreteModel()
    m.fs = FlowsheetBlock(default={"dynamic": False})
    m.fs.get_costing()
    m.fs.costing.CE_index = 550
    # create a unit model and variables
    m.fs.unit = pyo.Block()
    m.fs.unit.heat_duty = pyo.Var(initialize=1e6)
    m.fs.unit.pressure = pyo.Var(initialize=1e5)
    m.fs.unit.diameter = pyo.Var(initialize=10,
                                 domain=pyo.NonNegativeReals,
                                 doc='unit diameter in m')
    m.fs.unit.length = pyo.Var(initialize=10,
                               domain=pyo.NonNegativeReals,
                               doc='unit length in m')
    # create costing block
    m.fs.unit.costing = pyo.Block()

    cs.vessel_costing(m.fs.unit.costing,
                      alignment='vertical',
                      weight_limit='option2',
                      L_D_range='option2',
                      PL=True,
                      plates=True,
                      number_tray=100,
                      ref_parameter_diameter=m.fs.unit.diameter,
                      ref_parameter_length=m.fs.unit.length)
    # pressure design and shell thickness from Example 22.13 Product and
    # Process Design Principless
    m.fs.unit.heat_duty.fix(18390000)  # Btu/hr
    m.fs.unit.pressure.fix(123)  # psig
    # pressure design minimum thickness tp = 0.582 in
    # vessel is vertical + quite tall the tower is subject to wind load,
    # and earthquake. Assume wall thickness of 1.25 in.
    # The additional wall thickness at the bottom of the tower is 0.889 in
    # average thickness is 1.027, plus corrosion allowance of 1/8
    # 1.152 in, therefore steel plate thickness is 1.250 (ts)
    m.fs.unit.costing.shell_thickness = 1.250  # inches
    m.fs.unit.diameter.fix(10)  # ft
    m.fs.unit.length.fix(212)   # ft
    m.fs.unit.costing.number_trays = 100

    assert degrees_of_freedom(m) == 0
    # Check unit config arguments
    assert isinstance(m.fs.unit.costing.purchase_cost, pyo.Var)
    assert isinstance(m.fs.unit.costing.base_cost, pyo.Var)

    results = solver.solve(m, tee=False)
    # Check for optimal solution
    assert results.solver.termination_condition == \
        pyo.TerminationCondition.optimal
    assert results.solver.status == pyo.SolverStatus.ok
    assert (pytest.approx(pyo.value(m.fs.unit.costing.base_cost),
                          abs=1e-2) == 636959.6929)  # Example 22.13 Ref Book
    assert (pytest.approx(pyo.value(m.fs.unit.costing.base_cost_platf_ladders),
                          abs=1e-2) == 97542.9005)  # Example 22.13 Ref Book
    assert (pytest.approx(pyo.value(m.fs.unit.costing.purchase_cost_trays),
                          abs=1e-2) == 293006.086)  # Example 22.13 Ref Book
    assert (pytest.approx(pyo.value(m.fs.unit.costing.purchase_cost),
                          abs=1e-2) == 1100958.9396)  # Example 22.13 Ref Book
Example #7
0
    def test_stochastic_variable_bounds(self):
        m = aml.ConcreteModel()
        self._populate_block_with_vars(m)
        m.b = aml.Block()
        self._populate_block_with_vars(m.b)
        m.b_inactive = aml.Block()
        self._populate_block_with_vars(m.b_inactive)
        m.b_inactive.deactivate()
        m.B = aml.Block([1],
                        rule=lambda b: \
            self._populate_block_with_vars(b))

        a = StochasticVariableBoundsAnnotation()
        self.assertEqual(a.default, (True, True))
        self.assertEqual(a.has_declarations, False)
        a.declare(m.x)
        self.assertEqual(a.has_declarations, True)
        a.declare(m.X1)
        a.declare(m.X2[1])
        a.declare(m.b)
        a.declare(m.b_inactive)
        a.declare(m.B, lb=False, ub=True)
        with self.assertRaises(TypeError):
            a.declare(None, 1)
        self.assertEqual(
            set([(v[0].name, v[1]) for v in a.expand_entries()]),
            set([('x', (True,True)), ('X1[1]', (True,True)), ('X2[1]', (True,True)),
                 ('b.x', (True,True)), ('b.X1[1]', (True,True)), ('b.X2[1]', (True,True)),
                 ('B[1].x', (False,True)), ('B[1].X1[1]', (False,True)), ('B[1].X2[1]', (False,True))]))
        self.assertEqual(
            set([(v[0].name, v[1]) for v in a.expand_entries(expand_containers=False)]),
            set([('x', (True,True)), ('X1', (True,True)), ('X2[1]', (True,True)),
                 ('b.x', (True,True)), ('b.X1', (True,True)), ('b.X2', (True,True)),
                 ('B[1].x', (False,True)), ('B[1].X1', (False,True)), ('B[1].X2', (False,True))]))
Example #8
0
    def test_stochastic_data(self):
        m = pyo.ConcreteModel()
        self._populate_block_with_params(m)
        m.b = pyo.Block()
        self._populate_block_with_params(m.b)
        m.b_inactive = pyo.Block()
        self._populate_block_with_params(m.b_inactive)
        m.b_inactive.deactivate()
        m.B = pyo.Block([1],
                        rule=lambda b: \
            self._populate_block_with_params(b))

        a = StochasticDataAnnotation()
        self.assertEqual(a.default, None)
        self.assertEqual(a.has_declarations, False)
        a.declare(m.p, 1)
        self.assertEqual(a.has_declarations, True)
        a.declare(m.P1, 1)
        a.declare(m.P2[1], 1)
        a.declare(m.b, 1)
        a.declare(m.b_inactive, 1)
        a.declare(m.B, 2)
        with self.assertRaises(TypeError):
            a.declare(None, 1)
        self.assertEqual(
            set([(v[0].name, v[1]) for v in a.expand_entries()]),
            set([('p', 1), ('P1[1]', 1), ('P2[1]', 1), ('b.p', 1),
                 ('b.P1[1]', 1), ('b.P2[1]', 1), ('B[1].p', 2),
                 ('B[1].P1[1]', 2), ('B[1].P2[1]', 2)]))
        self.assertEqual(
            set([(v[0].name, v[1])
                 for v in a.expand_entries(expand_containers=False)]),
            set([('p', 1), ('P1', 1), ('P2[1]', 1), ('b.p', 1), ('b.P1', 1),
                 ('b.P2', 1), ('B[1].p', 2), ('B[1].P1', 2), ('B[1].P2', 2)]))
Example #9
0
    def test_stochastic_constraint_body(self):
        m = aml.ConcreteModel()
        self._populate_block_with_constraints(m)
        m.b = aml.Block()
        self._populate_block_with_constraints(m.b)
        m.b_inactive = aml.Block()
        self._populate_block_with_constraints(m.b_inactive)
        m.b_inactive.deactivate()
        m.B = aml.Block([1],
                        rule=lambda b: \
            self._populate_block_with_constraints(b))

        a = StochasticConstraintBodyAnnotation()
        self.assertEqual(a.default, None)
        self.assertEqual(a.has_declarations, False)
        a.declare(m.c)
        self.assertEqual(a.has_declarations, True)
        a.declare(m.C1)
        a.declare(m.C2[1])
        a.declare(m.C3)
        a.declare(m.b)
        a.declare(m.b_inactive)
        a.declare(m.B, variables=2)
        with self.assertRaises(TypeError):
            a.declare(None, 1)
        self.assertEqual(
            set([(v[0].name, v[1]) for v in a.expand_entries()]),
            set([('c', None), ('C1[1]', None), ('C2[1]', None), ('C3[1]', None),
                 ('b.c', None), ('b.C1[1]', None), ('b.C2[1]', None), ('b.C3[1]', None),
                 ('B[1].c', 2), ('B[1].C1[1]', 2), ('B[1].C2[1]', 2), ('B[1].C3[1]', 2)]))
        self.assertEqual(
            set([(v[0].name, v[1]) for v in a.expand_entries(expand_containers=False)]),
            set([('c', None), ('C1', None), ('C2[1]', None), ('C3', None),
                 ('b.c', None), ('b.C1', None), ('b.C2', None), ('b.C3', None),
                 ('B[1].c', 2), ('B[1].C1', 2), ('B[1].C2', 2), ('B[1].C3', 2)]))
Example #10
0
def test_units1_costing(build_costing):
    m = build_costing

    # Accounts with Feedwater Flow to HP section of HRSG, as the
    # reference/scaling parameter - Exhibit 5-15
    FW_accounts = ['3.1', '3.3', '8.4']

    # Accounts with Raw water withdrawal as the reference/scaling parameter
    # Exhibit 5-14
    RW_withdraw_accounts = ['3.2', '3.4', '3.5', '9.5', '14.6']
    m.fs.b2 = pyo.Block()

    m.fs.b2.raw_water_withdrawal = pyo.Var(initialize=2902)  # gpm
    m.fs.b2.raw_water_withdrawal.fix()
    get_PP_costing(m.fs.b2, RW_withdraw_accounts, m.fs.b2.raw_water_withdrawal,
                   'gpm', 6)

    # Accounts with fuel gas flowrate as the reference/scaling parameter
    # Exhibit 5-15 stream 2, Exhibit 5-8
    FuelG_accounts = ['3.6', '3.9', '6.1', '6.3', '6.4']
    m.fs.b3 = pyo.Block()
    # Obtain Fuel gas flowrate in acm
    fuelgas_value = 205630  # lb/hr

    m.fs.b3.fg_flowrate = pyo.Var(initialize=fuelgas_value)  # lb/hr
    m.fs.b3.fg_flowrate.fix()
    get_PP_costing(m.fs.b3, FuelG_accounts, m.fs.b3.fg_flowrate, 'lb/hr', 6)

    # Accounts with process water discharge as the reference/scaling parameter
    # Exhibit 5-14
    PW_discharge_accounts = ['3.7']
    m.fs.b4 = pyo.Block()

    m.fs.b4.process_water_discharge = pyo.Var(initialize=657)  # gpm
    m.fs.b4.process_water_discharge.fix()
    get_PP_costing(m.fs.b4, PW_discharge_accounts,
                   m.fs.b4.process_water_discharge, 'gpm', 6)

    # Initialize costing
    costing_initialization(m.fs)
    assert degrees_of_freedom(m) == 0

    # Solve the model
    results = solver.solve(m, tee=True)
    assert pyo.check_optimal_termination(results)

    # Accounts with raw water withdrawal as reference parameter
    assert pytest.approx(26.435, abs=0.5) \
        == sum(pyo.value(m.fs.b2.costing.total_plant_cost[ac])
               for ac in RW_withdraw_accounts)
    # Accounts with fuel gas as reference parameter
    assert pytest.approx(158.415, abs=0.5) \
        == sum(pyo.value(m.fs.b3.costing.total_plant_cost[ac])
               for ac in FuelG_accounts)

    # Accounts with process water discharge as reference parameter
    assert pytest.approx(11.608, abs=0.5) \
        == sum(pyo.value(m.fs.b4.costing.total_plant_cost[ac])
               for ac in PW_discharge_accounts)
Example #11
0
def test_8():
    # Test replace indexed by indexed var that doesn't have enough/right indexes
    rp = pyo.TransformationFactory("replace_variables")
    m = pyo.ConcreteModel()
    m.b1 = pyo.Block()
    m.b1.b2 = pyo.Block()
    x = m.b1.b2.x = pyo.Var(["a", "b", "c"], [1,2,3], initialize=2)
    m.y = pyo.Var(["a", "b", "d"], [1,2,3], initialize=3)

    with pytest.raises(ValueError):
        rp.apply_to(m, substitute=[(x, m.y)])
Example #12
0
    def test_variable_stage(self):
        m = pyo.ConcreteModel()
        self._populate_block_with_vars_expressions(m)
        m.b = pyo.Block()
        self._populate_block_with_vars_expressions(m.b)
        m.b_inactive = pyo.Block()
        self._populate_block_with_vars_expressions(m.b_inactive)
        m.b_inactive.deactivate()
        m.B = pyo.Block([1],
                        rule=lambda b: \
            self._populate_block_with_vars_expressions(b))

        a = VariableStageAnnotation()
        self.assertEqual(a.default, None)
        self.assertEqual(a.has_declarations, False)
        a.declare(m.x, 1)
        self.assertEqual(a.has_declarations, True)
        a.declare(m.X1, 1)
        a.declare(m.X2[1], 1)
        a.declare(m.e, 1)
        a.declare(m.E1, 1)
        a.declare(m.E2[1], 1)
        with self.assertRaises(TypeError):
            a.declare(m.b, None)
        a.declare(m.b, 1)
        a.declare(m.b_inactive, 1)
        a.declare(m.B, 2, derived=True)
        with self.assertRaises(TypeError):
            a.declare(None, 1)
        self.assertEqual(
            set([(v[0].name, v[1]) for v in a.expand_entries()]),
            set([('x', (1, False)), ('X1[1]', (1, False)),
                 ('X2[1]', (1, False)), ('e', (1, False)),
                 ('E1[1]', (1, False)), ('E2[1]', (1, False)),
                 ('b.x', (1, False)), ('b.X1[1]', (1, False)),
                 ('b.X2[1]', (1, False)), ('b.e', (1, False)),
                 ('b.E1[1]', (1, False)), ('b.E2[1]', (1, False)),
                 ('B[1].x', (2, True)), ('B[1].X1[1]', (2, True)),
                 ('B[1].X2[1]', (2, True)), ('B[1].e', (2, True)),
                 ('B[1].E1[1]', (2, True)), ('B[1].E2[1]', (2, True))]))
        self.assertEqual(
            set([(v[0].name, v[1])
                 for v in a.expand_entries(expand_containers=False)]),
            set([('x', (1, False)), ('X1', (1, False)), ('X2[1]', (1, False)),
                 ('e', (1, False)), ('E1', (1, False)), ('E2[1]', (1, False)),
                 ('b.x', (1, False)), ('b.X1', (1, False)),
                 ('b.X2', (1, False)), ('b.e', (1, False)),
                 ('b.E1', (1, False)), ('b.E2', (1, False)),
                 ('B[1].x', (2, True)), ('B[1].X1', (2, True)),
                 ('B[1].X2', (2, True)), ('B[1].e', (2, True)),
                 ('B[1].E1', (2, True)), ('B[1].E2', (2, True))]))
Example #13
0
    def test_cos(self):
        m = pyo.Block(concrete=True)
        m.x = pyo.Var(bounds=(0, math.pi))
        m.c = pyo.Constraint(expr=pyo.inequality(body=pyo.cos(m.x), lower=-0.5, upper=0.5))
        self.tightener(m)
        self.assertAlmostEqual(pyo.value(m.x.lb), math.acos(0.5))
        self.assertAlmostEqual(pyo.value(m.x.ub), math.acos(-0.5))

        m = pyo.Block(concrete=True)
        m.x = pyo.Var()
        m.c = pyo.Constraint(expr=pyo.inequality(body=pyo.cos(m.x), lower=-0.5, upper=0.5))
        self.tightener(m)
        self.assertEqual(m.x.lb, None)
        self.assertEqual(m.x.ub, None)
Example #14
0
def test_6():
    # Test non-variable exception
    rp = pyo.TransformationFactory("replace_variables")
    m = pyo.ConcreteModel()
    m.b1 = pyo.Block()
    m.b1.b2 = pyo.Block()
    x = m.b1.b2.x = pyo.Var(["a", "b", "c"], [1,2,3], initialize=2)
    m.y = pyo.Var(["a", "b", "c", "d"], [1,2,3], initialize=3)
    m.z = pyo.Var(initialize=0)

    with pytest.raises(TypeError):
        rp.apply_to(m, substitute=[(x, m.b1)])
    with pytest.raises(TypeError):
        rp.apply_to(m, substitute=[(m.b1, x)])
Example #15
0
    def test_tan(self):
        m = pe.Block(concrete=True)
        m.x = pe.Var(bounds=(-math.pi/2, math.pi/2))
        m.c = pe.Constraint(expr=pe.inequality(body=pe.tan(m.x), lower=-0.5, upper=0.5))
        fbbt(m)
        self.assertAlmostEqual(pe.value(m.x.lb), math.atan(-0.5))
        self.assertAlmostEqual(pe.value(m.x.ub), math.atan(0.5))

        m = pe.Block(concrete=True)
        m.x = pe.Var()
        m.c = pe.Constraint(expr=pe.inequality(body=pe.tan(m.x), lower=-0.5, upper=0.5))
        fbbt(m)
        self.assertEqual(m.x.lb, None)
        self.assertEqual(m.x.ub, None)
Example #16
0
def test_7():
    # Test replace indexed by non-indexed
    rp = pyo.TransformationFactory("replace_variables")
    m = pyo.ConcreteModel()
    m.b1 = pyo.Block()
    m.b1.b2 = pyo.Block()
    x = m.b1.b2.x = pyo.Var(["a", "b", "c"], [1,2,3], initialize=2)
    m.y = pyo.Var(["a", "b", "c", "d"], [1,2,3], initialize=3)
    m.z = pyo.Var(initialize=0)

    assert x.is_indexed()
    assert not m.z.is_indexed()
    with pytest.raises(TypeError):
        rp.apply_to(m, substitute=[(x, m.z)])
Example #17
0
    def test_rename_components(self):
        model = pyo.ConcreteModel()
        model.x = pyo.Var([1, 2, 3], bounds=(-10, 10), initialize=5.0)
        model.z = pyo.Var(bounds=(10, 20))
        model.obj = pyo.Objective(expr=model.z + model.x[1])

        def con_rule(m, i):
            return m.x[i] + m.z == i

        model.con = pyo.Constraint([1, 2, 3], rule=con_rule)
        model.zcon = pyo.Constraint(expr=model.z >= model.x[2])
        model.b = pyo.Block()
        model.b.bx = pyo.Var([1, 2, 3], initialize=42)
        model.b.bz = pyo.Var(initialize=42)

        c_list = list(
            model.component_objects(
                ctype=[pyo.Var, pyo.Constraint, pyo.Objective]))
        name_map = rename_components(model=model,
                                     component_list=c_list,
                                     prefix='scaled_')

        self.assertEqual(name_map[model.scaled_obj], 'obj')
        self.assertEqual(name_map[model.scaled_x], 'x')
        self.assertEqual(name_map[model.scaled_con], 'con')
        self.assertEqual(name_map[model.scaled_zcon], 'zcon')
        self.assertEqual(name_map[model.b.scaled_bz], 'b.bz')

        self.assertEqual(model.scaled_obj.name, 'scaled_obj')
        self.assertEqual(model.scaled_x.name, 'scaled_x')
        self.assertEqual(model.scaled_con.name, 'scaled_con')
        self.assertEqual(model.scaled_zcon.name, 'scaled_zcon')
        self.assertEqual(model.b.name, 'b')
        self.assertEqual(model.b.scaled_bz.name, 'b.scaled_bz')
Example #18
0
 def test_pow2(self):
     x_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8),
                 (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)]
     c_bounds = [(-2.5, 2.8), (0.5, 2.8), (0, 2.8), (1, 2.8), (0.5, 1)]
     for xl, xu in x_bounds:
         for cl, cu in c_bounds:
             m = pe.Block(concrete=True)
             m.x = pe.Var(bounds=(xl, xu))
             m.y = pe.Var()
             m.c = pe.Constraint(
                 expr=pe.inequality(body=m.y**m.x, lower=cl, upper=cu))
             fbbt(m)
             x = np.linspace(pe.value(m.x.lb) + 1e-6,
                             pe.value(m.x.ub),
                             100,
                             endpoint=False)
             z = np.linspace(pe.value(m.c.lower) + 1e-6,
                             pe.value(m.c.upper),
                             100,
                             endpoint=False)
             if m.y.lb is None:
                 yl = -np.inf
             else:
                 yl = m.y.lb
             if m.y.ub is None:
                 yu = np.inf
             else:
                 yu = m.y.ub
             for _x in x:
                 _y = np.exp(np.log(abs(z)) / _x)
                 self.assertTrue(np.all(yl <= _y))
                 self.assertTrue(np.all(yu >= _y))
def test_ASU_costing():
    # Create a Concrete Model as the top level object
    m = pyo.ConcreteModel()

    # Add a flowsheet object to the model
    m.fs = FlowsheetBlock(default={"dynamic": False})
    m.fs.get_costing(year='2017')

    m.fs.ASU = pyo.Block()
    m.fs.ASU.O2_flow = pyo.Var()
    m.fs.ASU.O2_flow.fix(13078)  # TPD

    get_ASU_cost(m.fs.ASU, m.fs.ASU.O2_flow)

    # try solving
    solver = get_solver()
    results = solver.solve(m, tee=True)

    assert results.solver.termination_condition == \
        pyo.TerminationCondition.optimal

    m.fs.ASU.costing.bare_erected_cost.display()

    assert pytest.approx(pyo.value(m.fs.ASU.costing.bare_erected_cost),
                         abs=1) == 3.2675e6 / 1e3

    return m
Example #20
0
 def test_sub1(self):
     if not numpy_available:
         raise unittest.SkipTest('Numpy is not available')
     x_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8),
                 (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)]
     c_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8),
                 (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)]
     for xl, xu in x_bounds:
         for cl, cu in c_bounds:
             m = pyo.Block(concrete=True)
             m.x = pyo.Var(bounds=(xl, xu))
             m.y = pyo.Var()
             m.c = pyo.Constraint(
                 expr=pyo.inequality(body=m.x - m.y, lower=cl, upper=cu))
             self.tightener(m)
             x = np.linspace(pyo.value(m.x.lb), pyo.value(m.x.ub), 100)
             z = np.linspace(pyo.value(m.c.lower), pyo.value(m.c.upper),
                             100)
             if m.y.lb is None:
                 yl = -np.inf
             else:
                 yl = m.y.lb
             if m.y.ub is None:
                 yu = np.inf
             else:
                 yu = m.y.ub
             for _x in x:
                 _y = _x - z
                 self.assertTrue(np.all(yl <= _y))
                 self.assertTrue(np.all(yu >= _y))
Example #21
0
 def test_sub2(self):
     x_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8),
                 (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)]
     c_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8),
                 (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)]
     for xl, xu in x_bounds:
         for cl, cu in c_bounds:
             m = pyo.Block(concrete=True)
             m.x = pyo.Var(bounds=(xl, xu))
             m.y = pyo.Var()
             m.c = pyo.Constraint(
                 expr=pyo.inequality(body=m.y - m.x, lower=cl, upper=cu))
             fbbt(m)
             x = np.linspace(pyo.value(m.x.lb), pyo.value(m.x.ub), 100)
             z = np.linspace(pyo.value(m.c.lower), pyo.value(m.c.upper),
                             100)
             if m.y.lb is None:
                 yl = -np.inf
             else:
                 yl = m.y.lb
             if m.y.ub is None:
                 yu = np.inf
             else:
                 yu = m.y.ub
             for _x in x:
                 _y = z + _x
                 self.assertTrue(np.all(yl <= _y))
                 self.assertTrue(np.all(yu >= _y))
Example #22
0
 def test_asin(self):
     m = pe.Block(concrete=True)
     m.x = pe.Var()
     m.c = pe.Constraint(expr=pe.inequality(body=pe.asin(m.x), lower=-0.5, upper=0.5))
     fbbt(m)
     self.assertAlmostEqual(pe.value(m.x.lb), math.sin(-0.5))
     self.assertAlmostEqual(pe.value(m.x.ub), math.sin(0.5))
Example #23
0
 def reconstruct_envelopes(self):
     self.del_component('lin_cuts')
     self.lin_cuts = pe.Block()
     for unit in itervalues(self.units):
         unit.reconstruct_envelopes()
     self.apply_linear_relaxations(nsegs=self._lin_cuts_nsegs,
                                   recurse=False)
Example #24
0
 def test_exp(self):
     if not numpy_available:
         raise unittest.SkipTest('Numpy is not available')
     c_bounds = [(-2.5, 2.8), (0.5, 2.8), (0, 2.8), (1, 2.8), (0.5, 1)]
     for cl, cu in c_bounds:
         m = pyo.Block(concrete=True)
         m.x = pyo.Var()
         m.c = pyo.Constraint(
             expr=pyo.inequality(body=pyo.exp(m.x), lower=cl, upper=cu))
         self.tightener(m)
         if pyo.value(m.c.lower) <= 0:
             _cl = 1e-6
         else:
             _cl = pyo.value(m.c.lower)
         z = np.linspace(_cl, pyo.value(m.c.upper), 100)
         if m.x.lb is None:
             xl = -np.inf
         else:
             xl = pyo.value(m.x.lb)
         if m.x.ub is None:
             xu = np.inf
         else:
             xu = pyo.value(m.x.ub)
         x = np.log(z)
         self.assertTrue(np.all(xl <= x))
         self.assertTrue(np.all(xu >= x))
def test_find_badly_scaled_vars():
    m = pyo.ConcreteModel()
    m.x = pyo.Var(initialize=1e6)
    m.y = pyo.Var(initialize=1e-8)
    m.z = pyo.Var(initialize=1e-20)
    m.b = pyo.Block()
    m.b.w = pyo.Var(initialize=1e10)

    a = [id(v) for v, sv in sc.badly_scaled_var_generator(m)]
    assert id(m.x) in a
    assert id(m.y) in a
    assert id(m.b.w) in a
    assert id(m.z) not in a

    m.scaling_factor = pyo.Suffix(direction=pyo.Suffix.EXPORT)
    m.b.scaling_factor = pyo.Suffix(direction=pyo.Suffix.EXPORT)
    m.scaling_factor[m.x] = 1e-6
    m.scaling_factor[m.y] = 1e6
    m.scaling_factor[m.z] = 1
    m.b.scaling_factor[m.b.w] = 1e-5

    a = [id(v) for v, sv in sc.badly_scaled_var_generator(m)]
    assert id(m.x) not in a
    assert id(m.y) not in a
    assert id(m.b.w) in a
    assert id(m.z) not in a
Example #26
0
 def test_add(self):
     x_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8),
                 (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)]
     c_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8),
                 (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)]
     for xl, xu in x_bounds:
         for cl, cu in c_bounds:
             m = pyo.Block(concrete=True)
             m.x = pyo.Var(bounds=(xl, xu))
             m.y = pyo.Var()
             m.p = pyo.Param(mutable=True)
             m.p.value = 1
             m.c = pyo.Constraint(expr=pyo.inequality(
                 body=m.x + m.y + (m.p + 1), lower=cl, upper=cu))
             new_bounds = fbbt(m)
             self.assertEqual(new_bounds[m.x],
                              (pyo.value(m.x.lb), pyo.value(m.x.ub)))
             self.assertEqual(new_bounds[m.y],
                              (pyo.value(m.y.lb), pyo.value(m.y.ub)))
             x = np.linspace(pyo.value(m.x.lb), pyo.value(m.x.ub), 100)
             z = np.linspace(pyo.value(m.c.lower), pyo.value(m.c.upper),
                             100)
             if m.y.lb is None:
                 yl = -np.inf
             else:
                 yl = m.y.lb
             if m.y.ub is None:
                 yu = np.inf
             else:
                 yu = m.y.ub
             for _x in x:
                 _y = z - _x - m.p.value - 1
                 self.assertTrue(np.all(yl <= _y))
                 self.assertTrue(np.all(yu >= _y))
Example #27
0
 def test_pow4(self):
     y_bounds = [(0.5, 2.8), (0, 2.8), (1, 2.8), (0.5, 1), (0, 0.5)]
     exp_vals = [-3, -2.5, -2, -1.5, -1, -0.5, 0.5, 1, 1.5, 2, 2.5, 3]
     for yl, yu in y_bounds:
         for _exp_val in exp_vals:
             m = pyo.Block(concrete=True)
             m.x = pyo.Var()
             m.y = pyo.Var(bounds=(yl, yu))
             m.c = pyo.Constraint(expr=m.x**_exp_val == m.y)
             fbbt(m)
             y = np.linspace(pyo.value(m.y.lb) + 1e-6,
                             pyo.value(m.y.ub),
                             100,
                             endpoint=True)
             if m.x.lb is None:
                 xl = -np.inf
             else:
                 xl = m.x.lb
             if m.x.ub is None:
                 xu = np.inf
             else:
                 xu = m.x.ub
             _x = np.exp(np.log(y) / _exp_val)
             self.assertTrue(np.all(xl <= _x))
             self.assertTrue(np.all(xu >= _x))
Example #28
0
 def test_acos(self):
     m = pe.Block(concrete=True)
     m.x = pe.Var()
     m.c = pe.Constraint(expr=pe.inequality(body=pe.acos(m.x), lower=1, upper=2))
     fbbt(m)
     self.assertAlmostEqual(pe.value(m.x.lb), math.cos(2))
     self.assertAlmostEqual(pe.value(m.x.ub), math.cos(1))
Example #29
0
    def model(self):
        m = pyo.ConcreteModel()
        m.fs = fs = pyo.Block()

        fs.input = pyo.Var(['a', 'b'], within=pyo.UnitInterval, initialize=0.5)
        fs.output = pyo.Var(['c', 'd'],
                            within=pyo.UnitInterval,
                            initialize=0.5)

        fs.slack = pyo.Var(['ab_slack', 'cd_slack'],
                           bounds=(0, 0),
                           initialize=0.0)
        fs.slack_penalty = pyo.Param(default=1000.,
                                     mutable=True,
                                     within=pyo.PositiveReals)

        fs.ab_constr = pyo.Constraint(
            expr=(fs.output['c'] + fs.slack['ab_slack'] == 2 * fs.input['a']))
        fs.cd_constr = pyo.Constraint(
            expr=(fs.output['d'] + fs.slack['cd_slack'] == 3 * fs.input['b']))

        fs.performance = pyo.Expression(expr=pyo.summation(fs.output))

        m.objective = pyo.Objective(
            expr=m.fs.performance -
            m.fs.slack_penalty * pyo.summation(m.fs.slack),
            sense=pyo.maximize)
        return m
Example #30
0
 def test_log10(self):
     if not numpy_available:
         raise unittest.SkipTest('Numpy is not available')
     c_bounds = [(-2.5, 2.8), (-2.5, -0.5), (0.5, 2.8), (-2.5, 0), (0, 2.8),
                 (-2.5, -1), (1, 2.8), (-1, -0.5), (0.5, 1)]
     for cl, cu in c_bounds:
         m = pyo.Block(concrete=True)
         m.x = pyo.Var()
         m.c = pyo.Constraint(
             expr=pyo.inequality(body=pyo.log10(m.x), lower=cl, upper=cu))
         self.tightener(m)
         z = np.linspace(pyo.value(m.c.lower), pyo.value(m.c.upper), 100)
         if m.x.lb is None:
             xl = -np.inf
         else:
             xl = pyo.value(m.x.lb)
         if m.x.ub is None:
             xu = np.inf
         else:
             xu = pyo.value(m.x.ub)
         x = 10**z
         print(xl, xu, cl, cu)
         print(x)
         self.assertTrue(np.all(xl <= x))
         self.assertTrue(np.all(xu >= x))