# add second order cone type constraint

**URL:** https://discourse.julialang.org/t/add-second-order-cone-type-constraint/18742
**Category:** Optimization (Mathematical)
**Created:** [December 17, 2018, 1:49pm UTC](https://discourse.julialang.org/t/add-second-order-cone-type-constraint/18742 "2018-12-17T13:49:22Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![WangQuanmeng](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@WangQuanmeng](https://discourse.julialang.org/u/WangQuanmeng)
#### Post date: [December 17, 2018, 1:49pm UTC](https://discourse.julialang.org/t/add-second-order-cone-type-constraint/18742/1 "2018-12-17T13:49:22Z")

</div>

Hi,

I am new to Julia and JuMP, and I am learning to solve SOCP with integer constraint.

The only example I can find online shows I should add the constraint in the following way.

`@constraint(m, norm(2x[i] - i for i=1:n if c[i] == 1) <= 1)`

Is there a way to call `norm` function with out a looping of iterator with in it ? For example, in my problem, a segment of code would be

```julia
for t in 1:n 
    for i in 0:t
        for j in ( t + 1 ):( n + 1 )
            # add socp constraint here
            @constraint( model , dd[k , i , j , t , 1] == sqrt( generationCostA( k , t ) ) * x[k , i , j , t] )
            @constraint( model , dd[k , i , j , t , 2] == 0.5 * ( d[k , i , j , t] - y[k , i , j] ) )
            @constraint( model , norm( dd[k , i , j , t , kk] for kk in 1:2 ) <= 0.5 * ( y[k , i , j] + d[k , i , j , t] ) )
        end
    end
end

```

the vector within `norm` function has dimension 2. I tried to use `norm([x , y])` , but it does not work. So I have to create dummy variables `dd` , and then I can use iterator within `norm` function.

Can I do it without introduce the dummy variables? Thanks for your time of reading, and much appreciated if you can help me on this!

Qm

---

<div class="post-metadata">

### Author: ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)
#### Post date: [December 17, 2018, 3:48pm UTC](https://discourse.julialang.org/t/add-second-order-cone-type-constraint/18742/2 "2018-12-17T15:48:40Z")

</div>

> [@WangQuanmeng](#):
>
> I am new to Julia and JuMP

Welcome 🙂

> [@WangQuanmeng](#):
>
> I tried to use `norm([x , y])` , but it does not work.

What doesn’t work? Maybe this should’ve been `norm([x ; y])`? It’s hard to say more without seeing code that reproduces the issue.

---

<div class="post-metadata">

### Author: ![WangQuanmeng](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@WangQuanmeng](https://discourse.julialang.org/u/WangQuanmeng)
#### Post date: [December 18, 2018, 3:16am UTC](https://discourse.julialang.org/t/add-second-order-cone-type-constraint/18742/3 "2018-12-18T03:16:17Z")

</div>

Hi,

Thanks for your reply!

When I add constraint in the way of calling `norm( [x , y] )` like the following

```julia
@constraint( model , norm( [sqrt( generationCostA( k , t ) ) * x[ k , i , j , t] , 0.5 * ( d[k , i , j , t] - y[k , i , j] ) ] ) <= 0.5 * ( y[k , i , j] + d[k , i , j , t] ) )

```

the error says `norm` not defined

```julia
ERROR: LoadError: UndefVarError: norm not defined
Stacktrace:
 [1] macro expansion at C:\Users\tommyricardo\.julia\packages\JuMP\PbnIJ\src\macros.jl:492 [inlined]
 [2] macro expansion at C:\Users\tommyricardo\.julia\packages\JuMP\PbnIJ\src\macros.jl:493 [inlined]
 [3] top-level scope at F:\a__top_secret\multiple_units_quadratic.jl:166 [inlined]
 [4] top-level scope at .\none:0
in expression starting at F:\a__top_secret\multiple_units_quadratic.jl:161

```

same thing happens if I called `norm( [x ; y] )` `norm( x , y )` or `norm( x ; y )`

```julia
@constraint( model , norm( [sqrt( generationCostA( k , t ) ) * x[ k , i , j , t] ; 0.5 * ( d[k , i , j , t] - y[k , i , j] ) ] ) <= 0.5 * ( y[k , i , j] + d[k , i , j , t] ) )

```

```julia
ERROR: LoadError: UndefVarError: norm not defined
Stacktrace:
 [1] macro expansion at C:\Users\tommyricardo\.julia\packages\JuMP\PbnIJ\src\macros.jl:492 [inlined]
 [2] macro expansion at C:\Users\tommyricardo\.julia\packages\JuMP\PbnIJ\src\macros.jl:493 [inlined]
 [3] top-level scope at F:\a__top_secret\multiple_units_quadratic.jl:166 [inlined]
 [4] top-level scope at .\none:0
in expression starting at F:\a__top_secret\multiple_units_quadratic.jl:161

```

It only works if I defined new variables `dd` to make a copy of `x,y` and call `norm( dd[i] for i in 1:2 )`.

```julia
@constraint( model , dd[k , i , j , t , 1] == sqrt( generationCostA( k , t ) ) * x[k , i , j , t] )
@constraint( model , dd[k , i , j , t , 2] == 0.5 * ( d[k , i , j , t] - y[k , i , j] ) )
@constraint( model , norm( dd[k , i , j , t , kk] for kk in 1:2 ) <= 0.5 * ( y[k , i , j] + d[k , i , j , t] ) )

```

But in this way, I have to create a lot of dummy variables `dd`. So I am just wondering if there is a way to call `norm` for adding the constraint without doing a loop?

I did not put all the codes because it is a bit long. But I think this is the part that produce the error, if you need to see the other part as well, please let me know.

Thanks again for your help.

Qm

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [December 18, 2018, 12:23pm UTC](https://discourse.julialang.org/t/add-second-order-cone-type-constraint/18742/4 "2018-12-18T12:23:21Z")

</div>

You need to do `using LinearAlgebra` for `norm` to be available.

---

<div class="post-metadata">

### Author: ![WangQuanmeng](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@WangQuanmeng](https://discourse.julialang.org/u/WangQuanmeng)
#### Post date: [December 19, 2018, 2:28am UTC](https://discourse.julialang.org/t/add-second-order-cone-type-constraint/18742/5 "2018-12-19T02:28:39Z")

</div>

Thanks! It works after I add `LinearAlgebra` packages.
