# Help with JuMP model formulation

**URL:** <https://discourse.julialang.org/t/help-with-jump-model-formulation/40002>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [May 22, 2020, 11:19pm UTC](https://discourse.julialang.org/t/help-with-jump-model-formulation/40002 "2020-05-22T23:19:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vondoe79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vondoe79/32/11790_2.png) [@Vondoe79](https://discourse.julialang.org/u/Vondoe79)\
**Post date:** [May 22, 2020, 11:19pm UTC](https://discourse.julialang.org/t/help-with-jump-model-formulation/40002/1 "2020-05-22T23:19:57Z")

</div>

Hello there,  
I kindly need help with my code. The following is a piece of my model. The entire model is quite lengthy, so I do not wish to bore you with that. The error (stated immediately below) :

```julia-auto
julia> for (key, value) in w
           @constraint(m, (value*(sum(d[i,j]*x[i,j] for i=1:V, j=1:V )-29)/29) <= Q)
       end
ERROR: BoundsError
Stacktrace:
 [1] getindex(::Float64, ::Int64, ::Int64) at .\number.jl:43
 [2] macro expansion at C:\Users\XX67\.julia\v0.6\JuMP\src\parseExpr_staged.jl:508 [inlined]
 [3] macro expansion at C:\Users\XX67\.julia\v0.6\JuMP\src\macros.jl:493 [inlined]
 [4] macro expansion at .\REPL[327]:2 [inlined]
 [5] anonymous at .\<missing>:?

```

It appears to be coming from the portion of my code listed below. I shall, therefore, be very grateful for your assistance in what I am doing wrong. Thank you in advance for the help.

```julia-auto
V = 5

d = 
1	2	3	4	5
999	8	4	9	9
8	999	6	7	10
4	6	999	5	6
9	7	5	999	4
9	10	6	4	999

## define Variables:------------#
@variable(m, x[i=1:V,j=1:V], Bin) #decision binary variable
@variable(m, 0.0<=Q<=1.0) #mini_max variable

#3 Assign weights: ________________________________ #
w = Pair{Tuple{Int64,Int64},Float64}[]
for i=1:V , j=1:V
		push!( w , (i,j) => i != j ? 0.00001 : 0.00003)
end

## define Objective function:---------------#
@objective(m, Min, Q) #variable for the min_max weighted percentage deviation from the target values for the goals.

## MOLP/MOMP Goal/target contraints:----
for (key, value) in w
	@constraint(m, (value*(sum(d[i,j]*x[i,j] for i=1:V, j=1:V )-29)/29) <= Q)
end

```

## NB: I have several other constraints, but the error as indicated above, is coming from the “MOLP/MOMP Goal/target constraint” as stated above.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [May 22, 2020, 11:49pm UTC](https://discourse.julialang.org/t/help-with-jump-model-formulation/40002/2 "2020-05-22T23:49:25Z")

</div>

~~Get rid of `value()`. It is for accessing the primal solution after a solve, not for adding in constraints.~~

 

~~Here’s the quickstart of the JuMP documentation: [Quick Start Guide · JuMP](https://www.juliaopt.org/JuMP.jl/stable/quickstart/)~~

I was too quick and missed the `*`. The real issue is that

```nohighlight
d = 
1	2	3	4	5
999	8	4	9	9
8	999	6	7	10
4	6	999	5	6
9	7	5	999	4
9	10	6	4	999

```

is not valid syntax for a matrix. You need

```julia
d = [
1	2	3	4	5
999	8	4	9	9
8	999	6	7	10
4	6	999	5	6
9	7	5	999	4
9	10	6	4	999]

```

---

<div class="post-metadata">

**Author:** ![Vondoe79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vondoe79/32/11790_2.png) [@Vondoe79](https://discourse.julialang.org/u/Vondoe79)\
**Post date:** [May 23, 2020, 1:58am UTC](https://discourse.julialang.org/t/help-with-jump-model-formulation/40002/3 "2020-05-23T01:58:00Z")

</div>

Actually, I read the matrix from a csv file using the following code. I just forgot to enclose it in bracket when I posted the code. Thank you anyways, but I don’t think that is the problem.

```julia
d = readcsv("C:/Users/XX67/Bmatrix5_dist.csv")[1]

```

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [May 23, 2020, 2:27am UTC](https://discourse.julialang.org/t/help-with-jump-model-formulation/40002/4 "2020-05-23T02:27:02Z")

</div>

Please make a minimal working example that demonstrates your problem. When I run the code you have (with the brackets), it works.

---

<div class="post-metadata">

**Author:** ![Vondoe79](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vondoe79/32/11790_2.png) [@Vondoe79](https://discourse.julialang.org/u/Vondoe79)\
**Post date:** [May 23, 2020, 4:34am UTC](https://discourse.julialang.org/t/help-with-jump-model-formulation/40002/5 "2020-05-23T04:34:15Z")

</div>

You’re right, I was missing the “header=true” argument of the “readcsv” command. It should be as follows:

```julia
d = readcsv("C:/Users/XX67/Bmatrix5_dist.csv", header=true)[1]

```

Thanks for making me take a harder look.
