Browse Source

GA Tournament Size and console Buttons

Tom 4 years ago
parent
commit
afe5a2b8fc

BIN
res/Button_Images/Bottom.png


BIN
res/Button_Images/Clear.png


BIN
res/Button_Images/Top.png


+ 28 - 2
src/exampleAlgorithms/GaAlgorithm.java

@@ -51,7 +51,7 @@ public class GaAlgorithm implements Algorithm {
 		 */
 		private int popsize = 20;
 		private int maxGenerations = 100;
-		private int tournamentSize = 2;
+		private double tournamentSize = 2.1;
 		private double fixedSwapProbability = 0.02;
 		private boolean useFixedSpawProbability = false;
 		private double fixedMutateProbability = 0.02;
@@ -150,6 +150,10 @@ public class GaAlgorithm implements Algorithm {
 			swapLabel.setEnabled(this.useFixedSpawProbability);
 			parameterPanel.add(swapLabel);
 			
+			JLabel tournamentLabel = new JLabel("TournamentSize:");
+			tournamentLabel.setBounds(20, 185, 150, 20);
+			parameterPanel.add(tournamentLabel);
+			
 			
 			JLabel progressLabel = new JLabel("Progress:");
 			progressLabel.setBounds(350, 135, 170, 20);
@@ -269,6 +273,18 @@ public class GaAlgorithm implements Algorithm {
 			parameterPanel.add(useFixSwapCheckBox);
 			
 			
+			NumberFormatter tournamentFormatter = new NumberFormatter(doubleFormat);
+			tournamentFormatter.setMinimum(1.0);
+			
+			
+			JFormattedTextField tournamentSizeTextField = new JFormattedTextField(tournamentFormatter);
+			tournamentSizeTextField.setValue(this.tournamentSize);
+			tournamentSizeTextField.setToolTipText("Only Double bigger or equal then 1.");
+			tournamentSizeTextField.addPropertyChangeListener(propertyChange -> this.tournamentSize = Double.parseDouble(tournamentSizeTextField.getValue().toString()));
+			tournamentSizeTextField.setBounds(160, 185, 50, 20);
+			parameterPanel.add(tournamentSizeTextField);
+			
+			
 			return parameterPanel;
 		}
 		public JPanel createButtonPanel() {
@@ -533,10 +549,20 @@ public class GaAlgorithm implements Algorithm {
 		 */
 		private Individual selectAParent(List<Individual> population,int popsize) {
 			Individual tournamentBest = population.get(Random.nextIntegerInRange(0, popsize));
-			for(int i = 2 ; i <= tournamentSize; i++) {
+			double participants;
+			for(participants = tournamentSize ; participants >= 2; participants -= 1.0) {
 				Individual next = population.get(Random.nextIntegerInRange(0, popsize));
 				if(next.fitness < tournamentBest.fitness) tournamentBest = next;
 			}
+			//if tournament size is not a whole number like 2.5 or 3.6
+			//the remaining part is the chance to fight another time; 2.7 -> 70% chance to fight a second time
+			if( participants > 1) {		
+				if(Random.nextDouble() < participants - 1.0) {
+					println("Chance to find a match");
+					Individual next = population.get(Random.nextIntegerInRange(0, popsize));
+					if(next.fitness < tournamentBest.fitness) tournamentBest = next;
+				}
+			}
 			return tournamentBest;
 		}
 		/**

+ 14 - 10
src/ui/view/Console.java

@@ -5,6 +5,7 @@ import java.awt.Component;
 import java.awt.FlowLayout;
 
 import javax.swing.Box;
+import javax.swing.ImageIcon;
 import javax.swing.JButton;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
@@ -28,19 +29,22 @@ public class Console extends JPanel {
 		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
 		scrollPane = new JScrollPane(textArea);
 		this.add(scrollPane, BorderLayout.CENTER);
-		JToolBar roolBar = new JToolBar();
-		roolBar.setFloatable(false);
-		JButton clearButton =  new JButton("Clear");
+		JToolBar toolBar = new JToolBar();
+		toolBar.setFloatable(false);
+		JButton clearButton  = new JButton("", new ImageIcon(Util.loadImage("/Button_Images/Clear.png", 24, 24)));
+		clearButton.setToolTipText("Clear Console");
 		clearButton.addActionListener(actionEvent -> clear());
-		roolBar.add(clearButton);
-		roolBar.add(Box.createHorizontalGlue());
-		JButton topButton =  new JButton("Top");
+		toolBar.add(clearButton);
+		toolBar.add(Box.createHorizontalGlue());
+		JButton topButton = new JButton("", new ImageIcon(Util.loadImage("/Button_Images/Top.png", 24, 24)));
+		topButton.setToolTipText("Scroll to top");
 		topButton.addActionListener(actionEvent -> scrollToTop());
-		roolBar.add(topButton);
-		JButton botButton =  new JButton("Bottom");
+		toolBar.add(topButton);
+		JButton botButton = new JButton("", new ImageIcon(Util.loadImage("/Button_Images/Bottom.png", 24, 24)));
+		botButton.setToolTipText("Scroll to bottom");
 		botButton.addActionListener(actionEvent -> scrollToBottom());
-		roolBar.add(botButton);
-		scrollPane.setColumnHeaderView(roolBar);
+		toolBar.add(botButton);
+		scrollPane.setColumnHeaderView(toolBar);
 	}
 	private void scrollToTop() {
 		textArea.setCaretPosition(0);

+ 8 - 1
src/ui/view/Main.java

@@ -5,6 +5,8 @@ import ui.controller.SingletonControl;
 import ui.model.Model;
 
 import javax.swing.*;
+import javax.swing.UIManager.LookAndFeelInfo;
+
 import java.awt.*;
 
 /**
@@ -41,7 +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();
+			}
 		}else {
 			loadNotLinuxLookAndFeel();	
 		}