username 5 سال پیش
والد
کامیت
de6cd6b9ab
1فایلهای تغییر یافته به همراه30 افزوده شده و 16 حذف شده
  1. 30 16
      src/exampleAlgorithms/FlexExample.java

+ 30 - 16
src/exampleAlgorithms/FlexExample.java

@@ -79,6 +79,11 @@ public class FlexExample implements Algorithm {
 
 		
 		
+		
+		
+		
+		
+		
 		public static void main(String[] args)
 		{
 		      JFrame newFrame = new JFrame("exampleWindow");
@@ -213,22 +218,26 @@ public class FlexExample implements Algorithm {
 			disableGuiInput(true);
 			startTimer();
 			control.resetSimulation();
+			RunResult result= new RunResult();
 			for(int i = 0; i < 100; i++) {
 				control.setCurIteration(i);
-				executeDemoAlgo();
+				executeDemoAlgo(result);
 				if(cancel) {
 					resetLast();
 					disableGuiInput(false);
 					return;
 				}				
 			}
+			updateVisual();
+			println("Amount of activatedFlex:" + result.activatedFlex + "   Amount of deactivatedElements:"+ result.deactivatedElements + "   TotalCost:"+result.totalCost);
 			printElapsedTime();
 			disableGuiInput(false);
 		}
 		private void run() {
 			disableGuiInput(true);
 			startTimer();
-			executeDemoAlgo();
+			executeDemoAlgo(new RunResult());
+			updateVisual();
 			printElapsedTime();
 			disableGuiInput(false);
 		}
@@ -342,11 +351,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()) {
@@ -356,6 +365,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();
@@ -364,18 +374,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();
@@ -384,26 +394,26 @@ 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;
 				
 			}
-			println("AlgoEnde....");
-			updateVisual();
 		}
 		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;
 		}
@@ -477,8 +487,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.
@@ -547,7 +557,11 @@ public class FlexExample implements Algorithm {
 		}
 		
 		
-		
+		private class RunResult {
+			public int activatedFlex = 0;
+			public int deactivatedElements = 0;
+			public int totalCost = 0;
+		}
 		
 		private   class  Handle<T>{
 			public T object;