Ejemplo n.º 1
0
    def __init__(self,range1, nrParticles,maxIterations, chosenFunction):
        self.chosenFunction = chosenFunction
        self.range = range1
        self.maxIterations = maxIterations
        self.hive = []
        self.alpha = 0.72
        self.T = 100

        for i in range(0,nrParticles,1):
            x = np.random.uniform(low= -self.range, high = self.range)
            y = np.random.uniform(low= -self.range, high = self.range)
            fitness = returnChosenFunction(x, y,self.chosenFunction)
            self.hive.append(Particle(x, y, fitness))
Ejemplo n.º 2
0
    def __init__(self, range1, nrVectors, maxIterations, chosenFunction):
        self.chosenFunction = chosenFunction
        self.maxIterations = maxIterations
        self.range = range1
        self.nrVectors = nrVectors
        self.hive = []
        self.C = 0.5
        self.F = 0.8

        for i in range(0, nrVectors, 1):
            x = np.random.uniform(low=-self.range, high=self.range)
            y = np.random.uniform(low=-self.range, high=self.range)
            z = returnChosenFunction(x, y, self.chosenFunction)
            self.hive.append(Vector3D(x=x, y=y, fitness=z, whichConstructor=0))
    def moveStar(self):
        for i in range(0, self.nrStars, 1):
            if (((self.hive[i].x + self.hive[i].velocity.x) < -self.range) or
                ((self.hive[i].x + self.hive[i].velocity.x) > self.range)):
                self.hive[i].x = np.random.uniform(low=-self.range,
                                                   high=self.range)
            else:
                self.hive[i].x += self.hive[i].velocity.x

            if (((self.hive[i].y + self.hive[i].velocity.y) < -self.range) or
                ((self.hive[i].y + self.hive[i].velocity.y) > self.range)):
                self.hive[i].y = np.random.uniform(low=-self.range,
                                                   high=self.range)
            else:
                self.hive[i].y += self.hive[i].velocity.y

            self.hive[i].fitness = -returnChosenFunction(
                self.hive[i].x, self.hive[i].y, self.chosenFunction)
Ejemplo n.º 4
0
    def startAlgorithm(self, whichAlgorithm):
        self.whichAlgorithm = whichAlgorithm
        self.closePlotWindow = False
        fig = plt.figure(1)
        fig.canvas.mpl_connect('close_event', self.quit_figure)
        ax = fig.add_subplot(111)
        u = np.linspace(-int(self.agentsRange), int(self.agentsRange), 100)
        x, y = np.meshgrid(u, u)
        z = returnChosenFunction(x, y, self.chosenFunction)
        plt.xlim(-int(self.agentsRange), int(self.agentsRange))
        plt.ylim(-int(self.agentsRange), int(self.agentsRange))
        ax.contourf(x, y, z)

        self.createHive()
        self.timer = Clock.schedule_interval(partial(self.timerFunction),
                                             float(self.interval))

        plt.show(block=False)
    def __init__(self,range1,nrAnts,maxIterations,chosenFunction):
        self.chosenFunction = chosenFunction
        self.hive = []
        self.maxIterations = maxIterations
        self.nrAnts = nrAnts
        self.range = range1
        self.a_T = 5
        self.hive = sorted(self.hive, key=attrgetter('z'))
        self.hive.reverse()


        for i in range(0,self.nrAnts,1):
            x = np.random.uniform(low= -self.range, high = self.range)
            y = np.random.uniform(low= -self.range, high = self.range)
            z = -returnChosenFunction(x,y,self.chosenFunction)
            self.hive.append(Ant(x, y, z))

        self.initAlhorithm()
    def doOneIteration(self, iteration):
        for i in range(0, self.numberOfFireflies, 1):
            for j in range(0, self.numberOfFireflies, 1):
                if (i == j):
                    continue

                distance = self.distanceBetweenFireflies3D(
                    self.hive[i], self.hive[j])

                if (self.calculateLightIntensity(self.hive[i], distance) <
                        self.calculateLightIntensity(self.hive[j], distance)):
                    attractivenessTemp = self.calculateBeta(
                        self.hive[i], distance)
                    self.alfa = 0.2 * np.power(np.random.random(), iteration)

                    if (attractivenessTemp < 0.000000000001):
                        self.hive[i].x = self.hive[i].x + self.alfa * (
                            np.random.random() - 0.5)
                        self.hive[i].y = self.hive[i].y + self.alfa * (
                            np.random.random() - 0.5)
                    else:
                        self.hive[i].x = self.hive[i].x + attractivenessTemp * (
                            self.hive[j].x -
                            self.hive[i].x) + self.alfa * (np.random.random())
                        self.hive[i].y = self.hive[i].y + attractivenessTemp * (
                            self.hive[j].y -
                            self.hive[i].y) + self.alfa * (np.random.random())

                    if self.hive[i].x >= self.range:
                        self.hive[i].x = self.range - 0.5
                    elif self.hive[i].x <= -self.range:
                        self.hive[i].x = -self.range + 0.5

                    if self.hive[i].y >= self.range:
                        self.hive[i].y = self.range - 0.5
                    elif self.hive[i].y <= -self.range:
                        self.hive[i].y = -self.range + 0.5

                    self.hive[i].lightIntensity = returnChosenFunction(
                        self.hive[i].x, self.hive[i].y, self.chosenFunction)
        self.hive = sorted(self.hive, key=attrgetter('lightIntensity'))
        self.hive.reverse()
