Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_division_rule_with_nonconstant_denominator(visitor, den_degree):
assume(not den_degree.is_constant())
child = pe.Var()
num = 1.0
expr = DivisionExpression([num, child])
poly = ComponentMap()
poly[num] = PolynomialDegree(0)
poly[child] = den_degree
matched, result = visitor.visit_expression(expr, poly)
assert matched
assert not result.is_polynomial()
def test_disjunction_unsat(self):
m = ConcreteModel()
m.x1 = Var(bounds=(0, 8))
m.x2 = Var(bounds=(0, 8))
m.obj = Objective(expr=m.x1 + m.x2, sense=minimize)
m.y1 = Disjunct()
m.y2 = Disjunct()
m.y1.c1 = Constraint(expr=m.x1 >= 9)
m.y1.c2 = Constraint(expr=m.x2 >= 2)
m.y2.c1 = Constraint(expr=m.x1 >= 3)
m.y2.c2 = Constraint(expr=m.x2 >= 9)
m.djn = Disjunction(expr=[m.y1, m.y2])
self.assertFalse(satisfiable(m))
def declareVariables(self, esM, pyM):
""" Declares design and operation variables """
# Function for setting lower and upper capacity bounds
def capBounds(pyM, loc, loc_, compName):
comp = self._componentsDict[compName]
return (comp._capacityMin[loc][loc_]
if (comp._capacityMin is not None and not comp._hasIsBuiltBinaryVariable) else 0,
comp._capacityMax[loc][loc_] if comp._capacityMax is not None else None)
# Capacity of components [powerUnit]
pyM.cap_trans = pyomo.Var(pyM.designDimensionVarSet_trans, domain=pyomo.NonNegativeReals, bounds=capBounds)
# Number of components [-]
pyM.nbReal_trans = pyomo.Var(pyM.continuousDesignDimensionVarSet_trans, domain=pyomo.NonNegativeReals)
# Number of components [-]
pyM.nbInt_trans = pyomo.Var(pyM.discreteDesignDimensionVarSet_trans, domain=pyomo.NonNegativeIntegers)
# Binary variables [-], indicate if a component is considered at a location or not
pyM.designBin_trans = pyomo.Var(pyM.designDecisionVarSet_trans, domain=pyomo.Binary)
# Operation of component [energyUnit]
pyM.op_trans = pyomo.Var(pyM.operationVarSet_trans, pyM.timeSet, domain=pyomo.NonNegativeReals)
model.Battery_Nominal_Capacity = Var(within=NonNegativeReals) # Capacity of the battery bank in Wh
model.Energy_Battery_Flow_Out = Var(model.scenario,model.periods, within=NonNegativeReals) # Battery discharge energy in wh
model.Energy_Battery_Flow_In = Var(model.scenario,model.periods, within=NonNegativeReals) # Battery charge energy in wh
model.State_Of_Charge_Battery = Var(model.scenario,model.periods, within=NonNegativeReals) # State of Charge of the Battery in wh
model.Maximun_Charge_Power= Var() # Maximun charge power in w
model.Maximun_Discharge_Power = Var() #Maximun discharge power in w
# Variables associated to the diesel generator
model.Period_Total_Cost_Generator = Var(model.scenario,model.periods, within=NonNegativeReals)
model.Energy_Generator_Total = Var(model.scenario, model.periods, within=NonNegativeReals)
model.Integer_generator = Var(within=NonNegativeIntegers)
model.Total_Cost_Generator = Var(model.scenario,within=NonNegativeReals)
model.Generator_Total_Period_Energy = Var(model.scenario,model.periods, within=NonNegativeReals)
model.Generator_Energy_Integer = Var(model.scenario, model.periods, within=NonNegativeIntegers)
model.Last_Energy_Generator = Var(model.scenario, model.periods, within=NonNegativeReals)
# Varialbles associated to the energy balance
model.Lost_Load = Var(model.scenario,model.periods, within=NonNegativeReals) # Energy not suply by the system kWh
model.Energy_Curtailment = Var(model.scenario,model.periods, within=NonNegativeReals) # Curtailment of solar energy in kWh
# Variables associated to the project
model.Cost_Financial = Var(within=NonNegativeReals) # Financial cost of each period in USD
model.Initial_Inversion = Var(within=NonNegativeReals)
model.Operation_Maintenance_Cost = Var(within=NonNegativeReals)
model.Total_Finalcial_Cost = Var(within=NonNegativeReals)
model.Battery_Reposition_Cost = Var(within=NonNegativeReals)
model.Scenario_Lost_Load_Cost = Var(model.scenario, within=NonNegativeReals) ####
model.Sceneario_Generator_Total_Cost = Var(model.scenario, within=NonNegativeReals)
model.Scenario_Net_Present_Cost = Var(model.scenario, within=NonNegativeReals)
"""
super(MethaneCombustionStateBlockData, self).build()
# Create state variables
self.flow_mol_comp = Var(self._params.component_list,
initialize=1.0,
doc='Component molar flowrate [mol/s]')
self.pressure = Var(domain=Reals,
initialize=101325.0,
bounds=(1e3, 1e6),
doc='State pressure [Pa]')
self.temperature = Var(domain=Reals,
initialize=1500,
bounds=(1450, 3000),
doc='State temperature [K]')
self.mole_frac_comp = Var(self._params.component_list,
domain=Reals,
initialize=0.0,
doc='State component mole fractions [-]')
# Create standard constraints
# Mole fractions
def mole_frac_constraint(b, j):
return b.flow_mol_comp[j] == (
b.mole_frac_comp[j] *
sum(b.flow_mol_comp[k]
for k in b._params.component_list))
self.mole_frac_constraint = Constraint(self._params.component_list,
rule=mole_frac_constraint)
def build(self):
"""
Callable method for Block construction
"""
super(InterrogatorStateBlockData, self).build()
# Add dummy vars for building Ports and returning expressions
self._dummy_var = Var(initialize=1)
self._dummy_var_phase = Var(self.params.phase_list,
initialize=1)
self._dummy_var_comp = Var(self.params.component_list,
initialize=1)
self._dummy_var_phase_comp = Var(
self.params.phase_list,
self.params.component_list,
initialize=1)
# define temporal variables
def p_bounds_rule(m, k, j, t):
return aml.value(m.pmin[j]), aml.value(m.pmax[j])
model.p = aml.Var(model.SCEN, model.NODE, model.TIME, bounds=p_bounds_rule, initialize=50.0)
model.dp = aml.Var(model.SCEN, model.LINK_A, model.TIME, bounds=(0.0, 100.0), initialize=10.0)
model.fin = aml.Var(model.SCEN, model.LINK, model.TIME, bounds=(1.0, 500.0), initialize=100.0)
model.fout = aml.Var(model.SCEN, model.LINK, model.TIME, bounds=(1.0, 500.0), initialize=100.0)
def s_bounds_rule(m, k, j, t):
return 0.01, aml.value(m.smax[j])
model.s = aml.Var(model.SCEN, model.SUP, model.TIME, bounds=s_bounds_rule, initialize=10.0)
model.dem = aml.Var(model.SCEN, model.DEM, model.TIME, initialize=100.0)
model.pow = aml.Var(model.SCEN, model.LINK_A, model.TIME, bounds=(0.0, 3000.0), initialize=1000.0)
model.slack = aml.Var(model.SCEN, model.LINK, model.TIME, model.DIS, bounds=(0.0, None), initialize=10.0)
# define spatio-temporal variables
# average 55.7278214666423
model.px = aml.Var(model.SCEN, model.LINK, model.TIME, model.DIS, bounds=(10.0, 100.0), initialize=50.0)
# average 43.19700578593625
model.fx = aml.Var(model.SCEN, model.LINK, model.TIME, model.DIS, bounds=(1.0, 100.0), initialize=100.0)
# define derivatives
model.dpxdt = dae.DerivativeVar(model.px, wrt=model.TIME, initialize=0)
model.dpxdx = dae.DerivativeVar(model.px, wrt=model.DIS, initialize=0)
model.dfxdt = dae.DerivativeVar(model.fx, wrt=model.TIME, initialize=0)
model.dfxdx = dae.DerivativeVar(model.fx, wrt=model.DIS, initialize=0)
# ----------- MODEL --------------
def create_model(eta1, eta2):
model = aml.ConcreteModel()
# variables
model.x1 = aml.Var(initialize=0.15)
model.x2 = aml.Var(initialize=0.15)
model.x3 = aml.Var(initialize=0.0)
# parameters
model.eta1 = aml.Var()
model.eta2 = aml.Var()
model.nominal_eta1 = aml.Param(initialize=eta1, mutable=True)
model.nominal_eta2 = aml.Param(initialize=eta2, mutable=True)
# constraints + objective
model.const1 = aml.Constraint(expr=6*model.x1+3*model.x2+2*model.x3 - model.eta1 == 0)
model.const2 = aml.Constraint(expr=model.eta2*model.x1+model.x2-model.x3-1 == 0)
model.cost = aml.Objective(expr=model.x1**2 + model.x2**2 + model.x3**2)
model.consteta1 = aml.Constraint(expr=model.eta1 == model.nominal_eta1)
model.consteta2 = aml.Constraint(expr=model.eta2 == model.nominal_eta2)
return model
def create_basic_model():
m = aml.ConcreteModel()
m.x = aml.Var([1, 2, 3], domain=aml.Reals)
for i in range(1, 4):
m.x[i].value = i
m.c1 = aml.Constraint(expr=m.x[1] ** 2 - m.x[2] - 1 == 0)
m.c2 = aml.Constraint(expr=m.x[1] - m.x[3] - 0.5 == 0)
m.d1 = aml.Constraint(expr=m.x[1] + m.x[2] <= 100.0)
m.d2 = aml.Constraint(expr=m.x[2] + m.x[3] >= -100.0)
m.d3 = aml.Constraint(expr=m.x[2] + m.x[3] + m.x[1] >= -500.0)
m.x[2].setlb(0.0)
m.x[3].setlb(0.0)
m.x[2].setub(100.0)
m.obj = aml.Objective(expr=m.x[2]**2)
return m