Vector from loop into matrix and optimization

1 次查看(过去 30 天)
Hello, I would like to put the results of ddd into a matrix results_vec(z,:)=ddd. So far, only the last vector of ddd is pasted into results_vec(z,:). However, I would like to have all z=3 vectors of ddd into results_vec matrix.
My actual goal is to optimize S_vec. For that reason I will use the matrix results_vec and sort for the first row so that I can find the appropriate values for C vector. Are there any other approaches available?
Thanks for help!
%Given:
Yn; % 1 x 90 vector with integers between 0 and 1
R1; % 90 x 1 vector with integers between 0 and -1
size_vec; %90 x 1 vector an/E1
D=exposures-E;% 1 x 90 vector with integers above 0
E; % 1 x 90 vector with integers above 0
% Optimize x1:x90:
%C=[x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 x31 x32 x33 x34 x35 x36 x37 x38 x39 x40 x41 x42 x43 x44 x45 x46 x47 x48 x49 x50 x51 x52 x53 x54 x55 x56 x57 x58 x59 x60 x61 x62 x63 x64 x65 x66 x67 x68 x69 x70 x71 x72 x73 x74 x75 x76 x77 x78 x79 x80 x81 x82 x83 x84 x85 x86 x87 x88 x89 x90]
% x1:x90 are the values we want to optimize! Thus, unknown yet. xi>0;
%sum(C)=200000; %Restriction
%Minimize Target function S_vec
z=3;
results_vec=zeros(z,91);
for i=1:z
C=randfixedsum(90,1,200000,0,200000);
%creates a 90 x 1 vector with random integers btw. 0 and 200000.
%restriction: sum of integers= 200000;
B_vec=diag(zeros(1,90));
for i=1:90
bn=(D(1,i)-C(i,1))/(E(1,i)+C(i,1));
B_vec(i,i)=bn;
end
B_vec(B_vec>30)=30;
S_vec = Yn*diag(size_vec)*diag(assetsalesf(B_vec,M,F1)); % 1 x 90 vec
%Optimize S_vec!
ddd=[sum(S_vec) transpose(C)];
results_vec(z,:)=ddd
end
% table_results_opti=table(resultsss_vec)
% filename = 'optimi_matlab.xlsx';
% writetable(table_results_opti,filename,'Sheet',1,'Range','A1');
%
% winopen('optimi_matlab.xlsx')

回答(3 个)

Guillaume
Guillaume 2018-5-11
编辑:Guillaume 2018-5-11
There are several problem with your code. In my opinion the biggest is that you don't use meaningful variable names and hence end up using the wrong variable.
The second problem is that you have a for loop with index i (meaningless variable name) and inside that loop you have another loop with the same index variable i.
The third problem is that you never actually use the loop index to put the ddd (extremely useless variable name!) into results_vec. Instead you use z which is a constant you set to 3. So, every time you write the data to row 3. Note that you couldn't use the row index i of the outer for loop since you've overwritten it with the i of the inner for loop.
Anyway:
numrows = 3; %much better name than z
results_vec = zeros(numrows, 91);
for row = 1:numrows %better name than i
%....
for diagindex = 1:90
%....
end
%...
please_give_me_a_meaningful_name = [sum(S_vec), transpose(C)]; %your horribly named ddd
results_vec(row, :) = please_give_me_a_meaningful_name;
end
%...
Note that I haven't really tried to understand what your code is doing, but it looks to me that the inner loop could be removed entirely and the diagonal calculated in one go:
B_vec = diag((D(1, :) - C(:, 1).') ./ (E(1, :) + C(:, 1).'));
And what a misleading name that B_vec is since it's actually a square matrix!

Zanjesh Kapoor
Zanjesh Kapoor 2018-5-12
Hello and many thanks so far! I have adjusted my script accordingly. Now, it's working:
%Given: Yn; % 1 x 90 vector with integers between 0 and 1 R1; % 90 x 1 vector with integers between 0 and -1 size_vec; %90 x 1 vector an/E1 D=exposures-E;% 1 x 90 vector with integers above 0 E; % 1 x 90 vector with integers above 0 % Optimize x1:x90:
%C=[x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x1 2 x13 x14 x15 x16 x17 x18 x19 x20 x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 x31 x32 x33 x34 x35 x36 x37 x38 x39 x40 x41 x42 x43 x44 x45 x46 x47 x48 x49 x50 x51 x52 x53 x54 x55 x56 x57 x58 x59 x60 x61 x62 x63 x64 x65 x66 x67 x68 x69 x70 x71 x72 x73 x74 x75 x76 x77 x78 x79 x80 x81 x82 x83 x84 x85 x86 x87 x88 x89 x90] % x1:x90 are the values we want to optimize! Thus, unknown yet. xi>0;
numrows = 3; %much better name than z output_matrix = zeros(numrows, 91);
for row = 1:numrows %better name than i C=randfixedsum(90,1,200000,0,200000); B_matrix=diag(zeros(1,90)); %creates a 90 x 1 vector with random integers btw. 0 and 200000. %restriction: sum of integers= 200000;
for diagindex = 1:90
bn=(D(1,diagindex)-C(diagindex,1))/(E(1,diagindex)+C(diagindex,1));
B_matrix(diagindex,diagindex)=bn; %....
end
B_matrix(B_matrix>30)=30; %restricts values to maximum 30.
S_vec = Yn*diag(size_vec)*diag(assetsalesf(B_matrix,M,F1)); % 1 x 90 vec %Now, I use B_matrix in my function assetsalesf to create output S_vec. %sum(S_vec) is the output which I want to minimize!!
output_for_S_vec_n_and_C=[sum(S_vec) transpose(C)]
output_matrix(row,:)=output_for_S_vec_n_and_C %my goal is to sort that results_matrix in an ascending order for column 1. % In total I have numrows times vectors with a value for the output % sum(S_vec) in column 1 and for the C in column 2 to 91. %My intention is to run that script 1 million times and sort for the minimum %so that I obtain the values for vector C which deliver the minimum for %sum(S_vec). Thus, I want to minimize sum(S_vec) given my constraint for C. % I do not know whether I can use the Optimization toolboox because C is in % the fraction bn=(D(1,row)-C(row,1))/(E(1,row)+C(row,1)). Is this still % somehow possible?
end

Zanjesh Kapoor
Zanjesh Kapoor 2018-5-12
My goal is to sort that results_matrix in an ascending order for column 1. In total I have numrows times vectors with a value for the output sum(S_vec) in column 1 and for the C in column 2 to 91. My intention is to run that script 1 million times or more and sort for the minimum so that I obtain the values for vector C which deliver the minimum for sum(S_vec). Thus, I want to minimize sum(S_vec) given my constraint for C. I do not know whether I can use the Optimization toolboox because C is in the fraction bn=(D(1,row)-C(row,1))/(E(1,row)+C(row,1)). That is why I came to my solution above. Any better ideas?

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by