problem in my programme

1 view (last 30 days)
MABHU JANI SHAIK
MABHU JANI SHAIK on 11 May 2022
Answered: Jan on 11 May 2022
I objective is to find out the minimization of cost. f=a.*p.^2+b.*p+c, where a,b and c are the three cost coefficients. For my objective function I have written two programmes, one is rao.m another is costf.m, which is function file. help me to over come errors as soon as possible. thank you. my email id is mabhujani.rs@andhrauniversity.edu.in
rao.m is a main programme
fun=@costf;
lb=[20 20 100 100 100 100 100 100 20 20 25 60 25 25 55 55 55 55 55 55];
ub=[130 130 460 465 160 455 455 470 80 80 85 300 162 162 55 55 55 55 55 55];
%% intialize Rao Parameters
N=10; %population size
D=20; %no.of variables
itermax=100;
%%generating the initial population
for i=1:N
for j=1:D
pos(i,j)=lb(j)+rand.*(ub(j)-lb(j));
end
end
%%Evaluate objective function
for p=1:N
f(p)=fun(pos(p,:)); error
end
fx=f(p)
[fminvalue,ind]=min(fx);
gbest=pos(ind,:);
%% Rao Main Loop Start
iter=1;
while iter<=itermax
Fgbest=fminvalue;
[minbest,minind]=min(fx);
xbest=pos(minind,:);
[worstbest,worstind]=max(fx);
xworst=pos(worstind,:);
for i=1:N
x=pos(i,:)
xnew=x+rand(1,D).*(xbest-xworst);
%%check bounds
xnew=max(xnew,lb);
xnew=min(xnew,ub);
fnew=fun(xnew);
%%greedy selection
if fnew<fx(i)
pos(i,:)=xnew
fx(i,:)=fnew
end
end
%%update GBEST
[fmin,find]=min(fx);
if fmin<Fgbest
Fgbest=fmin;
gbest=pos(find,:)
end
%%Memorise the best
[optval,optind]=min(fx)
bestFx(iter)=optval
bestx(iter,:)=pos(optind,:)
%%plotting the result
plot(bestFx,'linewidth',2)
xlabel('iteration number')
ylabel('fitness value')
title('convergence vs iteration')
grid on
iter=iter+1
end
objective function programme
function f=costf(a,b,c,x)
a=[0.00211 0.00211 0.00063 0.00078 0.00254 0.00048 0.00031 0.00043 0.00712 0.10908 0.00079 0.0007 0.00171 0.00398 0.00413 0.00951 0.00173 0.00214 0.00209 0.00222];
b=[16.51 16.51 21.05 21.04 22.68 16.19 17.26 21.6 22.26 19.58 27.74 23.9 23.71 19.7 25.92 22.54 27.79 27.91 28.12 27.27];
c=[702.7 702.7 1313.6 1168.1 372.2 1078.8 969.8 958.2 371 455.6 476.6 471.6 367.5 445.4 660.8 692.4 644.5 661.2 650.7 665.8];
f=sum(a.*x.^2+b.*x+c)
pd=3000;
g=pd-f<=0
pp=10^9;
for i=1:size(g,1)
for j=1:size(g,2)
if g(i,j)>0
penalty(i,j)=pp.*g(i,j);
else
penalty(i,j)=0;
end
end
end
out=fx+sum(penalty,2);
end
  1 Comment
Jan
Jan on 11 May 2022
Edited: Jan on 11 May 2022
Please use the tools on top of the field for editing to format your code. This improves the readability.
It is useful, if you mention, which problems you have with the code. Which error message do you get?
Publishing email addresses is the forum is a bad idea, because this increases the amounbt of spam you will receive. Private discussions by email do not support the nature of this forum also, wher help is offered for free and such, that all others can profit also.
The expression "as soon as possible" is considered as impolite. The help is offered for free by volunteers. Your problem is not more urgent than others. Thanks.
By the way, you can simplify:
pp=10^9;
for i=1:size(g,1)
for j=1:size(g,2)
if g(i,j)>0
penalty(i,j)=pp.*g(i,j);
else
penalty(i,j)=0;
end
end
end
to
penalty = 1e9 * g;

Sign in to comment.

Answers (1)

Jan
Jan on 11 May 2022
I guess, the problem is hidden here:
function f=costf(a,b,c,x)
...
f=sum(a.*x.^2+b.*x+c)
...
out=fx+sum(penalty,2); % What is fx?!
% "out" is not used anywhere, but f is replied?!
end

Categories

Find more on Simulink in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!