Swarm Intelligence Optimization

Optimization library based on swarm-intelligence

License

License

MIT
Categories

Categories

Java Languages
GroupId

GroupId

com.github.chen0040
ArtifactId

ArtifactId

java-swarm-intelligence
Last Version

Last Version

1.0.5
Release Date

Release Date

Type

Type

jar
Description

Description

Swarm Intelligence Optimization
Optimization library based on swarm-intelligence
Project URL

Project URL

https://github.com/chen0040/java-swarm-intelligence
Source Code Management

Source Code Management

https://github.com/chen0040/java-swarm-intelligence

Download java-swarm-intelligence

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.chen0040/java-swarm-intelligence/ -->
<dependency>
    <groupId>com.github.chen0040</groupId>
    <artifactId>java-swarm-intelligence</artifactId>
    <version>1.0.5</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.chen0040/java-swarm-intelligence/
implementation 'com.github.chen0040:java-swarm-intelligence:1.0.5'
// https://jarcasting.com/artifacts/com.github.chen0040/java-swarm-intelligence/
implementation ("com.github.chen0040:java-swarm-intelligence:1.0.5")
'com.github.chen0040:java-swarm-intelligence:jar:1.0.5'
<dependency org="com.github.chen0040" name="java-swarm-intelligence" rev="1.0.5">
  <artifact name="java-swarm-intelligence" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.chen0040', module='java-swarm-intelligence', version='1.0.5')
)
libraryDependencies += "com.github.chen0040" % "java-swarm-intelligence" % "1.0.5"
[com.github.chen0040/java-swarm-intelligence "1.0.5"]

Dependencies

compile (6)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.20
org.slf4j : slf4j-log4j12 jar 1.7.20
org.apache.commons : commons-math3 jar 3.2
com.github.chen0040 : java-data-frame jar 1.0.11
com.github.chen0040 : java-datasets-discrete-optimization jar 1.0.1
com.github.chen0040 : java-data-visualizer jar 1.0.1

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.6

test (10)

Group / Artifact Type Version
org.testng : testng jar 6.9.10
org.hamcrest : hamcrest-core jar 1.3
org.hamcrest : hamcrest-library jar 1.3
org.assertj : assertj-core jar 3.5.2
org.powermock : powermock-core jar 1.6.5
org.powermock : powermock-api-mockito jar 1.6.5
org.powermock : powermock-module-junit4 jar 1.6.5
org.powermock : powermock-module-testng jar 1.6.5
org.mockito : mockito-core jar 2.0.2-beta
org.mockito : mockito-all jar 2.0.2-beta

Project Modules

There are no modules declared in this project.

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.5</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));
 }
};
BeeSwarm swarm = new BeeSwarm();
swarm.setUpperBounds(Arrays.asList(5.0, 5.0));
swarm.setLowerBounds(Arrays.asList(-5.0, -5.0));
swarm.setDimension(2);
swarm.setCostFunction(Rosenbrock);
swarm.setMaxIterations(50);

Bee bestSolution = swarm.solve();
logger.info("best solution: {} cost: {}", bestSolution, bestSolution.getCost());

List<Double> trend = swarm.getCostTrend();
logger.info("trend: {}", trend);

To visualize the performance of the bees swarm algorithm over time:

CostTrend chart = new CostTrend(trend, "Cost vs Generation");
chart.showIt(true);

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));
 }
};

ParticleSwarm swarm = new ParticleSwarm();
swarm.setUpperBounds(Arrays.asList(5.0, 5.0));
swarm.setLowerBounds(Arrays.asList(-5.0, -5.0));
swarm.setDimension(2);
swarm.setCostFunction(Rosenbrock);
swarm.setMaxIterations(50);

Particle bestSolution = swarm.solve();
logger.info("best solution: {} cost: {}", bestSolution, bestSolution.getCost());

List<Double> trend = swarm.getCostTrend();
logger.info("trend: {}", trend);

To visualize the performance of the particle swarm algorithm over time:

CostTrend chart = new CostTrend(trend, "Cost vs Generation");
chart.showIt(true);

Ant System

The sample code below shows how to solve a TSP (Travelling Salesman Problem) instance using Ant System:

// load the bayg29 TSP instance
TspBenchmark benchmark = Tsp.get(Tsp.Instance.bayg29);

PathCostFunction costFunction = new PathCostFunction() {
 // compute the cost of the tour constructed by an ant on the problem bayg29
 @Override public double evaluate(List<Integer> path) {
    double cost = 0;
    for(int i=0; i < path.size(); ++i) {
       int j = (i+1) % path.size();
       double distance = benchmark.distance(path.get(i), path.get(j));
       cost += distance;
    }
    return cost;
 }

 // heuristic weight for transition from state1 to state2 during path construction
 // the higher the weight the more favorable to transit from state1 to state2
 @Override public double stateTransitionWeight(int state1, int state2) {
    return 1 / (1 + benchmark.distance(state1, state2));
 }
};



AntSystem antSystem = new AntSystem();
antSystem.setProblemSize(benchmark.size());
antSystem.setCostFunction(costFunction);

antSystem.setMaxIterations(100);

Ant bestAnt = antSystem.solve();

System.out.println("minimal total distance found by Ant System: " + bestAnt.getCost());
System.out.println("known minimal total distance: " + costFunction.evaluate(benchmark.optTour()));

System.out.println("best TSP path found: ");
for(int i=0; i < bestAnt.getPath().size(); ++i) {
 int j = (i + 1) % bestAnt.getPath().size();
 System.out.println(bestAnt.getPath().get(i) + " => " + bestAnt.getPath().get(j));
}

To visualize the performance of the ant system algorithm over time:

CostTrend chart = new CostTrend(antSystem.getCostTrend(), "Cost vs Generation");
chart.showIt(true);

Ant Colony System

The sample code below shows how to solve a TSP (Travelling Salesman Problem) instance using Ant Colony System:

TspBenchmark benchmark = Tsp.get(Tsp.Instance.bayg29);
 
PathCostFunction costFunction = new PathCostFunction() {
  @Override public double evaluate(List<Integer> path) {
     double cost = 0;
     for(int i=0; i < path.size(); ++i) {
        int j = (i+1) % path.size();
        double distance = benchmark.distance(path.get(i), path.get(j));
        cost += distance;
     }
     return cost;
  }

  // heuristic weight for transition from state1 to state2 during path construction
  // the higher the weight the more favorable to transit from state1 to state2
  @Override public double stateTransitionWeight(int state1, int state2) {
     return 1 / (1 + benchmark.distance(state1, state2));
  }
};



AntColonySystem antColonySystem = new AntColonySystem();
antColonySystem.setProblemSize(benchmark.size());
antColonySystem.setCostFunction(costFunction);

antColonySystem.setMaxIterations(100);

Ant bestAnt = antColonySystem.solve();

System.out.println("minimal total distance found: " + bestAnt.getCost());
System.out.println("best known cost: " + costFunction.evaluate(benchmark.optTour()));

System.out.println("best path found: ");
for(int i=0; i < bestAnt.getPath().size(); ++i) {
  int j = (i + 1) % bestAnt.getPath().size();
  System.out.println(bestAnt.getPath().get(i) + " => " + bestAnt.getPath().get(j));
}

To visualize the performance of the ant colony system algorithm over time:

CostTrend chart = new CostTrend(antColonySystem.getCostTrend(), "Cost vs Generation");
chart.showIt(true);

Versions

Version
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1