Optimization problem that fails to converge

Hi all,

I am very new to Julia and maths is quite old for me now, so I am struggling with my code.
The thing is that I would like to find the best linear combination of six parameters that fits the seventh parameter.
Let me explain:

  • I have seven 1-column matrices of 300 data for each (say C1,C2…C7)
  • I would like to find the best vector (a,b,c,d,e,f) (all positive) that minimizes C7 - aC1-bC2-cC3-dC4-eC5-fC6
    I have tried the optimization package using NLopt.LD_LBFGS() and NLopt.GN_DIRECT() but it failed to converge.
    Here is the code for the last one:
function optim(a,b,c,d,e,f)
	deviation = (C7 - a*C1 - b*C2 - c*C3 - d*C4 - e*C5 - f*C6)
	somme = 0
	for i in deviation
		somme = somme + i^2
	end
	return somme
end

optfun = OptimizationFunction(optim, Optimization.AutoForwardDiff())

begin
	init = zeros(6)
	prob = OptimizationProblem(optfun, init, lb = [0, 0, 0, 0, 0, 0], ub = [1.0, 1.0, 1.0, 1.0, 1.0,1.0])
end

sol = solve(prob, NLopt.GN_DIRECT(), maxtime = 10.0)

And it gives me “NLopt failed to converge: FORCED_STOP”

Would you have a suggestion for me?

Thanks a lot!

Iforire

Welcome to Julia and Julia Discourse.

Because you stated that your math is rusty, I will mention that the product of a matrix and a vector is just the linear combination of the columns of the matrix, with the vector elements as the linear coefficients. Therefore, your problem looks like a nonnegative least squares problem. It can be solved very simply using the NonNegLeastSquares package. Here is a solution using random values for the vectors c1 through c7:

using NonNegLeastSquares: nonneg_lsq
using LinearAlgebra: norm
c1_to_c6 = rand(300,6) # Contains columns c1 through c6
c7 = rand(300) # Contains column 7
coefs = nonneg_lsq(c1_to_c6, c7)
@show coefs
@show norm(c1_to_c6 * coefs - c7)

This produces the following output (it will be different for you due to the random entries):

coefs = [0.17908585324436196; 0.10343518649759845; 0.09408512183744712; 0.21328999063315954; 0.2224834340815174; 0.1077332059326006;;]
norm(c1_to_c6 * coefs - c7) = 5.595322154323634
5.595322154323634
1 Like

Probably it is expecting a function that takes a vector of arguments. Read the documentation.

2 Likes

Thanks both of you, it helped me a lot!
Both methods are now working perfectly.
I had to struggle a little bit with the matrix multiplications but in the end, it worked, thanks.