def lsq_pos_l1_penalty(matrix, rhs, cost_multiplier, weights_0): """ min 2-norm (matrix*w - rhs)** + 1-norm(cost_multiplier*(w-w0)) s.t. e'w = 1 w >= 0 """ # define model with Model('lsqSparse') as model: # introduce n non-negative weight variables weights = model.variable("weights", matrix.shape[1], Domain.inRange(0.0, +np.infty)) # e'*w = 1 model.constraint(Expr.sum(weights), Domain.equalsTo(1.0)) # sum of squared residuals v = __l2_norm_squared(model, "2-norm(res)**", __residual(matrix, rhs, weights)) # \Gamma*(w - w0), p is an expression p = Expr.mulElm(cost_multiplier, Expr.sub(weights, weights_0)) cost = model.variable("cost", matrix.shape[1], Domain.unbounded()) model.constraint(Expr.sub(cost, p), Domain.equalsTo(0.0)) t = __l1_norm(model, 'abs(weights)', cost) # Minimise v + t model.objective(ObjectiveSense.Minimize, __sum_weighted(1.0, v, 1.0, t)) # solve the problem model.solve() return np.array(weights.level())
def __linfty_norm(model, name, expr): t = model.variable(name, 1, Domain.unbounded()) # (t, w_i) \in Q2 for i in range(0, expr.size()): __quad_cone(model, t, expr.index(i)) return t
def __absolute(model, name, expr): t = model.variable(name, expr.size(), Domain.unbounded()) # (t_i, w_i) \in Q2 for i in range(0, int(expr.size())): __quad_cone(model, t.index(i), expr.index(i)) return t
def __l2_norm_squared(model, name, expr): """ Given an expression (e.g. a vector) this returns the squared L2-norm of this vector as an expression. It also introduces an auxiliary variables. Mosek requires a name for any variable that is added to a model. The user has to specify this name explicitly. This requirement may disappear in future version of this API. """ t = model.variable(name, 1, Domain.unbounded()) __rotated_quad_cone(model, 0.5, t, expr) return t
def __Declare_SpeedUp_Vars(self, COModel): n, N = self.n, 2 * self.m + 2 * self.n + len(self.roads) if self.co_params['speedup']['Tau'] is True: Tau = COModel.variable('Tau', 1, Domain.greaterThan(0.0)) # scalar else: Tau = COModel.variable('Tau', 1, Domain.unbounded()) # scalar if self.co_params['speedup']['Eta'] is True: Eta = COModel.variable('Eta', [n, n], Domain.inPSDCone(n)) # n by n matrix else: Eta = COModel.variable('Eta', [n, n], Domain.unbounded()) # n by n matrix if self.co_params['speedup']['W'] is True: W = COModel.variable('W', [N, N], Domain.inPSDCone(N)) # N by N matrix else: W = COModel.variable('W', [N, N], Domain.unbounded()) # N by N matrix return Tau, Eta, W
def Build_Co_Model(self): r = len(self.roads) mu, sigma = self.mu, self.sigma m, n, r = self.m, self.n, len(self.roads) f, h = self.f, self.h M, N = m + n + r, 2 * m + 2 * n + r A = self.__Construct_A_Matrix() A_Mat = Matrix.dense(A) b = self.__Construct_b_vector() # ---- build Mosek Model COModel = Model() # -- Decision Variable Z = COModel.variable('Z', m, Domain.inRange(0.0, 1.0)) I = COModel.variable('I', m, Domain.greaterThan(0.0)) Alpha = COModel.variable('Alpha', M, Domain.unbounded()) # M by 1 vector Beta = COModel.variable('Beta', M, Domain.unbounded()) # M by 1 vector Theta = COModel.variable('Theta', N, Domain.unbounded()) # N by 1 vector # M1_matrix related decision variables ''' [tau, xi^T, phi^T M1 = xi, eta, psi^t phi, psi, w ] ''' # no-need speedup variables Psi = COModel.variable('Psi', [N, n], Domain.unbounded()) Xi = COModel.variable('Xi', n, Domain.unbounded()) # n by 1 vector Phi = COModel.variable('Phi', N, Domain.unbounded()) # N by 1 vector # has the potential to speedup Tau, Eta, W = self.__Declare_SpeedUp_Vars(COModel) # M2 matrix decision variables ''' [a, b^T, c^T M2 = b, e, d^t c, d, f ] ''' a_M2 = COModel.variable('a_M2', 1, Domain.greaterThan(0.0)) b_M2 = COModel.variable('b_M2', n, Domain.greaterThan(0.0)) c_M2 = COModel.variable('c_M2', N, Domain.greaterThan(0.0)) e_M2 = COModel.variable('e_M2', [n, n], Domain.greaterThan(0.0)) d_M2 = COModel.variable('d_M2', [N, n], Domain.greaterThan(0.0)) f_M2 = COModel.variable('f_M2', [N, N], Domain.greaterThan(0.0)) # -- Objective Function obj_1 = Expr.dot(f, Z) obj_2 = Expr.dot(h, I) obj_3 = Expr.dot(b, Alpha) obj_4 = Expr.dot(b, Beta) obj_5 = Expr.dot([1], Expr.add(Tau, a_M2)) obj_6 = Expr.dot([2 * mean for mean in mu], Expr.add(Xi, b_M2)) obj_7 = Expr.dot(sigma, Expr.add(Eta, e_M2)) COModel.objective( ObjectiveSense.Minimize, Expr.add([obj_1, obj_2, obj_3, obj_4, obj_5, obj_6, obj_7])) # Constraint 1 _expr = Expr.sub(Expr.mul(A_Mat.transpose(), Alpha), Theta) _expr = Expr.sub(_expr, Expr.mul(2, Expr.add(Phi, c_M2))) _expr_rhs = Expr.vstack(Expr.constTerm([0.0] * n), Expr.mul(-1, I), Expr.constTerm([0.0] * M)) COModel.constraint('constr1', Expr.sub(_expr, _expr_rhs), Domain.equalsTo(0.0)) del _expr, _expr_rhs # Constraint 2 _first_term = Expr.add([ Expr.mul(Beta.index(row), np.outer(A[row], A[row]).tolist()) for row in range(M) ]) _second_term = Expr.add([ Expr.mul(Theta.index(k), Matrix.sparse(N, N, [k], [k], [1])) for k in range(N) ]) _third_term = Expr.add(W, f_M2) _expr = Expr.sub(Expr.add(_first_term, _second_term), _third_term) COModel.constraint('constr2', _expr, Domain.equalsTo(0.0)) del _expr, _first_term, _second_term, _third_term # Constraint 3 _expr = Expr.mul(-2, Expr.add(Psi, d_M2)) _expr_rhs = Matrix.sparse([[Matrix.eye(n)], [Matrix.sparse(N - n, n)]]) COModel.constraint('constr3', Expr.sub(_expr, _expr_rhs), Domain.equalsTo(0)) del _expr, _expr_rhs # Constraint 4: I <= M*Z COModel.constraint('constr4', Expr.sub(Expr.mul(20000.0, Z), I), Domain.greaterThan(0.0)) # Constraint 5: M1 is SDP COModel.constraint( 'constr5', Expr.vstack(Expr.hstack(Tau, Xi.transpose(), Phi.transpose()), Expr.hstack(Xi, Eta, Psi.transpose()), Expr.hstack(Phi, Psi, W)), Domain.inPSDCone(1 + n + N)) return COModel