def test_unitstr(self): x = gpkit.Variable("x", "ft") # pint issue 356 self.assertEqual(unitstr(gpkit.Variable("n", "count")), "count") self.assertEqual(unitstr(x), "ft") self.assertEqual(unitstr(x.key), "ft") self.assertEqual(unitstr(gpkit.Variable("y"), dimless="---"), "---") self.assertEqual(unitstr(None, dimless="--"), "")
def test_unitstr(self): x = gpkit.Variable("x", "ft") # pint issue 356 footstrings = ("ft", "foot") # backwards compatibility with pint 0.6 self.assertEqual(unitstr(gpkit.Variable("n", "count")), "count") self.assertIn(unitstr(x), footstrings) self.assertIn(unitstr(x.key), footstrings) self.assertEqual(unitstr(gpkit.Variable("y"), dimless="---"), "---") self.assertEqual(unitstr(None, dimless="--"), "")
def test_persistence(self): x = gpkit.Variable("x") y = gpkit.Variable("y") ymax = gpkit.Variable("y_{max}", 0.1) with gpkit.SignomialsEnabled(): m = gpkit.Model(x, [x >= 1 - y, y <= ymax]) m.substitutions[ymax] = 0.2 self.assertAlmostEqual(m.localsolve(verbosity=0)["cost"], 0.8, 3) m = gpkit.Model(x, [x >= 1 - y, y <= ymax]) self.assertAlmostEqual(m.localsolve(verbosity=0)["cost"], 0.9, 3)
def __init_vars__(self): self.p = [] for k in range(self.K): self.p.append(gp.Variable("p_" + str(k))) self.A = [] self.A_ = [] for k in range(self.K): Ak = [] Ak_ = [] for i in range(self.N): Ak.append(gp.Variable("a_" + str(i + 1) + "," + str(k))) Ak_.append(gp.Variable("a'_" + str(i + 1) + "," + str(k))) self.A.append(Ak) self.A_.append(Ak_)
def test_shapes(self): with gpkit.Vectorize(3): with gpkit.Vectorize(5): y = gpkit.Variable("y") x = gpkit.VectorVariable(2, "x") z = gpkit.VectorVariable(7, "z") self.assertEqual(y.shape, (5, 3)) self.assertEqual(x.shape, (2, 5, 3)) self.assertEqual(z.shape, (7, 3))
def solve_gp_by_core_task(core_index, se_task_index, rt_alloc, se_alloc, n_rt_task, rt_period_list, rt_wcet_list, se_period_list, se_des_period_list, se_max_period_list, se_wcet_list, verbose=True): """ Solve the GP for a given core for given task """ # solve the GP try: verbosity = 0 # no verbosity ti_des = se_des_period_list[se_task_index] ti_max = se_max_period_list[se_task_index] # Decision variables ti = gpkit.Variable("ti") rt_intf = 0 se_intf = 0 for i in range(0, n_rt_task): rt_intf += ((ti + rt_period_list[i]) * (1 / rt_period_list[i]) * rt_wcet_list[i]) * rt_alloc[i][core_index] for i in range(0, se_task_index): se_intf += ((ti + se_period_list[i]) * (1 / se_period_list[i]) * se_wcet_list[i]) * se_alloc[i][core_index] wcrt = se_wcet_list[se_task_index] + rt_intf + se_intf objective = (1/ti_des) * ti constraints = [] constraints.append(ti_des * (1/ti) <= 1) # period bound constraints.append((1/ti_max) * ti <= 1) # period bound constraints.append(wcrt * (1/ti) <= 1) # schedulability constraint m = gpkit.Model(objective, constraints) try: with HF.timeout_handler(PARAMS.GP_TIMEOUT): sol = m.solve(verbosity=verbosity) except (RuntimeWarning, HF.TimeoutException): # if there the solution is not optimal return None if verbose: print "PROP_SHCME: GP Solution is not optimal or timeout!" return None, None except Exception: if verbose: print "PROP_SHCME: Unable to find any solution!" return None, None eta = 1 / float(sol["cost"]) # make it inverse (since we maximize) # print "Ti_des:", ti_des # print "Optimal cost:", eta # print("Optimal Period: %s" % sol(ti)) # return the optimal eta and corresponding period return eta, sol(ti)
def __init_vars__(self): self.p = gp.Variable("p") self.p_ = gp.Variable("p_") self.A = [] self.A_ = [] self.B = [] self.B_ = [] for i in range(self.N): self.A.append(gp.Variable("a_" + str(i + 1))) self.A_.append(gp.Variable("a'_" + str(i + 1))) self.B.append(gp.Variable("b_" + str(i + 1))) self.B_.append(gp.Variable("b'_" + str(i + 1)))
"""Adapted from t_SP in tests/t_geometric_program.py""" import gpkit # Decision variables x = gpkit.Variable('x') y = gpkit.Variable('y') # must enable signomials for subtraction with gpkit.SignomialsEnabled(): constraints = [x >= 1 - y, y <= 0.1] # create and solve the SP m = gpkit.Model(x, constraints) print m.localsolve(verbosity=0).summary() assert abs(m.solution(x) - 0.9) < 1e-6
"""Truss generalized geometric program (GGP) example from section 6.3 of Boyd's tutorial. GPkit does not support GGPs, so I use helper variables (`t_1`, `t_2`) to convert the problem to a geometric program. """ import numpy as np import gpkit as gp A = gp.Variable('A', 'meter**2', 'Bar cross section area') r = gp.Variable('r', 'meter', 'Bar inner radius') r_min = gp.Variable('r_min', 0.01, 'meter', 'Bar inner radius minimum') R_max = gp.Variable('R_max', 0.2, 'meter', 'Bar outer radius maximum') sigma = gp.Variable('\\sigma', 100, 'megapascal', 'Bar stress limit') # The outer radius - this is an evaluated free variable - A is used instead as # a decision variable in the optimization R = gp.Variable('R', 'meter', evalfn=lambda v: (v[A] / (2 * np.pi) + v[r]**2)**0.5) w = gp.Variable('w', 'meter', 'Truss width') w_min = gp.Variable('w_min', 0.5, 'meter', 'Truss width lower bound') w_max = gp.Variable('w_max', 2, 'meter', 'Truss width upper bound') h = gp.Variable('h', 'meter', 'Truss height') h_min = gp.Variable('h_min', 0.5, 'meter', 'Truss height lower bound') h_max = gp.Variable('h_max', 2, 'meter', 'Truss height upper bound') F_1 = gp.Variable('F_1', 100, 'kilonewton', 'Vertical load') F_2 = gp.Variable('F_2', 100, 'kilonewton', 'Horizontal load')