Skip to content

The Spade_Multi_Agent_System GitHub repository hosts a multi-agent system built with the SPADE framework, featuring a Gerador (Generator) agent and a Resolvedor (Solver) agent.

Notifications You must be signed in to change notification settings

JvFg92/Spade_Multi_Agent_System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 

Repository files navigation

Python Function Generator & Solver Agents 🕵️‍♀️

This project demonstrates a multi-agent system using the spade library to generate and solve polynomial functions. The system consists of two main agents: Gerador (Generator) and Resolvedor (Solver).

🌟 Features

  • Polynomial Generation: The Gerador agent can generate random polynomial functions of degree 1, 2, or 3.
  • Function Evaluation: The Gerador agent can evaluate the generated function for a given x value upon request.
  • Degree Discovery: The Resolvedor agent can inquire about the degree of the function generated by Gerador.
  • Root Finding: The Resolvedor agent collects points from the Gerador and then attempts to find the real roots of the generated polynomial based on its degree.
  • Inter-Agent Communication: Agents communicate using FIPA-compliant messages to exchange information.

⚙️ How it Works

Gerador.py 🧬

This agent is responsible for generating a random polynomial function and providing its evaluation and degree upon request.

  • GenerateFunction (OneShotBehaviour):
    • Randomly selects a degree (1, 2, or 3).
    • Generates a polynomial of the chosen degree with random coefficients and real roots (for easier demonstration of root finding).
    • Stores the coefficients in self.agent.coefs.
    • Prints the generated function and its roots.
  • CalculateFunction (CyclicBehaviour):
    • Listens for messages with performative="subscribe".
    • Expects the message body to be an x value.
    • Calculates $f(x)$ using the stored coefficients.
    • Sends back the result to the sender.
  • InformFunctionDegree (CyclicBehaviour):
    • Listens for messages with performative="request".
    • Sends back the self.agent.degree to the sender.

Resolvedor.py 🔍

This agent aims to discover the polynomial generated by the Gerador and find its roots.

  • DiscoverDegree (CyclicBehaviour):
    • Sends a request message to Gerador to get the function's degree.
    • Once the degree is received, it initiates the SolveFunction behavior and then kills itself.
  • SolveFunction (CyclicBehaviour):
    • on_start:
      • Collects a sufficient number of (x, y) points by sending subscribe messages to Gerador for various x values. The number of points needed is degree + 1.
    • run:
      • Based on the self.agent.degree, it calls the appropriate solving method: solve_1grau, solve_2grau, or solve_3grau.
      • After attempting to solve, it kills itself and stops the agent.
    • check_fx(x):
      • A helper method to send an x value to Gerador and receive its corresponding y value.
    • solve_1grau():
      • Uses two points to determine the a and b coefficients for $f(x) = ax + b$ and calculates the single root.
    • solve_2grau():
      • Uses three points to set up a system of linear equations and solve for $a, b, c$ in $f(x) = ax^2 + bx + c$ using NumPy.
      • Calculates the roots using the quadratic formula.
    • solve_3grau():
      • Uses four points to set up a system of linear equations and solve for $k, b, c, d$ in $f(x) = kx^3 + bx^2 + cx + d$ using NumPy.
      • Attempts to find one real root using a simple bisection method based on sign changes between collected points.
      • If a root is found, it performs polynomial division to reduce the cubic to a quadratic, then solves the quadratic for the remaining two roots.
    • _bisection(a, b):
      • A private helper method for finding a root within an interval [a, b] where f(a) and f(b) have opposite signs.

🚀 Getting Started - Setting up Local Host:

On Linux 🐧:

On Linux 🐧:

sudo apt update
sudo apt install ejabberd
sudo ejabberdctl register "your_user" localhost "your_password"

On Windows 🪟:

  1. Check if Java is Installed:

    java -version

    If installed, you’ll see a version like java version "1.8.0_281". If not, proceed.

  2. Download and Install Java:

  3. Set JAVA_HOME:

    • Right-click This PC > Properties > Advanced system settings > Environment Variables.
    • Under System Variables, click New:
      • Variable name: JAVA_HOME
      • Variable value: Path to JDK (e.g., C:\Program Files\Java\jdk1.8.0_281).
    • Edit Path variable, add: %JAVA_HOME%\bin.
    • Verify:
      java -version

