Swarm.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package psoAlgoCode;
  2. import java.lang.reflect.GenericArrayType;
  3. import java.util.ArrayList;
  4. import java.util.Vector;
  5. public class Swarm {
  6. // A swarm contains a list of particles
  7. private ArrayList<Particle> swarm = new ArrayList<Particle>();
  8. // Record of best global values for each iteration
  9. private Vector<Double> globalBestRecord = new Vector<Double>();
  10. // The best particle so far
  11. private Particle global_best;
  12. public void addMember(Particle p) {
  13. swarm.add(p);
  14. }
  15. public void deleteMember(Particle p) {
  16. swarm.remove(p);
  17. }
  18. public int getSwarmSize() {
  19. return swarm.size();
  20. }
  21. public Particle getGlobalBest() {
  22. return global_best;
  23. }
  24. public double getGlobalBestValue() {
  25. return global_best.getLocalBestValue();
  26. }
  27. public Vector<Double> getGlobalBestRecord() {
  28. return globalBestRecord;
  29. }
  30. public void setGlobalBest(Particle new_global_best) {
  31. this.global_best = new_global_best;
  32. avgHemmingDistToGlobalBest();
  33. }
  34. public void updateGlobalBest(Particle new_global_best) {
  35. if (new_global_best.getLocalBestValue() < global_best.getLocalBestValue()) {
  36. setGlobalBest(new_global_best);
  37. }
  38. }
  39. /**
  40. * Update the local best value of each particle within the swarm
  41. */
  42. public void updateLocalBestValues() {
  43. for (int i = 0; i < Constants.SWARM_SIZE; i++) {
  44. swarm.get(i).updateLocalBest();
  45. }
  46. }
  47. public void addEleToRecord(double gb) {
  48. globalBestRecord.addElement(gb);
  49. }
  50. public ArrayList<Particle> getSwarm() {
  51. return swarm;
  52. }
  53. public String toString() {
  54. String output = "";
  55. for (int i = 0; i < getSwarmSize(); i++) {
  56. output += ("Particle " + (i + 1) + " with pos: " + getSwarm().get(i).getPositionAdv() + " ");
  57. }
  58. return output;
  59. }
  60. /**
  61. * Calculates the average hemming distance of the particles in the swarm towards the current global best position.
  62. * @return averate hemming distance
  63. */
  64. public int avgHemmingDistToGlobalBest() {
  65. int dist =0;
  66. Particle g_best = getGlobalBest();
  67. Vector<Vector<Object>> best_tmp = g_best.getBestLocalPosAdv().getCoords();
  68. for (Particle p : swarm) {
  69. int tmp_dist=0;
  70. Vector<Vector<Object>> curr_pos = p.getPositionAdv().getCoords();
  71. //System.out.println("test for g_best actual value: " + g_best.getActualValue());
  72. //System.out.println("test for g_best local best value: " + g_best.getLocalBestValue());
  73. //System.out.println("test for g_best best local position: " + g_best.getBestLocalPosAdv().toString());
  74. //System.out.println("test for particles " + p.getPositionAdv().getCoords().toString());
  75. //Vector<Object> bla = tmp.get(0);
  76. //System.out.print("what the hell is this for best: " + bla.toString());
  77. for(int i = 0; i < best_tmp.size(); i++){
  78. for(int j = 0; j < best_tmp.get(i).size(); j++) {
  79. if(best_tmp.get(i).get(j) != curr_pos.get(i).get(j)) {
  80. tmp_dist += 1;
  81. }
  82. }
  83. }
  84. //System.out.println("Distance bettwen p and global is : " + tmp_dist );
  85. dist += tmp_dist;
  86. }
  87. //System.out.println("The average hamming dist is: " + (dist/swarm.size()));
  88. return dist/swarm.size();
  89. }
  90. /**
  91. * Calculates and returns the current centroid for all particles in the swarm. The centroids coordinate entries are calculated by counting, which binary entry has the majority in all the particles. The centroids
  92. * entry at the same position is set to the majority entry of the particles
  93. * @return Coordinates of the centroid
  94. */
  95. public Vector<Vector<Object>> calculateSwarmCentroid(){
  96. Vector<Vector<Object>> centroid = new Vector<Vector<Object>>();
  97. Vector<Vector<Object>> coords = getSwarm().get(0).getPositionAdv().getCoords();
  98. for(int i = 0; i < coords.size(); i++) {
  99. centroid.addElement(new Vector<Object>());
  100. for(int j = 0; j < coords.get(i).size(); j++) {
  101. int count =0;
  102. for (Particle p : swarm) {
  103. if((boolean) p.getPositionAdv().getCoords().get(i).get(j)) {
  104. count++;
  105. }else {count--;}
  106. }
  107. //System.out.println("wohoooo count for entry " + j + " is: " + count);
  108. if (count >= 0) {
  109. centroid.get(i).addElement(true);
  110. } else centroid.get(i).addElement(false);
  111. }
  112. }
  113. return centroid;
  114. }
  115. }