Skip to content

Commit

Permalink
clarify exceptions and make them easier to catch
Browse files Browse the repository at this point in the history
  • Loading branch information
bqpd committed Mar 4, 2020
1 parent 5bb8c83 commit 0711480
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
6 changes: 3 additions & 3 deletions gpkit/constraints/sgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from time import time
from collections import OrderedDict
import numpy as np
from ..exceptions import InvalidGPConstraint, Infeasible, InvalidSGP
from ..exceptions import InvalidGPConstraint, Infeasible, UnnecessarySGP
from ..keydict import KeyDict
from ..nomials import Variable
from .gp import GeometricProgram
Expand Down Expand Up @@ -52,7 +52,7 @@ def __init__(self, cost, model, substitutions, *,
use_pccp=True, pccp_penalty=2e2, **initgpargs):
# pylint: disable=super-init-not-called,non-parent-init-called
if cost.any_nonpositive_cs:
raise InvalidSGP("""Sequential GPs need Posynomial objectives.
raise UnnecessarySGP("""Sequential GPs need Posynomial objectives.
The equivalent of a Signomial objective can be constructed by constraining
a dummy variable `z` to be greater than the desired Signomial objective `s`
Expand All @@ -78,7 +78,7 @@ def __init__(self, cost, model, substitutions, *,
lts = cs.as_approxlts()
self._lt_approxs.append(lts)
if not sgpconstraints["SP constraints"]:
raise InvalidSGP("""Model valid as a Geometric Program.
raise UnnecessarySGP("""Model valid as a Geometric Program.
SequentialGeometricPrograms should only be created with Models containing
Signomial Constraints, since Models without Signomials have global
Expand Down
21 changes: 13 additions & 8 deletions gpkit/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
"GPkit-specific Exception classes"
from . import DimensionalityError # pylint: disable=unused-import

class InvalidGPConstraint(ValueError):
"Raised when a non-GP-compatible constraint is used in a GP"
class MathematicallyInvalid(TypeError):
"Raised whenever something violates a mathematical definition."

class InvalidPosynomial(ValueError):
class InvalidPosynomial(MathematicallyInvalid):
"Raised when a Posynomial would be created with a negative coefficient"

class InvalidSGP(ValueError):
class InvalidGPConstraint(MathematicallyInvalid):
"Raised when a non-GP-compatible constraint is used in a GP"

class Infeasible(Exception):
class UnnecessarySGP(MathematicallyInvalid):
"Raised when an SGP is fully GP-compatible"


class UnboundedGP(ValueError):
"Raise when a GP is not fully bounded"


class Infeasible(RuntimeWarning):
"Raised when a model does not solve"

class UnknownInfeasible(Infeasible):
Expand All @@ -21,6 +29,3 @@ class PrimalInfeasible(Infeasible):

class DualInfeasible(Infeasible):
"Raised when a model returns a certificate of dual infeasibility"

class UnboundedGP(Exception):
"Raise when a GP is not fully bounded"
8 changes: 4 additions & 4 deletions gpkit/tests/t_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from gpkit.constraints.relax import ConstraintsRelaxedEqually
from gpkit.constraints.relax import ConstantsRelaxed
from gpkit.exceptions import (UnknownInfeasible,
InvalidGPConstraint, InvalidSGP,
InvalidGPConstraint, UnnecessarySGP,
PrimalInfeasible, DualInfeasible, UnboundedGP)


Expand Down Expand Up @@ -386,7 +386,7 @@ def test_sp_substitutions(self):

with SignomialsEnabled():
m = Model(x, [x + z >= y])
with self.assertRaises(InvalidSGP):
with self.assertRaises(UnnecessarySGP):
m.localsolve(verbosity=0, solver=self.solver)
with self.assertRaises(UnboundedGP):
m.solve(verbosity=0, solver=self.solver)
Expand Down Expand Up @@ -590,7 +590,7 @@ def test_sigs_not_allowed_in_cost(self):
y = Variable("y")
J = 0.01*((x - 1)**2 + (y - 1)**2) + (x*y - 1)**2
m = Model(J)
with self.assertRaises(InvalidSGP):
with self.assertRaises(UnnecessarySGP):
m.localsolve(verbosity=0, solver=self.solver)

def test_partial_sub_signomial(self):
Expand All @@ -612,7 +612,7 @@ def test_becomes_signomial(self):
with self.assertRaises(InvalidGPConstraint):
with SignomialsEnabled():
m.gp()
with self.assertRaises(InvalidSGP):
with self.assertRaises(UnnecessarySGP):
m.localsolve(solver=self.solver)

def test_reassigned_constant_cost(self):
Expand Down

0 comments on commit 0711480

Please sign in to comment.