Passed
Pull Request — master (#18)
by
unknown
56s
created

GreyWolfOptimizer.init()   A

Complexity

Conditions 3

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 3
1
"""Grey wolf optimizer.
2
3
Date: 11.2.2018
4
5
Author : Iztok Fister Jr.
6
7
License: MIT
8
9
Reference paper: Mirjalili, Seyedali, Seyed Mohammad Mirjalili, and Andrew Lewis.
10
"Grey wolf optimizer." Advances in engineering software 69 (2014): 46-61.
11
#& Grey Wold Optimizer (GWO) source codes version 1.0 (MATLAB)
12
13
TODO: Validation must be conducted! More tests are required!
14
"""
15
16
import random
17
18
__all__ = ['GreyWolfOptimizer']
19
20
21
class GreyWolfOptimizer(object):
22
    # pylint: disable=too-many-instance-attributes
23
24 View Code Duplication
    def __init__(self, D, NP, nFES, Lower, Upper, function):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
25
        self.D = D  # dimension of the problem
26
        self.NP = NP  # population size; number of search agents
27
        self.nFES = nFES  # number of function evaluations
28
        self.Lower = Lower  # lower bound
29
        self.Upper = Upper  # upper bound
30
        self.Fun = function
31
32
        self.Positions = [[0 for _i in range(self.D)]  # positions of search agents
33
                          for _j in range(self.NP)]
34
35
        self.evaluations = 0  # evaluations counter
36
37
        # TODO: "-inf" is in the case of maximization problems
38
        self.Alpha_pos = [0] * self.D  # init of alpha
39
        self.Alpha_score = float("inf")
40
41
        self.Beta_pos = [0] * self.D  # init of beta
42
        self.Beta_score = float("inf")
43
44
        self.Delta_pos = [0] * self.D  # init of delta
45
        self.Delta_score = float("inf")
46
47
    def init(self):
48
        # initialization of positions
49
        for i in range(self.NP):
50
            for j in range(self.D):
51
                self.Positions[i][j] = random.random(
52
                ) * (self.Upper - self.Lower) + self.Lower
53
54
    def bounds(self, position):
55
        for i in range(self.D):
56
            if position[i] < self.Lower:
57
                position[i] = self.Lower
58
            if position[i] > self.Upper:
59
                position[i] = self.Upper
60
        return position
61
62
    def move(self):
0 ignored issues
show
Comprehensibility introduced by
This function exceeds the maximum number of variables (20/15).
Loading history...
63
64
        self.init()
65
66
        while True:
67
            if self.evaluations == self.nFES:
68
                break
69
70
            for i in range(self.NP):
71
                self.Positions[i] = self.bounds(self.Positions[i])
72
73
                Fit = Fun(self.D, self.Positions[i])
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'Fun'
Loading history...
74
                self.evaluations = self.evaluations + 1
75
76
                if Fit < self.Alpha_score:
77
                    self.Alpha_score = Fit
78
                    self.Alpha_pos = self.Positions[i]
79
80
                if ((Fit > self.Alpha_score) and (Fit < self.Beta_score)):
81
                    self.Beta_score = Fit
82
                    self.Beta_pos = self.Positions[i]
83
84
                if ((Fit > self.Alpha_score) and (Fit > self.Beta_score) and (Fit < self.Delta_score)):
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (103/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
85
                    self.Delta_score = Fit
86
                    self.Delta_pos = self.Positions[i]
87
88
            a = 2 - self.evaluations * ((2) / self.nFES)
89
90
            for i in range(self.NP):
91
                for j in range(self.D):
92
93
                    r1 = random.random()
94
                    r2 = random.random()
95
96
                    A1 = 2 * a * r1 - a
97
                    C1 = 2 * r2
98
99
                    D_alpha = abs(
100
                        C1 * self.Alpha_pos[j] - self.Positions[i][j])
101
                    X1 = self.Alpha_pos[j] - A1 * D_alpha
102
103
                    r1 = random.random()
104
                    r2 = random.random()
105
106
                    A2 = 2 * a * r1 - a
107
                    C2 = 2 * r2
108
109
                    D_beta = abs(C2 * self.Beta_pos[j] - self.Positions[i][j])
110
                    X2 = self.Beta_pos[j] - A2 * D_beta
111
112
                    r1 = random.random()
113
                    r2 = random.random()
114
115
                    A3 = 2 * a * r1 - a
116
                    C3 = 2 * r2
117
118
                    D_delta = abs(
119
                        C3 * self.Delta_pos[j] - self.Positions[i][j])
120
                    X3 = self.Delta_pos[j] - A3 * D_delta
121
122
                    self.Positions[i][j] = (X1 + X2 + X3) / 3
123
124
        return self.Alpha_score
125