# add second order cone type constraint

#1

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

``````for t in 1:n
for i in 0:t
for j in ( t + 1 ):( n + 1 )
@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

#2

Welcome

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.

#3

Hi,

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

``````@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

``````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
``````

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

``````@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 ] )  )
``````
``````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
``````

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 )`.

``````@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.

You need to do `using LinearAlgebra` for `norm` to be available.
Thanks! It works after I add `LinearAlgebra` packages.