4

I created a matrix A, which is 300x4 array. And in the objective, i should minimize A*x, where x is a 1x4 vector. and my code is the following:

k = 3
m = length(u) 
n = k + 1
A = zeros(m,k+1)
for i = 1:m
    for j = 1:k+1
        A[i,j] = u[i]^(k+1-j)
    end
end
display(A)
using JuMP,Gurobi
m = Model(Gurobi.Optimizer)
@variable(m, x[1:k+1])
@objective(m, Min, sum((y - (A*x).^2) ))
optimize!(m)
uopt = value.(x)
println(x)

Output:

DimensionMismatch("dimensions must match")
Stacktrace:
 [1] promote_shape at .\indices.jl:154 [inlined]
 [2] promote_shape at .\indices.jl:145 [inlined]
 [3] -(::Array{Int64,1}, ::Array{GenericQuadExpr{Float64,VariableRef},1}) at .\arraymath.jl:38
\]
Oscar Dowson
  • 2,395
  • 1
  • 5
  • 13
Thea G
  • 41
  • 4
  • Welcome to StackOverflow! Could you edit your question to be slightly more specific on the error (what line raises the error? maybe you can post the error output as well). I do not know Julia at all, but the error sounds like applying an operation to a vector/matrix with the wrong dimensions, so check your loops and break conditions first. Also, break down complicated calculations into several lines where possible to keep track. Happy coding. :) – randmin Jul 17 '20 at 00:42

1 Answers1

5

If dim(A) = 300x4, and dim(x) = 1x4 (dimensions taken from your question), then A*x (as in "the product of the matrix multiplication") is not defined as the dimensions do not match. Note that it would work if you changed x to be of dimension 4x1, and the result would have a dimension of 300x1.

In case this does not solve your issue, find line 154 (and maybe 145) in your code, as the dimension mismatch happens in these lines, and check your code in that lines for plausibility.

I did not actually verify your code (and it seems like it is not the full code, also?), and I know there are more capable people who may help you there. Anyway, I hope this helps.

Happy coding.

randmin
  • 818
  • 8
  • 16
  • Lines 154 and 145 are red-herrings from the base Julia implementation. I recently [improved the error messages in the JuMP macros](https://github.com/jump-dev/JuMP.jl/pull/2276). The real issue is that `y` won't be a 300-length vector. – Oscar Dowson Jul 18 '20 at 22:04
  • You might want to mention that OP probably wanted `x` to be a 4 element `Vector` rather than a `Matrix`. – Oscar Smith Jul 20 '20 at 04:01
  • I must admit, after almost a year, that I still have no clue what Julia is or how it works^^. I was simply fiddeling with what was given, and I am glad that I could help one or another person. In my understanding, a 4 element Vector is a 4x1 Matrix, but there is likely a difference within the Julia implementation of these two datatypes (?). As I mentioned, there are more capable people in that regard. Thank you for the hints – randmin Apr 29 '21 at 02:18