Openfire is an open-source XMPP server used for local agent communication.

  1. Download Openfire:

    • Go to Openfire Downloads.
    • Download the latest Windows installer (e.g., openfire_4_8_0.exe).
  2. Install Openfire:

    • Run the installer.
    • Install to a directory (e.g., C:\Program Files\Openfire).
    • Accept default settings and install as a service.
  3. Start Openfire:

    • Openfire starts automatically post-installation. If not:
      • Open Services (Win + R, type services.msc).
      • Find Openfire, right-click, and select Start.
      • Or run manually:
        C:\Program Files\Openfire\bin\openfire.exe
  4. Configure Openfire:

    • Open a browser and go to http://localhost:9090.
    • Complete the setup wizard:
      • Language: Choose English (or preferred language).
      • Server Settings: Use default (localhost domain, ports 5222 for clients, 9090 for admin console).
      • Database: Select Embedded Database for simplicity.
      • Profile Settings: Use default (store users in database).
      • Admin Account: Set email and password (e.g., admin@localhost, password: admin123).

Step 3: Create XMPP Users

The agents require users "your_user"@localhost and "other_user"@localhost with password "your_password".

  1. Log into Openfire Admin Console:

    • Navigate to http://localhost:9090.
    • Log in with admin credentials (e.g., admin@localhost, admin123).
  2. Create User "your_user"@localhost:

    • Go to Users/Groups > Create New User.
    • Enter:
      • Username: "your_user"@localhost
      • Name: (optional, e.g., Gerador)
      • Email: (optional)
      • Password: "your_password"
      • Confirm Password: "your_password"
    • Click Create.
  3. Create User "other_user"@localhost:

    • Repeat:
      • Username: "other_user"@localhost
      • Name: (optional, e.g., Resolvedor)
      • Email: (optional)
      • Password: "your_password"
      • Confirm Password: "your_password"
    • Click Create.
  4. Verify Users:

    • Go to Users/Groups > User Summary and confirm "your_user" and "other_user" are listed.

🚀 Getting Started - Setting up your environment and Run:

0. Pre steps:

On Linux 🐧:

sudo apt update
sudo apt install python3-venv python3-full

On Windows 🪟: ⚠️ On PowerShell: ⚠️

python --version
pip --version

If these commands fail, you may need to reinstall Python, ensuring you check the "Add Python to PATH" option.

1. Virtual enviroment creation Linux/Windows:

# Create and enter a directory for your project
mkdir my_spade_project
cd my_spade_project

# Create the virtual environment named 'venv'
python3 -m venv venv

2. Activate the Virtual Environment:

On Linux 🐧:

source venv/bin/activate

On Windows 🪟:

.\venv\Scripts\activate

You will know it's active because (venv) will appear at the beginning of your terminal prompt.

3. Install Spade:

pip install spade numpy

4. Clone the Repository:

⚠️ Make sure that you still in the correct directory ⚠️

git clone https://github.com/JvFg92/Spade_Multi_Agent_System

5. Running the scripts: ▶️

Generator: ⚠️ Open a Terminal ⚠️

On Linux 🐧:

cd my_spade_project
source venv/bin/activate
cd Spade_Multi_Agent_System
python3 Gerador.py

On Windows 🪟:

cd my_spade_project
.\venv\Scripts\activate
cd Spade_Multi_Agent_System
#python Gerador.py
py Gerador.py

Solver: ⚠️ Open another Terminal ⚠️

On Linux 🐧:

cd my_spade_project
source venv/bin/activate
cd Spade_Multi_Agent_System
python3 Resolvedor.py

On Windows 🪟:

cd my_spade_project
.\venv\Scripts\activate
cd Spade_Multi_Agent_System
#python Resolvedor.py
py Resolvedor.py

6. When you're finished, you can deactivate the environment with a single command:

⚠️ Do it for each terminal ⚠️

deactivate
exit

About

The Spade_Multi_Agent_System GitHub repository hosts a multi-agent system built with the SPADE framework, featuring a Gerador (Generator) agent and a Resolvedor (Solver) agent.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages