|
@@ -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>{
|