# Linear programming with Jump

**URL:** <https://discourse.julialang.org/t/linear-programming-with-jump/34192>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, linear-programming\
**Created:** [February 5, 2020, 12:13am UTC](https://discourse.julialang.org/t/linear-programming-with-jump/34192 "2020-02-05T00:13:00Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![bandaya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bandaya/32/12956_2.png) [@bandaya](https://discourse.julialang.org/u/bandaya)\
**Post date:** [February 9, 2020, 5:40pm UTC](https://discourse.julialang.org/t/linear-programming-with-jump/34192/5 "2020-02-09T17:40:31Z")

</div>

Thank you. I have managed to fix my code but there is another point I would like to check. I have defined the constraint using a FOR loop, but I am trying to avoid the loop  
and use vectorized code.

I have given my code that runs without problems at the bottom. If I replace the constraint with the following vectorized code, it results in error message. The error message suggests that it is detecting it as a quadratic expression, but I cannot see any reason why that is the case. One possible reason is that the upper triangle elements in the array M are appearing as dots or question marks rather than zero.

```julia
d=cumprod(B[1:end-1])
d=[1; d]
M1=ones(8,8).*d
M=LowerTriangular(M1./M1')
@constraint(myModel, constraint1, ((M*A).*Disc).<=0.05*53.844)

```

MethodError: Cannot `convert` an object of type GenericQuadExpr{Float64,VariableRef} to an object of type GenericAffExpr{Float64,VariableRef}  
Closest candidates are:  
convert(::Type{GenericAffExpr{T,V}}, !Matched::Real) where {T, V} at C:\Users\a.julia\packages\JuMP\MsUSY\src\aff\_expr.jl:295  
convert(::Type{GenericAffExpr{T,V}}, !Matched::V) where {T, V} at C:\Users\a.julia\packages\JuMP\MsUSY\src\aff\_expr.jl:294  
convert(::Type{T}, !Matched::GenericAffExpr{T,VarType} where VarType) where T at C:\Users\a.julia\packages\JuMP\MsUSY\src\aff\_expr.jl:298

Stacktrace:  
[1] setindex! at .\array.jl:784 [inlined]  
[2] lmul!(::LowerTriangular{GenericAffExpr{Float64,VariableRef},Array{GenericAffExpr{Float64,VariableRef},2}}, ::Array{GenericAffExpr{Float64,VariableRef},1}) at D:\buildbot\worker\package\_win64\build\usr\share\julia\stdlib\v1.3\LinearAlgebra\src\triangular.jl:833  
[3] \*(::LowerTriangular{Float64,Array{Float64,2}}, ::Array{GenericAffExpr{Float64,VariableRef},1}) at D:\buildbot\worker\package\_win64\build\usr\share\julia\stdlib\v1.3\LinearAlgebra\src\triangular.jl:1874  
[4] top-level scope at C:\Users\a.julia\packages\JuMP\MsUSY\src\macros.jl:390  
[5] top-level scope at In[5]:38

**The code below runs without errors.**

```julia
using JuMP
using Cbc
using LinearAlgebra
myModel = Model(with_optimizer(Cbc.Optimizer))
prices = [99.74, 91.22, 98.71, 103.75, 97.15]
cashFlows = [4 5 2.5 5 4; 4 5 2.5 5 4; 4 5 2.5 5 4; 4 5 2.5 5 4; 4 5 102.5 5 4;4 5 0 105 104;4 105 0 0 0; 104 0 0 0 0]
Liab_CFs = [5, 7, 7, 6, 8, 7, 20, 0]*1000
nt=size(cashFlows,1)
nb=size(cashFlows,2)
Rates = [0.01, 0.015, 0.017, 0.019, 0.02, 0.025, 0.027, 0.029]
Disc= [0.99009901, 0.970661749, 0.950686094, 0.927477246, 0.90573081, 0.862296866, 0.829863942, 0.795567442]'
nBonds = [10, 100, 20, 30, 5]*1000
@variable(myModel, 0<= x[b = 1:length(nBonds)] <= nBonds[b])
@objective(myModel,Min,prices' *x) 
B=[1.01, 1.020024752, 1.02101183, 1.02502363, 1.024009823, 1.050370059, 1.039082218, 1.043109481]
A=Liab_CFs-cashFlows*x
M=zeros(length(A))
M=A[1]
for k=2:length(A)
    M(k)=M[k-1]*B[k-1]+A[k]
end
@constraint(myModel, constraint1,M.*Disc .<=0.05*53.844)
print(myModel)
optimize!(myModel)
println("Optimal Solutions:")
println("x = ", JuMP.value.(x))

```

---

_[View the full topic](https://discourse.julialang.org/t/linear-programming-with-jump/34192)._
