Passed
Push — master ( a84c75...60777c )
by
unknown
01:26
created

Cuckoo.simpleBound()   A

Complexity

Conditions 4

Size

Total Lines 6

Duplication

Lines 6
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
c 1
b 0
f 1
dl 6
loc 6
rs 9.2
1
import random as rnd
2
import copy
3
import numpy as npx
4
5
__all__ = ['CuckooSearchAlgorithm']
6
7
8
class Cuckoo(object):
9
    """Defines cuckoo for population."""
10
11
    def __init__(self, D, LB, UB):
12
        self.D = D  # dimension of the problem
13
        self.LB = LB  # lower bound
14
        self.UB = UB  # upper bound
15
        self.Solution = []
16
17
        self.Fitness = float('inf')
18
        self.generateCuckoo()
19
20
    def generateCuckoo(self):
21 View Code Duplication
        self.Solution = [self.LB + (self.UB - self.LB) * rnd.random()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
22
                         for _i in range(self.D)]
23
24
    def evaluate(self):
25
        self.Fitness = Cuckoo.FuncEval(self.D, self.Solution)
26
27
    def simpleBound(self):
28
        for i in range(self.D):
29
            if self.Solution[i] < self.LB:
30
                self.Solution[i] = self.LB
31
            if self.Solution[i] > self.UB:
32
                self.Solution[i] = self.UB
33
34
    def toString(self):
35
        pass
36
37
    def __eq__(self, other):
38
        return self.Solution == other.Solution and self.Fitness == other.Fitness
39
40
41
class CuckooSearchAlgorithm(object):
42
    """Cuckoo Search algorithm.
43
44
    Date: 12. 2. 2018
45
46
    Authors : Uros Mlakar
47
48
    License: MIT
49
50
    Reference paper: Yang, Xin-She, and Suash Deb. "Cuckoo search via Lévy flights." 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
51
    Nature & Biologically Inspired Computing, 2009. NaBIC 2009. 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
52
53
    TODO: Tests and validation!
54
    """
55 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
56
    def __init__(self, Np, D, nFES, Pa, Alpha, Lower, Upper, function):
57
        self.Np = Np
58
        self.D = D
59
        self.Pa = Pa
60
        self.Lower = Lower
61
        self.Upper = Upper
62
        self.Nests = []
63
        self.nFES = nFES
64
        self.FEs = 0
65
        self.Done = False
66
        self.Alpha = Alpha
67
        self.Beta = 1.5
68
        Cuckoo.FuncEval = staticmethod(function)
69
70
        self.gBest = Cuckoo(self.D, self.Lower, self.Upper)
71
72
    def evalNests(self):
73
        for c in self.Nests:
74
            c.evaluate()
75
            if c.Fitness < self.gBest.Fitness:
76
                self.gBest = copy.deepcopy(c)
77
78
    def initNests(self):
79
        for _i in range(self.Np):
80
            self.Nests.append(Cuckoo(self.D, self.Lower, self.Upper))
81
82
    def levyFlight(self, c):
83
        sigma = 0.6966
84
        u = npx.random.randn(1, self.D) * sigma
85
        v = npx.random.randn(1, self.D)
86
        step = u / (abs(v)**(1 / self.Beta))
87
        stepsize = self.Alpha * step * (npx.array(c.Solution) - npx.array(self.gBest.Solution)).flatten().tolist()
88
89
        c.Solution = (npx.array(c.Solution) + npx.array(stepsize) * npx.random.randn(1, self.D)).flatten().tolist()
90
91
    def tryEval(self, c):
92
        if self.FEs <= self.nFES:
93
            c.evaluate()
94
            self.FEs += 1
95
        else:
96
            self.Done = True
97
98
    def moveNests(self, Nests):
99
        MovedNests = []
100
        for c in Nests:
101
            self.levyFlight(c)
102
            c.simpleBound()
103
            self.tryEval(c)
104
105
            if c.Fitness < self.gBest.Fitness:
106
                self.gBest = copy.deepcopy(c)
107
108
            MovedNests.append(c)
109
        return MovedNests
110
111
    def resetNests(self, MovedNests):
112
        for _i in range(self.Np):
113
            if rnd.random() < self.Pa:
114
                m = rnd.randint(0, self.Np - 1)
115
                n = rnd.randint(0, self.Np - 1)
116
117
                newSolution = npx.array(MovedNests[_i].Solution) + (
118
                    rnd.random() * (npx.array(MovedNests[m].Solution) - npx.array(MovedNests[n].Solution))).flatten().tolist()
119
                MovedNests[_i].Solution = newSolution
120
                MovedNests[_i].simpleBound()
121
                self.tryEval(MovedNests[_i])
122
        return MovedNests
123
124
    def run(self):
125
        self.initNests()
126
        self.evalNests()
127
        self.FEs += self.Np
128
        while not self.Done:
129
            MovedNests = self.moveNests(self.Nests)
130
            self.Nests = self.resetNests(MovedNests)
131
132
        return self.gBest.Fitness
133