2022-03-30

norm of differences of variables in cvxpy

How can I take the following norm in cvxpy

enter image description here

sum(norm(x[i] - x[j], p=2), i=1, j>i, i, j = n)

where x is (n, 2) variable matrix defined by x = cp.Variable((n, 2)). For the problem, I am taking n=16. The following is the code I am trying to completely

import numpy as np
import cvxpy as cp


def solver(pts, var, lmb):
    objective = cp.Minimize(cp.norm(pts - var, 2) + cp.norm(var, 2))
    prob = cp.Problem(objective)
    prob.solve()
    print(prob.value)
    return np.round(var.value, 2)

pts = np.array([(-2, 3), (-3, 5), (-1, 4), (-3, 7) , (0, 3), 
                (-2, -2), (-3, 8), (3, 1), (1, -2), (2, 6), 
                (-2, 6), (-2, 5), (-4, 3), (-4, 6), (-3, 10), 
                (2, -3)])

n = pts.shape[0]
var = cp.Variable((n, 2))

solver(pts, var, 1)


No comments:

Post a Comment