Completed
Push — master ( 602eea...026eaa )
by
unknown
04:14 queued 01:18
created

Cuckoo.__eq__()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 2
Ratio 100 %

Importance

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

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

Loading history...
121
                MovedNests[_i].Solution = newSolution
122
                MovedNests[_i].simpleBound()
123
                self.tryEval(MovedNests[_i])
124
        return MovedNests
125
126
    def run(self):
127
        self.initNests()
128
        self.evalNests()
129
        self.FEs += self.Np
130
        while not self.Done:
131
            MovedNests = self.moveNests(self.Nests)
132
            self.Nests = self.resetNests(MovedNests)
133
134
        return self.gBest.Fitness
135