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"Show autosweep_1d functionality"
2import pickle
3import numpy as np
4import gpkit
5from gpkit import units, Variable, Model
6from gpkit.tools.autosweep import autosweep_1d
7from gpkit.small_scripts import mag
9A = Variable("A", "m**2")
10l = Variable("l", "m")
12m1 = Model(A**2, [A >= l**2 + units.m**2])
13tol1 = 1e-3
14bst1 = autosweep_1d(m1, tol1, l, [1, 10], verbosity=0)
15print("Solved after %2i passes, cost logtol +/-%.3g" % (bst1.nsols, bst1.tol))
16# autosweep solution accessing
17l_vals = np.linspace(1, 10, 10)
18sol1 = bst1.sample_at(l_vals)
19print("values of l: %s" % l_vals)
20print("values of A: [%s] %s" %
21 (" ".join("% .1f" % n for n in sol1("A").magnitude), sol1("A").units))
22cost_estimate = sol1["cost"]
23cost_lb, cost_ub = sol1.cost_lb(), sol1.cost_ub()
24print("cost lower bound:\n%s\n" % cost_lb)
25print("cost estimate:\n%s\n" % cost_estimate)
26print("cost upper bound:\n%s\n" % cost_ub)
27# you can evaluate arbitrary posynomials
28np.testing.assert_allclose(mag(2*sol1(A)), mag(sol1(2*A)))
29assert (sol1["cost"] == sol1(A**2)).all()
30# the cost estimate is the logspace mean of its upper and lower bounds
31np.testing.assert_allclose((np.log(mag(cost_lb)) + np.log(mag(cost_ub)))/2,
32 np.log(mag(cost_estimate)))
33# save autosweep to a file and retrieve it
34bst1.save("autosweep.pkl")
35bst1_loaded = pickle.load(open("autosweep.pkl", "rb"))
37# this problem is two intersecting lines in logspace
38m2 = Model(A**2, [A >= (l/3)**2, A >= (l/3)**0.5 * units.m**1.5])
39tol2 = {"mosek_cli": 1e-6, "mosek_conif": 1e-6,
40 "cvxopt": 1e-7}[gpkit.settings["default_solver"]]
41# test Model method
42sol2 = m2.autosweep({l: [1, 10]}, tol2, verbosity=0)
43bst2 = sol2.bst
44print("Solved after %2i passes, cost logtol +/-%.3g" % (bst2.nsols, bst2.tol))
45print("Table of solutions used in the autosweep:")
46print(bst2.solarray.table())