Tom 5 years ago
parent
commit
7cbde9c905
2 changed files with 136 additions and 13 deletions
  1. 130 7
      src/exampleAlgorithms/GaAlgorithm.java
  2. 6 6
      src/ui/view/Main.java

+ 130 - 7
src/exampleAlgorithms/GaAlgorithm.java

@@ -4,18 +4,23 @@ import java.awt.BorderLayout;
 import java.awt.Component;
 import java.awt.Dimension;
 import java.awt.FlowLayout;
+import java.awt.event.ActionListener;
 import java.awt.image.BufferedImage;
 import java.math.RoundingMode;
 import java.text.NumberFormat;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.ListIterator;
 import java.util.Locale;
+import java.util.Set;
+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;
@@ -25,6 +30,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;
@@ -51,11 +57,14 @@ public class GaAlgorithm implements Algorithm {
 		 */
 		private int popsize = 20;
 		private int maxGenerations = 100;
-		private double tournamentSize = 2.1;
+		private double tournamentSize = 2.0;
 		private double fixedSwapProbability = 0.02;
 		private boolean useFixedSpawProbability = false;
 		private double fixedMutateProbability = 0.02;
 		private boolean useFixedMutateProbability = false;
+		private boolean useIntervalMutation = true;
+		private double mutateProbabilityInterval = 0.01;
+		private double maxMutationPercent = 0.01;
 		private int rounds = 2;
 		
 		
@@ -140,7 +149,7 @@ public class GaAlgorithm implements Algorithm {
 			parameterPanel.add(roundLabel);
 			
 			JLabel mutationLabel = new JLabel("FixedMutation:");
-			mutationLabel.setBounds(50, 135, 150, 20);
+			mutationLabel.setBounds(50, 255, 150, 20);
 			mutationLabel.setEnabled(useFixedMutateProbability);
 			parameterPanel.add(mutationLabel);
 			
@@ -239,18 +248,20 @@ public class GaAlgorithm implements Algorithm {
 			limitFormatter.setMinimum(0.0);
 			limitFormatter.setMaximum(1.0);
 			
+			
+			
 			JFormattedTextField mutateTextField = new JFormattedTextField(limitFormatter);
 			mutateTextField.setValue(this.fixedMutateProbability);
 			mutateTextField.setEnabled(this.useFixedMutateProbability);
 			mutateTextField.setToolTipText("Only Double in range [0.0, 1.0] with DecimalSeperator Point('.').");
 			mutateTextField.addPropertyChangeListener(propertyChange -> fixedMutateProbability = Double.parseDouble(mutateTextField.getValue().toString()));
-			mutateTextField.setBounds(160, 135, 50, 20);
+			mutateTextField.setBounds(160, 255, 50, 20);
 			parameterPanel.add(mutateTextField);
 			
 			JCheckBox useFixMutateCheckBox = new JCheckBox();
 			useFixMutateCheckBox.setSelected(this.useFixedMutateProbability);
 			useFixMutateCheckBox.setToolTipText("If not checked its 1/ProblemSize");
-			useFixMutateCheckBox.setBounds(20, 135, 25, 20);
+			useFixMutateCheckBox.setBounds(20, 255, 25, 20);
 			useFixMutateCheckBox.addActionListener(actionEvent -> {
 				boolean state = useFixMutateCheckBox.isSelected();
 				this.useFixedMutateProbability = state;
@@ -293,8 +304,81 @@ public class GaAlgorithm implements Algorithm {
 			parameterPanel.add(tournamentSizeTextField);
 			
 			
+			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 jRadioMutateRolf = new  JRadioButton("Mutate Interval");
+			jRadioMutateRolf.setBounds(220, 230, 200, 20);
+			jRadioMutateRolf.setActionCommand("intervall");
+			jRadioMutateRolf.setSelected(useIntervalMutation);
+			parameterPanel.add(jRadioMutateRolf);
+			
+			ButtonGroup group = new ButtonGroup();
+			group.add(jRadioMutate);
+			group.add(jRadioMutateRolf);
+			ActionListener radioListener = e -> {
+				if(e.getActionCommand() == "normal") {
+					this.useIntervalMutation = false;
+					mutateTextField.setEnabled(true);
+					useFixMutateCheckBox.setEnabled(true);
+					mutationLabel.setEnabled(true);
+					mutationIntervallLabel.setEnabled(false);
+					mutationRateField.setEnabled(false);
+					maxMutationPercentLabel.setEnabled(false);
+					mutationMaxField.setEnabled(false);
+					
+				}else if(e.getActionCommand() == "intervall") {
+					this.useIntervalMutation = true;
+					mutateTextField.setEnabled(false);
+					useFixMutateCheckBox.setEnabled(false);
+					mutationLabel.setEnabled(false);
+					mutationIntervallLabel.setEnabled(true);
+					mutationRateField.setEnabled(true);
+					maxMutationPercentLabel.setEnabled(true);
+					mutationMaxField.setEnabled(true);
+				} 
+			};
+			jRadioMutate.addActionListener(radioListener);
+			jRadioMutateRolf.addActionListener(radioListener);
+			
 			return parameterPanel;
 		}
+		
+		
+		
+		
+		
 		public JPanel createButtonPanel() {
 			JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
 			JButton resetButton =  new JButton("ResetAll");
@@ -423,7 +507,6 @@ public class GaAlgorithm implements Algorithm {
 		
 		
 		
-		
 		private void executeAlgoWithParameter(){
 			
 			
@@ -504,8 +587,8 @@ public class GaAlgorithm implements Algorithm {
 					Individual childA = new Individual(parentA);
 					Individual childB = new Individual(parentB);
 					crossover(childA, childB, problemSize);
-					mutate(childA, problemSize);
-					mutate(childB, problemSize);
+					if(useIntervalMutation)mutateInterval(childA, problemSize);else mutate(childA, problemSize);
+					if(useIntervalMutation)mutateInterval(childB, problemSize);else mutate(childB, problemSize);
 					childList.add(childA);
 					childList.add(childB);
 				}
@@ -534,6 +617,46 @@ public class GaAlgorithm implements Algorithm {
 				}
 			}
 		}
+		
+		/**
+		 * Algorithm rolf
+		 * 
+		 */
+		private void mutateInterval(Individual child, int problemSize) {
+			//If not mutate skip
+			if(Random.nextDouble() >  this.mutateProbabilityInterval) {
+				return;
+			}
+			//println("problemSize:" + problemSize + "    maxMutationPercent:" + maxMutationPercent);
+			int maximumAmountOfMutatedBits = Math.max(1, (int)Math.round(((double) problemSize) * this.maxMutationPercent));
+			int randomUniformAmountOfMutatedValues = Random.nextIntegerInRange(0,maximumAmountOfMutatedBits + 1);
+			
+			//println("max:" + maximumAmountOfMutatedBits + "   actual:" + randomUniformAmountOfMutatedValues);
+			TreeSet<Integer> mutationLocation = new TreeSet<Integer>(); //sortedSet
+			//Choose the location to mutate
+			for(int i = 0; i< randomUniformAmountOfMutatedValues; i++) {
+				boolean success = mutationLocation.add(Random.nextIntegerInRange(0, problemSize));
+				if(!success) i--; //can be add up to some series long loops if maximumAmountOfMutatedBits get closed to problemsize.
+			}
+			//println("Set:" + mutationLocation);
+			ListIterator<Boolean> iter = child.position.listIterator();
+			if(mutationLocation.isEmpty()) return;
+			int firstindex = mutationLocation.pollFirst();
+			while(iter.hasNext()) {
+				int index = iter.nextIndex();
+				boolean boolValue = iter.next();
+				if(index == firstindex) {
+					iter.set(!boolValue);
+					//println("changed Value["+ index +"]");
+					if(mutationLocation.isEmpty()) break;
+					firstindex = mutationLocation.pollFirst();
+				}
+			}
+		}
+		
+		
+		
+		
 		/** 
 		 * Algorithm 25 Uniform Crossover.
 		 * Probability is set to 1/Problemsize when not changed.

+ 6 - 6
src/ui/view/Main.java

@@ -43,12 +43,12 @@ public class Main {
 //		}
 		
 		if (System.getProperty("os.name").startsWith("Linux")) {
-			try {
-				UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
-			} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
-					| UnsupportedLookAndFeelException e) {
-				e.printStackTrace();
-			}
+//			try {
+//				UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
+//			} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
+//					| UnsupportedLookAndFeelException e) {
+//				e.printStackTrace();
+//			}
 		}else {
 			loadNotLinuxLookAndFeel();	
 		}