Browse Source

fitness and swarm changes

Rolf Egert 7 years ago
parent
commit
fead0d9e7b
4 changed files with 828 additions and 753 deletions
  1. 57 58
      src/psoAlgoCode/Constants.java
  2. 220 212
      src/psoAlgoCode/SGFunctions.java
  3. 413 410
      src/psoAlgoCode/SimplePSO.java
  4. 138 73
      src/psoAlgoCode/Swarm.java

+ 57 - 58
src/psoAlgoCode/Constants.java

@@ -1,58 +1,57 @@
-package psoAlgoCode;
-
-public class Constants {
-	// Constants PSO
-	public static double ALPHA1 = 2;
-	public static double ALPHA2 = 2;
-	public static int SWARM_SIZE = 20;
-	public static int MAX_ITERATION = 100;
-	public static double PHI = 2.01;
-	public static double RMU = 1.0;
-
-	// 0 = Sphere function, 1 = Rastrigin, 2 = Griewank. Only for problems with
-	// double as input
-	public static int function = 0;
-
-	// Constants Chart
-	public static int ROUNDS = 1;
-
-	// Variables PSO
-	public static double START_OMEGA = 0.7;//1.2;//0.7;
-	public static double END_OMEGA = 0.4;//0.2;//0.4;
-	public static double DIFF_OMEGA = ((START_OMEGA - END_OMEGA) / MAX_ITERATION);
-
-	// Dimensions determines the length of the
-	public static int DIMENSIONS = 0;
-
-	public static void setDimensions(int dim) {
-		DIMENSIONS = dim;
-	}
-
-	public static void setAlpha1(double alpha) {
-		ALPHA1 = alpha;
-	}
-
-	public static void setAlpha2(double alpha) {
-		ALPHA2 = alpha;
-	}
-
-	public static void setSwarmSize(int size) {
-		SWARM_SIZE = size;
-	}
-
-	public static void setMaxIt(int it) {
-		MAX_ITERATION = it;
-	}
-
-	public static void setRounds(int rounds) {
-		ROUNDS = rounds;
-	}
-
-	public static void setPhi(double phi) {
-		PHI = phi;
-	}
-
-	public static void setRMU(double rmu) {
-		RMU = rmu;
-	}
-}
+package psoAlgoCode;
+
+public class Constants {
+	// Constants PSO
+	public static double ALPHA1 = 2;
+	public static double ALPHA2 = 2;
+	public static int SWARM_SIZE = 50;
+	public static int MAX_ITERATION = 1000;
+	public static double PHI = 2.01;//intervall [2.01,2.4]
+	public static double RMU = 1.0;
+
+	// 0 = Sphere function, 1 = Rastrigin, 2 = Griewank. Only for problems with
+	// double as input
+	public static int function = 0;
+
+	// Constants Chart
+	public static int ROUNDS = 1;
+
+	// Variables PSO
+	public static double START_OMEGA =0.7;//1.2;//0.7;
+	public static double END_OMEGA = 0.4;//0.2;//0.4;
+	public static double DIFF_OMEGA = ((START_OMEGA - END_OMEGA) / MAX_ITERATION);
+
+	// Dimensions determines the length of the
+	public static int DIMENSIONS = 0;
+
+	public static void setDimensions(int dim) {
+		DIMENSIONS = dim;
+	}
+
+	public static void setAlpha1(double alpha) {
+		ALPHA1 = alpha;
+	}
+
+	public static void setAlpha2(double alpha) {
+		ALPHA2 = alpha;
+	}
+	public static void setSwarmSize(int size) {
+		SWARM_SIZE = size;
+	}
+
+	public static void setMaxIt(int it) {
+		MAX_ITERATION = it;
+	}
+
+	public static void setRounds(int rounds) {
+		ROUNDS = rounds;
+	}
+
+	public static void setPhi(double phi) {
+		PHI = phi;
+	}
+
+	public static void setRMU(double rmu) {
+		RMU = rmu;
+	}
+}

+ 220 - 212
src/psoAlgoCode/SGFunctions.java

