File Exchange

image thumbnail

Grey Wolf Optimizer (GWO)

version 1.6 (1.85 MB) by Seyedali Mirjalili
GWO is a novel meta-heuristic algorithm for global optimization


Updated 22 May 2018

View Version History

View License

The GWO algorithm mimics the leadership hierarchy and hunting mechanism of grey wolves in nature. Four types of grey wolves such as alpha, beta, delta, and omega are employed for simulating the leadership hierarchy. In addition, three main steps of hunting, searching for prey, encircling prey, and attacking prey, are implemented to perform optimization.
This is the source codes of the paper: S. Mirjalili, S. M. Mirjalili, A. Lewis, Grey Wolf Optimizer, Advances in Engineering Software, Volume 69, March 2014, Pages 46-61, ISSN 0965-9978,
More information can be found in:
You can find the Grey Wolf optimizer Toolbox here:
Other relevant submissions:
I have a number of relevant courses in this area. You can enrol via the following links with 95% discount:

A course on “Optimization Problems and Algorithms: how to understand, formulation, and solve optimization problems”:

A course on “Introduction to Genetic Algorithms: Theory and Applications”

Cite As

Seyedali Mirjalili (2021). Grey Wolf Optimizer (GWO) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (48)

Sophia Owais

Hi can you please help me with the code for Optimal Power Flow for a 30 Bus system?
I have the data and finding some difficulty in interrelating,I would be thankful

Sophia Owais

aarthi arunagiri

hi sir,i can you please help me with the code for dynamic load dispatch for a simple 3 or 5 generator plant sir?
i got the data and finding some coding difficulty sir,I would be thankful

Flynn Moselly

Sir i wana Grey Wolf Optimization MATLAB CODE please


Gray wolf optimization technique matlab code
Email :

cy yang

Hi, how to set the optimal parameter selected by the gray wolf algorithm to an integer, because the input of the objective function is an integer. Thank you.

mahesh parimala

Dear sir, Can you please explain how to create search history plot which is given in the paper.

king wexia

i wonder if it can apply to the robot path planning, but i havent found the code yet

Surbhi Rai

Nguyen Thieu

Check out my github, I implemented this algorithm in Python (Numpy) for better performance.

abrha hiluf

hey my dear how can i use the GWO for micro grid optimization please if you can send the source code than for your support

Wenhao Che

aayush shrivastava

how to put constraint in this program

Ratheesh P M

Hi Sir,
Can we apply GWO in Image fusion (Frequency dmain), If so how to tailor?

seven z


Why is the result of Best_pos different every time I run the program? I am looking forward to your reply!

seven z


Why is the result of Best_pos different every time I run the program? I am looking forward to your reply!

Muhammad Ansari

clear all

SearchAgents_no=20; % Number of search agents

func_name='DG1'; % Name of the test function that can be from F1 to F23 (Table 1,2,3 in the paper)

Max_iteration=100; % Maximum number of iterations

% Load details of the selected benchmark function


figure('Position',[500 500 660 290])
%Draw search space
% func_plot(func_name);
title('Parameter space')
zlabel([func_name,'( x_1 , x_2 )'])
% %Draw objective space
% subplot(1,2,2);
% semilogy(GWO_cg_curve,'Color','r')
% title('Objective space')
% xlabel('Iteration');
% ylabel('Best score obtained so far');

% axis tight
% grid on
% box on
% legend('GWO')

display(['The best solution obtained by GWO is : ', num2str(Best_pos)]);
display(['The best optimal value of the objective funciton found by GWO is : ', num2str(Best_score,'%10.9e\n')]);
it's error line # 47 can anybody tell me

Muhammad Ansari

I'm working in IEEE-14,33 & 69 bus system and i want apply in GWO can anybody tell me how can put values GWO in mat-lab

Abhishek Gupta

check out hybrid GWO & PSO code at for better performance

Taief Alaa

Hello sir,
please, I would like to use GWo to select the optimum RBF (sigma,c) parameter for the SVM classifier.
but I don't know how to change the code to solve my problem.

please, can you give me any hints or the code?
my regards.

hello all
i have applied this GWO technique in LFC problem.
iam geeting the error as
??? Subscript indices must either be real positive integers or logicals.

Error in ==> GWO at 50
can any one resolve my error so that it will be helpful for me

maria tamoor

I used GWO but I want to add constaint that sum of all variables sgould be one ...I am not getting how to implement constraint in GWO?

Hi Seyedali, can you apply GWO on offshore structure optimization? If so, how do you tailor it?

Immad Shams

Dear the GWO main matlab file doesnt take fobj. what is that? is it 'F1','F2' .....?

eric githua

eric githua

Ekhlad Altaii

I want the code for the Grey Wolves algorithm written in a language MATLAB , please.

