Browse Source

ConsoleUpdate, GroupNode selection shows ID

Tom 5 years ago
parent
commit
4f8e2ccfd2

+ 6 - 11
src/exampleAlgorithms/GaAlgorithm.java

@@ -40,6 +40,7 @@ import ui.controller.Control;
 import ui.model.DecoratedGroupNode;
 import ui.model.DecoratedState;
 import ui.model.Model;
+import ui.view.Console;
 import ui.model.DecoratedHolonObject.HolonObjectState;
 
 public class GaAlgorithm implements Algorithm {
@@ -75,7 +76,7 @@ public class GaAlgorithm implements Algorithm {
 		
 		//Gui Part:
 		private Control  control;
-		private JTextArea textArea;
+		private Console console = new Console();
 		private JPanel content = new JPanel();
 		//ProgressBar
 		private JProgressBar progressBar = new JProgressBar();
@@ -105,12 +106,8 @@ public class GaAlgorithm implements Algorithm {
 		}
 		public GaAlgorithm() {
 			content.setLayout(new BorderLayout());
-		
-			textArea = new JTextArea();
-			textArea.setEditable(false);
-			JScrollPane scrollPane = new JScrollPane(textArea);
 			JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
-					createOptionPanel() , scrollPane);
+					createOptionPanel() , console);
 			splitPane.setResizeWeight(0.0);
 			content.add(splitPane, BorderLayout.CENTER);
 			content.setPreferredSize(new Dimension(800,800));	
@@ -283,9 +280,6 @@ public class GaAlgorithm implements Algorithm {
 			JButton cancelButton =  new JButton("Cancel Run");
 			cancelButton.addActionListener(actionEvent -> cancel());
 			buttonPanel.add(cancelButton);
-			JButton clearButton =  new JButton("Clear Console");
-			clearButton.addActionListener(actionEvent -> clear());
-			buttonPanel.add(clearButton);
 			JButton undoButton =  new JButton("Undo");
 			undoButton.setToolTipText("One Algo Step Back.");
 			undoButton.addActionListener(actionEvent -> resetLast());
@@ -363,10 +357,10 @@ public class GaAlgorithm implements Algorithm {
 			
 		}
 		private void clear() {
-			textArea.setText("");
+			console.clear();
 		}
 		private void println(String message) {
-			textArea.append(message  + "\n");
+			console.println(message);
 		}
 		private void selectGroupNode() {
 			Object[] possibilities = control.getSimManager().getActualVisualRepresentationalState().getCreatedGroupNodes().values().stream().map(aCps -> new Handle<DecoratedGroupNode>(aCps)).toArray();
@@ -456,6 +450,7 @@ public class GaAlgorithm implements Algorithm {
 		private Individual executeGaAlgo() {
 			Individual best = new Individual();
 			best.position = extractPositionAndAccess();
+			println("Bit-Array_length: " + best.position.size());
 			best.fitness = evaluatePosition(best.position, false);
 			println("Start with Fitness: " + best.fitness);
 			int problemSize = best.position.size();

+ 25 - 38
src/exampleAlgorithms/PSOAlgorithm.java

@@ -45,6 +45,7 @@ import classes.HolonObject;
 import classes.HolonSwitch;
 import ui.controller.Control;
 import ui.model.Model;
+import ui.view.Console;
 import ui.model.DecoratedHolonObject.HolonObjectState;
 import ui.model.DecoratedGroupNode;
 import ui.model.DecoratedNetwork;
@@ -81,7 +82,7 @@ public class PSOAlgorithm implements Algorithm {
 	
 	//Gui Part:
 	private Control  control;
-	private JTextArea textArea;
+	private Console console = new Console();
 	private JPanel content = new JPanel();
 	//ProgressBar
 	private JProgressBar progressBar = new JProgressBar();
@@ -102,10 +103,7 @@ public class PSOAlgorithm implements Algorithm {
 	}
 	public PSOAlgorithm() {
 		content.setLayout(new BorderLayout());
-	
-		textArea = new JTextArea();
-		textArea.setEditable(false);
-		JScrollPane scrollPane = new JScrollPane(textArea);
+		JScrollPane scrollPane = new JScrollPane(console);
 		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
 				createOptionPanel() , scrollPane);
 		splitPane.setResizeWeight(0.0);
@@ -296,9 +294,6 @@ public class PSOAlgorithm implements Algorithm {
 		JButton cancelButton =  new JButton("Cancel Run");
 		cancelButton.addActionListener(actionEvent -> cancel());
 		buttonPanel.add(cancelButton);
-		JButton clearButton =  new JButton("Clear Console");
-		clearButton.addActionListener(actionEvent -> clear());
-		buttonPanel.add(clearButton);
 		JButton folderButton =  new JButton("Change Plott-File");
 		folderButton.addActionListener(actionEvent -> setSaveFile());
 		buttonPanel.add(folderButton);
@@ -340,47 +335,47 @@ public class PSOAlgorithm implements Algorithm {
 	
 	private void cancel() {
 		if(runThread.isAlive()) {
-			println("");
-			println("Cancel run.");
+			console.println("");
+			console.println("Cancel run.");
 			cancel = true;
 			progressBar.setValue(0);
 		} else {
-			println("Nothing to cancel.");
+			console.println("Nothing to cancel.");
 		}
 	}
 	private void fitness() {
 		initDependentParameter();
 		double currentFitness = evaluatePosition(extractPositionAndAccess(control.getModel()), false);
-		println("Actual Fitnessvalue: " + currentFitness);
+		console.println("Actual Fitnessvalue: " + currentFitness);
 	}
 	private void setSaveFile() {
 		fileChooser.setFileFilter(new FileNameExtensionFilter("File", "txt"));
 		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
 		int result = fileChooser.showSaveDialog(content);
 		if(result == JFileChooser.APPROVE_OPTION) {
-			println("Set save File to:" + fileChooser.getSelectedFile().getAbsolutePath());
+			console.println("Set save File to:" + fileChooser.getSelectedFile().getAbsolutePath());
 		}
 		
 	}
 	private void plott() {
 		if(db!=null) {
-			println("Plott..");
+			console.println("Plott..");
 			db.initFileStream();
 		}else {
-			println("No run inistialized.");
+			console.println("No run inistialized.");
 		}
 	}
 	private void reset() {
 		if(initialState != null) {
-			println("Resetting..");
+			console.println("Resetting..");
 			resetState();
 			updateVisual();
 		}else {
-			println("No run inistialized.");
+			console.println("No run inistialized.");
 		}
 	}
 	private void printParameter() {
-		println("SwarmSize:" + swarmSize + ", MaxIter:" + maxIterations + ", Limit:" + limit + ", Dependency:" + dependency + ", Rounds:" + rounds +", DependentParameter: w:"+ w + ", c1:" + c1 + ", c2:" + c2 );
+		console.println("SwarmSize:" + swarmSize + ", MaxIter:" + maxIterations + ", Limit:" + limit + ", Dependency:" + dependency + ", Rounds:" + rounds +", DependentParameter: w:"+ w + ", c1:" + c1 + ", c2:" + c2 );
 	}
 	@Override
 	public JPanel getAlgorithmPanel() {
@@ -391,21 +386,13 @@ public class PSOAlgorithm implements Algorithm {
 		this.control = control;
 		
 	}
-	private void clear() {
-		textArea.setText("");
-	}
-	private void print(String message) {
-		textArea.append(message);
-	}
-	private void println(String message) {
-		textArea.append(message  + "\n");
-	}
+
 	private void selectGroupNode() {
 		Object[] possibilities = control.getSimManager().getActualVisualRepresentationalState().getCreatedGroupNodes().values().stream().map(aCps -> new Handle<DecoratedGroupNode>(aCps)).toArray();
 		@SuppressWarnings("unchecked")
 		Handle<DecoratedGroupNode> selected = (Handle<DecoratedGroupNode>) JOptionPane.showInputDialog(content, "Select GroupNode:", "GroupNode?",  JOptionPane.OK_OPTION,new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) , possibilities, "");
 		if(selected != null) {
-			println("Selected: " + selected);
+			console.println("Selected: " + selected);
 			dGroupNode = selected.object;
 		}
 	}
@@ -424,7 +411,7 @@ public class PSOAlgorithm implements Algorithm {
 	}
 	private void printElapsedTime(){
 		long elapsedMilliSeconds = System.currentTimeMillis() - startTime;
-		println("Execution Time of Algo in Milliseconds:" + elapsedMilliSeconds);
+		console.println("Execution Time of Algo in Milliseconds:" + elapsedMilliSeconds);
 	}
 	
 	
@@ -450,8 +437,8 @@ public class PSOAlgorithm implements Algorithm {
 		  resetState();
 		  if(lastRunBest.value < runBest.value) runBest = lastRunBest;
 		}
-		println("AlgoResult:" + runBest.value);
-		//println("[" + lastRunBest.position.stream().map(Object::toString).collect(Collectors.joining(", ")) + "]");
+		console.println("AlgoResult:" + runBest.value);
+		//console.println("[" + lastRunBest.position.stream().map(Object::toString).collect(Collectors.joining(", ")) + "]");
 		setState(runBest.position);
 		updateVisual();
 	}
@@ -500,7 +487,7 @@ public class PSOAlgorithm implements Algorithm {
 		Best globalBest = new Best();
 		globalBest.position = extractPositionAndAccess(control.getModel());
 		globalBest.value = evaluatePosition(globalBest.position, true);
-		print("Start Value:" + globalBest.value);
+		console.println("Start Value:" + globalBest.value);
 		int dimensions = globalBest.position.size();
 		List<Particle> swarm= initializeParticles(dimensions);
 		runList.add(globalBest.value);
@@ -523,7 +510,7 @@ public class PSOAlgorithm implements Algorithm {
 			evaluation(globalBest, swarm);
 			runList.add(globalBest.value);
 		}
-		println(" End Value:" + globalBest.value);
+		console.println(" End Value:" + globalBest.value);
 		return globalBest;
 	}
 	/**
@@ -647,7 +634,7 @@ public class PSOAlgorithm implements Algorithm {
 			object_fitness += net.getConsumerList().stream().map(con -> holonObjectSupplyPenaltyFunction(con.getSupplyBarPercentage()) + inactiveHolonElementPenalty(con.getModel())).reduce(0.0, (a, b) -> (a + b));
 			//warum war das im network fitness und nicht hier im Object fitness??
 			object_fitness += net.getConsumerList().stream().map(con -> StateToDouble(con.getState())).reduce(0.0, (a,b) -> (a+b));
-			//System.out.println("objectfitness for statestuff: " + object_fitness);
+			//System.out.console.println("objectfitness for statestuff: " + object_fitness);
 			//object_fitness += net.getPassivNoEnergyList().stream().map(con -> 1000.0).reduce(0.0, (a, b) -> (a + b));
 			object_fitness += net.getPassivNoEnergyList().stream().map(sup -> inactiveHolonElementPenalty(sup.getModel())).reduce(0.0, (a, b) -> (a + b));
 			object_fitness += net.getSupplierList().stream().map(sup -> inactiveHolonElementPenalty(sup.getModel())).reduce(0.0, (a, b) -> (a + b));
@@ -671,7 +658,7 @@ public class PSOAlgorithm implements Algorithm {
 		
 		//result = (float) Math.pow((maxElements -activeElements),2)*10;
 		result = (float) Math.pow(5, 4* ( (float) maxElements - (float) activeElements)/ (float) maxElements) - 1;
-		//System.out.println("max: " + maxElements + " active: " + activeElements + " results in penalty: " + result);
+		//System.out.console.println("max: " + maxElements + " active: " + activeElements + " results in penalty: " + result);
 	return result;
 		
 	}
@@ -813,7 +800,7 @@ public class PSOAlgorithm implements Algorithm {
 				printToStream(out);
 				out.close();
 			} catch (IOException e) {
-				println(e.getMessage());
+				console.println(e.getMessage());
 			}
 		}
 		
@@ -831,14 +818,14 @@ public class PSOAlgorithm implements Algorithm {
 
 			}
 			catch(IOException e) {
-				println(e.getMessage());
+				console.println(e.getMessage());
 			}
 			allRuns.forEach(run -> {
 				try {
 					out.write( run.stream().map(Object::toString).collect(Collectors.joining(", ")));
 					out.newLine();
 				} catch (IOException e) {
-					println(e.getMessage());
+					console.println(e.getMessage());
 				}
 			} );
 		/*	out.write("AverageRun:");

+ 1 - 1
src/ui/model/DecoratedGroupNode.java

@@ -117,7 +117,7 @@ public class DecoratedGroupNode {
 	
 	public String toString() {
 		return 
-				"GroupNode with [Supplier:" + getAmountOfSupplier() 
+				"GroupNode" + model.getId() + " with [Supplier:" + getAmountOfSupplier() 
 				+ ", NotSupplied:" + getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED) 
 				+ ", PartiallySupplied:" + getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED) 
 				+ ", Supplied:" + getAmountOfConsumerWithState(HolonObjectState.SUPPLIED) 

+ 47 - 7
src/ui/view/Console.java

@@ -1,27 +1,67 @@
 package ui.view;
 
+import java.awt.BorderLayout;
+import java.awt.Component;
+import java.awt.FlowLayout;
+
+import javax.swing.Box;
+import javax.swing.JButton;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
 import javax.swing.JTextArea;
+import javax.swing.JToolBar;
 import javax.swing.text.DefaultCaret;
 /**
  * Little new swing object to print data to a console.
  * @author tom
  *
  */
-public class Console extends JTextArea {
+public class Console extends JPanel {
+	private JTextArea textArea = new JTextArea();
+	private JScrollPane scrollPane;
+	
 	public Console() {
 		super();
-		this.setEditable(false);
-		DefaultCaret caret = (DefaultCaret)this.getCaret();
+		this.setLayout(new BorderLayout());
+		textArea.setEditable(false);
+		DefaultCaret caret = (DefaultCaret)textArea.getCaret();
 		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");
+		clearButton.addActionListener(actionEvent -> clear());
+		roolBar.add(clearButton);
+		roolBar.add(Box.createHorizontalGlue());
+		JButton topButton =  new JButton("Top");
+		topButton.addActionListener(actionEvent -> scrollToTop());
+		roolBar.add(topButton);
+		JButton botButton =  new JButton("Bottom");
+		botButton.addActionListener(actionEvent -> scrollToBottom());
+		roolBar.add(botButton);
+		scrollPane.setColumnHeaderView(roolBar);
+	}
+	private void scrollToTop() {
+		textArea.setCaretPosition(0);
 	}
+	private void scrollToBottom() {
+		textArea.setCaretPosition(textArea.getDocument().getLength());
+	}
+	
+	
+	
+	
+	
+	
 	public void clear() {
-		this.setText("");
+		textArea.setText("");
 	}
 	public void print(String message) {
-		this.append(message);
-
+		textArea.append(message);
+		
 	}
 	public void println(String message) {
-		this.append(message  + "\n");
+		textArea.append(message  + "\n");
 	}
 }