@@ -1,212 +1,220 @@
-package psoAlgoCode;
-
-import java.util.ArrayList;
-import java.util.Vector;
-
-import org.omg.PortableInterceptor.NON_EXISTENT;
-
-import classes.AbstractCpsObject;
-import classes.HolonElement;
-import classes.HolonObject;
-import classes.SubNet;
-import ui.controller.Control;
-import ui.model.Model;
-
-public class SGFunctions {
-
-	private Model model;
-	private Control control;
-	private Particle p;
-
-	public SGFunctions(Particle p, Model model, Control control) {
-		this.p = p;
-		this.model = model;
-		this.control = control;
-	}
-
-	public void implementState() {
-		Coordinate<Vector<Object>> pos = p.getPositionAdv();
-		// Set states of the HolonSwitches depending on the pos of the swarm (dim=0)
-		for (int i = 0; i < pos.getCoord(0).size(); i++) {
-			model.getSwitches().get(i).setManualMode(true);
-			model.getSwitches().get(i).setManualState((boolean) pos.getCoord(0).get(i));
-		}
-		int position = 0;
-		for (int j = 0; j < model.getObjectsOnCanvas().size(); j++) {
-			if (model.getObjectsOnCanvas().get(j) instanceof HolonObject) {
-				for (int h = 0; h < ((HolonObject) model.getObjectsOnCanvas().get(j)).getElements().size(); h++) {
-					((HolonObject) model.getObjectsOnCanvas().get(j)).getElements().get(h)
-							.setActive((boolean) pos.getCoord(1).get(position));
-					position++;
-				}
-
-			}
-		}
-
-	}
-
-	public double calcValuewithSliderState() {
-		implementState();
-		HelpFunctions.calculateStateForTimeStepPSO(model, control);
-		double value = 0.0;
-		double nw_fitness =0.0;
-		double object_fitness = 0.0;
-
-		nw_fitness = networkFitness();
-		object_fitness = holonObjectFitness();
-		
-		System.out.println("NW_fitness: " + nw_fitness +" objectFitness: " + object_fitness);
-		
-		value = nw_fitness + object_fitness;
-	
-		
-	
-		return value;
-	}
-
-	/**
-	 * Calculates a fitness value for the overall abstract network
-	 * @return
-	 */
-	private double networkFitness() {
-		double result = 0.0;
-		ArrayList<SubNet> tmp_sn = HelpFunctions.getCurrentSubnets();
-		
-		for (SubNet subNet : tmp_sn) {
-			ArrayList<HolonObject> tmp = subNet.getObjects();
-			for (HolonObject holonObject : tmp) {
-				System.out.println("Current state: " +holonObject.getState());
-			}
-		//	float sn_prod =0;
-			//float sn_cons =0;
-			
-			float production = control.getSimManager().calculateEnergyWithoutFlexDevices("prod",
-					subNet, model.getCurIteration());
-			float consumption = control.getSimManager().calculateEnergyWithoutFlexDevices("cons",
-					subNet, model.getCurIteration());
-			System.out.println("current production: " + production + " current consumption: " + consumption);
-			
-		result += Math.abs(production+consumption)*10;	
-		}
-		
-		//result = (tmp_sn.size() - 1) * 100;
-		
-		
-		
-		return result;
-	}
-	/**
-	 * Calculate a fitnessvalue concerned with the performance of individual holons of the network
-	 * @return
-	 */
-	private double holonFitness() {
-		double result = 0.0;
-		
-		return result;
-	}
-	/**
-	 * Calculates a fitnessvalue for the individual holon objects in the holons
-	 * @return
-	 */
-	private double holonObjectFitness() {
-		double result = 0.0;
-		ArrayList<AbstractCpsObject> objList = model.getObjectsOnCanvas();
-
-		for (AbstractCpsObject obj : objList) {
-			if (obj instanceof HolonObject) {
-				float suppPercentage = ((HolonObject) obj).getSuppliedPercentage();
-				/*float supply = ((HolonObject) obj).getCurrentSupply();
-				float energy =((HolonObject) obj).getMaxActiveEnergy();
-				float ownProduction = ((HolonObject) obj).getSelfMadeEnergy(model.getCurIteration());
-				float maxEnergy = ((HolonObject) obj).getMaxPossibleConsumption();
-				int elem = ((HolonObject) obj).getNumberOfActiveElements();
-			*/
-				if(!(obj.getName().contains("Plant"))) {
-					result += holonObjectSupplyPenaltyFunction(suppPercentage);
-					result += holonObjectStatePenalty((HolonObject)obj);
-					
-				}
-					result += inactiveHolonElementPenalty((HolonObject)obj);
-		
-			}
-			
-		}
-		
-		return result;
-	}
-	
-	/**
-	 * Calculates a penalty value based on the HOs current supply percentage
-	 * @param supplyPercentage
-	 * @return
-	 */
-	private double holonObjectSupplyPenaltyFunction(float supplyPercentage) {
-		float result = 0;
-		if(supplyPercentage == 1)
-			return result;
-		else if(supplyPercentage < 1 && supplyPercentage >= 0.25) // undersupplied inbetween 25% and 100%
-			result = (float) (1/supplyPercentage *15);
-		else if (supplyPercentage < 0.25) //undersupplied with less than 25%
-			result = 1/supplyPercentage * 20;
-		else if (supplyPercentage < 1.25)  //Oversupplied less than 25%
-			result = 1 * supplyPercentage *20 ;
-		else result = 1 * supplyPercentage * 30; //Oversupplied more than 25%
-		
-		if(Float.isInfinite(result) || Float.isNaN(result))
-			result = 100;
-
-		return result;
-	}
-	
-	private double inactiveHolonElementPenalty(HolonObject obj) {
-		float result = 0;
-		int activeElements = obj.getNumberOfActiveElements();
-		int maxElements = obj.getElements().size();
-		
-		if(activeElements == maxElements)
-			result =0;
-		else if(activeElements == 0) {
-			result = 10000;
-		}else result = ((maxElements -activeElements)*600);
-		
-	return result;
-		
-	}
-	/**
-	 * Pentalty Function that is based on the different states an object can have.
-	 * @param obj The holon object that is used for the assessment
-	 * @return pentalty value
-	 */
-	private double holonObjectStatePenalty(HolonObject obj) {
-		float result = 0;
-		
-		//TODO currently no penalties on undesired states, since the state assignment is broken...
-		int state = obj.getState();
-		switch (state) {
-		case 0: result = 0;
-				break;
-		case 1: result = 0;
-			break;
-		case 2: result = 0;
-			break;
-		case 3: result = 0;
-			break;
-		case 4: result = 0;
-			break;
-		case 5:	result = 0;
-			break;
-			
-
-		default:
-			break;
-		}
-		
-		
-		
-		
-		return result;
-	}
-	
-
-
-}
+package psoAlgoCode;
+
+import java.util.ArrayList;
+import java.util.Vector;
+
+import org.omg.PortableInterceptor.NON_EXISTENT;
+
+import classes.AbstractCpsObject;
+import classes.HolonElement;
+import classes.HolonObject;
+import classes.SubNet;
+import ui.controller.Control;
+import ui.model.Model;
+
+public class SGFunctions {
+
+	private Model model;
+	private Control control;
+	private Particle p;
+
+	public SGFunctions(Particle p, Model model, Control control) {
+		this.p = p;
+		this.model = model;
+		this.control = control;
+	}
+
+	public void implementState() {
+		Coordinate<Vector<Object>> pos = p.getPositionAdv();
+		// Set states of the HolonSwitches depending on the pos of the swarm (dim=0)
+		for (int i = 0; i < pos.getCoord(0).size(); i++) {
+			model.getSwitches().get(i).setManualMode(true);
+			model.getSwitches().get(i).setManualState((boolean) pos.getCoord(0).get(i));
+		}
+		int position = 0;
+		for (int j = 0; j < model.getObjectsOnCanvas().size(); j++) {
+			if (model.getObjectsOnCanvas().get(j) instanceof HolonObject) {
+				for (int h = 0; h < ((HolonObject) model.getObjectsOnCanvas().get(j)).getElements().size(); h++) {
+					((HolonObject) model.getObjectsOnCanvas().get(j)).getElements().get(h)
+							.setActive((boolean) pos.getCoord(1).get(position));
+					position++;
+				}
+
+			}
+		}
+
+	}
+
+	public double calcValuewithSliderState() {
+		implementState();
+		HelpFunctions.calculateStateForTimeStepPSO(model, control);
+		double value = 0.0;
+		double nw_fitness =0.0;
+		double object_fitness = 0.0;
+
+		nw_fitness = networkFitness();
+		object_fitness = holonObjectFitness();
+		
+	//	System.out.println("NW_fitness: " + nw_fitness +" objectFitness: " + object_fitness);
+		
+		value = nw_fitness + object_fitness;
+	
+		
+	
+		return value;
+	}
+
+	/**
+	 * Calculates a fitness value for the overall abstract network
+	 * @return
+	 */
+	private double networkFitness() {
+		double result = 0.0;
+		ArrayList<SubNet> tmp_sn = HelpFunctions.getCurrentSubnets();
+		
+		for (SubNet subNet : tmp_sn) {
+			ArrayList<HolonObject> tmp = subNet.getObjects();
+			for (HolonObject holonObject : tmp) {
+			//	System.out.println("Current state: " +holonObject.getState());
+				if(holonObject.getNumberOfActiveElements() == 0)
+					result += 5000;
+			}
+		//	float sn_prod =0;
+			//float sn_cons =0;
+			
+			float production = control.getSimManager().calculateEnergyWithoutFlexDevices("prod",
+					subNet, model.getCurIteration());
+			float consumption = control.getSimManager().calculateEnergyWithoutFlexDevices("cons",
+					subNet, model.getCurIteration());
+			//System.out.println("current production: " + production + " current consumption: " + consumption);
+			
+		result += Math.abs(production+consumption);	
+		}
+		
+		//result = (tmp_sn.size() - 1) * 100;
+		
+		
+		
+		return result;
+	}
+	/**
+	 * Calculate a fitnessvalue concerned with the performance of individual holons of the network
+	 * @return
+	 */
+	private double holonFitness() {
+		double result = 0.0;
+		
+		return result;
+	}
+	/**
+	 * Calculates a fitnessvalue for the individual holon objects in the holons
+	 * @return
+	 */
+	private double holonObjectFitness() {
+		double result = 0.0;
+		ArrayList<AbstractCpsObject> objList = model.getObjectsOnCanvas();
+
+		for (AbstractCpsObject obj : objList) {
+			if (obj instanceof HolonObject) {
+				/*float supply = ((HolonObject) obj).getCurrentSupply();
+				float energy =((HolonObject) obj).getMaxActiveEnergy();
+				float ownProduction = ((HolonObject) obj).getSelfMadeEnergy(model.getCurIteration());
+				float maxEnergy = ((HolonObject) obj).getMaxPossibleConsumption();
+				int elem = ((HolonObject) obj).getNumberOfActiveElements();
+			*/
+				if(!(obj.getName().contains("Plant"))) {
+					float suppPercentage = ((HolonObject) obj).getSuppliedPercentage();
+					result += holonObjectSupplyPenaltyFunction(suppPercentage);
+					//result += holonObjectStatePenalty((HolonObject)obj);
+					
+				}
+					result += inactiveHolonElementPenalty((HolonObject)obj);
+		
+			}
+			
+		}
+		
+		return result;
+	}
+	
+	/**
+	 * Calculates a penalty value based on the HOs current supply percentage
+	 * @param supplyPercentage
+	 * @return
+	 */
+	private double holonObjectSupplyPenaltyFunction(float supplyPercentage) {
+		float result = 0;
+		if(supplyPercentage == 1)
+			return result;
+		/*else if(supplyPercentage < 1)
+			result = (float) Math.pow(1/supplyPercentage, 2);
+		else result = (float) Math.pow(supplyPercentage,2) ;*/  
+		else if(supplyPercentage < 1 && supplyPercentage >= 0.25) // undersupplied inbetween 25% and 100%
+			result = (float) Math.pow(1/supplyPercentage, 2);
+		else if (supplyPercentage < 0.25) //undersupplied with less than 25%
+			result = (float) Math.pow(1/supplyPercentage,3);
+		else if (supplyPercentage < 1.25)  //Oversupplied less than 25%
+			result = (float) Math.pow(supplyPercentage,2) ;
+		else result = (float) Math.pow(supplyPercentage,3); //Oversupplied more than 25%
+		
+		
+		if(Float.isInfinite(result) || Float.isNaN(result))
+			result = 1000;
+
+		return result;
+	}
+	
+	private double inactiveHolonElementPenalty(HolonObject obj) {
+		float result = 0;
+		int activeElements = obj.getNumberOfActiveElements();
+		int maxElements = obj.getElements().size();
+		
+		if(activeElements == maxElements)
+			result =0;
+		else result = (float) Math.pow((maxElements -activeElements),2)*10
+				;
+		/*else if(activeElements == 0) {
+			result = 0;*/
+		//}
+		
+	return result;
+		
+	}
+	/**
+	 * Pentalty Function that is based on the different states an object can have.
+	 * @param obj The holon object that is used for the assessment
+	 * @return pentalty value
+	 */
+	private double holonObjectStatePenalty(HolonObject obj) {
+		float result = 0;
+		
+		//TODO currently no penalties on undesired states, since the state assignment is broken...
+		int state = obj.getState();
+		switch (state) {
+		case 0: result = 0;
+				break;
+		case 1: result = 0;
+			break;
+		case 2: result = 0;
+			break;
+		case 3: result = 0;
+			break;
+		case 4: result = 0;
+			break;
+		case 5:	result = 0;
+			break;
+			
+
+		default:
+			break;
+		}
+		
+		
+		
+		
+		return result;
+	}
+	
+
+
+}

