0% found this document useful (0 votes)
5 views4 pages

GWO Optimization for Objective Function

The document presents a Python implementation of the Grey Wolf Optimizer (GWO) algorithm to minimize a specified objective function. It initializes wolf positions, iteratively updates them based on fitness scores, and tracks the best solution found. The results are visualized through fitness vs iteration plots and contour plots of the objective function with the optimal solution marked.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

GWO Optimization for Objective Function

The document presents a Python implementation of the Grey Wolf Optimizer (GWO) algorithm to minimize a specified objective function. It initializes wolf positions, iteratively updates them based on fitness scores, and tracks the best solution found. The results are visualized through fitness vs iteration plots and contour plots of the objective function with the optimal solution marked.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import numpy as np

import [Link] as plt

# Objective function

def objective_function(x):

x1, x2 = x

return x1**2 - x1*x2 + x2**2 + 2*x1 + 4*x2 + 3

# GWO parameters

lb = -5

ub = 5

dim = 2

n_wolves = 10

max_iter = 500

# Initialize wolf positions

positions = [Link](lb, ub, (n_wolves, dim))

# Initialize Alpha, Beta, Delta

alpha_pos = [Link](dim)

beta_pos = [Link](dim)

delta_pos = [Link](dim)

alpha_score = [Link]

beta_score = [Link]

delta_score = [Link]

#best fitness per iteration

fitness_history = []

# Main GWO loop

for t in range(max_iter):

for i in range(n_wolves):

fitness = objective_function(positions[i])
if fitness < alpha_score:

delta_score, delta_pos = beta_score, beta_pos.copy()

beta_score, beta_pos = alpha_score, alpha_pos.copy()

alpha_score, alpha_pos = fitness, positions[i].copy()

elif fitness < beta_score:

delta_score, delta_pos = beta_score, beta_pos.copy()

beta_score, beta_pos = fitness, positions[i].copy()

elif fitness < delta_score:

delta_score, delta_pos = fitness, positions[i].copy()

#best fitness of this iteration

fitness_history.append(alpha_score)

a = 2 - t * (2 / max_iter)

for i in range(n_wolves):

for j in range(dim):

r1, r2 = [Link](), [Link]()

A1 = 2 * a * r1 - a

C1 = 2 * r2

D_alpha = abs(C1 * alpha_pos[j] - positions[i, j])

X1 = alpha_pos[j] - A1 * D_alpha

r1, r2 = [Link](), [Link]()

A2 = 2 * a * r1 - a

C2 = 2 * r2
D_beta = abs(C2 * beta_pos[j] - positions[i, j])

X2 = beta_pos[j] - A2 * D_beta

r1, r2 = [Link](), [Link]()

A3 = 2 * a * r1 - a

C3 = 2 * r2

D_delta = abs(C3 * delta_pos[j] - positions[i, j])

X3 = delta_pos[j] - A3 * D_delta

positions[i, j] = (X1 + X2 + X3) / 3

positions[i] = [Link](positions[i], lb, ub)

# results

print("Optimal solution (x1, x2):", alpha_pos)

print("Minimum objective value:", alpha_score)

#FITNESS VS ITERATION PLOT

[Link]()

[Link](fitness_history)

[Link]("Iteration")

[Link]("Best Fitness Value")

[Link]("GWO Convergence Curve (Fitness vs Iteration)")

[Link](True)

[Link]()

# CONTOUR PLOT

x = [Link](lb, ub, 200)

y = [Link](lb, ub, 200)

X, Y = [Link](x, y)
Z = X**2 - X*Y + Y**2 + 2*X + 4*Y + 3

[Link]()

[Link](X, Y, Z, levels=30)

[Link](alpha_pos[0], alpha_pos[1], marker='x')

[Link]("Objective Function with GWO Optimum")

[Link]("x1")

[Link]("x2")

[Link]()

OUTPUT VALUES

FITNESS VALUE VS ITERATION CURVE

You might also like