Skip to content

Commit

Permalink
pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
whoburg committed Jan 5, 2024
1 parent 42dcba9 commit 79b6384
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions gpkit/nomials/math.py
Expand Up @@ -3,7 +3,7 @@
import numpy as np
from .core import Nomial
from .array import NomialArray
from .. import units
from .. import units, NamedVariables
from ..constraints import SingleEquationConstraint
from ..globals import SignomialsEnabled
from ..small_classes import Numbers
Expand Down Expand Up @@ -116,8 +116,8 @@ def mono_approximation(self, x0):
x0, _, _ = parse_subs(self.vks, x0) # use only varkey keys
psub = self.hmap.sub(x0, self.vks, parsedsubs=True)
if EMPTY_HV not in psub or len(psub) > 1:
raise ValueError("Variables %s remained after substituting x0=%s"
" into %s" % (psub, x0, self))
raise ValueError(f"Variables {psub} remained after substituting "
f"x0={x0} into {self}")
c0, = psub.values()
c, exp = c0, HashVector()
for vk in self.vks:
Expand All @@ -129,14 +129,14 @@ def mono_approximation(self, x0):
exp[vk] = e
try:
c /= val**e
except OverflowError:
except OverflowError as exc:
raise OverflowError(
"While approximating the variable %s with a local value of"
" %s, %s/(%s**%s) overflowed. Try reducing the variable's"
" value by changing its unit prefix, or specify x0 values"
" for any free variables it's multiplied or divided by in"
" the posynomial %s whose expected value is far from 1."
% (vk, val, c, val, e, self))
f"While approximating the variable {vk} with a local value"
f" of {val}, {c}/({val}**{e}) overflowed. Try reducing the"
" variable's value by changing its unit prefix, or specify "
"x0 values for any free variables it's multiplied or "
f"divided by in the posynomial {self} whose expected value"
" is far from 1.") from exc
hmap = NomialMap({exp: c})
hmap.units = self.units
return Monomial(hmap)
Expand Down Expand Up @@ -337,7 +337,7 @@ def __eq__(self, other):
try: # if both are monomials, return a constraint
return MonomialEquality(self, other)
except (DimensionalityError, ValueError) as e:
print("Infeasible monomial equality: %s" % e)
print(f"Infeasible monomial equality: {e}")
return False
return super().__eq__(other)

Expand Down Expand Up @@ -376,7 +376,6 @@ def __init__(self, left, oper, right):
self.vks.update(exp)
else:
lr[i] = Signomial(sig)
from .. import NamedVariables
self.lineage = tuple(NamedVariables.lineage)
super().__init__(lr[0], oper, lr[1])

Expand All @@ -390,7 +389,7 @@ def relaxed(self, relaxvar):
return [self.left <= relaxvar*self.right,
relaxvar*self.left >= self.right]
raise ValueError(
"Constraint %s had unknown operator %s." % self.oper, self)
f"Constraint {self.oper} had unknown operator {self}.")


# pylint: disable=too-many-instance-attributes, invalid-unary-operand-type
Expand All @@ -409,7 +408,7 @@ def __init__(self, left, oper, right):
elif self.oper == ">=":
m_gt, p_lt = self.left, self.right
else:
raise ValueError("operator %s is not supported." % self.oper)
raise ValueError(f"operator {self.oper} is not supported.")

