3D and 2D matrix multiplication in the objective function

4 views (last 30 days)
I have a 3D A matrix NxNxM and 2D B matrix (NxN), which will be an integer optimization variable matrix for the solver. I want to multiply A and B to obtain (NxNXM) matrix. Then I need to sum all of the elements of the resulting 3D matrix.
My objective function is the following:
ndesign = optimproblem('Objective',sum(A.*B,'all'));
I can do this multiplication if I don't use the solver. However, I receive the following error when I put it in the objective function:
Error using optim.internal.problemdef.ElementwiseOperator/checkIsValid
Argument dimensions 1454-by-1454-by-50 and 1454-by-1454 must agree.
Error in optim.internal.problemdef.Times/checkIsValid
Error in optim.internal.problemdef.Times.getTimesOperator
Error in .*
Error in networkdes (line 290)
ndesign = optimproblem('Objective',sum(A.*B,'all')); % Create optimization problem
I also need to convert my 3D matrix to sparse 3D matrix. I'm using ndSparse library to create it but I could not use it in the objective function again. Thanks in advance.
Edit for more explanations. Here is my optimization script:
A = %(1454x1454x50) 3D matrix
A_sparse = ndSparse(A);
B = optimvar('b', [length(A),length(A)], 'Type', 'integer', 'LowerBound',0,'UpperBound',1);
ndesign = optimproblem('Objective',sum(A.*B,'all')); % Create optimization problem
% ndesign = optimproblem('Objective',sum(A_sparse.*B,'all')); % Create optimization problem for sparse A
ndesign.Constraints.const1 = ( B(diag(B)) == 0 ); % B_ii == 0
ndesign.Constraints.const2 = ( sum(B,'all') == 20 );
opts = optimoptions('intlinprog','Display','off','PlotFcn',@optimplotmilp);
[sol,fval,exitflag,output] = solve(ndesign,'options',opts);
  10 Comments
Walter Roberson
Walter Roberson on 25 Oct 2020
If you are using problem based optimization then that datatype is your only option. However if I understand correctly you can set up a for loop to construct the objective.
ebon T
ebon T on 26 Oct 2020
Thanks a lot for the help !
I reshaped my A matrix to (n*m, n) and B is still (n,n )
A_re = reshape(A, [size(A,1)*size(A,3), size(A,1 )]);
and changed my objective function to
ndesign = optimproblem('Objective',sum(A_re*B,'all '));
and it worked! However, I am not able to use ndSparse class sparse A matrix to do the same
A_sparse = ndSparse(A_re );
ndesign = optimproblem('Objective',sum(A_sparse*B,'all '));
When I run above script, I get the following error :
Error using double
Conversion to double from optim.problemdef.OptimizationVariable is not possible.
Error in ndSparse>mkCompat (line 2803 )
X=double(X);
Error in * (line 1083 )
obj = finalObject( mkCompat(L)*mkCompat(R));
Error in networkdes_rev (line 20 )
ndesign = optimproblem('Objective',sum(A_sparse*B,'all '));
How can I do this and use it in the objective function?

Sign in to comment.

Answers (0)

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!