예제 #1
0
class TestSimulation:
    def __init__(self):
        self.simulation = None

    def setup(self):
        self.simulation = None

    def test_flu_game(self):
        """
        Flu game simulates flu expanding through social network.
        """

        # Network construction.
        agents = {}

        for _ in range(100):
            agent = Agent()
            agent.set_fitness("infected", InfectedFitness())
            agents[agent.get_id()] = agent

        for agent in agents:
            agents[agent].update_fitness_measure("infected", True)
            break

        topology = generate_topology("clique", agents_names=agents.keys())

        changes = {1: "clique"}

        network = Network(agents, {"clique": topology}, changes)

        # Interaction construction.
        interactions = BehaviorSwitcher({
            "infection": InfectInteraction()
        }, {1: "infection"})

        # Environment construction.
        environment = BehaviorSwitcher({}, {})

        # Results construction.
        result = StandardResult(1)

        # Condition construction.
        condition = AllInfected()

        # Simulation construction.
        self.simulation = Simulation(network, interactions, environment,
                                     result, condition)

        simulation_result = self.simulation.run()
예제 #2
0
class SteelsClassifier:
    """
    Classifier founded on Steels and Belpaeme multi-agent network and interactions concept.

    :param classifiers: classifiers that will be used with agents creation.
    :type classifiers: list of classifiers.
    :param string role_model: the way of assign agents to roles. Described in GuessingGame.
    :param string topology: the name of topology used in classifier.
    :param integer iteration_number: the number of iteration.
    """

    def __init__(
        self,
        classifiers=None,
        role_model="RANDOM",
        topology="clique",
        iteration_number=1000,
        alpha=0.99,
        good_agent_measure=0.95,
    ):
        self.classifiers = classifiers
        self.alpha = alpha
        self.role_model = role_model
        self.good_agent_measure = good_agent_measure
        self.simulation = None
        self.topology = topology
        self.result = None
        self.iteration_number = iteration_number
        self.condition = IterationCondition(self.iteration_number)
        self.interactions = BehaviorSwitcher(
            GuessingGame(good_agent_measure=self.good_agent_measure, role_model=self.role_model)
        )

    def clone(self):
        return type(self)(
            classifiers=self.classifiers,
            role_model=self.role_model,
            topology=self.topology,
            iteration_number=self.iteration_number,
            alpha=self.alpha,
            good_agent_measure=self.good_agent_measure,
        )

    def get_params(self, deep):
        return {
            "classifiers": self.classifiers,
            "role_model": self.role_model,
            "topology": self.topology,
            "iteration_number": self.iteration_number,
            "alpha": self.alpha,
            "good_agent_measure": self.good_agent_measure,
        }

    def fit(self, x, y):
        """
        Fit the steels classifier according to the given training data.

        :param list x: samples.
        :param list y: samples' classes.
        """

        environment = Environment(x, y)

        agents = {}

        if self.classifiers is None:
            for _ in range(15):
                agent = SteelsClassificationAgent(alpha=self.alpha)
                agents[agent.id] = agent
        else:
            for classifier in self.classifiers:
                agent = SteelsClassificationAgent(classifier=classifier, alpha=self.alpha)
                agents[agent.id] = agent

        for agent in agents.values():
            agent.set_fitness("DG", CurrentFitness())
            agent.set_fitness("GG", CurrentFitness())

        network = Network(agents, {1: generate_topology(self.topology, agents_names=agents.keys())})

        self.simulation = Simulation(
            network, self.interactions, BehaviorSwitcher(environment), SteelsClassifierResults(), self.condition
        )

        self.result = self.simulation.run()

    def predict(self, samples):
        """
        :param list samples: the samples which classes are predicted.

        :raise: **NotFittedError** - if no data have been fitted yet.

        :return: the predicted classes of sample.
        :rtype: list
        """
        if self.result is not None:
            return self.result.predict(samples)
        else:
            raise NotFittedError