Ejemplo n.º 7
0
    def moveGlowworms(self):
        for i in range(0, self.nrGlowworms, 1):
            i_neighbours = []
            i_neighbours_luciferin_sum = 0
            for j in range(0, self.nrGlowworms, 1):
                if (i != j):
                    d = self.distanceBetweenGlowworms(self.hive[i],
                                                      self.hive[j])

                    if ((d < self.hive[i].sensorRange) and
                        (self.hive[i].luciferin < self.hive[j].luciferin)):
                        i_neighbours.append(self.hive[j])
                        i_neighbours_luciferin_sum += (self.hive[j].luciferin -
                                                       self.hive[i].luciferin)

            for j in range(0, len(i_neighbours), 1):
                i_neighbours[j].moveProbability = (
                    i_neighbours[j].luciferin -
                    self.hive[i].luciferin) / i_neighbours_luciferin_sum

            i_to_j_glowworm_index = self.getRandomBestGlowwormIndex(
                i_neighbours)

            if (i_to_j_glowworm_index != -1):
                i_j_difference_X = self.hive[
                    i_to_j_glowworm_index].x - self.hive[i].x
                self.hive[i].x += self.s * (i_j_difference_X /
                                            np.abs(i_j_difference_X))

                i_j_difference_Y = self.hive[
                    i_to_j_glowworm_index].y - self.hive[i].y
                self.hive[i].y += self.s * (i_j_difference_Y /
                                            np.abs(i_j_difference_Y))

                self.hive[i].fitness = returnChosenFunction(
                    self.hive[i].x, self.hive[i].y, self.chosenFunction)

                self.hive[i].sensorRange = min(
                    self.r_max,
                    max(
                        0, self.hive[i].sensorRange + self.beta *
                        (self.n_t - np.abs(len(i_neighbours)))))
    def doOneIteration(self,iteration):
        if (self.maxIterations >= iteration):
            for i in range(0,self.nrAnts,1):
                move_to_index = 0

                sum_range = 0
                rng_move = np.random.random()
                for j in range(0,self.nrAnts-1,1):
                    if ((rng_move >= sum_range) and (rng_move <= (sum_range + self.hive[j + 1].probability))):
                        move_to_index = j + 1
                        break

                    sum_range += np.round(self.hive[j+1].probability, 3)


                dx = np.random.uniform(low= 0, high = self.a_T)
                if (self.hive[i].x > self.hive[move_to_index].x):
                    self.hive[i].x -= dx
                elif (self.hive[i].x < self.hive[move_to_index].x):
                    self.hive[i].x += dx
                else:
                    dx = np.random.uniform(low= -self.a_T, high = self.a_T)
                    self.hive[i].x += dx

                dx = np.random.uniform(low= 0, high = self.a_T)
                if (self.hive[i].y > self.hive[move_to_index].y):
                    self.hive[i].y -= dx
                elif(self.hive[i].y < self.hive[move_to_index].y):
                    self.hive[i].y += dx
                else:
                    dx = np.random.uniform(low= -self.a_T, high = self.a_T)
                    self.hive[i].y += dx

                if (self.hive[i].y > self.range or self.hive[i].y < -self.range or self.hive[i].x > self.range or self.hive[i].x < -self.range):
                    self.hive[i].x = np.random.uniform(low= -self.range, high = self.range)
                    self.hive[i].y = np.random.uniform(low= -self.range, high = self.range)

                self.hive[i].z = -returnChosenFunction(self.hive[i].x, self.hive[i].y,self.chosenFunction)
            self.a_T *= 0.9
            self.initAlhorithm()
    def doOneIteration(self, iteration):
        for i in range(0, self.numberOfParticles, 1):
            self.hive[i].velocityX = np.random.random(
            ) * self.hive[i].velocityX + self.alfa * (np.random.random(
            )) * (self.globalBestX - self.hive[i].x) + self.beta * (
                np.random.random()) * (self.hive[i].theBestX - self.hive[i].x)

            self.hive[i].velocityY = np.random.random(
            ) * self.hive[i].velocityY + self.alfa * (np.random.random(
            )) * (self.globalBestY - self.hive[i].y) + self.beta * (
                np.random.random()) * (self.hive[i].theBestY - self.hive[i].y)

            oldZ = self.hive[i].z
            self.hive[i].x = self.hive[i].x + self.hive[i].velocityX
            self.hive[i].y = self.hive[i].y + self.hive[i].velocityY

            if self.hive[i].x >= self.range:
                self.hive[i].x = self.range - 0.5
            elif self.hive[i].x <= -self.range:
                self.hive[i].x = -self.range + 0.5
            if self.hive[i].y >= self.range:
                self.hive[i].y = self.range - 0.5
            elif self.hive[i].y <= -self.range:
                self.hive[i].y = -self.range + 0.5

            x = self.hive[i].x
            y = self.hive[i].y
            self.hive[i].z = returnChosenFunction(x, y, self.chosenFuntion)
            if (self.hive[i].z < oldZ):
                self.hive[i].theBestX = self.hive[i].x
                self.hive[i].theBestY = self.hive[i].y
        self.hive = sorted(self.hive, key=attrgetter('z'))

        if (self.globalBestZ > self.hive[0].z):
            self.globalBestX = self.hive[0].x
            self.globalBestY = self.hive[0].y
            self.globalBestZ = self.hive[0].z
