def build_path_ggp(self, prim_in=0, prim_out=0, mode=DELAY_MIN, fixed_l=[], fixed_s=[]): s = gp.VectorVariable(self.n, "s") l = gp.VectorVariable(self.n - 1, "l") constraints = [] if not fixed_s: self.__set_s_bounds(s, constraints) else: self.__fix_s(fixed_s, s, constraints) if not fixed_l: self.__set_l_bounds(l, constraints) self.__set_l_sum(l, constraints) else: self.__fix_l(fixed_l, l, constraints) self.__set_primary_in_out(s, prim_in, prim_out, constraints) delay = self.__get_delay_function(s, l) power = self.__get_power(s) if mode == DELAY_MIN: return gp.Model(delay, constraints), s, l elif mode == POWER_MIN: constraints.append(delay <= self.max_delay) return gp.Model(power, constraints), s, l else: return gp.Model(power + delay, constraints), s, l
def __set_s_bounds(self, s, constraints): s_min = gp.VectorVariable(self.n, "smin", [self.smin for dummy in xrange(self.n)]) s_max = gp.VectorVariable(self.n, "smax", [self.smax for dummy in xrange(self.n)]) for i in range(self.n): constraints.append(s[i] <= s_max[i]) constraints.append(s[i] >= s_min[i])
def __set_l_bounds(self, l, constraints): l_min = gp.VectorVariable(self.n - 1, "lmin", [self.lmin for dummy in xrange(self.n - 1)]) l_max = gp.VectorVariable( self.n - 1, "lmax", [self.length for dummy in xrange(self.n - 1)]) for i in range(self.n - 1): constraints.append(l[i] <= l_max[i]) constraints.append(l[i] >= l_min[i])
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 build_path_ggp(self, prim_in=0, prim_out=0): s = gp.VectorVariable(self.n, "s") l = gp.VectorVariable(self.n - 1, "l") delay = self.__get_delay_function(s, l) constraints = [] self.__set_bounds(s, l, constraints) self.__set_l_sum(l, constraints) self.__set_primary_in_out(s, prim_in, prim_out, constraints) return gp.Model(delay, constraints)
def test_quantity_sub(self): if gpkit.units: x = Variable("x", 1, "cm") y = Variable("y", 1) self.assertEqual(x.sub({x: 1 * gpkit.units.m}).c.magnitude, 100) # NOTE: uncomment the below if requiring Quantity substitutions # self.assertRaises(ValueError, x.sub, x, 1) self.assertRaises(ValueError, x.sub, {x: 1 * gpkit.units.N}) self.assertRaises(ValueError, y.sub, {y: 1 * gpkit.units.N}) v = gpkit.VectorVariable(3, "v", "cm") subbed = v.sub({v: [1, 2, 3] * gpkit.units.m}) self.assertEqual([z.c.magnitude for z in subbed], [100, 200, 300])
def test_quantity_sub(self): if gpkit.units: x = Variable("x", 1, "cm") y = Variable("y", 1) self.assertEqual(x.sub({x: 1 * gpkit.units.m}).c.magnitude, 100) # NOTE: uncomment the below if requiring Quantity substitutions # self.assertRaises(ValueError, x.sub, x, 1) self.assertRaises(ValueError, x.sub, {x: 1 * gpkit.ureg.N}) self.assertRaises(ValueError, y.sub, {y: 1 * gpkit.ureg.N}) v = gpkit.VectorVariable(3, "v", "cm") subbed = v.sub({v: [1, 2, 3] * gpkit.ureg.m}) self.assertEqual([z.c.magnitude for z in subbed], [100, 200, 300]) v = VectorVariable(1, "v", "km") v_min = VectorVariable(1, "v_min", "km") m = Model(v.prod(), [v >= v_min], {v_min: [2 * gpkit.units("nmi")]}) cost = m.solve(verbosity=0)["cost"] self.assertAlmostEqual(cost / (3.704 * gpkit.ureg("km")), 1.0) m = Model(v.prod(), [v >= v_min], {v_min: np.array([2]) * gpkit.units("nmi")}) cost = m.solve(verbosity=0)["cost"] self.assertAlmostEqual(cost / (3.704 * gpkit.ureg("km")), 1.0)
"""Radio power control example from section 4.1 of Boyd's tutorial.""" import numpy as np import gpkit as gp # Seed random number generator for consistency np.random.seed(3254) # Number of transmitter/receiver pairs n = 4 P = gp.VectorVariable(n, 'P', 'watt', 'Transmitter powers') # sigma = gp.VectorVariable(n, '\\sigma', 0.1 * np.ones(n), 'watt', 'Noise power at receivers') sigma = gp.VectorVariable(n, '\\sigma', 0.1 * np.random.rand(n), 'watt', 'Noise power at receivers') # Path gain matrix # G = 0.001 * np.ones((n, n)) G = 0.001 * np.random.rand(n, n) for i in range(n): G[i, i] = 0.1 # Signal to interference and noise ratio minimums S_min = gp.VectorVariable(n, 'S_min', n * [10], 'dimensionless', 'Minimum SINR') # Signal to interference and noise ratio inverse for each receiver S_inv = (sigma + np.dot(G - np.diag(np.diag(G)), P)) / (np.diagonal(G) * P) # Formulate the Model objective = np.sum(P)
def solve_gp_by_core_for_esearch(rt_alloc, se_alloc, n_rt_task, n_se_task, rt_period_list, rt_wcet_list, se_period_list, se_des_period_list, se_max_period_list, se_wcet_list, verbose=True): """ This routine will solve the GP for a given secruity task assignment for given core_index Return: Objecttive eta and the Period Vector """ # solve the GP try: verbosity = 0 # no verbosity # Decision variables ti = gpkit.VectorVariable(n_se_task, "ti") omegai = range(1, n_se_task+1) # a list of weights omegai = omegai[::-1] # reverse (shorter index higher weight) # omegai = [i / sum(omegai) for i in omegai] # normalize the sum of weignt to 1 omegai = [i*0 + 1 for i in omegai] # normalize the sum of weignt to 1 constraints = [] objective = 0 for stindx in range(0, n_se_task): ti_des = se_des_period_list[stindx] ti_max = se_max_period_list[stindx] core_index = list(se_alloc[stindx, :]).index(1) # find the where this task is allocated # calculate WCRT rt_intf = 0 for i in range(0, n_rt_task): rt_intf += ((ti[stindx] + rt_period_list[i]) * (1 / rt_period_list[i]) * rt_wcet_list[i]) * rt_alloc[i][core_index] se_intf = 0 for i in range(0, stindx): se_intf += ((ti[stindx] + se_period_list[i]) * (1 / se_period_list[i]) * se_wcet_list[i]) * se_alloc[i][core_index] wcrt = se_wcet_list[stindx] + rt_intf + se_intf constraints.append(ti_des * (1 / ti[stindx]) <= 1) # period bound constraints.append((1 / ti_max) * ti[stindx] <= 1) # period bound constraints.append(wcrt * (1 / ti[stindx]) <= 1) # schedulability constraint objective += (1/(omegai[stindx] * ti_des)) * ti[stindx] 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 "EXHAUSTIVE_SEARCH: GP Solution is not optimal or timeout!" return None, None, None except Exception: if verbose: print "EXHAUSTIVE_SEARCH: Unable to find any Solution!" return None, None, None # eta = 1 / float(sol["cost"]) # make it inverse (since we maximize) pstar = sol(ti) # print "Ti_des:", se_des_period_list # print "Optimal cost:", eta # print("Optimal Period: %s" % sol(ti)) eta_list = calculate_eta_per_task(pstar, se_des_period_list) xi = calculate_xi(pstar, se_des_period_list, se_max_period_list) # print "eta list:", eta_list return eta_list, pstar, xi
def solve_gp_all_task_single_core(n_se_task, se_period_list, se_des_period_list, se_max_period_list, se_wcet_list): """ This routine will solve the GP for all security tasks running a given single core index Return: Objecttive eta and the Period Vector """ # solve the GP try: verbosity = 0 # no verbosity # Decision variables ti = gpkit.VectorVariable(n_se_task, "ti") omegai = range(1, n_se_task+1) # a list of weights omegai = omegai[::-1] # reverse (shorter index higher weight) omegai = [i / sum(omegai) for i in omegai] # normalize the sum of weignt to 1 constraints = [] objective = 0 for stindx in range(0, n_se_task): ti_des = se_des_period_list[stindx] ti_max = se_max_period_list[stindx] # calculate WCRT rt_intf = 0 # there is no interference from RT task # for i in range(0, n_rt_task): # rt_intf += ((ti[stindx] + rt_period_list[i]) * (1 / rt_period_list[i]) * rt_wcet_list[i]) * rt_alloc[i][core_index] se_intf = 0 for i in range(0, stindx): se_intf += ((ti[stindx] + se_period_list[i]) * (1 / se_period_list[i]) * se_wcet_list[i]) wcrt = se_wcet_list[stindx] + rt_intf + se_intf constraints.append(ti_des * (1 / ti[stindx]) <= 1) # period bound constraints.append((1 / ti_max) * ti[stindx] <= 1) # period bound constraints.append(wcrt * (1 / ti[stindx]) <= 1) # schedulability constraint objective += (1/(omegai[stindx] * ti_des)) * ti[stindx] 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 print "REF_SCHEME: GP Solution is not optimal or timeout!" return None, None except Exception: print "REF_SCHEME: Unable to find any Solution!" return None, None #eta = 1 / float(sol["cost"]) # make it inverse (since we maximize) pstar = sol(ti) # print "Ti_des:", se_des_period_list # print "Optimal cost:", eta # print("Optimal Period: %s" % sol(ti)) eta_list = calculate_eta_per_task(pstar, se_des_period_list) # print "eta list:", eta_list return eta_list, pstar