Browse Source

PSO Mutate Intervall, HO Algo random active

Tom 5 years ago
parent
commit
74cc62796f

+ 8 - 8
src/exampleAlgorithms/GaAlgorithm.java

@@ -338,15 +338,15 @@ public class GaAlgorithm implements Algorithm {
 			jRadioMutate.setActionCommand("normal");
 			parameterPanel.add(jRadioMutate);
 			
-			JRadioButton jRadioMutateRolf = new  JRadioButton("Mutate Interval");
-			jRadioMutateRolf.setBounds(220, 230, 200, 20);
-			jRadioMutateRolf.setActionCommand("intervall");
-			jRadioMutateRolf.setSelected(useIntervalMutation);
-			parameterPanel.add(jRadioMutateRolf);
+			JRadioButton jRadioMutateInterval = new  JRadioButton("Mutate Interval");
+			jRadioMutateInterval.setBounds(220, 230, 200, 20);
+			jRadioMutateInterval.setActionCommand("intervall");
+			jRadioMutateInterval.setSelected(useIntervalMutation);
+			parameterPanel.add(jRadioMutateInterval);
 			
 			ButtonGroup group = new ButtonGroup();
 			group.add(jRadioMutate);
-			group.add(jRadioMutateRolf);
+			group.add(jRadioMutateInterval);
 			ActionListener radioListener = e -> {
 				if(e.getActionCommand() == "normal") {
 					this.useIntervalMutation = false;
@@ -370,7 +370,7 @@ public class GaAlgorithm implements Algorithm {
 				} 
 			};
 			jRadioMutate.addActionListener(radioListener);
-			jRadioMutateRolf.addActionListener(radioListener);
+			jRadioMutateInterval.addActionListener(radioListener);
 			
 			return parameterPanel;
 		}
@@ -629,7 +629,7 @@ public class GaAlgorithm implements Algorithm {
 			}
 			//println("problemSize:" + problemSize + "    maxMutationPercent:" + maxMutationPercent);
 			int maximumAmountOfMutatedBits = Math.max(1, (int)Math.round(((double) problemSize) * this.maxMutationPercent));
-			int randomUniformAmountOfMutatedValues = Random.nextIntegerInRange(0,maximumAmountOfMutatedBits + 1);
+			int randomUniformAmountOfMutatedValues = Random.nextIntegerInRange(1,maximumAmountOfMutatedBits + 1);
 			
 			//println("max:" + maximumAmountOfMutatedBits + "   actual:" + randomUniformAmountOfMutatedValues);
 			TreeSet<Integer> mutationLocation = new TreeSet<Integer>(); //sortedSet

+ 142 - 24
src/exampleAlgorithms/PSOAlgorithm.java

@@ -6,6 +6,7 @@ import java.awt.Cursor;
 import java.awt.Dimension;
 import java.awt.FlowLayout;
 import java.awt.Font;
+import java.awt.event.ActionListener;
 import java.awt.image.BufferedImage;
 import java.io.BufferedWriter;
 import java.io.File;
@@ -18,9 +19,11 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
+import java.util.TreeSet;
 import java.util.stream.Collectors;
 
 import javax.swing.BorderFactory;
+import javax.swing.ButtonGroup;
 import javax.swing.ImageIcon;
 import javax.swing.JButton;
 import javax.swing.JCheckBox;
@@ -31,6 +34,7 @@ import javax.swing.JLabel;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JProgressBar;
+import javax.swing.JRadioButton;
 import javax.swing.JScrollPane;
 import javax.swing.JSplitPane;
 import javax.swing.JTextArea;
@@ -63,6 +67,12 @@ public class PSOAlgorithm implements Algorithm {
 	private double dependency = 2.07; 
 	private int rounds = 20;
 	private int mutationInterval = 1;
+	private boolean useIntervalMutation = true;
+	private double mutateProbabilityInterval = 0.01;
+	private double maxMutationPercent = 0.01;
+	
+	
+	
 	
 	//Settings For GroupNode using and plotting
 	private boolean append = false;
@@ -138,7 +148,7 @@ public class PSOAlgorithm implements Algorithm {
 		parameterPanel.add(maxIterLabel);
 		
 		JLabel limitLabel = new JLabel("Limit:");
-		limitLabel.setBounds(20, 110, 100, 20);
+		limitLabel.setBounds(20, 255, 100, 20);
 		parameterPanel.add(limitLabel);
 		
 		JLabel dependecyLabel = new JLabel("Dependency:");
@@ -242,7 +252,7 @@ public class PSOAlgorithm implements Algorithm {
 		limitTextField.setValue(limit);
 		limitTextField.setToolTipText("Only Double in range [0.0, 1.0] with DecimalSeperator Point('.').");
 		limitTextField.addPropertyChangeListener(propertyChange -> limit = Double.parseDouble(limitTextField.getValue().toString()));
-		limitTextField.setBounds(125, 110, 50, 20);
+		limitTextField.setBounds(125, 255, 50, 20);
 		parameterPanel.add(limitTextField);
 		
 		//Limit Formatter:
@@ -285,6 +295,78 @@ public class PSOAlgorithm implements Algorithm {
 		parameterPanel.add(mutationIntervalTextfield);
 	//--- previously Rolf Did stuff ------------------------------------------------------------------------------------------
 		
+		
+		
+		
+		JLabel mutationIntervallLabel = new JLabel("MutationRate:");
+		mutationIntervallLabel.setBounds(220, 255, 150, 20);
+		mutationIntervallLabel.setEnabled(useIntervalMutation);
+		parameterPanel.add(mutationIntervallLabel);
+		
+		
+		
+		JFormattedTextField mutationRateField = new JFormattedTextField(limitFormatter);
+		mutationRateField.setValue(this.mutateProbabilityInterval);
+		mutationRateField.setEnabled(this.useIntervalMutation);
+		mutationRateField.setToolTipText("Only Double in range [0.0, 1.0] with DecimalSeperator Point('.').");
+		mutationRateField.addPropertyChangeListener(propertyChange -> this.mutateProbabilityInterval = Double.parseDouble(mutationRateField.getValue().toString()));
+		mutationRateField.setBounds(400, 255, 50, 20);
+		parameterPanel.add(mutationRateField);
+		
+		JLabel maxMutationPercentLabel = new JLabel("Max Mutation Percent:");
+		maxMutationPercentLabel.setBounds(220, 280, 200, 20);
+		maxMutationPercentLabel.setEnabled(useIntervalMutation);
+		parameterPanel.add(maxMutationPercentLabel);
+		
+		JFormattedTextField mutationMaxField = new JFormattedTextField(limitFormatter);
+		mutationMaxField.setValue(this.maxMutationPercent);
+		mutationMaxField.setEnabled(this.useIntervalMutation);
+		mutationMaxField.setToolTipText("Only Double in range [0.0, 1.0] with DecimalSeperator Point('.').");
+		mutationMaxField.addPropertyChangeListener(propertyChange -> this.maxMutationPercent = Double.parseDouble(mutationMaxField.getValue().toString()));
+		mutationMaxField.setBounds(400, 280, 50, 20);
+		parameterPanel.add(mutationMaxField);
+		
+		
+		
+		JRadioButton jRadioMutate = new  JRadioButton("Normal Mutate");
+		jRadioMutate.setBounds(20, 230, 200, 20);
+		jRadioMutate.setSelected(!useIntervalMutation);
+		jRadioMutate.setActionCommand("normal");
+		parameterPanel.add(jRadioMutate);
+		
+		JRadioButton jRadioMutateInterval = new  JRadioButton("Mutate Interval");
+		jRadioMutateInterval.setBounds(220, 230, 200, 20);
+		jRadioMutateInterval.setActionCommand("intervall");
+		jRadioMutateInterval.setSelected(useIntervalMutation);
+		parameterPanel.add(jRadioMutateInterval);
+		
+		ButtonGroup group = new ButtonGroup();
+		group.add(jRadioMutate);
+		group.add(jRadioMutateInterval);
+		ActionListener radioListener = e -> {
+			if(e.getActionCommand() == "normal") {
+				this.useIntervalMutation = false;
+				limitTextField.setEnabled(true);
+				limitLabel.setEnabled(true);
+				mutationIntervallLabel.setEnabled(false);
+				mutationRateField.setEnabled(false);
+				maxMutationPercentLabel.setEnabled(false);
+				mutationMaxField.setEnabled(false);
+				
+			}else if(e.getActionCommand() == "intervall") {
+				this.useIntervalMutation = true;
+				limitTextField.setEnabled(false);
+				limitLabel.setEnabled(false);
+				mutationIntervallLabel.setEnabled(true);
+				mutationRateField.setEnabled(true);
+				maxMutationPercentLabel.setEnabled(true);
+				mutationMaxField.setEnabled(true);
+			} 
+		};
+		jRadioMutate.addActionListener(radioListener);
+		jRadioMutateInterval.addActionListener(radioListener);
+		
+		
 		return parameterPanel;
 	}
 	public JPanel createButtonPanel() {
@@ -495,14 +577,25 @@ public class PSOAlgorithm implements Algorithm {
 		for (int iteration = 0; iteration < maxIterations ; iteration++) {
 			int mutationAllowed = iteration % mutationInterval;
 			for (int particleNumber = 0; particleNumber < swarmSize; particleNumber++) {
-				Particle particle = swarm.get(particleNumber);				
-				for(int index = 0; index < dimensions; index++) {
-					updateVelocity(particle, index, globalBest);
-					updateGenotype(particle, index);
-		//-------Rolf changes below------------------------
-				if(mutationAllowed == 0 && iteration != 0)
-					mutation(particle, index);
-					decode(particle, index);
+				Particle particle = swarm.get(particleNumber);		
+				
+				if(this.useIntervalMutation) {
+					boolean allowMutation = (Random.nextDouble() <  this.mutateProbabilityInterval);
+					TreeSet<Integer> mutationLocation = null;
+					if(allowMutation)mutationLocation = locationsToMutate(dimensions);
+					for(int index = 0; index < dimensions; index++) {
+						updateVelocity(particle, index, globalBest);
+						updateGenotype(particle, index);
+						if(allowMutation &&mutationAllowed == 0 && iteration != 0 && mutationLocation.contains(index))mutation(particle, index);
+						decode(particle, index);
+					}
+				}else {					
+					for(int index = 0; index < dimensions; index++) {
+						updateVelocity(particle, index, globalBest);
+						updateGenotype(particle, index);
+						if(mutationAllowed == 0 && iteration != 0)mutation(particle, index);
+						decode(particle, index);
+					}
 				}
 			}
 			if(cancel)return null;
@@ -512,6 +605,17 @@ public class PSOAlgorithm implements Algorithm {
 		console.println(" End Value:" + globalBest.value);
 		return globalBest;
 	}
+	private TreeSet<Integer> locationsToMutate(int dimensions) {
+		TreeSet<Integer> mutationLocation = new TreeSet<Integer>(); //sortedSet
+		int maximumAmountOfMutatedBits = Math.max(1, (int)Math.round(((double) dimensions) * this.maxMutationPercent));
+		int randomUniformAmountOfMutatedValues = Random.nextIntegerInRange(1,maximumAmountOfMutatedBits + 1);
+		for(int i = 0; i< randomUniformAmountOfMutatedValues; i++) {
+			boolean success = mutationLocation.add(Random.nextIntegerInRange(0, dimensions));
+			if(!success) i--; //can be add up to some series long loops if maximumAmountOfMutatedBits get closed to problemsize.
+		}
+		//console.println(mutationLocation.toString());
+		return mutationLocation;
+	}
 	/**
 	 * 	Eq. (6):v<sub>i,j</sub>(t + 1) = wv<sub>i,j</sub>+c<sub>1</sub>R<sub>1</sub>(P<sub>best,i,j</sub>-x<sub>p,i,j</sub>(t))+c<sub>2</sub>R<sub>2</sub>(g<sub>best,i,j</sub>-x<sub>p,i,j</sub>(t))<br>
 	 * @param particle
@@ -940,21 +1044,35 @@ public class PSOAlgorithm implements Algorithm {
 	* To create Random and maybe switch the random generation in the future.
 	*/
 	private static class Random{
-			/**
-			 * True or false
-			 * @return the random boolean.
-			 */
-			public static boolean nextBoolean(){
-				return (Math.random() < 0.5);
-			}
-			/**
-			 * Between 0.0 and 1.0
-			 * @return the random double.
-			 */
-			public static double nextDouble(){
-				return Math.random();
-			}
+		
+		
+		private static java.util.Random random = new java.util.Random();
+	
+		/**
+		 * True or false
+		 * @return the random boolean.
+		 */
+		public static boolean nextBoolean(){
+			return random.nextBoolean();
 		}
+		/**
+		 * Between 0.0(inclusive) and 1.0 (exclusive)
+		 * @return the random double.
+		 */
+		public static double nextDouble() {
+			return random.nextDouble();
+		}
+		
+		/**
+		 * Random Int in Range [min;max[ with UniformDistirbution
+		 * @param min
+		 * @param max
+		 * @return
+		 */
+		public static int nextIntegerInRange(int min, int max) {
+			return min + random.nextInt(max - min);
+		}
+	}
 	
 	
 	private   class  Handle<T>{

+ 30 - 4
src/exampleAlgorithms/Randomizer.java

@@ -54,6 +54,7 @@ public class Randomizer implements Algorithm {
 	      newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
 	private File file;
+	private double randomChance = 1.0;
 	
 	public Randomizer(){
 		content.setLayout(new BorderLayout());
@@ -101,6 +102,13 @@ public class Randomizer implements Algorithm {
 		chooseFileButton.setBounds(20, 10, 200, 30);
 		chooseFileButton.addActionListener(actionEvent -> file = openCatalogFile());
 		parameterPanel.add(chooseFileButton);
+		
+		
+		JSlider bitSlider = createFlipChanceSlider();
+		bitSlider.setBounds(10, 110, 280, 80);
+		parameterPanel.add(bitSlider);
+		
+		
 		return parameterPanel;
 	}
 	
@@ -108,14 +116,18 @@ public class Randomizer implements Algorithm {
 		List<HolonObjectSketch> holonObjectCatalog = new ArrayList<HolonObjectSketch>();
 		if(file == null)file = openCatalogFile();
 		if(file == null) return;
-		readMsgFromJSONwithGSON(file, holonObjectCatalog);
+		readCatalogFromJson(file, holonObjectCatalog);
+		holonObjectCatalog.forEach(con -> con.checkMinMax());
+		
 		List<HolonObject> consumerList = new ArrayList<HolonObject>();
 		rollOutNodes(control.getModel().getObjectsOnCanvas(), consumerList, control.getModel().getCurIteration());
 		consumerList.forEach(con -> {
 			con.getElements().clear();
 			int randomAmountOfElementsToAdd = Random.nextIntegerInRange(minAmountOfElements, maxAmountOfElements + 1);
 			for(int i = 0; i < randomAmountOfElementsToAdd; i++) {
-				con.getElements().add(holonObjectCatalog.get(Random.nextIntegerInRange(0, holonObjectCatalog.size())).createHolonElement(control.getModel()));				
+				HolonElement ele = holonObjectCatalog.get(Random.nextIntegerInRange(0, holonObjectCatalog.size())).createHolonElement(control.getModel());
+				ele.setActive(Random.nextDouble() < randomChance);
+				con.getElements().add(ele);				
 			}
 		});
 		control.calculateStateAndVisualForCurrentTimeStep();
@@ -175,7 +187,21 @@ public class Randomizer implements Algorithm {
 		
 		
 	}
-	
+	private JSlider createFlipChanceSlider() {
+		JSlider flipChance =new JSlider(JSlider.HORIZONTAL,0, 100, 100);
+		flipChance.setBorder(BorderFactory.createTitledBorder("ActiveProbability"));
+		flipChance.setMajorTickSpacing(50);
+		flipChance.setMinorTickSpacing(5);
+		flipChance.setPaintTicks(true);
+		Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
+		labelTable.put( Integer.valueOf(0), new JLabel("0.0") );
+		labelTable.put( Integer.valueOf(50), new JLabel("0.5") );
+		labelTable.put( Integer.valueOf(100), new JLabel("1.0") );
+		flipChance.addChangeListener(actionEvent ->randomChance  =(double)flipChance.getValue()/100.0);
+		flipChance.setLabelTable( labelTable );
+		flipChance.setPaintLabels(true);
+		return flipChance;
+	}
 	
 	
 	
@@ -209,7 +235,7 @@ public class Randomizer implements Algorithm {
 			return min + random.nextInt(max - min);
 		}
 	}
-	private void readMsgFromJSONwithGSON(File file, List<HolonObjectSketch> catalog) {
+	private void readCatalogFromJson(File file, List<HolonObjectSketch> catalog) {
 		Gson gson = new Gson();
 		JsonParser parser = new JsonParser();
 		try {