sol1 = bst1.sample_at(l_vals) print("values of l: %s" % l_vals) print("values of A: [%s] %s" % (" ".join("% .1f" % n for n in sol1("A").magnitude), sol1("A").units)) cost_estimate = sol1["cost"] cost_lb, cost_ub = sol1.cost_lb(), sol1.cost_ub() print("cost lower bound:\n%s\n" % cost_lb) print("cost estimate:\n%s\n" % cost_estimate) print("cost upper bound:\n%s\n" % cost_ub) # you can evaluate arbitrary posynomials np.testing.assert_allclose(mag(2*sol1(A)), mag(sol1(2*A))) assert (sol1["cost"] == sol1(A**2)).all() # the cost estimate is the logspace mean of its upper and lower bounds np.testing.assert_allclose((np.log(mag(cost_lb)) + np.log(mag(cost_ub)))/2, np.log(mag(cost_estimate))) # save autosweep to a file and retrieve it bst1.save("autosweep.pkl") bst1_loaded = pickle.load(open("autosweep.pkl", "rb")) # this problem is two intersecting lines in logspace m2 = Model(A**2, [A >= (l/3)**2, A >= (l/3)**0.5 * units.m**1.5]) tol2 = {"mosek": 1e-12, "cvxopt": 1e-7, "mosek_cli": 1e-6, 'mosek_conif': 1e-6}[gpkit.settings["default_solver"]] # test Model method sol2 = m2.autosweep({l: [1, 10]}, tol2, verbosity=0) bst2 = sol2.bst print("Solved after %2i passes, cost logtol +/-%.3g" % (bst2.nsols, bst2.tol)) print("Table of solutions used in the autosweep:") print(bst2.solarray.table())
"Demonstrates manual and auto sweeping and plotting" import matplotlib as mpl mpl.use('Agg') # comment out the imports above and `show()` to show figures in a window import numpy as np from gpkit import Model, Variable, units x = Variable("x", "m", "Swept Variable") y = Variable("y", "m^2", "Cost") m = Model(y, [y >= (x/2)**-0.5 * units.m**2.5 + 1*units.m**2, y >= (x/2)**2]) # arguments are: model, swept: values, posnomial for y-axis sol = m.sweep({x: np.linspace(1, 3, 20)}, verbosity=0) f, ax = sol.plot(y) ax.set_title("Manually swept (20 points)") # f.show() f.savefig("plot_sweep1d.png") # arguments are: model, swept: (min, max, optional logtol), posnomial for y-axis sol = m.autosweep({x: (1, 3)}, tol=0.001, verbosity=0) f, ax = sol.plot(y) ax.set_title("Autoswept (7 points)\nGuaranteed to be in blue region") # f.show() f.savefig("plot_autosweep1d.png")
"Demonstrates manual and auto sweeping and plotting" import matplotlib as mpl mpl.use('Agg') # comment out the lines above to show figures in a window import numpy as np from gpkit import Model, Variable, units x = Variable("x", "m", "Swept Variable") y = Variable("y", "m^2", "Cost") m = Model( y, [y >= (x / 2)**-0.5 * units.m**2.5 + 1 * units.m**2, y >= (x / 2)**2]) # arguments are: model, swept: values, posnomial for y-axis sol = m.sweep({x: np.linspace(1, 3, 20)}, verbosity=0) f, ax = sol.plot(y) ax.set_title("Manually swept (20 points)") f.show() f.savefig("plot_sweep1d.png") # arguments are: model, swept: (min, max, optional logtol), posnomial for y-axis sol = m.autosweep({x: (1, 3)}, tol=0.001, verbosity=0) f, ax = sol.plot(y) ax.set_title("Autoswept (7 points)\nGuaranteed to be in blue region") f.show() f.savefig("plot_autosweep1d.png")