The Swarms module in the JuliaOS framework provides a comprehensive interface for creating, managing, and interacting with swarms. This page explains how to use the Swarms module in your applications.
Overview
The Swarms module connects to the Julia backend through the JuliaBridge and provides methods for creating, retrieving, starting, stopping, and running optimizations with swarms. It also includes various swarm algorithms for different optimization and coordination tasks.
Installation
Copy npm install @juliaos/framework
Usage
Initializing the Swarms Module
Copy import { JuliaBridge } from '@juliaos/julia-bridge';
import { Swarms } from '@juliaos/framework';
// Initialize the bridge
const bridge = new JuliaBridge({ host: 'localhost', port: 8052 });
await bridge.initialize();
// Create the Swarms module
const swarms = new Swarms(bridge);
Creating a Swarm
Copy const swarm = await swarms.createSwarm({
name: 'MySwarm',
algorithm: 'PSO',
config: {
particles: 30,
c1: 2.0,
c2: 2.0,
w: 0.7,
dimensions: 10,
bounds: [[-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10]],
objective: 'minimize'
}
});
console.log('Created swarm:', swarm);
Starting a Swarm
Copy await swarms.startSwarm(swarm.id);
console.log('Swarm started');
Adding Agents to a Swarm
Copy // Assuming you have created an agent
await swarms.addAgentToSwarm(swarm.id, agent.id);
console.log('Agent added to swarm');
Running an Optimization
Copy // Define an objective function
const objectiveFunction = 'function(x) return sum(x.^2) end';
// Run the optimization
const result = await swarms.runOptimization(swarm.id, objectiveFunction, [], {
max_iterations: 100,
tolerance: 1e-6
});
console.log('Optimization result:', result);
Getting Swarm Status
Copy const status = await swarms.getSwarmStatus(swarm.id);
console.log('Swarm status:', status);
Stopping a Swarm
Copy await swarms.stopSwarm(swarm.id);
console.log('Swarm stopped');
Listing Swarms
Copy const allSwarms = await swarms.listSwarms();
console.log('All swarms:', allSwarms);
Removing an Agent from a Swarm
Copy await swarms.removeAgentFromSwarm(swarm.id, agent.id);
console.log('Agent removed from swarm');
Deleting a Swarm
Copy await swarms.deleteSwarm(swarm.id);
console.log('Swarm deleted');
Swarm Algorithms
The Swarms module supports various swarm intelligence algorithms:
Particle Swarm Optimization (PSO)
Copy const psoSwarm = await swarms.createSwarm({
name: 'PSOSwarm',
algorithm: 'PSO',
config: {
particles: 30,
c1: 2.0,
c2: 2.0,
w: 0.7,
dimensions: 10,
bounds: [[-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10]],
objective: 'minimize'
}
});
Differential Evolution (DE)
Copy const deSwarm = await swarms.createSwarm({
name: 'DESwarm',
algorithm: 'DE',
config: {
population: 50,
F: 0.8,
CR: 0.9,
dimensions: 10,
bounds: [[-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10]],
objective: 'minimize'
}
});
Grey Wolf Optimizer (GWO)
Copy const gwoSwarm = await swarms.createSwarm({
name: 'GWOSwarm',
algorithm: 'GWO',
config: {
wolves: 30,
a_start: 2.0,
a_end: 0.0,
dimensions: 10,
bounds: [[-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10]],
objective: 'minimize'
}
});
Ant Colony Optimization (ACO)
Copy const acoSwarm = await swarms.createSwarm({
name: 'ACOSwarm',
algorithm: 'ACO',
config: {
ants: 30,
alpha: 1.0,
beta: 2.0,
rho: 0.5,
dimensions: 10,
bounds: [[-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10]],
objective: 'minimize'
}
});
Genetic Algorithm (GA)
Copy const gaSwarm = await swarms.createSwarm({
name: 'GASwarm',
algorithm: 'GA',
config: {
population: 100,
crossover_rate: 0.8,
mutation_rate: 0.1,
dimensions: 10,
bounds: [[-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10]],
objective: 'minimize'
}
});
Whale Optimization Algorithm (WOA)
Copy const woaSwarm = await swarms.createSwarm({
name: 'WOASwarm',
algorithm: 'WOA',
config: {
whales: 30,
b: 1.0,
dimensions: 10,
bounds: [[-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10]],
objective: 'minimize'
}
});
Hybrid DEPSO
Copy const depsoSwarm = await swarms.createSwarm({
name: 'DEPSOSwarm',
algorithm: 'DEPSO',
config: {
population: 50,
F: 0.8,
CR: 0.9,
w: 0.7,
c1: 1.5,
c2: 1.5,
hybrid_ratio: 0.5,
adaptive: true,
dimensions: 10,
bounds: [[-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10], [-10, 10]],
objective: 'minimize'
}
});
Listing Available Algorithms
Copy const algorithms = await swarms.listAlgorithms();
console.log('Available algorithms:', algorithms);
Error Handling
The Swarms module includes robust error handling:
Copy try {
const swarm = await swarms.createSwarm({
name: 'MySwarm',
algorithm: 'PSO',
config: { /* ... */ }
});
} catch (error) {
console.error('Failed to create swarm:', error);
}
See Also
Agents Module - Learn about agents and how to use them with swarms
Bridge Module - Learn about the bridge that connects to the Julia backend
Algorithms - Learn more about the swarm algorithms