4
$\begingroup$

The optimization problem is
$$\min_{x\in K} \|h - x\|_2$$ where $$K = \{v\in R^n : \exists \lambda \geq 0\ v_1=v_2=\ldots=v_k=\lambda \ \text{and} \ |v_i| \leq \lambda \ \text{for} \ i=k+1,\ldots,n \}$$ , where $h, k, n$ are all known.

Could someone tell me how to write down this constraint in cvx? I couldn't think of a convenient way of specifying the constraints which define the convex set $K$.

$\endgroup$
  • $\begingroup$ Is $k$ a known constant? Or in the definition of $K$ does it mean “there exists a $k$ for which this holds?” $\endgroup$ – cdipaolo Feb 11 at 7:58
  • $\begingroup$ @cdipaolo Oh I should have mentioned that $h$, $k$, $n$ are all known. $\endgroup$ – Bihu Duo Feb 11 at 8:00
2
$\begingroup$

Is it what you need? (λ is unknown)

# INPUT
N,K = 5,3
h=[1.1,1.2,1.7,0.5,0.3]

Xk = cp.Variable()# unknown Lambda
Xn = [cp.Variable() for i in range(N-K)]
constraints = []
for x in Xn:
    constraints.append(cp.abs(x)<=Xk)                

X = [Xk]*K+Xn
obj = cp.Minimize(cp.sum([(h[i]-X[i])**2 for i in range(N)]))        
prob = cp.Problem(obj, constraints)
rez = prob.solve()       

for i in range(K):
    print("{:.1f}".format(Xk.value))
for i in range(N-K):
    print("{:.1f}".format(Xn[i].value))

print("Objective function : {:.1f}".format(rez))

Result: 1.3 1.3 1.3 0.5 0.3 Objective function : 0.2

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ It looks like $\lambda$ is not known from the original post. $\endgroup$ – cdipaolo Feb 11 at 17:03
  • $\begingroup$ @cdipaolo in second part of the solution λ is not known. I think it make sense remove solution with known λ $\endgroup$ – Sergey Feb 11 at 17:18
  • $\begingroup$ @cdipaolo Thank you so much! The $\lambda$ isn't known so this is exactly what I hoped for. $\endgroup$ – Bihu Duo Feb 11 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.