Can someone help me to optimize this matrix?

I have the following matrix
wall0 = [0 0 0 0 0 0 0 0 0 0;...
0 0 0 0 0 0 0 0 0 0;...
0 a 0 b 0 a 0 0 0 0;...
0 0 a 0 b 0 a 0 0 0;...
0 0 0 a 0 a 0 0 0 0;...
0 a 0 b 0 a 0 0 0 0;...
0 0 a 0 b 0 a 0 0 0;...
0 0 0 a 0 a 0 0 0 0;...
0 0 b 0 b 0 b 0 0 0;...
0 0 0 0 0 0 0 0 0 0];
Where
a = 0.3;
b = 0.5;
The numbers in the matrix represent probabilities that a 'sound wave' would be absorbed, where 0 is a zero probability.
All this means is that a cell is chosen randomly in my code, and an 'if' statement determines whether or not the 'wave' is absorbed based on another random number between 0 and 1 that is attached to it.
An algorithm that randomizes the rows, as follows:
[rows,cols] = size(wall0) ;
for i=1:rows
idx = randperm(cols) ;
wallR(i,idx) = wall0(i,:);
end
An example of what I mean is:
wave1 hits wall0 (randomly) at [4,3] -> wall0(4,3) = a
if (different) random number for wave1 < a, wave1 is absorbed
number of absorptions += 1
repeat x number of times
The only constraints are that the matrix dimensions are fixed; and the number of a's and b's (and zeros) are fixed and can only move within the rows.
Basically, I would like to optimize this matrix such that the number of absorptions is maximized. I am hoping that someone may be able to help as I am a beginner in regards to optimization.

12 commentaires

