Skip to content

Commit

Permalink
fix warnings table for intermittent warnings in sweeps
Browse files Browse the repository at this point in the history
  • Loading branch information
bqpd committed Mar 10, 2021
1 parent 3f0381a commit 98b6d16
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 12 deletions.
3 changes: 2 additions & 1 deletion gpkit/constraints/bounded.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
from .. import Variable
from .set import ConstraintSet
from ..small_scripts import appendsolwarning
from ..small_scripts import appendsolwarning, initsolwarning


def varkey_bounds(varkeys, lower, upper):
Expand Down Expand Up @@ -77,6 +77,7 @@ def process_result(self, result):
def check_boundaries(self, result):
"Creates (and potentially prints) a dictionary of unbounded variables."
out = defaultdict(set)
initsolwarning(result, "Arbitrarily Bounded Variables")
for i, varkey in enumerate(self.bound_varkeys):
value = result["variables"][varkey]
c_senss = [result["sensitivities"]["constraints"].get(c, 0)
Expand Down
3 changes: 2 additions & 1 deletion gpkit/constraints/loose.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"Implements Loose"
from .set import ConstraintSet
from ..small_scripts import appendsolwarning
from ..small_scripts import appendsolwarning, initsolwarning


class Loose(ConstraintSet):
Expand All @@ -15,6 +15,7 @@ def __init__(self, constraints, *, senstol=None):
def process_result(self, result):
"Checks that all constraints are satisfied with equality"
super().process_result(result)
initsolwarning(result, "Unexpectedly Tight Constraints")
for constraint in self.flat():
c_senss = result["sensitivities"]["constraints"].get(constraint, 0)
if c_senss >= self.senstol:
Expand Down
13 changes: 9 additions & 4 deletions gpkit/constraints/prog_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def run_sweep(genfunction, self, solution, skipsweepfailures,
for (var, grid) in zip(sweepvars, sweep_grids)}

if verbosity > 0:
print("Sweeping over %i solves." % N_passes)
print("Sweeping with %i solves:" % N_passes)
tic = time()

self.program = []
Expand All @@ -143,6 +143,8 @@ def run_sweep(genfunction, self, solution, skipsweepfailures,
program, solvefn = genfunction(self, constants)
self.program.append(program) # NOTE: SIDE EFFECTS
try:
if verbosity > 1:
print("\nSolve %i:" % i)
result = solvefn(solver, verbosity=verbosity-1, **solveargs)
if solveargs.get("process_result", True):
self.process_result(result)
Expand All @@ -151,10 +153,13 @@ def run_sweep(genfunction, self, solution, skipsweepfailures,
last_error = e
if not skipsweepfailures:
raise RuntimeWarning(
"Sweep halted! Progress saved to m.program. To skip over"
" such failures, solve with skipsweepfailures=True.") from e
"Solve %i was infeasible; progress saved to m.program."
" To continue sweeping after failures, solve with"
" skipsweepfailures=True." % i) from e
if verbosity > 0:
print("Solve %i was %s." % (i, e.__class__.__name__))
if not solution:
raise RuntimeWarning("No sweeps solved successfully.") from last_error
raise RuntimeWarning("All solves were infeasible.") from last_error

solution["sweepvariables"] = KeyDict()
ksweep = KeyDict(sweep)
Expand Down
5 changes: 4 additions & 1 deletion gpkit/constraints/relax.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ..nomials import Variable, VectorVariable, parse_subs, NomialArray
from ..keydict import KeyDict
from .. import NamedVariables, SignomialsEnabled
from ..small_scripts import appendsolwarning, mag
from ..small_scripts import appendsolwarning, initsolwarning, mag


class ConstraintsRelaxedEqually(ConstraintSet):
Expand Down Expand Up @@ -50,6 +50,7 @@ def process_result(self, result):

def check_relaxed(self, result):
"Adds relaxation warnings to the result"
initsolwarning(result, "Relaxed Constraints")
for val, msg in get_relaxed([result["freevariables"][self.relaxvar]],
["All constraints relaxed by %i%%"]):
appendsolwarning(msg % (0.9+(val-1)*100), self, result,
Expand Down Expand Up @@ -103,6 +104,7 @@ def check_relaxed(self, result):
"Adds relaxation warnings to the result"
relaxed = get_relaxed(result["freevariables"][self.relaxvars],
range(len(self["relaxed constraints"])))
initsolwarning(result, "Relaxed Constraints")
for relaxval, i in relaxed:
relax_percent = "%i%%" % (0.5+(relaxval-1)*100)
oldconstraint = self.original_constraints[i]
Expand Down Expand Up @@ -224,6 +226,7 @@ def check_relaxed(self, result):
"Adds relaxation warnings to the result"
relaxed = get_relaxed([result["freevariables"][r]
for r in self.relaxvars], self.freedvars)
initsolwarning(result, "Relaxed Constants")
for (_, freed) in relaxed:
msg = (" %s: relaxed from %-.4g to %-.4g"
% (freed,
Expand Down
3 changes: 2 additions & 1 deletion gpkit/constraints/tight.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"Implements Tight"
from .set import ConstraintSet
from ..small_scripts import mag
from ..small_scripts import appendsolwarning
from ..small_scripts import appendsolwarning, initsolwarning
from .. import SignomialsEnabled


Expand All @@ -18,6 +18,7 @@ def process_result(self, result):
"Checks that all constraints are satisfied with equality"
super().process_result(result)
variables = result["variables"]
initsolwarning(result, "Unexpectedly Loose Constraints")
for constraint in self.flat():
with SignomialsEnabled():
leftval = constraint.left.sub(variables).value
Expand Down
8 changes: 6 additions & 2 deletions gpkit/small_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import numpy as np


def appendsolwarning(msg, data, result, category="uncategorized"):
"Append a particular category of warnings to a solution."
def initsolwarning(result, category="uncategorized"):
"Creates a results dictionary for a particular category of warning."
if "warnings" not in result:
result["warnings"] = {}
if category not in result["warnings"]:
result["warnings"][category] = []


def appendsolwarning(msg, data, result, category="uncategorized"):
"Append a particular category of warnings to a solution."
result["warnings"][category].append((msg, data))


Expand Down
6 changes: 4 additions & 2 deletions gpkit/solution_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ def warnings_table(self, _, **kwargs):
return []
for wtype in sorted(self["warnings"]):
data_vec = self["warnings"][wtype]
if len(data_vec) == 0:
continue
if not hasattr(data_vec, "shape"):
data_vec = [data_vec]
for i, data in enumerate(data_vec):
Expand Down Expand Up @@ -650,8 +652,8 @@ def summary(self, showvars=(), ntopsenss=5, **kwargs):
return out

def table(self, showvars=(),
tables=("cost", "warnings", "sweepvariables",
"model sensitivities", "freevariables",
tables=("cost", "warnings", "model sensitivities",
"sweepvariables", "freevariables",
"constants", "sensitivities", "tightest constraints"),
sortmodelsbysenss=True, **kwargs):
"""A table representation of this SolutionArray
Expand Down

0 comments on commit 98b6d16

Please sign in to comment.