Ejemplo n.º 10
0
    def __init__(self, range1, nrGlowworms, maxIterations, chosenFunction):
        self.chosenFunction = chosenFunction
        self.maxIterations = maxIterations
        self.range = range1
        self.hive = []
        self.nrGlowworms = nrGlowworms
        self.alpha = 0.4
        self.beta = 0.08
        self.gamma = 0.6
        self.r_max = (2 * self.range) / float(3)
        self.s = self.range / float(((self.range * self.range + 2)))
        self.n_t = 5

        for i in range(0, self.nrGlowworms, 1):
            x = np.random.uniform(low=-self.range, high=self.range)
            y = np.random.uniform(low=-self.range, high=self.range)
            fitness = returnChosenFunction(x, y, self.chosenFunction)
            sensor_range = (2 * self.range) / float(4)
            self.hive.append(
                Glowworm(x=x,
                         y=y,
                         fitness=fitness,
                         sensor_range=sensor_range,
                         luciferin=5))
    def doOneIteration(self, iterations):
        self.hive = sorted(self.hive, key=attrgetter('fitness'))
        self.hive.reverse()

        constG = self.initialConstG * np.exp(
            -self.constBeta * (iterations / self.constMaxIterations))
        delta = 0.001

        if (iterations % 1) == 0:
            if self.k > 3:
                self.k -= 1
        helpMassSum = 0
        for i in range(0, self.numberOfParticles, 1):
            self.hive[i].helpMass = (
                self.hive[i].fitness -
                self.hive[self.numberOfParticles - 1].fitness) / (
                    self.hive[0].fitness -
                    self.hive[self.numberOfParticles - 1].fitness)
            helpMassSum = helpMassSum + self.hive[i].helpMass

        for i in range(0, self.numberOfParticles, 1):
            self.hive[i].activeMass = self.hive[i].helpMass / helpMassSum

        for i in range(0, self.numberOfParticles - 1, 1):
            self.hive[i].forceX = 0
            self.hive[i].forceY = 0

            for j in range(0, self.k, 1):
                if (i != j):
                    self.hive[i].forceX = self.hive[
                        i].forceX + np.random.random() * constG * (
                            (self.hive[i].activeMass * self.hive[j].activeMass)
                            / ((self.distanceBetweenParticles(
                                self.hive[i], self.hive[j]) + delta))) * (
                                    self.hive[j].x - self.hive[i].x)

                    self.hive[i].forceY = self.hive[
                        i].forceY + np.random.random() * constG * (
                            (self.hive[i].activeMass * self.hive[j].activeMass)
                            / ((self.distanceBetweenParticles(
                                self.hive[i], self.hive[j]) + delta))) * (
                                    self.hive[j].y - self.hive[i].y)

            self.hive[i].accelerationX = self.hive[i].forceX / self.hive[
                i].activeMass
            self.hive[i].accelerationY = self.hive[i].forceY / self.hive[
                i].activeMass

            self.hive[i].velocityX = (
                np.random.random() *
                self.hive[i].velocityX) + self.hive[i].accelerationX
            self.hive[i].velocityY = (
                np.random.random() *
                self.hive[i].velocityY) + self.hive[i].accelerationY

            if (((self.hive[i].x + self.hive[i].velocityX) < -self.range) or
                ((self.hive[i].x + self.hive[i].velocityX) > self.range)):
                self.hive[i].x = np.random.uniform(low=-self.range,
                                                   high=self.range)
            else:
                self.hive[i].x = self.hive[i].x + self.hive[i].velocityX

            if (((self.hive[i].y + self.hive[i].velocityY) < -self.range) or
                ((self.hive[i].y + self.hive[i].velocityY) > self.range)):
                self.hive[i].y = np.random.uniform(low=-self.range,
                                                   high=self.range)
            else:
                self.hive[i].y = self.hive[i].y + self.hive[i].velocityY

            self.hive[i].fitness = returnChosenFunction(
                self.hive[i].x, self.hive[i].y, self.chosenFunction)
 def __init__(self, x, y, chosenFunction):
     self.x = x
     self.y = y
     self.lightIntensity = returnChosenFunction(self.x, self.y,
                                                chosenFunction)
     self.attractiveness = 1
