why fminimax lost the point i want to optimize but patternsearch doesnt?

3 views (last 30 days)
i want to use fminimax because i like to have quantize variable so i like to use it but it try to optimize another point(zero) i think cost func should be correct because patternsearch work almost correct
this is cost func i want to implement
% close all
clear all
t0 = tic();
elementNumb=32;
%%%%%%%%%%%
num_iterations = 1; % Adjust this as needed
%
for iteratt = 1:num_iterations
%% Generate random values for w1, w2, w3, w4, w5 within the specified range
global w
w=ones(1,32);
w(1:10) = randi([1, 10]) * rand(1);
w(11:18) = randi([1, 10]) * rand(1);
w(19) = randi([1, 10]) * rand(1);
w(20) = randi([1, 10]) * rand(1);
w(21) = randi([1, 10]) * rand(1);
w(22) = randi([1, 10]) * rand(1);
w(23) = randi([1, 10]) * rand(1);
w(24) = randi([1, 10]) * rand(1);
w(25:32) = randi([1, 10]) * rand(1);
% Set the random values as weights
w_values = w;
%%%%%%%%%%%%
%%
random_numbersw = ones(1,elementNumb);
ubb=1;
lbb=-1;
random_numberst =-0.23889*(ones(1 , elementNumb));
x0 = [ random_numbersw,random_numberst ] ;
ub = zeros(1, elementNumb*2); % Initialize a matrix of zeros with 40 elements
ub(1:elementNumb) = 1; % Set values from 1 to 21 to 0
ub(elementNumb+1:elementNumb*2) = ubb; % Set values from 22 to 40 to 90
lb = zeros(1, elementNumb*2); % Initialize a matrix of zeros with 40 elements
lb(1:elementNumb) = 0.03125; % Set values from 1 to 21 to 0
lb(elementNumb+1:elementNumb*2) = lbb; % Set values from 22 to 40 to 90
%% Algoritm
%% PSO
hybridopts = optimoptions('patternsearch','Display', 'iter','MaxIterations',1e5);
options = optimoptions('particleswarm','SwarmSize',150,'HybridFcn',{'patternsearch',hybridopts},...
'Display', 'iter','InitialSwarmMatrix' , x0,'MaxIterations',1e5);
x = particleswarm(@costy,length(x0),lb,ub,options);
%%
%% fminimax
% options = optimoptions(@fminimax,'FunctionTolerance',1e-12, ...
% 'MaxIterations',9e3,'OptimalityTolerance',1e-40,'MaxFunctionEvaluations',2e5 );
% [x,fval,maxfval,exitflag,output,lambda] = fminimax(@costy,x0,[],[],[],[],lb,ub,[],options);
%
%%
output_filename = sprintf('output_data_iteration%d.mat', iteratt);
% save(output_filename, 'x', 'fval', 'maxfval', 'exitflag', 'output', 'lambda', 'w_values');
save(output_filename, 'x', 'w_values');
%%after optimize finish get me result
elementNumb=32;
deltaTeta=x(1:elementNumb*2);
f=9.5*10^9;
j=sqrt(-1);
l=(3*10^8)/f;
k=(2*pi)/l;
d=0.7*l;
teta=((-1*87.6):0.1:(1*84));
y=0;
% for h=1:elementNumb
% y=y+deltaTeta(h)*exp(j*(h-1)*(k*d*sind(teta+180*deltaTeta(h+elementNumb))));
% end
g = zeros(elementNumb, length(teta));
for h=1:elementNumb
for iteta=1:length(teta)
g(h,iteta)=g(h,iteta)+deltaTeta(h)*exp(j*(h-1)*(k*d*sind(teta(iteta)+180*deltaTeta(h+elementNumb))));
end
end
bb = abs(sum(g));
figure;
%
plot(teta,20*log10(abs(bb)),'k')
%
axis([-100 100 -60 40]);
end
dt = toc(t0);
beep;
beep;
%% my main function
function y = costy(x)
global w;
freq = 9.5*(10^9); %Freq
j = sqrt(-1); %Define Imaginary
l =(3*(10^8))/freq; %Lambda
k = (2*pi)/l; %Constant
d = 0.7*l; %Distant of each element
elementNumb = 32;
step = 0.1;
% teta=((-1*87.6):step:(1*85));
teta=((-1*36.2):step:(1*72.4));
ww=180;
g = zeros(elementNumb, length(teta));
for h=1:elementNumb
for iteta=1:length(teta)
g(h,iteta)=g(h,iteta)+x(h)*exp(j*(h-1)*(k*d*sind(teta(iteta)+ww*x(h+elementNumb))));
end
end
bb = abs(sum(g));
[bbmax,bddmax]=max(bb);
bb=bb/(bbmax);
bb=20*log10(abs(bb));
% t= [ -87.6000000000000 -83.5000000000000 -79 -73.8000000000000 -67.4000000000000 -57.8000000000000 -36.2000000000000 -26.6000000000000 -20.2000000000000 -15 -10.5000000000000 -6.40000000000001 -2.59999999999999 1 4.30000000000000 7.50000000000000 10.6000000000000 13.6000000000000 16.5000000000000 19.3000000000000 22.1000000000000 24.8000000000000 27.5000000000000 30.1000000000000 32.7000000000000 35.3000000000000 37.9000000000000 40.4000000000000 45.6000000000000 48.1000000000000 50.7000000000000 53.3000000000000 55.9000000000000 58.5000000000000 61.2000000000000 63.9000000000000 66.7000000000000 69.5000000000000 72.4000000000000 75.4000000000000 78.5000000000000 81.7000000000000 85];
t= [ -36.2000000000000 -26.6000000000000 -20.2000000000000 -15 -10.5000000000000 -6.40000000000001 -2.59999999999999 1 4.30000000000000 7.50000000000000 10.6000000000000 13.6000000000000 16.5000000000000 19.3000000000000 22.1000000000000 24.8000000000000 27.5000000000000 30.1000000000000 32.7000000000000 35.3000000000000 37.9000000000000 40.4000000000000 45.6000000000000 48.1000000000000 50.7000000000000 53.3000000000000 55.9000000000000 58.5000000000000 61.2000000000000 63.9000000000000 66.7000000000000 69.5000000000000 72.4000000000000 ];
%i dont know ehy it happens but when my function is zero it shoul change
%the x
for opi=1:length(t)
if t(opi)<0.001 && t(opi)>-0.001
t(opi)=0;
end
end
% Initialize a counter
tCount = length(t);
% length of each interval
lentt = cell(1, tCount-1);
for iou = 1:tCount-1
if iou == 1
lentt{iou} =length( t(iou) : step : t(iou + 1));
else
lentt{iou} = length(t(iou)+ step : step : t(iou + 1));
end
end
lentt{13}=lentt{13}+1;
lentt{17}=lentt{17}+1;
% value of each inervals get lable
for uiu=1:tCount-1
if uiu==1
lenttt(uiu)=(lentt{uiu});
else
lenttt(uiu)=lenttt(uiu-1)+lentt{uiu};
end
end
% Different intervals for optimization
cv = cell(1, tCount-1); %there are cv for each bb
for jjj=1:tCount-1
if jjj==1
cv{jjj} = bb(1:(lenttt(jjj)))*1;
else
cv{jjj} = bb(lenttt(jjj-1)+1:(lenttt(jjj)))*1;
end
end
sll=[-28.7693818430320 -29.2269260908810 -29.5817391344837 -29.8386379476817 -30.0092809774479 -30.0986002759175 -30.0933861813434 -30.0130434044565 -29.8472703208587 -29.5796408403258 -29.2280789837052 -28.7750639926713 -28.2010451569390 -27.4986515399832 -26.6454094872800 -25.6022425933528 -24.3178386609530 -22.7110953803777 -20.6212057739930 -17.7459944773407 -13.2421565888999 0 -13.2421565888999 -17.7459944773407 -20.6212057739930 -22.7110953803777 -24.3178386609530 -25.6022425933529 -26.6454094872800 -27.4986515399832 -28.2010451569390 -28.7750639926713 ];
sll(1:9)=sll(1:9)+1;
sll(10:18)=sll(10:18)-3;
sll(19:21)=sll(19:21)-2;
sll(22:25)=sll(22:25)-1;
sll(26:29)=sll(26:29)-2;
sll(30:32)=sll(30:32)-2;
sll(22)=0;
% %%%Keng Algorithm
maximo=22;
steppyup=2;
steppydown=2;
D1 = 0 ;
D2 = 0;
D3 = 0;
for lll=1:tCount-1
bt=cv{lll};
%% this function is main fuction which i used in another file
if lll==maximo
for ppp=(1+steppydown):(length(bt)-steppyup)
if sll(lll)>bt(ppp) %decrease max
D2 = D2 + sll(lll) - bt(ppp);
end
end
D2=w(lll)*D2;
else
for ppp=1:length(bt)
if bt(ppp)>sll(lll)
D1 = D1 + bt(ppp)-sll(lll);
end
end
D3=w(lll)*D1+D3;
D1=0;
end
end
a1 = 1; %0.4;
a2 = 1; %0.3;
a3 = 1; % 0.4;
y=a1*D3+a2*D2;
%%
end

Answers (1)

Ronit
Ronit on 24 Sep 2024
Hello Koorosh,
There could be several reasons why you are encountering issues while using the "fminimax" function for optimization such as the setup of the cost function, initial conditions, or constraints.
I would suggest you to adjust the options for "fminimax". You may need to experiment with parameters like "MaxIterations", "FunctionTolerance" , and "OptimalityTolerance". Sometimes, more iterations or a different tolerance can lead to better results.
options = optimoptions('fminimax', ...
'Display', 'iter', ...
'FunctionTolerance', 1e-6, ...
'MaxIterations', 1000, ...
'OptimalityTolerance', 1e-6, ...
'MaxFunctionEvaluations', 5000);
Utilizing a code similar to the one mentioned above may assist in achieving better results. In this manner, "fminimax" is able to locate a point locally that satisfies the constraints.
I hope it helps with your query!

Community Treasure Hunt

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

Start Hunting!