Kamalova Albina

syed ali

Sir how to input objective function

mohamed abu eita

how can we compute the x_p of prey in GWO. as well as, what the role of x_p in GWO.

Ajay Bhardwaj

hello seyedali sir, thanks for the code, i want to incorporate equality constraint ,for an example, if demand pd=100 MW, generation should be 100 MW, can you kindly provide solution to this problem.Thanks sir, i will wait for your reply.

Ajay Bhardwaj

wallace liu

enas malkawi

hi sir, can this GWO solve Economic Dispatch problem? are thier any matlab code for this??


hello sir, first of all, congratulations! For this awesome technique. One question: when i use my objective function in this code. The global best score does not changes with iterations. For eg, if i have taken 100 iterations the value at 1st iteration, in between iterations and last iteration remain same. Any suggestions sir?

marwa marwan

hi sir
the source code is not updated yet
please I need it as soon as possible

dalia elkashef

Hi . Sir

i want to ask you about code of GWO-EPD?


hi sir, can this GWO solve Economic Load Dispatch problem?

Seyedali Mirjalili

Hi Amr Hessan,

My thanks for identifying this issue. You are right. Every time alpha updates, we have to update beta and delta and well. I will consider this and update the files soon.

Amr Hassan

Hi, Thanks for your code. good job
just have one concern. and please correct me if I was wrong. when updating Alpha, Beta and Delta Positions and score
fitness=fobj(Positions(i,:)); %Amr: best_tour = calculate tour length in tsp
% Update Alpha, Beta, and Delta
if fitness<Alpha_score
Alpha_score=fitness; % Update alpha
if fitness>Alpha_score && fitness<Beta_score
Beta_score=fitness; % Update beta
if fitness>Alpha_score && fitness>Beta_score && fitness<Delta_score
Delta_score=fitness; % Update delta
this doesn't correctly update Beta and Delta. consider this scenario:
for example, after 3rd iteration alpha_score=1000, Beta_score=1200, Delta_score=1500
if the fitness of 4th iteration is 800 then alpha_score will be updated and beta_score will remain 1200 while it should be 1000 and the same for delta_score
proposed correction,

fitness=fobj(Positions(i,:)); %Amr: best_tour = calculate tour length in tsp
% Update Alpha, Beta, and Delta
if fitness<Alpha_score
Alpha_score=fitness; % Update alpha
if fitness>Alpha_score && fitness<Beta_score
Beta_score=fitness; % Update beta
if fitness>Alpha_score && fitness>Beta_score && fitness<Delta_score
Delta_score=fitness; % Update delta


Ander Biguri

An heuritic algorthm based on Grey wolf behavior. WOW. Just WOW.

Seyedali Mirjalili

Hi Dalia,

This problem originates from your F5 function.

o=(100*(x(2:dim)-(x(1:dim-1).^2)).^2+(1-x(1:dim-1)).^2) returns a vector, while there should be a single fitness value for each search agent.
The formula for the F5 function has a sum in front of the expression as follows:

Generally speaking, each search agent should be assigned with only one fitness value.

Change your F5 function so that it returns a single value for each input vector, give it a try, and let me know.


dalia elkashef

i try to solve this eq
function o = F5(x)

case 'F5'
fobj = @F5;
give me error

Operands to the || and && operators must be convertible to logical scalar values.

Error in GWO (line 51)
if fitness>Alpha_score && fitness<Beta_score

Error in main (line 45)
pls help me

Seyedali Mirjalili


You are right, some search agents may go beyond the boundaries of the search space in the last iteration and there is no more chance for them to be returned back to the search space. I moved position boundary checking to the end of the second for loop (right after updating the positions). I have also updated the source file accordingly.

Thanks for that and please let me know if there was any other issue.


Ali, when you enter the loop for a 1st run, your data is in the bounds apriori (because it is just generated). And when you did the _last_ loop iteration you may have some search agents not in the bounds, so when you look in the final Positions, you may find incorrect data.
I found this when I tried to work with search agents with integer values: my bounds were 1..10, but after the last loop iteration I got few agents with 0's. Maybe for real values it will be not so significant.

Seyedali Mirjalili

Hi Daniil,

Thanks for your compliment and comment. I think the current codes work correctly because it is important to return back the search agents that go beyond the boundaries when you want to calculate their "fitnesses". I agree with you that we can do this at the end of the second for loop where the positions are updated or at the top of the firs for loop. However, the important issue here is that the boundary checking should be done right before updating the fitnesses. I hope these make sense. Once again, thanks for your vigilance and pointing out this matter.



Hi. Great optimizer. Thank you.
I found one issue: we need to do "Return back the particles that go beyond the boundaries of the search" at the bottom of "for" loop, but not at the top.

MATLAB Release Compatibility
Created with R2011b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!