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).
- Polynomial Generation: The
Geradoragent can generate random polynomial functions of degree 1, 2, or 3. - Function Evaluation: The
Geradoragent can evaluate the generated function for a givenxvalue upon request. - Degree Discovery: The
Resolvedoragent can inquire about the degree of the function generated byGerador. - Root Finding: The
Resolvedoragent collects points from theGeradorand 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.
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
xvalue. - Calculates
$f(x)$ using the stored coefficients. - Sends back the result to the sender.
- Listens for messages with
-
InformFunctionDegree(CyclicBehaviour):- Listens for messages with
performative="request". - Sends back the
self.agent.degreeto the sender.
- Listens for messages with
This agent aims to discover the polynomial generated by the Gerador and find its roots.
-
DiscoverDegree(CyclicBehaviour):- Sends a
requestmessage toGeradorto get the function's degree. - Once the degree is received, it initiates the
SolveFunctionbehavior and then kills itself.
- Sends a
-
SolveFunction(CyclicBehaviour):-
on_start:- Collects a sufficient number of
(x, y)points by sendingsubscribemessages toGeradorfor variousxvalues. The number of points needed isdegree + 1.
- Collects a sufficient number of
-
run:- Based on the
self.agent.degree, it calls the appropriate solving method:solve_1grau,solve_2grau, orsolve_3grau. - After attempting to solve, it kills itself and stops the agent.
- Based on the
-
check_fx(x):- A helper method to send an
xvalue toGeradorand receive its correspondingyvalue.
- A helper method to send an
-
solve_1grau():- Uses two points to determine the
aandbcoefficients for$f(x) = ax + b$ and calculates the single root.
- Uses two points to determine the
-
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.
- Uses three points to set up a system of linear equations and solve for
-
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.
- Uses four points to set up a system of linear equations and solve for
-
_bisection(a, b):- A private helper method for finding a root within an interval
[a, b]wheref(a)andf(b)have opposite signs.
- A private helper method for finding a root within an interval
-
On Linux 🐧:
sudo apt updatesudo apt install ejabberdsudo ejabberdctl register "your_user" localhost "your_password"-
Check if Java is Installed:
java -version
If installed, you’ll see a version like
java version "1.8.0_281". If not, proceed. -
Download and Install Java:
- Visit Oracle Java Downloads or AdoptOpenJDK.
- Download JDK 8 or 11 for Windows (e.g.,
.exeinstaller). - Run the installer, following the prompts.
-
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).
- Variable name:
- Edit Path variable, add:
%JAVA_HOME%\bin. - Verify:
java -version
Openfire is an open-source XMPP server used for local agent communication.
-
Download Openfire:
- Go to Openfire Downloads.
- Download the latest Windows installer (e.g.,
openfire_4_8_0.exe).
-
Install Openfire:
- Run the installer.
- Install to a directory (e.g.,
C:\Program Files\Openfire). - Accept default settings and install as a service.
-
Start Openfire:
- Openfire starts automatically post-installation. If not:
- Open Services (
Win + R, typeservices.msc). - Find Openfire, right-click, and select Start.
- Or run manually:
C:\Program Files\Openfire\bin\openfire.exe
- Open Services (
- Openfire starts automatically post-installation. If not:
-
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 (
localhostdomain, ports5222for clients,9090for 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).
- Open a browser and go to
Step 3: Create XMPP Users
The agents require users "your_user"@localhost and "other_user"@localhost with password "your_password".
-
Log into Openfire Admin Console:
- Navigate to
http://localhost:9090. - Log in with admin credentials (e.g.,
admin@localhost,admin123).
- Navigate to
-
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"
- Username:
- Click Create.
-
Create User
"other_user"@localhost:- Repeat:
- Username:
"other_user"@localhost - Name: (optional, e.g., Resolvedor)
- Email: (optional)
- Password:
"your_password" - Confirm Password:
"your_password"
- Username:
- Click Create.
- Repeat:
-
Verify Users:
- Go to Users/Groups > User Summary and confirm
"your_user"and"other_user"are listed.
- Go to Users/Groups > User Summary and confirm
On Linux 🐧:
sudo apt update
sudo apt install python3-venv python3-fullOn Windows 🪟:
python --version
pip --versionIf these commands fail, you may need to reinstall Python, ensuring you check the "Add Python to PATH" option.
# 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 venvOn Linux 🐧:
source venv/bin/activateOn Windows 🪟:
.\venv\Scripts\activateYou will know it's active because (venv) will appear at the beginning of your terminal prompt.
pip install spade numpygit clone https://github.com/JvFg92/Spade_Multi_Agent_SystemGenerator:
On Linux 🐧:
cd my_spade_projectsource venv/bin/activatecd Spade_Multi_Agent_Systempython3 Gerador.pyOn Windows 🪟:
cd my_spade_project.\venv\Scripts\activatecd Spade_Multi_Agent_System#python Gerador.py
py Gerador.pySolver:
On Linux 🐧:
cd my_spade_projectsource venv/bin/activatecd Spade_Multi_Agent_Systempython3 Resolvedor.pyOn Windows 🪟:
cd my_spade_project.\venv\Scripts\activatecd Spade_Multi_Agent_System#python Resolvedor.py
py Resolvedor.pydeactivateexit