Skip to content

chen0040/java-swarm-intelligence

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

java-swarm-intelligence

Optimization framework based on swarm intelligence

Build Status Coverage Status Documentation Status

Features

  • Bees algorithm (Continuous Optimization)
  • Ant Colony Optimization (Combinatorial Optimization)
  • Particle Swarm Optimization (Continuous Optimization)

Install

Add the following dependency to your POM file:

<dependency> <groupId>com.github.chen0040</groupId> <artifactId>java-swarm-intelligence</artifactId> <version>1.0.2</version> </dependency>

Usage

Bees Swarm

The sample code below shows how to use the bees algorithm to solve the Rosenbrock minimization problem:

CostFunction Rosenbrock = new CostFunction() { public double calc(double x, double y) { double expr1 = (x*x - y); double expr2 = 1 - x; return 100 * expr1*expr1 + expr2*expr2; } @Override public double evaluate(List<Double> solution, List<Double> lowerBounds, List<Double> upperBounds) { return calc(solution.get(0), solution.get(1)); } }; Mediator mediator = new Mediator(); mediator.setUpperBounds(Arrays.asList(5.0, 5.0)); mediator.setLowerBounds(Arrays.asList(-5.0, -5.0)); mediator.setDimension(2); mediator.setCostFunction(Rosenbrock); BeeSwarm swarm = new BeeSwarm(); swarm.setMediator(mediator); swarm.setMaxIterations(50); Bee bestSolution = swarm.solve(); logger.info("best solution: {} cost: {}", bestSolution, bestSolution.getCost()); List<Double> trend = swarm.getCostTrend(); logger.info("trend: {}", trend);

Particle Swarm Optimization

The sample code below shows how to use the PSO algorithm to solve the Rosenbrock minimization problem:

CostFunction Rosenbrock = new CostFunction() { public double calc(double x, double y) { double expr1 = (x*x - y); double expr2 = 1 - x; return 100 * expr1*expr1 + expr2*expr2; } @Override public double evaluate(List<Double> solution, List<Double> lowerBounds, List<Double> upperBounds) { return calc(solution.get(0), solution.get(1)); } }; Mediator mediator = new Mediator(); mediator.setUpperBounds(Arrays.asList(5.0, 5.0)); mediator.setLowerBounds(Arrays.asList(-5.0, -5.0)); mediator.setDimension(2); mediator.setCostFunction(Rosenbrock); ParticleSwarm swarm = new ParticleSwarm(); swarm.setMediator(mediator); swarm.setMaxIterations(50); Particle bestSolution = swarm.solve(); logger.info("best solution: {} cost: {}", bestSolution, bestSolution.getCost()); List<Double> trend = swarm.getCostTrend(); logger.info("trend: {}", trend); assertThat(bestSolution.getCost()).isCloseTo(0, within(0.01));