Skip to content

Commit

Permalink
Merge 451d1dd into 1ac62d2
Browse files Browse the repository at this point in the history
  • Loading branch information
bqpd committed Feb 16, 2022
2 parents 1ac62d2 + 451d1dd commit de8b25c
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 23 deletions.
16 changes: 13 additions & 3 deletions docs/source/examples/breakdowns.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@
from packaging import version
from gpkit.breakdowns import Breakdowns

if version.parse(pint.__version__) >= version.parse("0.9"):
dirpath = os.path.dirname(os.path.realpath(__file__)) + os.sep
if version.parse(pint.__version__) >= version.parse("0.13"):
sol = pickle.load(open(dirpath+"solar_13.p", "rb"))
elif version.parse(pint.__version__) >= version.parse("0.12"):
sol = pickle.load(open(dirpath+"solar_12.p", "rb"))
elif version.parse(pint.__version__) >= version.parse("0.10"):
sol = pickle.load(open(dirpath+"solar_10.p", "rb"))
elif version.parse(pint.__version__) == version.parse("0.9"):
sol = pickle.load(open(dirpath+"solar.p", "rb"))
else:
sol = None

if sol is not None:
# the code to create solar.p is in ./breakdowns/solartest.py
filepath = os.path.dirname(os.path.realpath(__file__)) + os.sep + "solar.p"
sol = pickle.load(open(filepath, "rb"))
bds = Breakdowns(sol)

print("Cost breakdown (as seen in solution tables)")
Expand Down
2 changes: 1 addition & 1 deletion docs/source/examples/breakdowns/solartest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
M = Mission(Vehicle, latitude=[20])
M.cost = M[M.aircraft.Wtotal]

M.localsolve().save("solar.p")
M.localsolve().save("solar_13.p") # suffix is min pint version worked for
Binary file added docs/source/examples/solar_10.p
Binary file not shown.
Binary file added docs/source/examples/solar_12.p
Binary file not shown.
Binary file added docs/source/examples/solar_13.p
Binary file not shown.
15 changes: 0 additions & 15 deletions gpkit/breakdowns.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,6 @@ def get_free_vks(posy, solution):
return set(vk for vk in posy.vks if vk not in solution["constants"])

def get_model_breakdown(solution):
breakdowns = {"|sensitivity|": 0}
for modelname, senss in solution["sensitivities"]["models"].items():
senss = abs(senss) # for those monomial equalities
*namespace, name = modelname.split(".")
subbd = breakdowns
subbd["|sensitivity|"] += senss
for parent in namespace:
if parent not in subbd:
subbd[parent] = {parent: {}}
subbd = subbd[parent]
if "|sensitivity|" not in subbd:
subbd["|sensitivity|"] = 0
subbd["|sensitivity|"] += senss
subbd[name] = {"|sensitivity|": senss}
# print(breakdowns["HyperloopSystem"]["|sensitivity|"])
breakdowns = {"|sensitivity|": 0}
for constraint, senss in solution["sensitivities"]["constraints"].items():
senss = abs(senss) # for those monomial
Expand Down
13 changes: 11 additions & 2 deletions gpkit/constraints/prog_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ def run_sweep(genfunction, self, solution, skipsweepfailures,
for (var, grid) in zip(sweepvars, sweep_grids)}

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

self.program = []
Expand All @@ -170,24 +169,34 @@ def run_sweep(genfunction, self, solution, skipsweepfailures,
program, solvefn = genfunction(self, constants, **kwargs)
program.model = None # so it doesn't try to debug
self.program.append(program) # NOTE: SIDE EFFECTS
if i == 0 and verbosity > 0: # wait for successful program gen
# TODO: use full string when minimum lineage is set automatically
sweepvarsstr = ", ".join([sv.name for sv in sweepvars])
print("Sweeping %s with %i solves:" % (sweepvarsstr, N_passes))
try:
if verbosity > 1:
print("\nSolve %i:" % i)
result = solvefn(solver, verbosity=verbosity-1, **kwargs)
if kwargs.get("process_result", True):
self.process_result(result)
solution.append(result)
if verbosity == 1:
print(".", end="", flush=True)
except Infeasible as e:
last_error = e
if not skipsweepfailures:
raise RuntimeWarning(
"Solve %i was infeasible; progress saved to m.program."
" To continue sweeping after failures, solve with"
" skipsweepfailures=True." % i) from e
if verbosity > 0:
if verbosity > 1:
print("Solve %i was %s." % (i, e.__class__.__name__))
if verbosity == 1:
print("!", end="", flush=True)
if not solution:
raise RuntimeWarning("All solves were infeasible.") from last_error
if verbosity == 1:
print()

solution["sweepvariables"] = KeyDict()
ksweep = KeyDict(sweep)
Expand Down
6 changes: 5 additions & 1 deletion gpkit/constraints/sgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class SequentialGeometricProgram:

def __init__(self, cost, model, substitutions,
*, use_pccp=True, pccp_penalty=2e2, **kwargs):
self.cost = cost
self.pccp_penalty = pccp_penalty
if cost.any_nonpositive_cs:
raise InvalidPosynomial("""an SGP's cost must be Posynomial
Expand Down Expand Up @@ -164,7 +165,9 @@ def localsolve(self, solver=None, *, verbosity=1, x0=None, reltol=1e-4,
if verbosity > 2:
result = gp.generate_result(solver_out, verbosity=verbosity-3)
self._results.append(result)
print(result.table(self.sgpvks))
vartable = result.table(self.sgpvks, tables=["freevariables"])
vartable = "\n" + vartable.replace("Free", "SGP", 1)
print(vartable)
elif verbosity > 1:
print("Solved cost was %.4g." % cost)
if prevcost is None:
Expand Down Expand Up @@ -204,6 +207,7 @@ def localsolve(self, solver=None, *, verbosity=1, x0=None, reltol=1e-4,
" `use_pccp=False` and start it from this model's"
" solution: e.g. `m.localsolve(use_pccp=False, x0="
"m.solution[\"variables\"])`." % self.pccp_penalty)
self.result["cost function"] = self.cost
del self.result["freevariables"][self.slack.key] # pylint: disable=no-member
del self.result["variables"][self.slack.key] # pylint: disable=no-member
del self.result["sensitivities"]["variables"][self.slack.key] # pylint: disable=no-member
Expand Down
3 changes: 2 additions & 1 deletion gpkit/solution_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,8 @@ def almost_equal(self, other, reltol=1e-3):
if svks != ovks:
return False
for key in svks:
if np.max(abs(cast(np.divide, svars[key], ovars[key]) - 1)) >= reltol:
reldiff = np.max(abs(cast(np.divide, svars[key], ovars[key]) - 1))
if reldiff >= reltol:
return False
return True

Expand Down

0 comments on commit de8b25c

Please sign in to comment.