self.unsubbed = self._gen_unsubbed(p_lt, m_gt)
self.bounded = set()
Expand All @@ -430,9 +429,9 @@ def _simplify_posy_ineq(self, hmap, pmap=None, fixed=None):
if coeff >= -self.feastol and len(hmap) == 1:
return None # a tautological monomial!
if coeff < -self.feastol:
msg = "'%s' is infeasible by %.2g%%" % (self, -coeff*100)
msg = f"'{self}' is infeasible by {-coeff*100:.2g}%"
if fixed:
msg += " after substituting %s." % fixed
msg += f" after substituting {fixed}."
raise PrimalInfeasible(msg)
scaled = hmap/coeff
scaled.units = hmap.units
Expand All @@ -454,8 +453,9 @@ def _gen_unsubbed(self, p_lt, m_gt):
try:
m_exp, = m_gt.hmap.keys()
m_c, = m_gt.hmap.values()
except ValueError:
raise TypeError("greater-than side '%s' is not monomial." % m_gt)
except ValueError as valerr:
raise TypeError(
f"greater-than side '{m_gt}' is not monomial.") from valerr
m_c *= units.of_division(m_gt, p_lt)
hmap = p_lt.hmap.copy()
for exp in list(hmap):
Expand All @@ -474,8 +474,8 @@ def as_hmapslt1(self, substitutions):
hmap = self._simplify_posy_ineq(hmap, self.pmap, fixed)
if hmap is not None:
if any(c <= 0 for c in hmap.values()):
raise InvalidGPConstraint("'%s' became Signomial after sub"
"stituting %s" % (self, fixed))
raise InvalidGPConstraint(f"'{self}' became Signomial "
f"after substituting {fixed}")
hmap.parent = self
out.append(hmap)
return out
Expand Down Expand Up @@ -528,7 +528,7 @@ def __init__(self, left, right):
self.meq_bounded[(key, "upper")] = frozenset([ubs])
self.meq_bounded[(key, "lower")] = frozenset([lbs])

def _gen_unsubbed(self, left, right): # pylint: disable=arguments-differ
def _gen_unsubbed(self, left, right): # pylint: disable=arguments-renamed
"Returns the unsubstituted posys <= 1."
unsubbed = PosynomialInequality._gen_unsubbed
l_over_r = unsubbed(self, left, right)
Expand Down Expand Up @@ -574,7 +574,7 @@ def __init__(self, left, oper, right):
elif self.oper == ">=":
pgt, plt = self.left, self.right
else:
raise ValueError("operator %s is not supported." % self.oper)
raise ValueError(f"operator {self.oper} is not supported.")
self.unsubbed = [plt - pgt]
self.bounded = self.as_gpconstr({}).bounded

Expand All @@ -584,17 +584,17 @@ def as_hmapslt1(self, substitutions):
siglt0 = siglt0.sub(substitutions, require_positive=False)
posy, negy = siglt0.posy_negy()
if posy is 0: # pylint: disable=literal-comparison
print("Warning: SignomialConstraint %s became the tautological"
" constraint 0 <= %s after substitution." % (self, negy))
print(f"Warning: SignomialConstraint {self} became the tautological"
f" constraint 0 <= {negy} after substitution.")
return []
if negy is 0: # pylint: disable=literal-comparison
raise ValueError("%s became the infeasible constraint %s <= 0"
" after substitution." % (self, posy))
raise ValueError(f"{self} became the infeasible constraint {posy} "
"<= 0 after substitution.")
if hasattr(negy, "cs") and len(negy.cs) > 1:
raise InvalidGPConstraint(
"%s did not simplify to a PosynomialInequality; try calling"
" `.localsolve` instead of `.solve` to form your Model as a"
" SequentialGeometricProgram." % self)
f"{self} did not simplify to a PosynomialInequality; try "
"calling `.localsolve` instead of `.solve` to form your Model "
"as a SequentialGeometricProgram.")
# all but one of the negy terms becomes compatible with the posy
p_ineq = PosynomialInequality(posy, "<=", negy)
p_ineq.parent = self
Expand Down Expand Up @@ -653,7 +653,7 @@ def subval(posy):
- (subval(self._negysig.diff(var))
* subval(coeff) * invnegy_val**2))
var_val = result["variables"][var]
sens = (nu_i*inv_mon_val*d_mon_d_var*var_val)
sens = nu_i*inv_mon_val*d_mon_d_var*var_val
assert isinstance(sens, float)
self.v_ss[var] = sens + self.v_ss.get(var, 0)
return self.v_ss, la
Expand Down

0 comments on commit 79b6384

Please sign in to comment.