You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
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);
About
Optimization framework based on swarm intelligence