Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""Implement the GeometricProgram class"""
2import sys
3import warnings as pywarnings
4from time import time
5from collections import defaultdict
6import numpy as np
7from ..repr_conventions import lineagestr
8from ..small_classes import CootMatrix, SolverLog, Numbers, FixedScalar
9from ..small_scripts import appendsolwarning, initsolwarning
10from ..keydict import KeyDict
11from ..solution_array import SolutionArray
12from .set import ConstraintSet
13from ..exceptions import (InvalidPosynomial, Infeasible, UnknownInfeasible,
14 PrimalInfeasible, DualInfeasible, UnboundedGP,
15 InvalidLicense)
18DEFAULT_SOLVER_KWARGS = {"cvxopt": {"kktsolver": "ldl"}}
19SOLUTION_TOL = {"cvxopt": 1e-3, "mosek_cli": 1e-4, "mosek_conif": 1e-3}
22class MonoEqualityIndexes:
23 "Class to hold MonoEqualityIndexes"
25 def __init__(self):
26 self.all = set()
27 self.first_half = set()
30def _get_solver(solver, kwargs):
31 """Get the solverfn and solvername associated with solver"""
32 if solver is None:
33 from .. import settings
34 try:
35 solver = settings["default_solver"]
36 except KeyError:
37 raise ValueError("No default solver was set during build, so"
38 " solvers must be manually specified.")
39 if solver == "cvxopt":
40 from ..solvers.cvxopt import optimize
41 elif solver == "mosek_cli":
42 from ..solvers.mosek_cli import optimize_generator
43 optimize = optimize_generator(**kwargs)
44 elif solver == "mosek_conif":
45 from ..solvers.mosek_conif import optimize
46 elif hasattr(solver, "__call__"):
47 solver, optimize = solver.__name__, solver
48 else:
49 raise ValueError("Unknown solver '%s'." % solver)
50 return solver, optimize
53class GeometricProgram:
54 # pylint: disable=too-many-instance-attributes
55 """Standard mathematical representation of a GP.
57 Attributes with side effects
58 ----------------------------
59 `solver_out` and `solve_log` are set during a solve
60 `result` is set at the end of a solve if solution status is optimal
62 Examples
63 --------
64 >>> gp = gpkit.constraints.gp.GeometricProgram(
65 # minimize
66 x,
67 [ # subject to
68 x >= 1,
69 ], {})
70 >>> gp.solve()
71 """
72 _result = solve_log = solver_out = model = v_ss = nu_by_posy = None
73 choicevaridxs = integersolve = None
75 def __init__(self, cost, constraints, substitutions,
76 *, checkbounds=True, **_):
77 self.cost, self.substitutions = cost, substitutions
78 for key, sub in self.substitutions.items():
79 if isinstance(sub, FixedScalar):
80 sub = sub.value
81 if hasattr(sub, "units"):
82 sub = sub.to(key.units or "dimensionless").magnitude
83 self.substitutions[key] = sub
84 if not isinstance(sub, (Numbers, np.ndarray)):
85 raise TypeError("substitution {%s: %s} has invalid value type"
86 " %s." % (key, sub, type(sub)))
87 cost_hmap = cost.hmap.sub(self.substitutions, cost.vks)
88 if any(c <= 0 for c in cost_hmap.values()):
89 raise InvalidPosynomial("a GP's cost must be Posynomial")
90 hmapgen = ConstraintSet.as_hmapslt1(constraints, self.substitutions)
91 self.hmaps = [cost_hmap]
92 hmapset = set()
93 for i, hmap in enumerate(hmapgen):
94 if hmap not in hmapset: # de-duplicate hmaps
95 hmapset.add(hmap)
96 self.hmaps.append(hmap)
97 self.gen() # Generate various maps into the posy- and monomials
98 if checkbounds:
99 self.check_bounds(err_on_missing_bounds=True)
101 def check_bounds(self, *, err_on_missing_bounds=False):
102 "Checks if any variables are unbounded, through equality constraints."
103 missingbounds = {}
104 for var, locs in self.varlocs.items():
105 upperbound, lowerbound = False, False
106 for i in locs:
107 if i not in self.meq_idxs.all:
108 if self.exps[i][var] > 0: # pylint:disable=simplifiable-if-statement
109 upperbound = True
110 else:
111 lowerbound = True
112 if upperbound and lowerbound:
113 break
114 if not upperbound:
115 missingbounds[(var, "upper")] = "."
116 if not lowerbound:
117 missingbounds[(var, "lower")] = "."
118 if not missingbounds:
119 return {} # all bounds found in inequalities
120 meq_bounds = gen_meq_bounds(missingbounds, self.exps, self.meq_idxs)
121 fulfill_meq_bounds(missingbounds, meq_bounds)
122 if missingbounds and err_on_missing_bounds:
123 raise UnboundedGP(
124 "\n\n".join("%s has no %s bound%s" % (v, b, x)
125 for (v, b), x in missingbounds.items()))
126 return missingbounds
128 def gen(self):
129 """Generates nomial and solve data (A, p_idxs) from posynomials.
131 k [posys]: number of monomials (rows of A) present in each constraint
132 m_idxs [mons]: monomial indices of each posynomial
133 p_idxs [mons]: posynomial index of each monomial
134 cs, exps [mons]: coefficient and exponents of each monomial
135 varlocs: {vk: monomial indices of each variables' location}
136 meq_idxs: {all indices of equality mons} and {the first index of each}
137 varidxs: {vk: which column corresponds to it in A}
138 A [mons, vks]: sparse array of each monomials' variables' exponents
140 """
141 self.k = [len(hmap) for hmap in self.hmaps]
142 self.m_idxs, self.p_idxs, self.cs, self.exps = [], [], [], []
143 self.varkeys = self.varlocs = defaultdict(list)
144 self.meq_idxs = MonoEqualityIndexes()
145 m_idx = 0
146 row, col, data = [], [], []
147 for p_idx, (N_mons, hmap) in enumerate(zip(self.k, self.hmaps)):
148 self.p_idxs.extend([p_idx]*N_mons)
149 self.m_idxs.append(slice(m_idx, m_idx+N_mons))
150 if getattr(self.hmaps[p_idx], "from_meq", False):
151 self.meq_idxs.all.add(m_idx)
152 if len(self.meq_idxs.all) > 2*len(self.meq_idxs.first_half):
153 self.meq_idxs.first_half.add(m_idx)
154 self.exps.extend(hmap)
155 self.cs.extend(hmap.values())
156 for exp in hmap:
157 if not exp: # space out A matrix with constants for mosek
158 row.append(m_idx)
159 col.append(0)
160 data.append(0)
161 for var in exp:
162 self.varlocs[var].append(m_idx)
163 m_idx += 1
164 self.p_idxs = np.array(self.p_idxs, "int32") # to use array equalities
165 self.varidxs = {vk: i for i, vk in enumerate(self.varlocs)}
166 self.choicevaridxs = {vk: i for i, vk in enumerate(self.varlocs)
167 if vk.choices}
168 for j, (var, locs) in enumerate(self.varlocs.items()):
169 row.extend(locs)
170 col.extend([j]*len(locs))
171 data.extend(self.exps[i][var] for i in locs)
172 self.A = CootMatrix(row, col, data)
174 # pylint: disable=too-many-statements, too-many-locals
175 def solve(self, solver=None, *, verbosity=1, gen_result=True, **kwargs):
176 """Solves a GeometricProgram and returns the solution.
178 Arguments
179 ---------
180 solver : str or function (optional)
181 By default uses a solver found during installation.
182 If "mosek_conif", "mosek_cli", or "cvxopt", uses that solver.
183 If a function, passes that function cs, A, p_idxs, and k.
184 verbosity : int (default 1)
185 If greater than 0, prints solver name and solve time.
186 gen_result : bool (default True)
187 If True, makes a human-readable SolutionArray from solver output.
188 **kwargs :
189 Passed to solver constructor and solver function.
192 Returns
193 -------
194 SolutionArray (or dict if gen_result is False)
195 """
196 solvername, solverfn = _get_solver(solver, kwargs)
197 if verbosity > 0:
198 print("Using solver '%s'" % solvername)
199 print(" for %i free variables" % len(self.varlocs))
200 print(" in %i posynomial inequalities." % len(self.k))
202 solverargs = DEFAULT_SOLVER_KWARGS.get(solvername, {})
203 solverargs.update(kwargs)
204 if self.choicevaridxs and solvername == "mosek_conif":
205 solverargs["choicevaridxs"] = self.choicevaridxs
206 self.integersolve = True
207 starttime = time()
208 solver_out, infeasibility, original_stdout = {}, None, sys.stdout
209 try:
210 sys.stdout = SolverLog(original_stdout, verbosity=verbosity-2)
211 solver_out = solverfn(c=self.cs, A=self.A, meq_idxs=self.meq_idxs,
212 k=self.k, p_idxs=self.p_idxs, **solverargs)
213 except Infeasible as e:
214 infeasibility = e
215 except InvalidLicense as e:
216 raise InvalidLicense("license for solver \"%s\" is invalid."
217 % solvername) from e
218 except Exception as e:
219 raise UnknownInfeasible("Something unexpected went wrong.") from e
220 finally:
221 self.solve_log = "\n".join(sys.stdout)
222 sys.stdout = original_stdout
223 self.solver_out = solver_out
225 solver_out["solver"] = solvername
226 solver_out["soltime"] = time() - starttime
227 if verbosity > 0:
228 print("Solving took %.3g seconds." % solver_out["soltime"])
230 if infeasibility:
231 if isinstance(infeasibility, PrimalInfeasible):
232 msg = ("The model had no feasible points; "
233 "you may wish to relax some constraints or constants.")
234 elif isinstance(infeasibility, DualInfeasible):
235 msg = ("The model ran to an infinitely low cost;"
236 " bounding the right variables would prevent this.")
237 elif isinstance(infeasibility, UnknownInfeasible):
238 msg = ("Solver failed for an unknown reason. Relaxing"
239 " constraints/constants, bounding variables, or"
240 " using a different solver might fix it.")
241 if (verbosity > 0 and solver_out["soltime"] < 1
242 and hasattr(self, "model")): # fast, top-level model
243 print(msg + "\nSince the model solved in less than a second,"
244 " let's run `.debug()` to analyze what happened.\n`")
245 return self.model.debug(solver=solver)
246 # else, raise a clarifying error
247 msg += (" Running `.debug()` or increasing verbosity may pinpoint"
248 " the trouble.")
249 raise infeasibility.__class__(msg) from infeasibility
251 if not gen_result:
252 return solver_out
253 # else, generate a human-readable SolutionArray
254 self._result = self.generate_result(solver_out, verbosity=verbosity-2)
255 return self.result
257 @property
258 def result(self):
259 "Creates and caches a result from the raw solver_out"
260 if not self._result:
261 self._result = self.generate_result(self.solver_out)
262 return self._result
264 def generate_result(self, solver_out, *, verbosity=0, dual_check=True):
265 "Generates a full SolutionArray and checks it."
266 if verbosity > 0:
267 soltime = solver_out["soltime"]
268 tic = time()
269 # result packing #
270 result = self._compile_result(solver_out) # NOTE: SIDE EFFECTS
271 if verbosity > 0:
272 print("Result packing took %.2g%% of solve time." %
273 ((time() - tic) / soltime * 100))
274 tic = time()
275 # solution checking #
276 initsolwarning(result, "Solution Inconsistency")
277 try:
278 tol = SOLUTION_TOL.get(solver_out["solver"], 1e-5)
279 self.check_solution(result["cost"], solver_out['primal'],
280 solver_out["nu"], solver_out["la"], tol)
281 except Infeasible as chkerror:
282 msg = str(chkerror)
283 if not ("Dual" in msg and not dual_check):
284 appendsolwarning(msg, None, result, "Solution Inconsistency")
285 if verbosity > -4:
286 print("Solution check warning: %s" % msg)
287 if verbosity > 0:
288 print("Solution checking took %.2g%% of solve time." %
289 ((time() - tic) / soltime * 100))
290 return result
292 def _generate_nula(self, solver_out):
293 if "nu" in solver_out:
294 # solver gave us monomial sensitivities, generate posynomial ones
295 solver_out["nu"] = nu = np.ravel(solver_out["nu"])
296 nu_by_posy = [nu[mi] for mi in self.m_idxs]
297 solver_out["la"] = la = np.array([sum(nup) for nup in nu_by_posy])
298 elif "la" in solver_out:
299 la = np.ravel(solver_out["la"])
300 if len(la) == len(self.hmaps) - 1:
301 # assume solver dropped the cost's sensitivity (always 1.0)
302 la = np.hstack(([1.0], la))
303 # solver gave us posynomial sensitivities, generate monomial ones
304 solver_out["la"] = la
305 z = np.log(self.cs) + self.A.dot(solver_out["primal"])
306 m_iss = [self.p_idxs == i for i in range(len(la))]
307 nu_by_posy = [la[p_i]*np.exp(z[m_is])/sum(np.exp(z[m_is]))
308 for p_i, m_is in enumerate(m_iss)]
309 solver_out["nu"] = np.hstack(nu_by_posy)
310 else:
311 raise RuntimeWarning("The dual solution was not returned.")
312 return la, nu_by_posy
314 def _compile_result(self, solver_out):
315 result = {"cost": float(solver_out["objective"])}
316 primal = solver_out["primal"]
317 if len(self.varlocs) != len(primal):
318 raise RuntimeWarning("The primal solution was not returned.")
319 result["freevariables"] = KeyDict(zip(self.varlocs, np.exp(primal)))
320 result["constants"] = KeyDict(self.substitutions)
321 result["variables"] = KeyDict(result["freevariables"])
322 result["variables"].update(result["constants"])
323 result["soltime"] = solver_out["soltime"]
324 if self.integersolve:
325 result["choicevariables"] = KeyDict( \
326 {k: v for k, v in result["freevariables"].items()
327 if k in self.choicevaridxs})
328 result["warnings"] = {"No Dual Solution": [(\
329 "This model has the discretized choice variables"
330 " %s and hence no dual solution. You can fix those variables"
331 " to their optimal value and get sensitivities to the resulting"
332 " continuous problem by updating your model's substitions with"
333 " `sol[\"choicevariables\"]`."
334 % sorted(self.choicevaridxs.keys()), self.choicevaridxs)]}
335 return SolutionArray(result)
336 if self.choicevaridxs:
337 result["warnings"] = {"Freed Choice Variables": [(\
338 "This model has the discretized choice variables"
339 " %s, but since the '%s' solver doesn't support discretization"
340 " they were treated as continuous variables."
341 % (sorted(self.choicevaridxs.keys()), solver_out["solver"]),
342 self.choicevaridxs)]}
344 result["sensitivities"] = {"constraints": {}}
345 la, self.nu_by_posy = self._generate_nula(solver_out)
346 cost_senss = sum(nu_i*exp for (nu_i, exp) in zip(self.nu_by_posy[0],
347 self.cost.hmap))
348 gpv_ss = cost_senss.copy()
349 m_senss = defaultdict(float)
350 for las, nus, c in zip(la[1:], self.nu_by_posy[1:], self.hmaps[1:]):
351 while getattr(c, "parent", None) is not None:
352 c = c.parent
353 v_ss, c_senss = c.sens_from_dual(las, nus, result)
354 for vk, x in v_ss.items():
355 gpv_ss[vk] = x + gpv_ss.get(vk, 0)
356 while getattr(c, "generated_by", None):
357 c = c.generated_by
358 result["sensitivities"]["constraints"][c] = abs(c_senss)
359 m_senss[lineagestr(c)] += abs(c_senss)
360 # add fixed variables sensitivities to models
361 for vk, senss in gpv_ss.items():
362 m_senss[lineagestr(vk)] += abs(senss)
363 result["sensitivities"]["models"] = dict(m_senss)
364 # carry linked sensitivities over to their constants
365 for v in list(v for v in gpv_ss if v.gradients):
366 dlogcost_dlogv = gpv_ss.pop(v)
367 val = np.array(result["constants"][v])
368 for c, dv_dc in v.gradients.items():
369 with pywarnings.catch_warnings(): # skip pesky divide-by-zeros
370 pywarnings.simplefilter("ignore")
371 dlogv_dlogc = dv_dc * result["constants"][c]/val
372 gpv_ss[c] = gpv_ss.get(c, 0) + dlogcost_dlogv*dlogv_dlogc
373 if v in cost_senss:
374 if c in self.cost.vks:
375 dlogcost_dlogv = cost_senss.pop(v)
376 before = cost_senss.get(c, 0)
377 cost_senss[c] = before + dlogcost_dlogv*dlogv_dlogc
378 result["sensitivities"]["cost"] = cost_senss
379 result["sensitivities"]["variables"] = KeyDict(gpv_ss)
380 result["sensitivities"]["constants"] = \
381 result["sensitivities"]["variables"] # NOTE: backwards compat.
382 return SolutionArray(result)
384 def check_solution(self, cost, primal, nu, la, tol, abstol=1e-20):
385 """Run checks to mathematically confirm solution solves this GP
387 Arguments
388 ---------
389 cost: float
390 cost returned by solver
391 primal: list
392 primal solution returned by solver
393 nu: numpy.ndarray
394 monomial lagrange multiplier
395 la: numpy.ndarray
396 posynomial lagrange multiplier
398 Raises
399 ------
400 Infeasible if any problems are found
401 """
402 A = self.A.tocsr()
403 def almost_equal(num1, num2):
404 "local almost equal test"
405 return (num1 == num2 or abs((num1 - num2) / (num1 + num2)) < tol
406 or abs(num1 - num2) < abstol)
407 # check primal sol #
408 primal_exp_vals = self.cs * np.exp(A.dot(primal)) # c*e^Ax
409 if not almost_equal(primal_exp_vals[self.m_idxs[0]].sum(), cost):
410 raise Infeasible("Primal solution computed cost did not match"
411 " solver-returned cost: %s vs %s." %
412 (primal_exp_vals[self.m_idxs[0]].sum(), cost))
413 for mi in self.m_idxs[1:]:
414 if primal_exp_vals[mi].sum() > 1 + tol:
415 raise Infeasible("Primal solution violates constraint: %s is "
416 "greater than 1" % primal_exp_vals[mi].sum())
417 # check dual sol #
418 if self.integersolve:
419 return
420 # note: follows dual formulation in section 3.1 of
421 # http://web.mit.edu/~whoburg/www/papers/hoburg_phd_thesis.pdf
422 if not almost_equal(self.nu_by_posy[0].sum(), 1):
423 raise Infeasible("Dual variables associated with objective sum"
424 " to %s, not 1" % self.nu_by_posy[0].sum())
425 if any(nu < 0):
426 minnu = min(nu)
427 if minnu < -tol/1000:
428 raise Infeasible("Dual solution has negative entries as"
429 " large as %s." % minnu)
430 if any(np.abs(A.T.dot(nu)) > tol):
431 raise Infeasible("Dual: sum of nu^T * A did not vanish.")
432 b = np.log(self.cs)
433 dual_cost = sum(
434 self.nu_by_posy[i].dot(
435 b[mi] - np.log(self.nu_by_posy[i]/la[i]))
436 for i, mi in enumerate(self.m_idxs) if la[i])
437 if not almost_equal(np.exp(dual_cost), cost):
438 raise Infeasible("Dual cost %s does not match primal cost %s"
439 % (np.exp(dual_cost), cost))
442def gen_meq_bounds(missingbounds, exps, meq_idxs): # pylint: disable=too-many-locals,too-many-branches
443 "Generate conditional monomial equality bounds"
444 meq_bounds = defaultdict(set)
445 for i in meq_idxs.first_half:
446 p_upper, p_lower, n_upper, n_lower = set(), set(), set(), set()
447 for key, x in exps[i].items():
448 if (key, "upper") in missingbounds:
449 if x > 0:
450 p_upper.add((key, "upper"))
451 else:
452 n_upper.add((key, "upper"))
453 if (key, "lower") in missingbounds:
454 if x > 0:
455 p_lower.add((key, "lower"))
456 else:
457 n_lower.add((key, "lower"))
458 # (consider x*y/z == 1)
459 # for a var (e.g. x) to be upper bounded by this monomial equality,
460 # - vars of the same sign (y) must be lower bounded
461 # - AND vars of the opposite sign (z) must be upper bounded
462 p_ub = n_lb = frozenset(n_upper).union(p_lower)
463 n_ub = p_lb = frozenset(p_upper).union(n_lower)
464 for keys, ub in ((p_upper, p_ub), (n_upper, n_ub)):
465 for key, _ in keys:
466 needed = ub.difference([(key, "lower")])
467 if needed:
468 meq_bounds[(key, "upper")].add(needed)
469 else:
470 del missingbounds[(key, "upper")]
471 for keys, lb in ((p_lower, p_lb), (n_lower, n_lb)):
472 for key, _ in keys:
473 needed = lb.difference([(key, "upper")])
474 if needed:
475 meq_bounds[(key, "lower")].add(needed)
476 else:
477 del missingbounds[(key, "lower")]
478 return meq_bounds
481def fulfill_meq_bounds(missingbounds, meq_bounds):
482 "Bounds variables with monomial equalities"
483 still_alive = True
484 while still_alive:
485 still_alive = False # if no changes are made, the loop exits
486 for bound in set(meq_bounds):
487 if bound not in missingbounds:
488 del meq_bounds[bound]
489 continue
490 for condition in meq_bounds[bound]:
491 if not any(bound in missingbounds for bound in condition):
492 del meq_bounds[bound]
493 del missingbounds[bound]
494 still_alive = True
495 break
496 for (var, bound) in meq_bounds:
497 boundstr = (", but would gain it from any of these sets: ")
498 for condition in list(meq_bounds[(var, bound)]):
499 meq_bounds[(var, bound)].remove(condition)
500 newcond = condition.intersection(missingbounds)
501 if newcond and not any(c.issubset(newcond)
502 for c in meq_bounds[(var, bound)]):
503 meq_bounds[(var, bound)].add(newcond)
504 boundstr += " or ".join(str(list(condition))
505 for condition in meq_bounds[(var, bound)])
506 missingbounds[(var, bound)] = boundstr