MATLAB Answers

adjusting the code from optimization output from minimum to maximum

2 views (last 30 days)
ahmed tarek
ahmed tarek on 20 Jul 2021
Answered: Ahmed raafat on 21 Jul 2021
this code is supposed to get me the optimization for the minmum output of the surrogate model what i want is to adjust the code to get me the maximum instead.
% Test distribution of data points to be sure the is being changed NOT FIXED VALUES
clear all; clc; close all;
cd ( 'E:\material\Project\matlab\surrogate');
Data = xlsread('E:\material\Project\matlab\surrogate\Ahmed.xlsx','Sheet1');
LL =Data( : , 1);
Th =Data( : , 2);
LA =Data ( : , 3);
AO =Data ( : , 4);
close all ;
hfig1 = figure ('Position', [50 , 100, 1000 , 500]);
hhsubplot1 = subplot (5 ,1 ,2);
Newp = get ( hhsubplot1 ,'position');
Newp (4) = Newp(4)*1.5;
set ( hhsubplot1 , 'position', Newp );
ID_Data =1: length(AO);
plot ( ID_Data , LL , 'ok','MarkerSize',2,'MarkerfaceColor','k' ); hold on
plot ( ID_Data , Th ,'^b','MarkerSize',2,'MarkerfaceColor','b' );
plot ( ID_Data , LA , 'sr','MarkerSize',2,'MarkerfaceColor','r' );
ylabel ('Input variables', 'fontsize',12);
xlabel ('Run number [-]');
legend ( 'Leg Length' , 'Hot Temp' , 'Leg Area' , 'Location' , 'EastOutside');
legend boxoff
set (gca ,'FontSize' ,14);
box on
xlim ([0 155]) ;
ylim([0 700]);
hhsubplot2 = subplot (5 ,1 ,4);
Newp = get ( hhsubplot2 ,'position');
set ( hhsubplot2 , 'position', Newp );
plot ( ID_Data ,AO ,'sk','MarkerSize',2,'MarkerfaceColor','k' );
xlabel ('Run number [-]');
set (gca ,'FontSize' ,12);
box on
xlim ([0 155]) ;
ylim ([0 0.2])
ylabel (' Max Volt [V]');
print (hfig1 ,'-depsc',' Distribution data.eps','-r600');
print (hfig1 ,'-dpng',' Distribution data.png');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Building surrogate model
S=[ LL Th LA ];
Y= AO;
Mean_S = mean (S);
Std_S = std (S);
fprintf('\n Mean of input variables,= %f ',Mean_S);
fprintf('\n Standard deviation of input variables= %f',Std_S);
global kind_of_RBFNN;
kind_of_RBFNN=1;
buildRBFNN_2016(S,Y);
FX_RBFNN = Eval_RBFNN_2016 (S);
max ( FX_RBFNN );
fprintf ('\n For statstical analysis of given and predicted data \n');
hfig2 = figure (2) ;
fprintf ('For output number' )
[~, Rsquared , ~, MeanSquaredError , ~, ~, ~, ~]=Function_Linear_regression_2016 ( FX_RBFNN,Y);
% Measuring the performance of trained network
[M,B,R] = postreg ( FX_RBFNN,Y) ;
%M the slope of line Best value of M=1
% B the y-intercept Best value of B=0
%
fprintf ('\n Rsquared = %g ', Rsquared) ;
fprintf('\n MeanSquaredError = %g',MeanSquaredError);
fprintf('\n Slope of the linear regression = %g',M);
fprintf('\n intercept = %g \n',B);
LL= findobj (gca ,'Type','line'); set (LL ,'Color','k');
hChild = get (gcf ,'Children');
hChildLeg = hChild ( strcmp ( get ( hChild (:) ,'Tag'),'legend')==1) ;
set ( hChildLeg ,'box','off');
FileName2 = strcat ('Network_Fitting_Output','.eps');
print (hfig2 ,'-dpng','-r600',FileName2 ) ;
hfig3 = figure (3);
plot (Y, FX_RBFNN , 'sk','MarkerSize',5,'MarkerfaceColor','k' );
xlabel ('Given data','fontsize' ,14);
ylabel ('Predicted values','fontsize',14);
FileName3 = strcat ('Network_Fitting_Matching','.png');
print (hfig3 ,'-dpng','-r600',FileName3 );
close all;
[m1 ,n1 ]= size ( FX_RBFNN);
no_of_elements1 =m1*n1;
abscissa = linspace (1, no_of_elements1 , no_of_elements1 ) ;
RBFNN_ordinate_Simulated_output_values = reshape ( FX_RBFNN,1,[ ]);
ordinate_output_values = reshape (Y ,1 ,[ ]);
hfig3 = figure (4) ;
plot ( abscissa , ordinate_output_values ,'bs',abscissa , RBFNN_ordinate_Simulated_output_values ,'k.','LineWidth' ,1.0 , 'MarkerSize' ,12) ;
xlim ([0 32]);
ylim([0 0.2]);
xlabel ('Run number','FontWeight','bold');
ylabel ('Max Voltage [V]','FontWeight','bold','Fontsize',14);
legend1 = legend ('Given data','RBFNN output');
set ( legend1 ,'Location','NorthEast','FontWeight','bold');
legend boxoff
FileName2 = strcat ('Agreement_RBFNN_Output','.png');
print (hfig3 ,'-dpng','-r600',FileName2 );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Screening & Interaction variables
rng (1) ;
d=3;
p=2^5;
xi=p/2;
r=p;
Range=[0.5 323 0.2; 4 623 2];
labels ={ '$L$', '$T_h$' ,'$A$'};
FUNCTION_NAME ='Eval_Multi_RBFNN_1_2016';
X = screeningsample_2016 (d,p,xi ,r);
[sm , ssd ]= screeningplot_kh_2016 (X,str2func ( FUNCTION_NAME ),Range , xi , p, labels ); % S mean , S standard deviation
disp ('Elementray effect distribution'); disp (sm); disp (ssd);
fprintf ('\n Output: For x axis min =%g, max =%g \n For y axis min =%g, max =%g \n', min (sm),max (sm), min (ssd ),max ( ssd ));
hf= gcf ;
set ( findall (hf ,'type','text' ),'fontsize',12);
xlim ([ -0.5 0.5]) ; ylim ([0 4]) ;
figureNameEPS = strcat ( FUNCTION_NAME ,'Screening.eps') ;
set (gca ,'fontsize', 12 );
print (hf ,'-depsc','-r600', figureNameEPS );
fprintf ('\n Elementray effect distribution plot is ready in %s \n', figureNameEPS );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Tile plot
%close all ;
FUNCTION_NAME ='Eval_Multi_RBFNN_1_2016'; Min_value =[];Max_value =[];
range = Range ;
baseline = mean ( range ) ;
N_mesh =40;
Cont =1 ;
if ishandle (2) && strcmp ( get (2,'type'), 'figure')
close (2)
end
hf= figure (2) ;
tileplot_mod_kh2_2016 ( baseline , range , labels , str2func (FUNCTION_NAME ), N_mesh , Min_value , Max_value , Cont ,12)
figureNameEPS = strcat ( FUNCTION_NAME ,'TilePlot','.png') ;
set (gca ,'fontsize', 12 );
print (hf ,'-dpng', figureNameEPS ); hold off ;
fprintf ('\n Tile plot is ready in %s \n', figureNameEPS );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PLOT_3D_Surface
% W_ch & H_ch
%stop
close all ;
FUNCTION_NAME ='Eval_Multi_RBFNN_1_2016'; Z_Label ='Max Volt [V]';
ScaledValue ='_';
if ishandle (5) && strcmp ( get (5,'type'),'figure')
close (5)
end
hf5 = figure (5) ;
x=0.5:0.5:4;
y= 323:50:623;
[XX ,YY] = meshgrid (x,y);
ni= size (XX ,1) ; ZZ= zeros ( size (XX));
nj= size (XX ,2) ;
for i =1: ni
for j =1: nj
POINT =[ XX(i,j) YY(i,j) 20 ];
ZZ(i,j)=(feval( FUNCTION_NAME , POINT ));
end
end
mesh (XX ,YY ,ZZ); hold on ; grid on ;
[~,h]= contourf (XX ,YY ,ZZ);
hh = get (h,'Children');
set (hh , {'ZData'}, cellfun (@(x) -0* ones ( size (x)), get (hh ,{'XData'}) ,'UniformOutput',false ));
view (gca ,[ 45 45]) ;
xlabel ('L','FontSize',12 );
ylabel ('T_h','FontSize',12 );
zlim([0 100]);
zlabel ( Z_Label ,'FontSize',12);
set (gca ,'fontsize', 14 );
figureNameEPS = strcat ( FUNCTION_NAME , ScaledValue ,'FunctionPlot_x1x2','.png');
print (hf5 ,'-dpng', figureNameEPS )
% W_ch & N_ch
if ishandle (6) && strcmp ( get (6,'type'), 'figure')
close (6)
end
hf6 = figure (6) ;
x=0.5:.5:4;
y= 0.2:0.2:2;
[XX ,YY] = meshgrid (x,y);
ni= size (XX ,1) ; ZZ= zeros ( size (XX));
nj= size (XX ,2) ;
for i =1: ni
for j =1: nj
POINT =[ 0.4 XX(i,j) YY(i,j) ];
ZZ(i,j)=( feval ( FUNCTION_NAME , POINT ));
end
end
mesh (XX ,YY ,ZZ); hold on ; grid on
[~,h]= contourf (XX ,YY ,ZZ);
hh = get (h,'Children');
set (hh , {'ZData'}, cellfun (@(x) -0* ones ( size (x)), get (hh ,{'XData'}) ,'UniformOutput',false ));
view (gca ,[ 45 45]) ;
xlabel ('L','FontSize',12 );
ylabel ('A','FontSize',12 );
zlim([0 100]);
zlabel ( Z_Label ,'FontSize',12);
set (gca ,'fontsize', 14 );
figureNameEPS = strcat ( FUNCTION_NAME , ScaledValue ,'FunctionPlot_x1x3','.png');
print (hf6 ,'-dpng', figureNameEPS )
% N_ch & H_ch
if ishandle (7) && strcmp ( get (7,'type'), 'figure')
close (7)
end
hf7 = figure (7) ;
x=0.2:0.2:2;
y= 323:50:623;
[XX ,YY] = meshgrid (x,y);
ni= size (XX ,1) ; ZZ= zeros ( size (XX));
nj= size (XX ,2) ;
for i =1: ni
for j =1: nj
POINT =[ XX(i,j) 20 YY(i,j) ];
ZZ(i,j)=( feval ( FUNCTION_NAME , POINT ));
end
end
mesh (XX ,YY ,ZZ); hold on ; grid on
[~,h]= contourf (XX ,YY ,ZZ);
hh = get (h,'Children');
set (hh , {'ZData'}, cellfun (@(x) -0* ones ( size (x)), get (hh ,{'XData'}) ,'UniformOutput',false ));
view (gca ,[ 45 45]) ;
xlabel ('A','FontSize',12 );
ylabel ('T_h','FontSize',12 );
zlim([0 100]);
zlabel ( Z_Label ,'FontSize',12);
set (gca ,'fontsize', 14 );
figureNameEPS = strcat ( FUNCTION_NAME , ScaledValue ,'FunctionPlot_x1x4','.png');
print (hf7 ,'-dpng', figureNameEPS )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Effect of Variables
% W_ch
YLabel ='Max Volt [V]';
FUNCTION_NAME ='Eval_Multi_RBFNN_1_2016';
%close all
hf7 = figure (7) ;
iii =1; XLabel ='L[mm]'; XValues =0.5:0.5:4 ;
FixedValueX1_min =0.5; FixedValueX2_min =0.5; FixedValueX3_min =0.5;
FixedValueX1_mean =2.25; FixedValueX2_mean =2.25;FixedValueX3_mean =2.25;
FixedValuex1_max =4; FixedValuex2_max =4; FixedValuex3_max =4;
N_values = length ( XValues );
ZZ1 = zeros ( size ( XValues ));
ZZ2 = zeros ( size ( XValues ));
ZZ3 = zeros ( size ( XValues ));
UnScaled_Values = zeros ( size ( XValues ));
for jj =1: N_values
POINT =[ XValues(jj) FixedValueX2_min FixedValueX3_min];
ZZ1 (jj)=( feval ( FUNCTION_NAME , POINT ));
POINT =[ XValues(jj) FixedValueX2_mean FixedValueX3_mean ];
ZZ2 (jj)=( feval ( FUNCTION_NAME , POINT ));
POINT =[ XValues(jj) FixedValuex2_max FixedValuex3_max];
ZZ3 (jj)=( feval ( FUNCTION_NAME , POINT ));
UnScaled_Values(jj)= XValues (jj);
end
plot ( UnScaled_Values ,ZZ1 ,'-k',UnScaled_Values ,ZZ2,'--b',UnScaled_Values ,ZZ3 ,'-.r','LineWidth' ,2) ;
LG= legend ('Lower bound','Middle','Upper bound');
set (LG ,'FontSize',20,'Orientation','horizontal','Location','NorthOutside');
xlabel ( XLabel ,'FontSize',20);
ylabel ( YLabel ,'FontSize',20);
set (gca ,'FontSize', 18 )
figureNameEPS = strcat ( FUNCTION_NAME ,' _EffectPlot_x',num2str ( iii ),'.eps') ;
print (hf7 ,'-depsc','-r600', figureNameEPS );
%H_ch
%close all
hf7 = figure (7) ;
iii =2; XLabel ='Th [K]'; XValues =323:50:623 ;
N_values = length ( XValues );
ZZ1 = zeros ( size ( XValues ));
ZZ2 = zeros ( size ( XValues ));
ZZ3 = zeros ( size ( XValues ));
UnScaled_Values = zeros ( size ( XValues ));
for jj =1: N_values
POINT =[ FixedValueX2_min XValues(jj) FixedValueX3_min];
ZZ1 (jj)=( feval ( FUNCTION_NAME , POINT ));
POINT =[ FixedValueX2_mean XValues(jj) FixedValueX3_mean ];
ZZ2 (jj)=( feval ( FUNCTION_NAME , POINT ));
POINT =[ FixedValuex2_max XValues(jj) FixedValuex3_max];
ZZ3 (jj)=( feval ( FUNCTION_NAME , POINT ));
UnScaled_Values(jj)= XValues (jj);
end
plot ( UnScaled_Values ,ZZ1 ,'-k',UnScaled_Values ,ZZ2,'--b',UnScaled_Values ,ZZ3 ,'-.r','LineWidth' ,2) ;
LG= legend ('Lower bound','Middle','Upper bound');
set (LG ,'FontSize',20,'Orientation','horizontal','Location','NorthOutside');
xlabel ( XLabel ,'FontSize',20);
ylabel ( YLabel ,'FontSize',20);
set (gca ,'FontSize', 18 )
figureNameEPS = strcat ( FUNCTION_NAME ,' _EffectPlot_x',num2str ( iii ),'.eps') ;
print (hf7 ,'-depsc','-r600', figureNameEPS );
%N_ch
%close all
hf7 = figure (7) ;
iii =3; XLabel ='A [mm^2]'; XValues =0.2:0.2:2 ;
N_values = length ( XValues );
ZZ1 = zeros ( size ( XValues ));
ZZ2 = zeros ( size ( XValues ));
ZZ3 = zeros ( size ( XValues ));
UnScaled_Values = zeros ( size ( XValues ));
for jj =1: N_values
POINT =[ FixedValueX2_min FixedValueX3_min XValues(jj)];
ZZ1 (jj)=( feval ( FUNCTION_NAME , POINT ));
POINT =[ FixedValueX2_mean FixedValueX3_mean XValues(jj) ];
ZZ2 (jj)=( feval ( FUNCTION_NAME , POINT ));
POINT =[ FixedValuex2_max FixedValuex3_max XValues(jj)];
ZZ3 (jj)=( feval ( FUNCTION_NAME , POINT ));
UnScaled_Values(jj)= XValues (jj);
end
PLOT = plot ( UnScaled_Values ,ZZ1 ,'-k',UnScaled_Values ,ZZ2,'--b',UnScaled_Values ,ZZ3 ,'-.r','LineWidth' ,2) ;
LG= legend ('Lower bound','Middle','Upper bound');
set (LG ,'FontSize',20,'Orientation','horizontal','Location','NorthOutside');
xlabel ( XLabel ,'FontSize',20);
ylabel ( YLabel ,'FontSize',20);
set (gca ,'FontSize', 18 )
figureNameEPS = strcat ( FUNCTION_NAME ,' _EffectPlot_x',num2str ( iii ),'eps') ;
print (hf7 ,'-depsc','-r600', figureNameEPS );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Single optimization
close all ;
YLabel ='Max Volt [V]';
FUNCTION_NAME ='Eval_Multi_RBFNN_1_2016';
options = gaoptimset ;
options = gaoptimset(options,'PopulationSize', 300, 'PlotFcn', @gaplotbestf);
%options = gaoptimset(options,'MigrationDirection', 'both');
options = gaoptimset(options,'Generations', 1000);
options = gaoptimset(options,'SelectionFcn', {@selectiontournament});
options = gaoptimset(options,'CrossoverFcn', {@crossoverintermediate},'CrossoverFraction',0.8);
options = gaoptimset(options,'MutationFcn', {@mutationadaptfeasible}); % @mutationgaussian
%options = gaoptimset(options,'HybridFcn', { @fminsearch [] });
options = gaoptimset(options,'Display', 'off');
disp('Minimum');
disp(FUNCTION_NAME) ;
[Scaled_x_Single,fval,exitflag,output,population,scores] = ga(str2func(FUNCTION_NAME),3,[],[],[],[],[0.5 323 0.2],[4 623 2],[],[],options);
Unscaled_x_Single=Scaled_x_Single;
fprintf('\n %s optimum =%g, @ x1=%g,x2=%g,x3=%g \n',FUNCTION_NAME, (fval),Unscaled_x_Single(1),Unscaled_x_Single(2),Unscaled_x_Single(3));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function FX_RBFNN=Eval_RBFNN_2016(X)
global RBFNN_net Scales_4_input_values Scales_4_output_values ;
X=X';
SCALED_input_values = mapminmax('apply',X,Scales_4_input_values);
Simulated_SCALED_output=sim(RBFNN_net,SCALED_input_values);
FX_RBFNN = mapminmax('reverse',Simulated_SCALED_output,Scales_4_output_values) ;
FX_RBFNN=FX_RBFNN';
function FX_RBFNN=Eval_Multi_RBFNN_1_2016(X)
FX_RBFNN=Eval_RBFNN_2016(X);
FX_RBFNN=FX_RBFNN(:,1);
  1 Comment
Torsten
Torsten on 20 Jul 2021
I don't know where you have to make changes to the code you posted, but
max f(x)
is equivalent to
min -f(x)

Sign in to comment.

Answers (2)

Matt J
Matt J on 20 Jul 2021
Edited: Matt J on 20 Jul 2021
fun=str2func(FUNCTION_NAME);
fun=@(x) -fun(x);
[Scaled_x_Single,fval,exitflag,output,population,scores] = ga(,3,[],[],[],[],[0.5 323 0.2],[4 623 2],[],[],options);
fval=-fval;

Ahmed raafat
Ahmed raafat on 21 Jul 2021
update the last lines of main code
FUNCTION_NAME ='Eval_Multi_RBFNN_1_2016';
[Scaled_x_Single,fval,exitflag,output,population,scores] = ga(str2func(FUNCTION_NAME),3,[],[],[],[],[0.5 323 0.2],[4 623 2],[],[],options);
to @Matt J Lines

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by