I already described a strategy using ga() .
David Mueller
David Mueller le 27 Nov 2017
Modifié(e) : Walter Roberson le 27 Nov 2017
Just FYI, the following is the full code for my project:
clc; clear variables; close;
% sets the matrix which represents the wall to wall0. a and b both represent
% the audio foam panels. Their numbers are the probability which will be
% evaluated against a random number to determine if the sound wave is
% absorbed or not.
a = 0.3;
b = 0.5;
n = 100;
wall0 = [0 0 0 0 0 0 0 0 0 0;...
0 0 0 0 0 0 0 0 0 0;...
0 a 0 b 0 a 0 0 0 0;...
0 0 a 0 b 0 a 0 0 0;...
0 0 0 a 0 a 0 0 0 0;...
0 a 0 b 0 a 0 0 0 0;...
0 0 a 0 b 0 a 0 0 0;...
0 0 0 a 0 a 0 0 0 0;...
0 0 b 0 b 0 b 0 0 0;...
0 0 0 0 0 0 0 0 0 0];
% d0 is the perpendicular distance of the source from the wall. h0 and w0
% are the starting height and width (corresponding indexes on the
% matrix)
% AvgR will be used to average the number of absorbed waves per
% configuration.
d0 = 4;
h0 = 4;
w0 = 5;
AvgR = [];
allAVec = [];
%%begin for loop. This loop creates c number of configurations for the
for x = 1:10
% JR is a placeholder vector that will concatenate the loop variable
% j, basically to create an 'x' variable to graph against number of
% absorptions. AbsorbVecR is another placeholder that will contain
% the number of absorbed waves on the initial wall configuration,
% per iteration.
JR = []; AbsorbVecR = [];
j = 0; wallR = wall0;
%The initial configuration is shuffled, row by row to get random
%configurations. This is stored in wallR, and is repeated on each loop.
[rows,cols] = size(wall0) ;
for t=1:rows
idx = randperm(cols) ;
wallR(t,idx) = wall0(t,:);
end
wallR
while j < 1000
absorbR = 0; %initializes number of absorptions to zero
%initializes vectors that will contain the matrix indices
%of where the wave hits. Height -> row, width -> column
relHeightR = [];
relWidthR = [];
%creates a vector of random numbers, each corresponding to the
%wave components. To be evaluated against a, b, or zero.
rnjesusR = rand(1, n);
%creates the azimuthal (thetaR) and longitudinal (phiR) angles
%between -pi/4 & pi/4 used in determining wave 'hit' position.
thetaR = (pi/4) * 2*(rand(1, n) - 0.5);
phiR = (pi/4) * 2 *(rand(1, n) - 0.5);
%dHR and dWR are the vertical and horizontal distances the wave travels
%from source.
dHR = int8(d0.*tan(thetaR));
dWR = int8(d0.*tan(phiR));
%loops for each sound wave (elements of dHR and dWR)
for i = 1:length(dHR)
%determines the actual hit indices on the matrix
relHeightR = [relHeightR, (dHR(i) + h0) + 1];
relWidthR = [relWidthR, (dWR(i) + w0) + 1];
%tests to see if wave is absorbed, adds to absorb total
if rnjesusR(i) < wallR(relHeightR(i) , relWidthR(i))
absorbR = absorbR + 1;
end
end
%for each vector of random waves, the number of absorptions
%are concatenated in AbsorbVecR, giving a vector which can be
%used to find the average absorption for the configuration
%loop variable j is incremented, iteration number added to JR.
AbsorbVecR = [AbsorbVecR, absorbR];
j = j + 1;
JR = [JR, j];
end
AbsorbtotR = 0;
for i = 1:length(AbsorbVecR)
AbsorbtotR = AbsorbtotR + AbsorbVecR(i);
end
AvgR = [AvgR,(AbsorbtotR)/length(AbsorbVecR)];
plot(JR, AbsorbVecR)
xlabel('Iteration Number', 'fontsize', 12);
ylabel('Number of Absorbtions (per iteration)', 'fontsize'...
,12);
hold on
end
You did not seem to pay attention to my description of how to use ga() for this problem, and you have deleted all the work I put into that.
I'm out.
John D'Errico
John D'Errico le 27 Nov 2017
You can't use an optimization tool to optimize an objective that has randomness inside it.
the cyclist
the cyclist le 27 Nov 2017
David, why did you delete the original version of this question where Walter and I both tried to help you? You are now wasting at least three volunteer's time, because John is now asking a question that Walter addressed in his comments on the former question.
David Mueller
David Mueller le 27 Nov 2017
To Walter and the Cyclist:
I meant no offense by deleting my previous question. Walter, I did read and re-read your answers multiple times but was having trouble understanding what you meant, and I do appreciate the help. However, as I said, I am a beginner to optimization. I asked for clarification or an example, but had not received a response. I realize that is not your problem, It is completely my own. But I was hoping that by re-posting the question, someone new may see it and help. Obviously the group of volunteers is much smaller than I had imagined. Thanks again for your help guys, I'm sorry for causing this mess.
-David
The volunteers are often dealing with multiple posted questions simultaneously, and with emailed questions... and laundry, and cooking, and dishes...
David Mueller
David Mueller le 27 Nov 2017
That's a fair point, and I can appreciate it. But again, I have only used this forum (or any forum outside of yahoo answers or reddit, for that matter) a few times and was not aware of the etiquette of deleting a question, or about the people volunteering answers. Of course, I won't be repeating those mistakes again. I don't know what else to say other than sorry. I already had to learn the hard way about commenting my code more comprehensively and naming variables more appropriately.
If you don't mind still helping me, I still wasn't quite understanding what you meant regarding the ga() implementation. Would it be possible to show me what you mean in code or in a specific example?
Today I'm my mother's systems administration, and her computer has just failed to reboot during an OS upgrade... she's 1000 miles away...
ren yanze
ren yanze le 28 Nov 2017
I want to help you,but not understand clearly. Does wave1 have the same dimension to wall0? Can wave1 taken as the random matrix completely? You want to optimize the wallR to make the maximum number of absorptions when wave1 hits wallR,right?
The a and b are fixed as to their rows and the number of each in a given row, but may move within the row. The matrix therefore cannot be taken fully randomly.
You could take a random matrix within those constraints, but the problem is doing meaningful optimization against that, short of just trying all of the possibilities.
David Mueller
David Mueller le 28 Nov 2017
Modifié(e) : David Mueller le 28 Nov 2017
Ren, thanks for your response. Wall0 and wallR are both 10x10 matrices, the difference being that wallR has its rows randomly shuffled. And yes, I want to optimize wallR for the greatest number of absorptions as you said
The wave is composed of two 100x1 vectors (I just called it wave1 in the pseudocode for simplicity, although now I see that may have been less helpful than intended). One is called relHeight which is just the row numbers where each wave hits, and the other is relWidth which is the column numbers. Together they give the contact index on the wall for each wave. I am open to suggestions regarding changing the organization if it would help.
Walter, yes that is correct regarding the a's and b's. Although, it doesn't necessarily have to be completely random shuffling. If there is a way to look at all the permutations and find the optimal arrangement using other than rng, I would be willing to try.

Connectez-vous pour commenter.

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by