123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package psoAlgoCode;
- import java.lang.reflect.GenericArrayType;
- import java.util.ArrayList;
- import java.util.Vector;
- public class Swarm {
- // A swarm contains a list of particles
- private ArrayList<Particle> swarm = new ArrayList<Particle>();
- // Record of best global values for each iteration
- private Vector<Double> globalBestRecord = new Vector<Double>();
- // The best particle so far
- private Particle global_best;
- public void addMember(Particle p) {
- swarm.add(p);
- }
- public void deleteMember(Particle p) {
- swarm.remove(p);
- }
- public int getSwarmSize() {
- return swarm.size();
- }
- public Particle getGlobalBest() {
- return global_best;
- }
- public double getGlobalBestValue() {
- return global_best.getLocalBestValue();
- }
- public Vector<Double> getGlobalBestRecord() {
- return globalBestRecord;
- }
- public void setGlobalBest(Particle new_global_best) {
- this.global_best = new_global_best;
- //avgHemmingDistToGlobalBest();
- }
- public void updateGlobalBest(Particle new_global_best) {
- if (new_global_best.getLocalBestValue() < global_best.getLocalBestValue()) {
- setGlobalBest(new_global_best);
- }
- }
- /**
- * Update the local best value of each particle within the swarm
- */
- public void updateLocalBestValues() {
- for (int i = 0; i < Constants.SWARM_SIZE; i++) {
- swarm.get(i).updateLocalBest();
- }
- }
- public void addEleToRecord(double gb) {
- globalBestRecord.addElement(gb);
- }
- public ArrayList<Particle> getSwarm() {
- return swarm;
- }
- public String toString() {
- String output = "";
- for (int i = 0; i < getSwarmSize(); i++) {
- output += ("Particle " + (i + 1) + " with pos: " + getSwarm().get(i).getPositionAdv() + " ");
- }
- return output;
- }
-
- /**
- * Calculates the average hemming distance of the particles in the swarm towards the current global best position.
- * @return averate hemming distance
- */
- public int avgHemmingDistToGlobalBest() {
- int dist =0;
- Particle g_best = getGlobalBest();
- Vector<Vector<Object>> best_tmp = g_best.getBestLocalPosAdv().getCoords();
-
- for (Particle p : swarm) {
- int tmp_dist=0;
- Vector<Vector<Object>> curr_pos = p.getPositionAdv().getCoords();
- //System.out.println("test for g_best actual value: " + g_best.getActualValue());
- //System.out.println("test for g_best local best value: " + g_best.getLocalBestValue());
- //System.out.println("test for g_best best local position: " + g_best.getBestLocalPosAdv().toString());
- //System.out.println("test for particles " + p.getPositionAdv().getCoords().toString());
- //Vector<Object> bla = tmp.get(0);
- //System.out.print("what the hell is this for best: " + bla.toString());
- for(int i = 0; i < best_tmp.size(); i++){
- for(int j = 0; j < best_tmp.get(i).size(); j++) {
- if(best_tmp.get(i).get(j) != curr_pos.get(i).get(j)) {
- tmp_dist += 1;
- }
-
- }
- }
- //System.out.println("Distance bettwen p and global is : " + tmp_dist );
- dist += tmp_dist;
- }
-
- //System.out.println("The average hamming dist is: " + (dist/swarm.size()));
- return dist/swarm.size();
-
- }
-
- /**
- * 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
- * entry at the same position is set to the majority entry of the particles
- * @return Coordinates of the centroid
- */
- public Vector<Vector<Object>> calculateSwarmCentroid(){
- Vector<Vector<Object>> centroid = new Vector<Vector<Object>>();
- Vector<Vector<Object>> coords = getSwarm().get(0).getPositionAdv().getCoords();
- for(int i = 0; i < coords.size(); i++) {
- centroid.addElement(new Vector<Object>());
- for(int j = 0; j < coords.get(i).size(); j++) {
- int count =0;
- for (Particle p : swarm) {
- if((boolean) p.getPositionAdv().getCoords().get(i).get(j)) {
- count++;
- }else {count--;}
- }
- //System.out.println("wohoooo count for entry " + j + " is: " + count);
- if (count >= 0) {
- centroid.get(i).addElement(true);
- } else centroid.get(i).addElement(false);
-
- }
- }
-
- return centroid;
- }
- }
|