Hello! I want to know the general AMPL code to solve a linear least squares problem.
I think it’s quite tricky for me to write the AMPL code for this problem.
Here is the assumption and the equation:
If we have this data declaration, I think the matrices are in the dense format.
set M;
set N;
set K;
param A {M, N};
param b {M, K};
var X {N, K};
The question is how I can define my minimization function for the problem? Is there any way to use functions that will work for matrix or vector representation? If not, then can I store the intermediate results for the minimization?
My question is actually to ask how I can get each row of A\mathbf{x} to subtract it by \mathbf{b}. I want to do like this:
minimize length :
sum{i in M} # sum of each vector for each row
{j in N, k in K} # just for indexing
row_Ax := A[i, j] * X[j, k] # store the row i of Ax for the matrix multiplication.
sum {k in K} # to get the length of (Ax - b)
(row_Ax[k] - b[i, k]) * (row_Ax[k] - b[i, k]) # (the actual minimization code)
I think it’s not the way of the AMPL. But can anyone let me know how to solve this and even solve this one in a sparse matrix format? I don’t know it’s possible to declare a sparse matrix and do the operations to solve the linear least squares problem with the AMPL.