+ 413 - 410
src/psoAlgoCode/SimplePSO.java

@@ -1,410 +1,413 @@
-package psoAlgoCode;
-
-import java.util.Random;
-import java.util.Vector;
-
-import ui.controller.Control;
-import ui.model.Model;
-
-public class SimplePSO {
-
-	// Random factors for calculations with "doubles"
-	private double D1;
-	private double D2;
-
-	private boolean B1;
-	private boolean B2;
-
-	// New BPSO
-	private double R1;
-	private double R2;
-	private double w;
-	private double phi;
-	private double c1;
-	private double c2;
-	private double rmu;
-
-	// Function to be used
-	private Function f;
-
-	private int iterations;
-	private Swarm swarm = new Swarm();
-	private Random random = new Random();
-
-	public SimplePSO(Model model, Control control, Coordinate<Vector<Object>> startPos) {
-		iterations = 0;
-		if (startPos == null) {
-			Constants.setDimensions(10);
-		} else {
-			Constants.setDimensions(startPos.getCoords().size());
-		}
-		f = new Function();
-	}
-
-	/**
-	 * Initiate the swarm with random values depending on the type of calculate
-	 * (boolean or doubles)
-	 */
-	private void initSwarmRandom() {
-		for (int i = 0; i < Constants.SWARM_SIZE; i++) {
-			Particle p = new Particle(Constants.DIMENSIONS);
-			Coordinate<Vector<Object>> tempPos = new Coordinate<Vector<Object>>();
-			Coordinate<Vector<Object>> tempVel = new Coordinate<Vector<Object>>();
-			for (int j = 0; j < Constants.DIMENSIONS; j++) {
-				tempPos.setCoord(RandomFunctions.nextDoubles(1), j);
-				tempVel.setCoord(RandomFunctions.nextDoubles(1), j);
-			}
-			p.setPositionAdv(tempPos);
-			p.setVelocityAdv(tempVel);
-			swarm.addMember(p);
-		}
-		initCoeff();
-	}
-
-	private void initSwarm(Coordinate<Vector<Object>> startPos) {
-		for (int i = 0; i < Constants.SWARM_SIZE; i++) {
-			Particle p = new Particle(Constants.DIMENSIONS);
-			Coordinate<Vector<Object>> tempPos = new Coordinate<Vector<Object>>();
-			Coordinate<Vector<Object>> tempVel = new Coordinate<Vector<Object>>();
-			for (int j = 0; j < Constants.DIMENSIONS; j++) {
-				int dim = startPos.getCoord(j).size();
-				tempPos.setCoord(RandomFunctions.nextBoolean(dim), j);
-				tempVel.setCoord(RandomFunctions.nextRandoms(dim), j);
-			}
-			p.setPositionAdv(tempPos);
-			p.setVelocityAdv(tempVel);
-			swarm.addMember(p);
-		}
-		initCoeff();
-	}
-
-	private void runFunction(Model model, Control control) {
-		for (int i = 0; i < Constants.SWARM_SIZE; i++) {
-			swarm.getSwarm().get(i).setActualValue(f.execute(swarm.getSwarm().get(i), i + 1, model, control));
-		}
-	}
-
-	private void initCoeff() {
-		// Coefficients for the calculation of velocity after S. Lee
-		phi = Constants.PHI;
-		rmu = Constants.RMU;
-		w = 1 / ((phi - 1) + Math.sqrt(Math.pow(phi, 2) - 2 * phi));
-		c1 = phi * w;
-		c2 = c1;
-	}
-
-	public void caclSimplePSO(Model model, Control control, Coordinate<Vector<Object>> startPos) {
-		initSwarm(startPos);
-		runFunction(model, control);
-		evaluate();
-
-		while (iterations <= Constants.MAX_ITERATION) {
-			for (int i = 0; i < Constants.SWARM_SIZE; i++) {
-				Particle temp = swarm.getSwarm().get(i);
-
-				// Binary PSO 2 (S. Lee)
-				// Update Velocity
-				temp.setVelocityAdv(updateNewVelAdv(temp.getVelocityAdv(), temp.getPositionAdv(),
-						temp.getBestLocalPosAdv(), iterations));
-				// Update Position
-				temp.setPositionAdv(updateNewPosAdv(temp.getVelocityAdv(), temp.getPositionAdv()));
-				// Mutation Position
-				temp.setPositionAdv(mutatePos(temp.getPositionAdv()));
-				// Decode Position
-				temp.setPositionAdv(decodePos(temp.getPositionAdv()));
-
-				// Uncomment this two line and change the init of Vel to nextBoolean(dim) ->
-				// binary PSO 1
-				// temp.setVelocityAdv(updateVelAdv(temp.getVelocityAdv(),
-				// temp.getPositionAdv(),
-				// temp.getBestLocalPosAdv(), iterations));
-				// temp.setPositionAdv(updatePosAdv(temp.getVelocityAdv(),
-				// temp.getPositionAdv()));
-			}
-			// plotSwarm();
-			iterations++;
-			runFunction(model, control);
-			evaluate();
-		}
-	}
-
-	public void caclNextItSimplePSO(Model model, Control control, Coordinate<Vector<Object>> startPos) {
-		initSwarm(startPos);
-		runFunction(model, control);
-		evaluate();
-		for (int i = 0; i < Constants.SWARM_SIZE; i++) {
-			Particle temp = swarm.getSwarm().get(i);
-			// Update Velocity
-			temp.setVelocityAdv(updateNewVelAdv(temp.getVelocityAdv(), temp.getPositionAdv(), temp.getBestLocalPosAdv(),
-					iterations));
-			// Update Position
-			temp.setPositionAdv(updateNewPosAdv(temp.getVelocityAdv(), temp.getPositionAdv()));
-			// Mutation Position
-			temp.setPositionAdv(mutatePos(temp.getPositionAdv()));
-			// Decode Position
-			temp.setPositionAdv(decodePos(temp.getPositionAdv()));
-		}
-		evaluate();
-		iterations++;
-	}
-
-	/**
-	 * The boolean-based function to update the velocity of a particle depending on
-	 * the inertia, social and cognitive factors. Here is important to consider the
-	 * analogy of the operators.This calculation is based in the binary PSO version
-	 * from S Lee.
-	 * 
-	 * @param vel
-	 * @param pos
-	 * @param bestLocal
-	 * @param index
-	 * @return
-	 */
-	private Coordinate<Vector<Object>> updateNewVelAdv(Coordinate<Vector<Object>> vel, Coordinate<Vector<Object>> pos,
-			Coordinate<Vector<Object>> bestLocal, int index) {
-		Coordinate<Vector<Object>> result = new Coordinate<Vector<Object>>();
-		for (int dim = 0; dim < Constants.DIMENSIONS; dim++) {
-			Vector<Object> newCoord = new Vector<Object>();
-			R1 = Math.random();
-			R2 = Math.random();
-			for (int i = 0; i < vel.getCoord(dim).size(); i++) {
-				if (pos.getCoord(dim).get(i).getClass().equals(Boolean.class)) {
-					// Random coefficients for the calculation of velocity after S. Lee
-
-					double value = 0.0;
-					value = (w * (double) vel.getCoord(dim).get(i))
-							+ (c1 * R1
-									* (transformBoolToDouble((boolean) bestLocal.getCoord(dim).get(i))
-											- transformBoolToDouble((boolean) pos.getCoord(dim).get(i))))
-							+ (c2 * R2
-									* (transformBoolToDouble(
-											(boolean) swarm.getGlobalBest().getPositionAdv().getCoord(dim).get(i))
-											- transformBoolToDouble((boolean) pos.getCoord(dim).get(i))));
-
-					newCoord.add(i, value);
-
-				} else if (vel.getCoord(dim).get(i).getClass().equals(Double.class)) {
-					D1 = random.nextDouble();
-					D2 = random.nextDouble();
-					double value = 0.0;
-					value = getOmega(index) * (double) vel.getCoord(dim).get(i)
-							+ (Constants.ALPHA1 * D1
-									* ((double) bestLocal.getCoord(dim).get(i) - (double) pos.getCoord(dim).get(i)))
-							+ (Constants.ALPHA2 * D2
-									* ((double) swarm.getGlobalBest().getPositionAdv().getCoord(dim).get(i)
-											- (double) pos.getCoord(dim).get(i)));
-					newCoord.add(i, value);
-				}
-			}
-			result.setCoord(newCoord, dim);
-		}
-		return result;
-	}
-
-	/**
-	 * The boolean-based function to update the position of a particle adding the
-	 * current position with the new velocity (calculated in updateVel(Coordinates
-	 * vel, Coordinates pos, Coordinates bestLocal, int i)). Here is important to
-	 * consider the analogy of the operators. This calculation is based in the
-	 * binary PSO version from S Lee.
-	 * 
-	 * @param vel
-	 *            the updated velocity
-	 * @param pos
-	 *            the current position
-	 * @return the updated position of the particle
-	 */
-	private Coordinate<Vector<Object>> updateNewPosAdv(Coordinate<Vector<Object>> vel, Coordinate<Vector<Object>> pos) {
-		Coordinate<Vector<Object>> newPos = new Coordinate<Vector<Object>>();
-		for (int dim = 0; dim < Constants.DIMENSIONS; dim++) {
-			Vector<Object> newCoord = new Vector<Object>();
-			for (int i = 0; i < vel.getCoord(dim).size(); i++) {
-				if (pos.getCoord(dim).get(i).getClass().equals(Boolean.class)) {
-					double value = 0.0;
-					value = transformBoolToDouble((boolean) pos.getCoord(dim).get(i))
-							+ (double) vel.getCoord(dim).get(i);
-					newCoord.add(i, value);
-				}
-				if (pos.getCoord(dim).get(i).getClass().equals(Double.class)) {
-					double value = 0.0;
-					value = (double) pos.getCoord(dim).get(i) + (double) vel.getCoord(dim).get(i);
-					newCoord.add(i, value);
-				}
-			}
-			newPos.setCoord(newCoord, dim);
-		}
-		return newPos;
-	}
-
-	private Coordinate<Vector<Object>> mutatePos(Coordinate<Vector<Object>> pos) {
-		Coordinate<Vector<Object>> newPos = new Coordinate<Vector<Object>>();
-		for (int dim = 0; dim < Constants.DIMENSIONS; dim++) {
-			Vector<Object> newCoord = new Vector<Object>();
-			for (int i = 0; i < pos.getCoord(dim).size(); i++) {
-				if (Math.random() < rmu) {
-					newCoord.add(i, -(double) pos.getCoord(dim).get(i));
-				} else {
-					newCoord.add(i, (double) pos.getCoord(dim).get(i));
-				}
-			}
-			newPos.setCoord(newCoord, dim);
-		}
-		return newPos;
-	}
-
-	private Coordinate<Vector<Object>> decodePos(Coordinate<Vector<Object>> pos) {
-		Coordinate<Vector<Object>> newPos = new Coordinate<Vector<Object>>();
-		for (int dim = 0; dim < Constants.DIMENSIONS; dim++) {
-			Vector<Object> newCoord = new Vector<Object>();
-			for (int i = 0; i < pos.getCoord(dim).size(); i++) {
-				double decoder = sigmoid((double) pos.getCoord(dim).get(i));
-				if (Math.random() >= decoder) {
-					newCoord.add(i, true);
-				} else {
-					newCoord.add(i, false);
-				}
-			}
-			newPos.setCoord(newCoord, dim);
-		}
-		return newPos;
-	}
-
-	private double sigmoid(double input) {
-		double result = 0.0;
-		result = 1 / (1 + Math.pow(Math.E, -input));
-		return result;
-	}
-
-	private void evaluate() {
-		swarm.updateLocalBestValues();
-		// Update Global Best
-		Particle temp_best = getBestValue();
-		temp_best.updateLocalBest();
-		if (iterations == 0) {
-			swarm.setGlobalBest(temp_best);
-		} else {
-			swarm.updateGlobalBest(temp_best);
-		}
-		swarm.addEleToRecord(swarm.getGlobalBest().getLocalBestValue());
-	}
-
-	private double transformBoolToDouble(boolean input) {
-		if (input == true) {
-			return 1.0;
-		} else {
-			return 0.0;
-		}
-	}
-
-	/**
-	 * Calculation of binary XOR operator
-	 * 
-	 * @param b1
-	 *            Boolean 1
-	 * @param b2
-	 *            Boolean 2
-	 * @return b1 XOR b2
-	 */
-	private Boolean calcXOR(Boolean b1, Boolean b2) {
-		Boolean result = false;
-		result = (!b1 && b2) || (b1 && !b2);
-		return result;
-	}
-
-	public Particle getBestValue() {
-		Particle temp_particle = swarm.getSwarm().get(0);
-		temp_particle.setActualValue(swarm.getSwarm().get(0).getActualValue());
-		for (int i = 1; i < Constants.SWARM_SIZE; i++) {
-			if (swarm.getSwarm().get(i).getActualValue() < temp_particle.getActualValue()) {
-				temp_particle = swarm.getSwarm().get(i);
-			}
-		}
-		return temp_particle;
-	}
-
-	public Vector<Double> getGBRecord() {
-		return swarm.getGlobalBestRecord();
-	}
-
-	public Coordinate<Vector<Object>> getBestConfig() {
-		Coordinate<Vector<Object>> result = new Coordinate<Vector<Object>>();
-		result = swarm.getGlobalBest().getBestLocalPosAdv();
-		return result;
-	}
-
-	public String getNameOfFunc() {
-		return f.name;
-	}
-
-	/**
-	 * Return the Omega depending the current iteration.
-	 * 
-	 * @param ite
-	 * @return
-	 */
-	public double getOmega(int ite) {
-		double newOmega = Constants.START_OMEGA - (Constants.DIFF_OMEGA * ite);
-		return newOmega;
-	}
-
-	public int getIteration() {
-		return iterations;
-	}
-
-	private Coordinate<Vector<Object>> updateVelAdv(Coordinate<Vector<Object>> vel, Coordinate<Vector<Object>> pos,
-			Coordinate<Vector<Object>> bestLocal, int index) {
-		Coordinate<Vector<Object>> result = new Coordinate<Vector<Object>>();
-		for (int dim = 0; dim < Constants.DIMENSIONS; dim++) {
-			Vector<Object> newCoord = new Vector<Object>();
-			for (int i = 0; i < vel.getCoord(dim).size(); i++) {
-				if (pos.getCoord(dim).get(i).getClass().equals(Boolean.class)) {
-					boolean omega = random.nextBoolean();
-					B1 = random.nextBoolean();
-					B2 = random.nextBoolean();
-					boolean value = true;
-
-					value = (omega && (boolean) vel.getCoord(dim).get(i)) || (B1
-							&& (calcXOR((boolean) bestLocal.getCoord(dim).get(i), (boolean) pos.getCoord(dim).get(i))))
-							|| (B2 && (calcXOR((boolean) swarm.getGlobalBest().getPositionAdv().getCoord(dim).get(i),
-									(boolean) pos.getCoord(dim).get(i))));
-					newCoord.add(i, value);
-				} else if (vel.getCoord(dim).get(i).getClass().equals(Double.class)) {
-					D1 = random.nextDouble();
-					D2 = random.nextDouble();
-					double value = 0.0;
-					value = getOmega(index) * (double) vel.getCoord(dim).get(i)
-							+ (Constants.ALPHA1 * D1
-									* ((double) bestLocal.getCoord(dim).get(i) - (double) pos.getCoord(dim).get(i)))
-							+ (Constants.ALPHA2 * D2
-									* ((double) swarm.getGlobalBest().getPositionAdv().getCoord(dim).get(i)
-											- (double) pos.getCoord(dim).get(i)));
-					newCoord.add(i, value);
-				}
-			}
-			result.setCoord(newCoord, dim);
-		}
-		return result;
-	}
-
-	private Coordinate<Vector<Object>> updatePosAdv(Coordinate<Vector<Object>> vel, Coordinate<Vector<Object>> pos) {
-		Coordinate<Vector<Object>> newPos = new Coordinate<Vector<Object>>();
-		for (int dim = 0; dim < Constants.DIMENSIONS; dim++) {
-			Vector<Object> newCoord = new Vector<Object>();
-			for (int i = 0; i < vel.getCoord(dim).size(); i++) {
-				if (pos.getCoord(dim).get(i).getClass().equals(Boolean.class)) {
-					boolean value = false;
-					value = calcXOR((boolean) pos.getCoord(dim).get(i), (boolean) vel.getCoord(dim).get(i));
-					newCoord.add(i, value);
-				}
-				if (pos.getCoord(dim).get(i).getClass().equals(Double.class)) {
-					double value = 0.0;
-					value = (double) pos.getCoord(dim).get(i) + (double) vel.getCoord(dim).get(i);
-					newCoord.add(i, value);
-				}
-			}
-			newPos.setCoord(newCoord, dim);
-		}
-		return newPos;
-	}
-
-}
+package psoAlgoCode;
+
+import java.util.Random;
+import java.util.Vector;
+
+import ui.controller.Control;
+import ui.model.Model;
+
+public class SimplePSO {
+
+	// Random factors for calculations with "doubles"
+	private double D1;
+	private double D2;
+
+	private boolean B1;
+	private boolean B2;
+
+	// New BPSO
+	private double R1;
+	private double R2;
+	private double w;
+	private double phi;
+	private double c1;
+	private double c2;
+	private double rmu;
+
+	// Function to be used
+	private Function f;
+
+	private int iterations;
+	private Swarm swarm = new Swarm();
+	private Random random = new Random();
+
+	public SimplePSO(Model model, Control control, Coordinate<Vector<Object>> startPos) {
+		iterations = 0;
+		if (startPos == null) {
+			Constants.setDimensions(10);
+		} else {
+			Constants.setDimensions(startPos.getCoords().size());
+		}
+		f = new Function();
+	}
+
+	/**
+	 * Initiate the swarm with random values depending on the type of calculate
+	 * (boolean or doubles)
+	 */
+	private void initSwarmRandom() {
+		for (int i = 0; i < Constants.SWARM_SIZE; i++) {
+			Particle p = new Particle(Constants.DIMENSIONS);
+			Coordinate<Vector<Object>> tempPos = new Coordinate<Vector<Object>>();
+			Coordinate<Vector<Object>> tempVel = new Coordinate<Vector<Object>>();
+			for (int j = 0; j < Constants.DIMENSIONS; j++) {
+				tempPos.setCoord(RandomFunctions.nextDoubles(1), j);
+				tempVel.setCoord(RandomFunctions.nextDoubles(1), j);
+			}
+			p.setPositionAdv(tempPos);
+			p.setVelocityAdv(tempVel);
+			swarm.addMember(p);
+		}
+		initCoeff();
+	}
+
+	private void initSwarm(Coordinate<Vector<Object>> startPos) {
+		for (int i = 0; i < Constants.SWARM_SIZE; i++) {
+			Particle p = new Particle(Constants.DIMENSIONS);
+			Coordinate<Vector<Object>> tempPos = new Coordinate<Vector<Object>>();
+			Coordinate<Vector<Object>> tempVel = new Coordinate<Vector<Object>>();
+			for (int j = 0; j < Constants.DIMENSIONS; j++) {
+				int dim = startPos.getCoord(j).size();
+				tempPos.setCoord(RandomFunctions.nextBoolean(dim), j);
+				tempVel.setCoord(RandomFunctions.nextRandoms(dim), j);
+			}
+			p.setPositionAdv(tempPos);
+			p.setVelocityAdv(tempVel);
+			swarm.addMember(p);
+		}
+		initCoeff();
+	}
+
+	private void runFunction(Model model, Control control) {
+		for (int i = 0; i < Constants.SWARM_SIZE; i++) {
+			swarm.getSwarm().get(i).setActualValue(f.execute(swarm.getSwarm().get(i), i + 1, model, control));
+		}
+	}
+
+	private void initCoeff() {
+		// Coefficients for the calculation of velocity after S. Lee
+		phi = Constants.PHI;
+		rmu = Constants.RMU;
+		w = 1 / ((phi - 1) + Math.sqrt(Math.pow(phi, 2) - 2 * phi));
+		c1 = phi * w;
+		c2 = c1;
+	}
+
+	public void caclSimplePSO(Model model, Control control, Coordinate<Vector<Object>> startPos) {
+		initSwarm(startPos);
+		runFunction(model, control);
+		evaluate();
+
+		while (iterations <= Constants.MAX_ITERATION) {
+			for (int i = 0; i < Constants.SWARM_SIZE; i++) {
+				Particle temp = swarm.getSwarm().get(i);
+
+				// Binary PSO 2 (S. Lee)
+				// Update Velocity
+				temp.setVelocityAdv(updateNewVelAdv(temp.getVelocityAdv(), temp.getPositionAdv(),
+						temp.getBestLocalPosAdv(), iterations));
+				// Update Position
+				temp.setPositionAdv(updateNewPosAdv(temp.getVelocityAdv(), temp.getPositionAdv()));
+				// Mutation Position
+				temp.setPositionAdv(mutatePos(temp.getPositionAdv()));
+				// Decode Position
+				temp.setPositionAdv(decodePos(temp.getPositionAdv()));
+
+				//System.out.println("calculated centroid: " + swarm.calculateSwarmCentroid().toString());
+				// Uncomment this two line and change the init of Vel to nextBoolean(dim) ->
+				// binary PSO 1
+				// temp.setVelocityAdv(updateVelAdv(temp.getVelocityAdv(),
+				// temp.getPositionAdv(),
+				// temp.getBestLocalPosAdv(), iterations));
+				// temp.setPositionAdv(updatePosAdv(temp.getVelocityAdv(),
+				// temp.getPositionAdv()));
+			}
+			// plotSwarm();
+			iterations++;
+			runFunction(model, control);
+			evaluate();
+		}
+	}
+
+	public void caclNextItSimplePSO(Model model, Control control, Coordinate<Vector<Object>> startPos) {
+		initSwarm(startPos);
+		runFunction(model, control);
+		evaluate();
+		for (int i = 0; i < Constants.SWARM_SIZE; i++) {
+			Particle temp = swarm.getSwarm().get(i);
+			// Update Velocity
+			temp.setVelocityAdv(updateNewVelAdv(temp.getVelocityAdv(), temp.getPositionAdv(), temp.getBestLocalPosAdv(),
+					iterations));
+			// Update Position
+			temp.setPositionAdv(updateNewPosAdv(temp.getVelocityAdv(), temp.getPositionAdv()));
+			// Mutation Position
+			temp.setPositionAdv(mutatePos(temp.getPositionAdv()));
+			// Decode Position
+			temp.setPositionAdv(decodePos(temp.getPositionAdv()));
+		}
+		System.out.println("calculated centroid: " + swarm.calculateSwarmCentroid().toString());
+
+		evaluate();
+		iterations++;
+	}
+
+	/**
+	 * The boolean-based function to update the velocity of a particle depending on
+	 * the inertia, social and cognitive factors. Here is important to consider the
+	 * analogy of the operators.This calculation is based in the binary PSO version
+	 * from S Lee.
+	 * 
+	 * @param vel
+	 * @param pos
+	 * @param bestLocal
+	 * @param index
+	 * @return
+	 */
+	private Coordinate<Vector<Object>> updateNewVelAdv(Coordinate<Vector<Object>> vel, Coordinate<Vector<Object>> pos,
+			Coordinate<Vector<Object>> bestLocal, int index) {
+		Coordinate<Vector<Object>> result = new Coordinate<Vector<Object>>();
+		for (int dim = 0; dim < Constants.DIMENSIONS; dim++) {
+			Vector<Object> newCoord = new Vector<Object>();
+			R1 = Math.random();
+			R2 = Math.random();
+			for (int i = 0; i < vel.getCoord(dim).size(); i++) {
+				if (pos.getCoord(dim).get(i).getClass().equals(Boolean.class)) {
+					// Random coefficients for the calculation of velocity after S. Lee
+
+					double value = 0.0;
+					value = (w * (double) vel.getCoord(dim).get(i))
+							+ (c1 * R1
+									* (transformBoolToDouble((boolean) bestLocal.getCoord(dim).get(i))
+											- transformBoolToDouble((boolean) pos.getCoord(dim).get(i))))
+							+ (c2 * R2
+									* (transformBoolToDouble(
+											(boolean) swarm.getGlobalBest().getPositionAdv().getCoord(dim).get(i))
+											- transformBoolToDouble((boolean) pos.getCoord(dim).get(i))));
+
+					newCoord.add(i, value);
+
+				} else if (vel.getCoord(dim).get(i).getClass().equals(Double.class)) {
+					D1 = random.nextDouble();
+					D2 = random.nextDouble();
+					double value = 0.0;
+					value = getOmega(index) * (double) vel.getCoord(dim).get(i)
+							+ (Constants.ALPHA1 * D1
+									* ((double) bestLocal.getCoord(dim).get(i) - (double) pos.getCoord(dim).get(i)))
+							+ (Constants.ALPHA2 * D2
+									* ((double) swarm.getGlobalBest().getPositionAdv().getCoord(dim).get(i)
+											- (double) pos.getCoord(dim).get(i)));
+					newCoord.add(i, value);
+				}
+			}
+			result.setCoord(newCoord, dim);
+		}
+		return result;
+	}
+
+	/**
+	 * The boolean-based function to update the position of a particle adding the
+	 * current position with the new velocity (calculated in updateVel(Coordinates
+	 * vel, Coordinates pos, Coordinates bestLocal, int i)). Here is important to
+	 * consider the analogy of the operators. This calculation is based in the
+	 * binary PSO version from S Lee.
+	 * 
+	 * @param vel
+	 *            the updated velocity
+	 * @param pos
+	 *            the current position
+	 * @return the updated position of the particle
+	 */
+	private Coordinate<Vector<Object>> updateNewPosAdv(Coordinate<Vector<Object>> vel, Coordinate<Vector<Object>> pos) {
+		Coordinate<Vector<Object>> newPos = new Coordinate<Vector<Object>>();
+		for (int dim = 0; dim < Constants.DIMENSIONS; dim++) {
+			Vector<Object> newCoord = new Vector<Object>();
+			for (int i = 0; i < vel.getCoord(dim).size(); i++) {
+				if (pos.getCoord(dim).get(i).getClass().equals(Boolean.class)) {
+					double value = 0.0;
+					value = transformBoolToDouble((boolean) pos.getCoord(dim).get(i))
+							+ (double) vel.getCoord(dim).get(i);
+					newCoord.add(i, value);
+				}
+				if (pos.getCoord(dim).get(i).getClass().equals(Double.class)) {
+					double value = 0.0;
+					value = (double) pos.getCoord(dim).get(i) + (double) vel.getCoord(dim).get(i);
+					newCoord.add(i, value);
+				}
+			}
+			newPos.setCoord(newCoord, dim);
+		}
+		return newPos;
+	}
+
+	private Coordinate<Vector<Object>> mutatePos(Coordinate<Vector<Object>> pos) {
+		Coordinate<Vector<Object>> newPos = new Coordinate<Vector<Object>>();
+		for (int dim = 0; dim < Constants.DIMENSIONS; dim++) {
+			Vector<Object> newCoord = new Vector<Object>();
+			for (int i = 0; i < pos.getCoord(dim).size(); i++) {
+				if (Math.random() < rmu) {
+					newCoord.add(i, -(double) pos.getCoord(dim).get(i));
+				} else {
+					newCoord.add(i, (double) pos.getCoord(dim).get(i));
+				}
+			}
+			newPos.setCoord(newCoord, dim);
+		}
+		return newPos;
+	}
+
+	private Coordinate<Vector<Object>> decodePos(Coordinate<Vector<Object>> pos) {
+		Coordinate<Vector<Object>> newPos = new Coordinate<Vector<Object>>();
+		for (int dim = 0; dim < Constants.DIMENSIONS; dim++) {
+			Vector<Object> newCoord = new Vector<Object>();
+			for (int i = 0; i < pos.getCoord(dim).size(); i++) {
+				double decoder = sigmoid((double) pos.getCoord(dim).get(i));
+				if (Math.random() >= decoder) {
+					newCoord.add(i, true);
+				} else {
+					newCoord.add(i, false);
+				}
+			}
+			newPos.setCoord(newCoord, dim);
+		}
+		return newPos;
+	}
+
+	private double sigmoid(double input) {
+		double result = 0.0;
+		result = 1 / (1 + Math.pow(Math.E, -input));
+		return result;
+	}
+
+	private void evaluate() {
+		swarm.updateLocalBestValues();
+		// Update Global Best
+		Particle temp_best = getBestValue();
+		temp_best.updateLocalBest();
+		if (iterations == 0) {
+			swarm.setGlobalBest(temp_best);
+		} else {
+			swarm.updateGlobalBest(temp_best);
+		}
+		swarm.addEleToRecord(swarm.getGlobalBest().getLocalBestValue());
+	}
+
+	private double transformBoolToDouble(boolean input) {
+		if (input == true) {
+			return 1.0;
+		} else {
+			return 0.0;
+		}
+	}
+
+	/**
+	 * Calculation of binary XOR operator
+	 * 
+	 * @param b1
+	 *            Boolean 1
+	 * @param b2
+	 *            Boolean 2
+	 * @return b1 XOR b2
+	 */
+	private Boolean calcXOR(Boolean b1, Boolean b2) {
+		Boolean result = false;
+		result = (!b1 && b2) || (b1 && !b2);
+		return result;
+	}
+
+	public Particle getBestValue() {
+		Particle temp_particle = swarm.getSwarm().get(0);
+		temp_particle.setActualValue(swarm.getSwarm().get(0).getActualValue());
+		for (int i = 1; i < Constants.SWARM_SIZE; i++) {
+			if (swarm.getSwarm().get(i).getActualValue() < temp_particle.getActualValue()) {
+				temp_particle = swarm.getSwarm().get(i);
+			}
+		}
+		return temp_particle;
+	}
+
+	public Vector<Double> getGBRecord() {
+		return swarm.getGlobalBestRecord();
+	}
+
+	public Coordinate<Vector<Object>> getBestConfig() {
+		Coordinate<Vector<Object>> result = new Coordinate<Vector<Object>>();
+		result = swarm.getGlobalBest().getBestLocalPosAdv();
+		return result;
+	}
+
+	public String getNameOfFunc() {
+		return f.name;
+	}
+
+	/**
+	 * Return the Omega depending the current iteration.
+	 * 
+	 * @param ite
+	 * @return
+	 */
+	public double getOmega(int ite) {
+		double newOmega = Constants.START_OMEGA - (Constants.DIFF_OMEGA * ite);
+		return newOmega;
+	}
+
+	public int getIteration() {
+		return iterations;
+	}
+
+	private Coordinate<Vector<Object>> updateVelAdv(Coordinate<Vector<Object>> vel, Coordinate<Vector<Object>> pos,
+			Coordinate<Vector<Object>> bestLocal, int index) {
+		Coordinate<Vector<Object>> result = new Coordinate<Vector<Object>>();
+		for (int dim = 0; dim < Constants.DIMENSIONS; dim++) {
+			Vector<Object> newCoord = new Vector<Object>();
+			for (int i = 0; i < vel.getCoord(dim).size(); i++) {
+				if (pos.getCoord(dim).get(i).getClass().equals(Boolean.class)) {
+					boolean omega = random.nextBoolean();
+					B1 = random.nextBoolean();
+					B2 = random.nextBoolean();
+					boolean value = true;
+
+					value = (omega && (boolean) vel.getCoord(dim).get(i)) || (B1
+							&& (calcXOR((boolean) bestLocal.getCoord(dim).get(i), (boolean) pos.getCoord(dim).get(i))))
+							|| (B2 && (calcXOR((boolean) swarm.getGlobalBest().getPositionAdv().getCoord(dim).get(i),
+									(boolean) pos.getCoord(dim).get(i))));
+					newCoord.add(i, value);
+				} else if (vel.getCoord(dim).get(i).getClass().equals(Double.class)) {
+					D1 = random.nextDouble();
+					D2 = random.nextDouble();
+					double value = 0.0;
+					value = getOmega(index) * (double) vel.getCoord(dim).get(i)
+							+ (Constants.ALPHA1 * D1
+									* ((double) bestLocal.getCoord(dim).get(i) - (double) pos.getCoord(dim).get(i)))
+							+ (Constants.ALPHA2 * D2
+									* ((double) swarm.getGlobalBest().getPositionAdv().getCoord(dim).get(i)
+											- (double) pos.getCoord(dim).get(i)));
+					newCoord.add(i, value);
+				}
+			}
+			result.setCoord(newCoord, dim);
+		}
+		return result;
+	}
+
+	private Coordinate<Vector<Object>> updatePosAdv(Coordinate<Vector<Object>> vel, Coordinate<Vector<Object>> pos) {
+		Coordinate<Vector<Object>> newPos = new Coordinate<Vector<Object>>();
+		for (int dim = 0; dim < Constants.DIMENSIONS; dim++) {
+			Vector<Object> newCoord = new Vector<Object>();
+			for (int i = 0; i < vel.getCoord(dim).size(); i++) {
+				if (pos.getCoord(dim).get(i).getClass().equals(Boolean.class)) {
+					boolean value = false;
+					value = calcXOR((boolean) pos.getCoord(dim).get(i), (boolean) vel.getCoord(dim).get(i));
+					newCoord.add(i, value);
+				}
+				if (pos.getCoord(dim).get(i).getClass().equals(Double.class)) {
+					double value = 0.0;
+					value = (double) pos.getCoord(dim).get(i) + (double) vel.getCoord(dim).get(i);
+					newCoord.add(i, value);
+				}
+			}
+			newPos.setCoord(newCoord, dim);
+		}
+		return newPos;
+	}
+
+}

+ 138 - 73
src/psoAlgoCode/Swarm.java

@@ -1,73 +1,138 @@
-package psoAlgoCode;
-
-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;
-	}
-
-	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;
-	}
-}
+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;		
+	}
+}