示例#1
0
    def _business_three(self, agents, function):
        """Performs the third business phase.

        Args:
            agents (list): List of agents.
            function (Function): A Function object that will be used as the objective function.

        """

        # Sorts the agents
        agents.sort(key=lambda x: x.fit)

        # Calculates the probability of handling the business
        pr = [i / len(agents) for i in range(1, len(agents) + 1)]

        # Iterates through all agents
        for i, agent in enumerate(agents):
            # Creates another temporary agent
            a = copy.deepcopy(agent)

            # Iterates through all decision variables
            for j in range(agent.n_variables):
                # Generates a uniform random number
                r1 = r.generate_uniform_random_number()

                # If random number is smaller than probability of handling the business
                if r1 < pr[i]:
                    # Randomly selects two individuals
                    A_1, A_2 = np.random.choice(agents, 2, replace=False)

                    # Generates an Erlang number
                    e = r.generate_gamma_random_number(1, 0.5, 1)

                    # Updates temporary agent's position (Eq. 17)
                    a.position[j] = A_1.position[j] + e * (A_2.position[j] -
                                                           a.position[j])

                # Evaluates the agent
                a.fit = function(a.position)

                # If the new fitness is better than the current agent's fitness
                if a.fit < agent.fit:
                    # Replaces the current agent's position
                    agent.position = copy.deepcopy(a.position)

                    # Also replaces its fitness
                    agent.fit = copy.deepcopy(a.fit)
示例#2
0
def test_generate_gamma_random_number():
    gamma_array = random.generate_gamma_random_number(1, 1, 5)

    assert gamma_array.shape == (5, )
示例#3
0
    def _business_two(self, agents: List[Agent], function: Function) -> None:
        """Performs the second business phase.

        Args:
            agents: List of agents.
            function: A Function object that will be used as the objective function.

        """

        # Sorts agents
        agents.sort(key=lambda x: x.fit)

        # Copies temporary agents to represent `A_1`, `A_2` and `A_3`
        A_1, A_2, A_3 = (
            copy.deepcopy(agents[0]),
            copy.deepcopy(agents[1]),
            copy.deepcopy(agents[2]),
        )

        # Calculates the number of agents in each queue
        q_1, q_2, _ = self._calculate_queue(len(agents), A_1.fit, A_2.fit,
                                            A_3.fit)

        # Calculates the probability of handling the business
        pr = [i / len(agents) for i in range(1, len(agents) + 1)]

        # Calculates the confusion degree
        cv = A_1.fit / (A_2.fit + A_3.fit + c.EPSILON)

        # Iterates through all agents
        for i, agent in enumerate(agents):
            # Creates another temporary agent
            a = copy.deepcopy(agent)

            # If index is smaller than the number of agents in first queue
            if i < q_1:
                # `A` will receive a copy from `A_1`
                A = copy.deepcopy(A_1)

            # If index is between first and second queues
            elif q_1 <= i < q_1 + q_2:
                # `A` will receive a copy from `A_2`
                A = copy.deepcopy(A_2)

            # If index is between second and third queues
            else:
                # `A` will receive a copy from `A_3`
                A = copy.deepcopy(A_3)

            # Generates a uniform random number
            r1 = r.generate_uniform_random_number()

            # If random number is smaller than probability of handling the business
            if r1 < pr[i]:
                # Randomly selects two individuals
                A_1, A_2 = np.random.choice(agents, 2, replace=False)

                # Generates another uniform random number
                r2 = r.generate_uniform_random_number()

                # Generates an Erlang number
                e = r.generate_gamma_random_number(1, 0.5, 1)

                # If random number is smaller than confusion degree
                if r2 < cv:
                    # Calculates the fluctuation (eq. 14)
                    F_1 = e * (A_1.position - A_2.position)

                    # Update agent's position (eq. 12)
                    a.position += F_1

                # If random number is bigger than confusion degree
                else:
                    # Calculates the fluctuation (eq. 15)
                    F_2 = e * (A.position - A_1.position)

                    # Update agent's position (eq. 13)
                    a.position += F_2

                # Evaluates the agent
                a.fit = function(a.position)

                # If the new fitness is better than the current agent's fitness
                if a.fit < agent.fit:
                    # Replaces the current agent's position and fitness
                    agent.position = copy.deepcopy(a.position)
                    agent.fit = copy.deepcopy(a.fit)
示例#4
0
            # If index is between second and third queues
            else:
                # If index is the first agent in third queue
                if i == q_1 + q_2:
                    # Defines the case as one
                    case = 1

                # `A` will receive a copy from `A_3`
                A = copy.deepcopy(A_3)

            # Generates a uniform random number
            alpha = r.generate_uniform_random_number(-1, 1)

            # Generates an Erlang distribution
            E = r.generate_gamma_random_number(
                1, 0.5, (agent.n_variables, agent.n_dimensions))

            # If case is defined as one
            if case == 1:
                # Generates an Erlang number
                e = r.generate_gamma_random_number(1, 0.5, 1)

                # Calculates the fluctuation (eq. 6)
                F_1 = beta * alpha * (E * np.fabs(A.position - a.position)
                                      ) + e * (A.position - a.position)

                # Updates the temporary agent's position (eq. 4)
                a.position = A.position + F_1

                # Evaluates the agent
                a.fit = function(a.position)
import opytimizer.math.random as r

# Generating a binary random number array
b = r.generate_binary_random_number(size=10)
print(b)

# Generating an Erlang/gamma random number array
e = r.generate_gamma_random_number(shape=1.0, scale=1.0, size=10)
print(e)

# Generating an integer random number array
i = r.generate_integer_random_number(low=0, high=1, size=1)
print(i)

# Generating a random uniform number array
u = r.generate_uniform_random_number(low=0.0, high=1.0, size=1)
print(u)

# Generating a random gaussian number array
g = r.generate_gaussian_random_number(mean=0.5, variance=1.0, size=10)
print(g)