Ejemplo n.º 13
0
    def doOneIteration(self, iterations):
        self.theBestX = self.hive[0].x
        self.theBestY = self.hive[0].y
        self.theBestZ = self.hive[0].z

        for i in range(0, self.numberOfParticles, 1):
            self.hive[i].electricCharge = (
                self.hive[i].z - self.hive[self.numberOfParticles - 1].z) / (
                    self.hive[0].z - self.hive[self.numberOfParticles - 1].z)
        helpForceX = None
        helpForceY = None
        probability = None
        alfa = None
        beta = None

        for i in range(0, self.numberOfParticles, 1):
            helpForceX = 0
            helpForceY = 0
            for j in range(0, self.numberOfParticles, 1):
                if (i == j):
                    continue

                if (((self.hive[i].z - self.hive[0].z) /
                     (self.hive[j].z - self.hive[i].z) > np.random.random())
                        or (self.hive[j].z > self.hive[i].z)):
                    probability = 1
                else:
                    probability = 0

                distanceBetweenParticles = self.distanceBetweenParticles3D(
                    self.hive[i], self.hive[j])

                if (distanceBetweenParticles < self.particleDiameter):
                    alfa = 1
                    beta = 0
                else:
                    alfa = 0
                    beta = 1

                helpForceX += (
                    (alfa * self.hive[j].electricCharge / np.power(
                        self.particleDiameter, 3) * distanceBetweenParticles) +
                    (beta * self.hive[j].electricCharge /
                     np.power(distanceBetweenParticles, 2))
                ) * self.particleDiameter * distanceBetweenParticles * probability * (
                    self.hive[j].x - self.hive[i].x)

                helpForceY += (
                    (alfa * self.hive[j].electricCharge / np.power(
                        self.particleDiameter, 3) * distanceBetweenParticles) +
                    (beta * self.hive[j].electricCharge /
                     np.power(distanceBetweenParticles, 2))
                ) * self.particleDiameter * distanceBetweenParticles * probability * (
                    self.hive[j].y - self.hive[i].y)

            self.hive[i].forceX = self.hive[i].electricCharge * helpForceX
            self.hive[i].forceY = self.hive[i].electricCharge * helpForceY

            oldX = self.hive[i].x
            oldY = self.hive[i].y

            newX = (self.hive[i].x + 0.5 *
                    (1 - iterations / self.constIterations) *
                    np.random.random() * self.hive[i].velocityX) + (
                        0.5 * np.random.random() *
                        (1 + iterations / self.constIterations) *
                        self.hive[i].forceX)

            newY = (self.hive[i].y + 0.5 *
                    (1 - iterations / self.constIterations) *
                    np.random.random() * self.hive[i].velocityY) + (
                        0.5 * np.random.random() *
                        (1 + iterations / self.constIterations) *
                        self.hive[i].forceY)

            if (((newX + self.hive[i].velocityX) < -self.range)
                    or ((newX + self.hive[i].velocityX) > self.range)):
                self.hive[i].x = np.random.uniform(low=-self.range,
                                                   high=self.range)
            else:
                self.hive[i].x = newX

            if (((newY + self.hive[i].velocityY) < -self.range)
                    or ((newY + self.hive[i].velocityY) > self.range)):
                self.hive[i].y = np.random.uniform(low=-self.range,
                                                   high=self.range)
            else:
                self.hive[i].y = newY

            self.hive[i].velocityX = self.hive[i].x - oldX
            self.hive[i].velocityY = self.hive[i].y - oldY

            self.hive[i].z = returnChosenFunction(self.hive[i].x,
                                                  self.hive[i].y,
                                                  self.chosenFunction)
        self.hive = sorted(self.hive, key=attrgetter('z'))
        self.hive.reverse()