Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Optimization framework based on swarm intelligence

License

NotificationsYou must be signed in to change notification settings

chen0040/java-swarm-intelligence

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Optimization framework based on swarm intelligence

Build StatusCoverage StatusDocumentation 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:

CostFunctionRosenbrock =newCostFunction() {publicdoublecalc(doublex,doubley) {doubleexpr1 = (x*x -y);doubleexpr2 =1 -x;return100 *expr1*expr1 +expr2*expr2; }@Overridepublicdoubleevaluate(List<Double>solution,List<Double>lowerBounds,List<Double>upperBounds) {returncalc(solution.get(0),solution.get(1)); }};BeeSwarmswarm =newBeeSwarm();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);BeebestSolution =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:

CostTrendchart =newCostTrend(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:

CostFunctionRosenbrock =newCostFunction() {publicdoublecalc(doublex,doubley) {doubleexpr1 = (x*x -y);doubleexpr2 =1 -x;return100 *expr1*expr1 +expr2*expr2; }@Overridepublicdoubleevaluate(List<Double>solution,List<Double>lowerBounds,List<Double>upperBounds) {returncalc(solution.get(0),solution.get(1)); }};ParticleSwarmswarm =newParticleSwarm();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);ParticlebestSolution =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:

CostTrendchart =newCostTrend(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 instanceTspBenchmarkbenchmark =Tsp.get(Tsp.Instance.bayg29);PathCostFunctioncostFunction =newPathCostFunction() {// compute the cost of the tour constructed by an ant on the problem bayg29@Overridepublicdoubleevaluate(List<Integer>path) {doublecost =0;for(inti=0;i <path.size(); ++i) {intj = (i+1) %path.size();doubledistance =benchmark.distance(path.get(i),path.get(j));cost +=distance;    }returncost; }// heuristic weight for transition from state1 to state2 during path construction// the higher the weight the more favorable to transit from state1 to state2@OverridepublicdoublestateTransitionWeight(intstate1,intstate2) {return1 / (1 +benchmark.distance(state1,state2)); }};AntSystemantSystem =newAntSystem();antSystem.setProblemSize(benchmark.size());antSystem.setCostFunction(costFunction);antSystem.setMaxIterations(100);AntbestAnt =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(inti=0;i <bestAnt.getPath().size(); ++i) {intj = (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:

CostTrendchart =newCostTrend(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:

TspBenchmarkbenchmark =Tsp.get(Tsp.Instance.bayg29);PathCostFunctioncostFunction =newPathCostFunction() {@Overridepublicdoubleevaluate(List<Integer>path) {doublecost =0;for(inti=0;i <path.size(); ++i) {intj = (i+1) %path.size();doubledistance =benchmark.distance(path.get(i),path.get(j));cost +=distance;     }returncost;  }// heuristic weight for transition from state1 to state2 during path construction// the higher the weight the more favorable to transit from state1 to state2@OverridepublicdoublestateTransitionWeight(intstate1,intstate2) {return1 / (1 +benchmark.distance(state1,state2));  }};AntColonySystemantColonySystem =newAntColonySystem();antColonySystem.setProblemSize(benchmark.size());antColonySystem.setCostFunction(costFunction);antColonySystem.setMaxIterations(100);AntbestAnt =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(inti=0;i <bestAnt.getPath().size(); ++i) {intj = (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:

CostTrendchart =newCostTrend(antColonySystem.getCostTrend(),"Cost vs Generation");chart.showIt(true);

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp