How to vectorize with GA optimization solver? - Non-singleton dimensions

Hello. I have 4D and 2D matrix and need to use optimization solvers.
There's no problem with fmincon solver.
My script is here: It load previously calculated Y.mat and assign initial guess.
%Global variables to function
global Y;
global k l i j;
load('Y.mat', 'Y'); % Y can be changed randi(10,3,5,3,5) to test.
[k l i j] = size(Y); % k=3, l=5, i=3, j=5
x0 = ones(kmax,lmax); % Initial guess
options = optimoptions('fmincon');
options = optimoptions(options, 'Display', 'item');
options = optimoptions(options, 'UseParallel', 1);
[x,fval] = fmincon(@myfun,x0,[],[],[],[],[],[],@confun,options);
My function code is here: Multiple summation with bsxfun and its standard deviation
function out = myfun(x)
global Y;
global k l i j;
z = bsxfun(@times,Y,x);
mysum = squeeze(sum(reshape(z,k*l,1,m,n)));
out = std(mysum(:));
end
Q1. Is there any good way to vectorize multiple summation loop with 4D and 2D?
Having said that, the function code works with fmincon solver but when running GA solver there is a error.
Error using bxsfun
Non-singleton dimensions of the two input arrays must match each other.
This means GA solver only support vectorized form and I have to match each matrix dimensions.
.
Q2. How to send variables from workspace to function in order to use parallel processing?
If I use global variable to send variables in workspace to function,
global Y;
global k l i j;
It is not able to run parallel.
options = optimoptions(options, 'UseParallel', 1);
Thank you for taking the time to read my long post.
James.

This question is closed.

Asked:

on 23 Feb 2016

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!