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_power_with_non_polynomial_exponent(visitor, base, expo, base_poly):
poly = ComponentMap()
poly[base] = base_poly
poly[expo] = PolynomialDegree(None)
expr = base ** expo
matched, result = visitor.visit_expression(expr, poly)
assert matched
assert not result.is_polynomial()
def _rule_result(self, visitor, base, cvx_base, mono_base, bounds_base, expo):
convexity = ComponentMap()
convexity[base] = cvx_base
convexity[expo] = C.Linear
mono = ComponentMap()
mono[base] = mono_base
mono[expo] = M.Constant
bounds = ComponentMap()
bounds[base] = bounds_base
bounds[expo] = I(expo, expo)
expr = PowExpression([base, expo])
matched, result = visitor.visit_expression(expr, convexity, mono, bounds)
assert matched
return result
def _rule_result(self, cvx_f, cvx_g, mono_f, mono_g, bounds_f, bounds_g):
rule = DivisionRule()
f = PE()
g = PE()
convexity = ComponentMap()
convexity[f] = cvx_f
convexity[g] = cvx_g
mono = ComponentMap()
mono[f] = mono_f
mono[g] = mono_g
bounds = ComponentMap()
bounds[f] = bounds_f
bounds[g] = bounds_g
return rule.apply(PE(ET.Division, [f, g]), convexity, mono, bounds)
def _rule_result(self, visitor, base, expo, cvx_expo, mono_expo, bounds_expo):
convexity = ComponentMap()
convexity[expo] = cvx_expo
convexity[base] = C.Linear
mono = ComponentMap()
mono[expo] = mono_expo
mono[base] = M.Constant
bounds = ComponentMap()
bounds[base] = I(base, base)
bounds[expo] = bounds_expo
expr = PowExpression([base, expo])
matched, result = visitor.visit_expression(expr, convexity, mono, bounds)
assert matched
return result
def test_division(visitor, g, mono_g, bound_g, expected):
num = NumericConstant(1.0)
bounds = ComponentMap()
bounds[num] = I(1.0, 1.0)
bounds[g] = bound_g
mono = ComponentMap()
mono[num] = M.Constant
mono[g] = mono_g
print(mono)
print(mono[num])
expr = DivisionExpression([num, g])
matched, result = visitor.visit_expression(expr, mono, bounds)
assert matched
assert result == expected
def _result_with_terms(self, visitor, expr_with_monos):
children = [c for c, _ in expr_with_monos]
monos = [m for _, m in expr_with_monos]
monotonicity = ComponentMap()
for child, mono in expr_with_monos:
monotonicity[child] = mono
expr = SumExpression(children)
matched, result = visitor.visit_expression(expr, monotonicity, None)
assert matched
return result
def _result_with_mono_bounds(self, visitor, g, mono_g, bounds_g):
mono = ComponentMap()
mono[g] = mono_g
bounds = ComponentMap()
bounds[g] = bounds_g
expr = pe.cos(g)
matched, result = visitor.visit_expression(expr, mono, bounds)
assert matched
return result
def _set_instance(self, model, kwds={}):
self._range_constraints = set()
DirectOrPersistentSolver._set_instance(self, model, kwds)
self._pyomo_con_to_solver_con_map = dict()
self._solver_con_to_pyomo_con_map = ComponentMap()
self._pyomo_var_to_solver_var_map = ComponentMap()
self._solver_var_to_pyomo_var_map = ComponentMap()
try:
if model.name is not None:
self._solver_model = self._gurobipy.Model(model.name)
else:
self._solver_model = self._gurobipy.Model()
except Exception:
e = sys.exc_info()[1]
msg = ("Unable to create Gurobi model. "
"Have you installed the Python "
"bindings for Gurboi?\n\n\t"+
"Error message: {0}".format(e))
raise Exception(msg)
self._add_block(model)
def _build_equality_set(m):
"""Construct an equality set map.
Maps all variables to the set of variables that are linked to them by
equality. Mapping takes place using id(). That is, if you have x = y, then
you would have id(x) -> ComponentSet([x, y]) and id(y) -> ComponentSet([x,
y]) in the mapping.
"""
#: dict: map of var UID to the set of all equality-linked var UIDs
eq_var_map = ComponentMap()
relevant_vars = ComponentSet()
for constr in m.component_data_objects(ctype=Constraint,
active=True,
descend_into=True):
# Check to make sure the constraint is of form v1 - v2 == 0
if (value(constr.lower) == 0 and value(constr.upper) == 0 and
constr.body.polynomial_degree() == 1):
repn = generate_standard_repn(constr.body)
# only take the variables with nonzero coefficients
vars_ = [v for i, v in enumerate(repn.linear_vars)
if repn.linear_coefs[i]]
if (len(vars_) == 2 and
sorted(l for l in repn.linear_coefs if l) == [-1, 1]):
# this is an a == b constraint.
v1 = vars_[0]
v2 = vars_[1]
def _set_instance(self, model, kwds={}):
self._pyomo_var_to_ndx_map = ComponentMap()
self._ndx_count = 0
self._range_constraints = set()
DirectOrPersistentSolver._set_instance(self, model, kwds)
try:
self._solver_model = self._cplex.Cplex()
except Exception:
e = sys.exc_info()[1]
msg = ("Unable to create CPLEX model. "
"Have you installed the Python "
"bindings for CPLEX?\n\n\t"+
"Error message: {0}".format(e))
raise Exception(msg)
self._add_block(model)
for var, n_ref in self._referenced_variables.items():