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 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 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 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 solve(self, solver = 'mosek_cli', verbose = False): self.model = gp.Model(self.obj, self.constraints) self.solution = self.model.solve(verbosity = verbose, solver=solver) sol = self.solution['freevariables'] self._P = np.array([ sol["p_" + str(k)] for k in range(self.K)], dtype="float") self._AKI = np.zeros((self.N, self.K), dtype="float") self._A_KI = np.zeros((self.N, self.K), dtype="float") for i in range(self.N): for k in range(self.K): self._AKI [i][k] = sol[ "a_" + str(i+1) + "," + str(k) ] self._A_KI[i][k] = sol[ "a'_" + str(i+1) + "," + str(k) ] self._ready_()
def solve(self, solver='cvxopt', verbose=False): self.model = gp.Model(self.obj, self.constraints) self.solution = self.model.solve(verbosity=verbose, solver=solver) sol = self.solution['freevariables'] self._p = sol["p"] self._p_ = sol["p_"] self._A = np.zeros(self.N, dtype="float") self._A_ = np.zeros(self.N, dtype="float") self._B = np.zeros(self.N, dtype="float") self._B_ = np.zeros(self.N, dtype="float") for i in range(self.N): self._A[i] = sol["a_" + str(i + 1)] self._A_[i] = sol["a'_" + str(i + 1)] self._B[i] = sol["b_" + str(i + 1)] self._B_[i] = sol["b'_" + str(i + 1)] self._ready_()
"""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
F_1 = gp.Variable('F_1', 100, 'kilonewton', 'Vertical load') F_2 = gp.Variable('F_2', 100, 'kilonewton', 'Horizontal load') # helper variables to convert the GGP to GP t_1 = gp.Variable('t_1', 'meter**2', 'Helper variable') t_2 = gp.Variable('t_2', 'meter**2', 'Helper variable') objective = 2 * A * t_1**0.5 constraints = [ F_1 * t_1**0.5 / h <= sigma * A, F_2 * t_1**0.5 / w <= sigma * A, w**2 + h**2 <= t_1, w_min <= w, w <= w_max, h_min <= h, h <= h_max, t_2**0.5 <= R_max, A / (2 * np.pi) + r**2 <= t_2, 0.21 * r**2 <= A / (2 * np.pi), r_min <= r, ] m = gp.Model(objective, constraints) m.unique_varkeys = set([R.key]) # idk what this does? sol = m.solve() print sol.table()
# 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) constraints = [S_inv <= 1 / S_min] radio_model = gp.Model(objective, constraints) # Solve sol = radio_model.solve() print sol.table()
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