The second question within two days which shows how rusty I’ve become not having used Julia on a daily basis for over a year… I’m trying to optimize a simple objective function using NLopt
, which requires multiplying a matrix and a column vector (think of a simple regression framework with an n observations and m regressors, with a covariate matrix X sized (n x m) and a vector of coefficients sized (m x 1).
As NLopt only accepts one input argument, I’ve included the covariate matrix as a default argument in my obective function. Minimal (non-) working example below:
using NLopt
X = rand(100,10) # 100x10 Array
w_0 = repmat([0.5],10) # Float64[10]
X*w_0 # works as expected: Float64[100]
function find_w(w, X=X)
X*w
end
find_w(w_0) # works as above: Float64[100]
to_min = Opt(:GN_MLSL, 10)
lower_bounds!(to_min, zeros(10))
upper_bounds!(to_min, ones(10))
min_objective!(to_min, find_w)
NLopt.optimize(to_min, w_0) # Fails: DimensionMismatch("Cannot multiply two vectors")
I’ve included print statements in my objective function which show that mat
in the find_w
function is an empty Array{Float64,1}
when the function is called within the NLopt optimization routine. That suggests to me that there is some sort of scope issue where the mat
defined outside the function before optimization isn’t available to the function when called within the optimization. Any hints on how I need to define the objective function to get this to work are greatly appreciated!
(And as an aside: Am I reading the docs for the Optim
package correctly in that it’s still not possible to do gradient-free multivariate boxed optimization with the package?)