Optimization framework based on swarm intelligence
- Bees algorithm (Continuous Optimization)
- Ant Colony Optimization (Combinatorial Optimization)
- Particle Swarm Optimization (Continuous Optimization)
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>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);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));