소스 검색

Merge branch 'updateInProgress' of git.tk.informatik.tu-darmstadt.de:carlos.garcia/praktikum-holons into updateInProgress

Tom 5 년 전
부모
커밋
2087333aad
4개의 변경된 파일161개의 추가작업 그리고 23개의 파일을 삭제
  1. 139 22
      src/exampleAlgorithms/FlexExample.java
  2. 1 1
      src/ui/controller/Control.java
  3. 20 0
      src/ui/model/DecoratedNetwork.java
  4. 1 0
      src/ui/view/MyCanvas.java

+ 139 - 22
src/exampleAlgorithms/FlexExample.java

@@ -46,6 +46,7 @@ import ui.model.DecoratedGroupNode;
 import ui.model.DecoratedNetwork;
 import ui.model.DecoratedState;
 import ui.model.Model;
+import ui.model.DecoratedHolonObject.HolonObjectState;
 
 public class FlexExample implements Algorithm {
 
@@ -56,7 +57,9 @@ public class FlexExample implements Algorithm {
 		private boolean useGroupNode = false;
 		private DecoratedGroupNode dGroupNode = null;
 		private boolean cancel = false;
-
+		private boolean overAllTimeSteps = false;
+		
+		
 
 		//Parameter defined by Algo
 		private HashMap<Integer, AccessWrapper> access;
@@ -77,6 +80,11 @@ public class FlexExample implements Algorithm {
 
 		
 		
+		
+		
+		
+		
+		
 		public static void main(String[] args)
 		{
 		      JFrame newFrame = new JFrame("exampleWindow");
@@ -143,6 +151,14 @@ public class FlexExample implements Algorithm {
 			borderPanel.add(useGroupNodeCheckBox);
 			
 			
+			JCheckBox overAllTimeStepsCheckbox = new JCheckBox("overAllTimeSteps");
+			overAllTimeStepsCheckbox.setSelected(false);
+			overAllTimeStepsCheckbox.setBounds(20, 30, 250, 30);
+			overAllTimeStepsCheckbox.addActionListener(actionEvent -> {
+				overAllTimeSteps = overAllTimeStepsCheckbox.isSelected();
+			});
+			parameterPanel.add(overAllTimeStepsCheckbox);
+			
 			NumberFormat format = NumberFormat.getIntegerInstance();
 			format.setGroupingUsed(false);
 			format.setParseIntegerOnly(true);
@@ -177,7 +193,10 @@ public class FlexExample implements Algorithm {
 			buttonPanel.add(undoButton);
 			JButton runButton =  new JButton("Run");
 			runButton.addActionListener(actionEvent -> {
-				Runnable task = () -> run();
+				Runnable task = () -> {
+					if(this.overAllTimeSteps)runAll();
+					else run();
+				};
 				runThread = new Thread(task);
 				runThread.start();
 			});
@@ -195,16 +214,32 @@ public class FlexExample implements Algorithm {
 			}
 		}
 		
-		private void run() {
+		private void runAll() {
 			cancel = false;
 			disableGuiInput(true);
 			startTimer();
-			executeDemoAlgo();
-			if(cancel) {
-				resetLast();
-				disableGuiInput(false);
-				return;
+			control.resetSimulation();
+			RunResult result= new RunResult();
+			for(int i = 0; i < 100; i++) {
+				control.setCurIteration(i);
+				executeDemoAlgo(result);
+				if(cancel) {
+					resetLast();
+					disableGuiInput(false);
+					return;
+				}				
 			}
+			updateVisual();
+			calculateAllResults(result);
+			println("Amount of activatedFlex:" + result.activatedFlex + "   Amount of deactivatedElements:"+ result.deactivatedElements + "   TotalCost:"+result.totalCost);
+			printElapsedTime();
+			disableGuiInput(false);
+		}
+		private void run() {
+			disableGuiInput(true);
+			startTimer();
+			executeDemoAlgo(new RunResult());
+			updateVisual();
 			printElapsedTime();
 			disableGuiInput(false);
 		}
@@ -318,11 +353,11 @@ public class FlexExample implements Algorithm {
 		 * End
 		 * 
 		 */
-		private void executeDemoAlgo() {
+		private void executeDemoAlgo(RunResult result) {
 			extractPositionAndAccess();
 			int actualIteration = control.getModel().getCurIteration();
-			println("AlgoStart....");
-			control.calculateStateAndVisualForCurrentTimeStep();
+			println("TimeStep:" + actualIteration);
+			control.calculateStateOnlyForCurrentTimeStep();
 			List<Priority> priorityListASC = createPriorityListASC();
 			DecoratedState actualstate = control.getSimManager().getActualDecorState();	
 			for(DecoratedNetwork net : actualstate.getNetworkList()) {
@@ -332,6 +367,7 @@ public class FlexExample implements Algorithm {
 				println("production: " + production);
 				println("consumption: " + consumption);
 				println("difference: " + difference);
+				if(production > consumption) continue;
 				if(difference == 0)continue;
 				Set<HolonElement> allHolonElemntsInThisNetwork = createListOfAllHolonElemnts(net);
 				FlexManager flexManager = control.getSimManager().getActualFlexManager();
@@ -340,18 +376,18 @@ public class FlexExample implements Algorithm {
 				List<FlexWrapper> allFlexThatGetMeEnergy = allOfferedFlex.stream().filter(flexWrapper -> (flexWrapper.getFlex().bringtmir() > 0)).collect(Collectors.toList());
 				float amountOfAllEnergyOffered = sumEnergyAvailable(allFlexThatGetMeEnergy);
 				println("amountOfAllFlexEnergyOffered:" + amountOfAllEnergyOffered);
-				if(production > consumption) continue;
 				//ShuddownPriorities
 				for(Priority emergencyShutDownPriority: priorityListASC) {
 					if(amountOfAllEnergyOffered >= difference) break; 
 					println("ShutDown: " + emergencyShutDownPriority);
-					difference -= shutDownAllConsumerElementsWithPriority(flexManager, allHolonElemntsInThisNetwork, emergencyShutDownPriority);
+					difference -= shutDownAllConsumerElementsWithPriority(flexManager, allHolonElemntsInThisNetwork, emergencyShutDownPriority, result);
 				}
 				
 				//SortFlexes
 				allFlexThatGetMeEnergy.sort((flex1, flex2) -> Float.compare(flex1.getFlex().cost / flex1.getFlex().bringtmir(), flex2.getFlex().cost / flex2.getFlex().bringtmir()));
 				//OrderFlexes
 				float costForThisTimeStep = 0f;
+				int amountflexActivated = 0;
 				for(FlexWrapper flexWrapper : allFlexThatGetMeEnergy) {
 					if(!flexWrapper.canOrder()) continue;
 					float energy = flexWrapper.getFlex().bringtmir();
@@ -360,26 +396,61 @@ public class FlexExample implements Algorithm {
 						difference -= energy;
 						costForThisTimeStep += flexWrapper.getFlex().cost;
 						flexWrapper.order();
+						amountflexActivated++;
 						continue;
 					}
 				}
-				println("CostForThisTimeStep:" + costForThisTimeStep);
+				result.activatedFlex += 	amountflexActivated++;
 				
-				println("Ende");
+				println("Activated FlexThisTimeStep: "+ amountflexActivated+"   CostForThisTimeStep:" + costForThisTimeStep);
+				result.totalCost += costForThisTimeStep;
+			}
+			calculateStateResult(result);
+		}
+		private void calculateStateResult(RunResult result) {
+			control.calculateStateOnlyForCurrentTimeStep();
+			RunResult.TimeStepStateResult timeStepState = result.addTimeStepStateResult();
 			
-				
+			for(DecoratedNetwork network: control.getSimManager().getActualDecorState().getNetworkList()) {
+				timeStepState.amountOfConsumer += network.getAmountOfConsumer();
+				timeStepState.amountOfConsumerOverSupplied += network.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
+				timeStepState.amountOfConsumerPartiallySupplied += network.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
+				timeStepState.amountOfConsumerSupplied += network.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
+				timeStepState.amountOfConsumerUnSupplied += network.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
+				timeStepState.amountOfPassiv += network.getAmountOfPassiv();
+				timeStepState.amountOfProducer += network.getAmountOfSupplier();	
 			}
-			println("AlgoEnde....");
-			updateVisual();
+			println("Producer: " + timeStepState.amountOfProducer);
+			println("Consumer: " + timeStepState.amountOfConsumer);
+			println("ConsumerOverSupplied: " + timeStepState.amountOfConsumerOverSupplied);
+			println("ConsumerSupplied: " + timeStepState.amountOfConsumerSupplied);
+			println("ConsumerPartiallySupplied: " + timeStepState.amountOfConsumerPartiallySupplied);
+			println("ConsumerUnSupplied: " + timeStepState.amountOfConsumerUnSupplied);
+			println("ConsumerUnSupplied: " + timeStepState.amountOfPassiv);
+		}
+		private void calculateAllResults(RunResult result) {
+			println("----------");
+			println("Average producer proportion: " + result.getAvergaeProportionWithState(HolonObjectState.PRODUCER));
+			println("Average producer OverSupplied: " + result.getAvergaeProportionWithState(HolonObjectState.OVER_SUPPLIED));
+			println("Average producer Supplied: " + result.getAvergaeProportionWithState(HolonObjectState.SUPPLIED));
+			println("Average producer PartiallySupplied: " + result.getAvergaeProportionWithState(HolonObjectState.PARTIALLY_SUPPLIED));
+			println("Average producer NotSupplied: " + result.getAvergaeProportionWithState(HolonObjectState.NOT_SUPPLIED));
+			println("Average producer NoEnergy: " + result.getAvergaeProportionWithState(HolonObjectState.NO_ENERGY));
 		}
+		
+		
+		
+		
+		
 		private float shutDownAllConsumerElementsWithPriority(FlexManager flexManager, Set<HolonElement> allHolonElemntsInThisNetwork,
-				Priority emergencyShutDownPriority) {
+				Priority emergencyShutDownPriority, RunResult result) {
 			
 			List<HolonElement> elementsOfPriorityToShutdown = allHolonElemntsInThisNetwork.stream().filter(hElement -> hElement.isConsumer() && hElement.getPriority() == emergencyShutDownPriority && !hElement.isFlexActive(flexManager) && hElement.isActive()).collect(Collectors.toList());
 					//.forEach(hElement -> hElement.setActive(false));
 			float energyGained = elementsOfPriorityToShutdown.stream().map(hElement -> -hElement.getEnergyPerElement() * hElement.getAmount()).reduce(0.0f, (a, b) -> a + b);
 			elementsOfPriorityToShutdown.forEach(hElement -> hElement.setActive(false));
 			int shutdownCount = elementsOfPriorityToShutdown.size();
+			result.deactivatedElements += shutdownCount;
 			println("Gained " + energyGained + "Energy from Shutdown with Priority:" + emergencyShutDownPriority + " AmountOfShutDowned HolonElements: " + shutdownCount);
 			return energyGained;
 		}
@@ -453,8 +524,8 @@ public class FlexExample implements Algorithm {
 		 */
 		private void updateVisual() {
 			control.calculateStateAndVisualForCurrentTimeStep();
-			control.updateCanvas();
-			control.getGui().triggerUpdateController(null);
+			//control.updateCanvas();
+			//control.getGui().triggerUpdateController(null);
 		}
 		/**
 		 * Sets the Model back to its original State before the LAST run.
@@ -523,7 +594,53 @@ public class FlexExample implements Algorithm {
 		}
 		
 		
-		
+		private class RunResult {
+			public int activatedFlex = 0;
+			public int deactivatedElements = 0;
+			public int totalCost = 0;
+			public LinkedList<TimeStepStateResult> timeStepList = new LinkedList<TimeStepStateResult>();
+			
+			
+			public TimeStepStateResult addTimeStepStateResult(){
+				TimeStepStateResult aResult = new TimeStepStateResult();
+				timeStepList.add(aResult);
+				return aResult;
+			}
+			
+			
+			public class TimeStepStateResult{
+				public int amountOfProducer = 0;
+				public int amountOfConsumer = 0;
+				public int amountOfPassiv = 0;
+				public int amountOfConsumerOverSupplied = 0;
+				public int amountOfConsumerSupplied = 0;
+				public int amountOfConsumerPartiallySupplied = 0;
+				public int amountOfConsumerUnSupplied= 0;
+				
+				public float getProportionWithState(HolonObjectState state) {
+					float amountOfObjects = amountOfProducer + amountOfConsumer + amountOfPassiv;
+					switch(state) {
+					case NOT_SUPPLIED:
+						return (float) amountOfConsumerUnSupplied / amountOfObjects;
+					case NO_ENERGY:
+						return (float) amountOfPassiv / amountOfObjects;
+					case OVER_SUPPLIED:
+						return (float) amountOfConsumerOverSupplied / amountOfObjects;
+					case PARTIALLY_SUPPLIED:
+						return (float) amountOfConsumerPartiallySupplied / amountOfObjects;
+					case PRODUCER:
+						return (float) amountOfProducer / amountOfObjects;
+					case SUPPLIED:
+						return (float) amountOfConsumerSupplied / amountOfObjects;
+					default:
+						return 0.f;
+					}
+				}
+			}
+			public float getAvergaeProportionWithState(HolonObjectState state) {
+				return timeStepList.stream().map(step -> step.getProportionWithState(state)).reduce((a,b) -> (a + b)).orElse(0.f) / (float) 100;
+			}
+		}
 		
 		private   class  Handle<T>{
 			public T object;

+ 1 - 1
src/ui/controller/Control.java

@@ -486,7 +486,7 @@ public class Control {
      */
     public void setCurIteration(int curit) {
         globalController.setCurIteration(curit);
-        getGui().getTimePanel().getTimeSlider().setValue(curit);
+        //getGui().getTimePanel().getTimeSlider().setValue(curit);
     }
 
     /**

+ 20 - 0
src/ui/model/DecoratedNetwork.java

@@ -281,4 +281,24 @@ public class DecoratedNetwork {
 		con.getSupplierList().add(con.new SupplierListEntry(sup, energy));
 		con.setEnergyFromNetwork(con.getEnergyFromNetwork() + energy);
 	}
+
+
+	public int getAmountOfConsumer() {
+		return consumerList.size() + this.consumerSelfSuppliedList.size();
+	}
+
+
+	public int getAmountOfSupplier() {
+		return supplierList.size();
+	}
+
+
+	public int getAmountOfConsumerWithState(HolonObjectState state) {
+		return (int) (consumerList.stream().filter(con -> con.getState() == state).count() + consumerSelfSuppliedList.stream().filter(con -> con.getState() == state).count());
+	}
+
+
+	public int getAmountOfPassiv() {
+		return passivNoEnergyList.size();
+	}
 }

+ 1 - 0
src/ui/view/MyCanvas.java

@@ -584,6 +584,7 @@ public class MyCanvas extends AbstractCanvas implements MouseListener,
 		
 		VisualRepresentationalState  visualState = controller.getSimManager().getActualVisualRepresentationalState();
 		//VisualState Representation:
+		if(visualState == null) System.out.println("AHHH"); 
 		for(ExitCable cable : visualState.getExitCableList()) {
 			paintExitCable(g2d, cable);
 		}