Browse Source

Randomizer Update Flexibilities

Tom Troppmann 4 years ago
parent
commit
071f143253
2 changed files with 90 additions and 15 deletions
  1. 88 14
      src/exampleAlgorithms/Randomizer.java
  2. 2 1
      src/ui/view/FlexWindow.java

+ 88 - 14
src/exampleAlgorithms/Randomizer.java

@@ -28,10 +28,13 @@ import com.google.gson.JsonElement;
 import com.google.gson.JsonIOException;
 import com.google.gson.JsonParser;
 import com.google.gson.JsonSyntaxException;
+import com.google.gson.annotations.Expose;
 
 import api.AddOn;
 import classes.AbstractCpsObject;
+import classes.Constrain;
 import classes.CpsUpperNode;
+import classes.Flexibility;
 import classes.HolonElement;
 import classes.HolonObject;
 import classes.HolonSwitch;
@@ -54,7 +57,7 @@ public class Randomizer implements AddOn {
 	      newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
 	private File file;
-	private double randomChance = 1.0;
+	public double randomChance = 1.0;
 	
 	public Randomizer(){
 		content.setLayout(new BorderLayout());
@@ -113,11 +116,11 @@ public class Randomizer implements AddOn {
 	}
 	
 	private void run() {
-		List<HolonObjectSketch> holonObjectCatalog = new ArrayList<HolonObjectSketch>();
+		List<HolonElementSketch> holonElementCatalog = new ArrayList<HolonElementSketch>();
 		if(file == null)file = openCatalogFile();
 		if(file == null) return;
-		readCatalogFromJson(file, holonObjectCatalog);
-		holonObjectCatalog.forEach(con -> con.checkMinMax());
+		readCatalogFromJson(file, holonElementCatalog);
+		holonElementCatalog.forEach(con -> con.checkValues());
 		
 		List<HolonObject> consumerList = new ArrayList<HolonObject>();
 		rollOutNodes(control.getModel().getObjectsOnCanvas(), consumerList, control.getModel().getCurIteration());
@@ -125,8 +128,7 @@ public class Randomizer implements AddOn {
 			con.getElements().clear();
 			int randomAmountOfElementsToAdd = Random.nextIntegerInRange(minAmountOfElements, maxAmountOfElements + 1);
 			for(int i = 0; i < randomAmountOfElementsToAdd; i++) {
-				HolonElement ele = holonObjectCatalog.get(Random.nextIntegerInRange(0, holonObjectCatalog.size())).createHolonElement(control.getModel());
-				ele.setActive(Random.nextDouble() < randomChance);
+				HolonElement ele = holonElementCatalog.get(Random.nextIntegerInRange(0, holonElementCatalog.size())).createHolonElement(control.getModel(), Random.nextDouble() < randomChance , con.getName());
 				con.getElements().add(ele);				
 			}
 		});
@@ -159,22 +161,41 @@ public class Randomizer implements AddOn {
 		
 	}
 
-	private class HolonObjectSketch {
+	private class HolonElementSketch {
+		//HolonElement
 		public String name;
 		public int minAmount;
 		public int maxAmount;
 		public float energy;
-		public HolonObjectSketch(String name, int minAmount, int maxAmount, float energy) {
+		//Flexibility
+		public double flexChance;
+		@Expose
+		public float minCost;
+		public float maxCost;
+		
+		/** The Duration in TimeSteps how long the Flexibility is activated.*/
+		private int minDuration;
+		private int maxDuration;
+		/** The Duration after a successful activation between the next possible activation.*/
+		private int minCooldown;
+		private int maxCooldown;
+		
+		
+		
+		public HolonElementSketch(String name, int minAmount, int maxAmount, float energy) {
 			this.name = name;
 			this.minAmount = minAmount;
 			this.maxAmount = maxAmount;
 			this.energy = energy;
 		}
-		public HolonElement createHolonElement(Model model) {
-			return new HolonElement(name, Random.nextIntegerInRange(minAmount, maxAmount + 1), energy , model);
+		public HolonElement createHolonElement(Model model, boolean active, String nameOfHolonObject) {
+			HolonElement ele = new HolonElement(name, Random.nextIntegerInRange(minAmount, maxAmount + 1), energy , model);
+			ele.setActive(active);
+			if(Random.nextDouble() < flexChance)addFlex(ele, nameOfHolonObject);
+			return ele;
 		}
 		
-		public void checkMinMax() {
+		public void checkValues() {
 			minAmount = Math.abs(minAmount);
 			maxAmount = Math.abs(maxAmount);
 			if(maxAmount < minAmount) {
@@ -183,9 +204,53 @@ public class Randomizer implements AddOn {
 				minAmount = maxAmount;
 				maxAmount = intermediate;
 			}
+			
+			minDuration = Math.abs(minDuration);
+			maxDuration = Math.abs(maxDuration);
+			if(maxDuration < minDuration) {
+				//Swap
+				int intermediate = minDuration;
+				minDuration = maxDuration;
+				maxDuration = intermediate;
+			}
+			minCooldown = Math.abs(minCooldown);
+			maxCooldown = Math.abs(maxCooldown);
+			if(maxCooldown < minCooldown) {
+				//Swap
+				int intermediate = minCooldown;
+				minCooldown = maxCooldown;
+				maxCooldown = intermediate;
+			}
+			minCost = Math.abs(minCost);
+			maxCost = Math.abs(maxCost);
+			if(maxCost < minCost) {
+				//Swap
+				float intermediate = minCost;
+				minCost = maxCost;
+				maxCost = intermediate;
+			}
+			flexChance = Math.max(0, Math.min(1, flexChance)); //Clamp
 		}
 		
-		
+		public void addFlex(HolonElement ele, String nameOfHolonObject) {
+			Flexibility toCreateFlex = new Flexibility(ele);
+			boolean flexType = ele.isActive();
+			
+			
+			
+			toCreateFlex.name = nameOfHolonObject + "_" + ele.getEleName() + (flexType?"_OnFlex":"_OffFlex");
+			toCreateFlex.speed = 0;
+			toCreateFlex.setDuration(Random.nextIntegerInRange(minDuration, maxDuration+1));
+			toCreateFlex.cost = Random.nextFloatInRange(minCost, maxCost);
+			toCreateFlex.setCooldown(Random.nextIntegerInRange(minCooldown, maxCooldown+1));
+			toCreateFlex.offered=true;
+			if(flexType) {
+				toCreateFlex.constrainList.add(Constrain.createOnConstrain());
+			}else {
+				toCreateFlex.constrainList.add(Constrain.createOffConstrain());
+			}
+			ele.flexList.add(toCreateFlex);
+		}
 	}
 	private JSlider createFlipChanceSlider() {
 		JSlider flipChance =new JSlider(JSlider.HORIZONTAL,0, 100, 100);
@@ -225,6 +290,15 @@ public class Randomizer implements AddOn {
 			return random.nextDouble();
 		}
 		
+		public static float nextFloatInRange(float min, float max) {
+			return min + random.nextFloat() * Math.abs(max - min);
+		}
+		
+		
+		public static double nextDoubleInRange(double min, double max) {
+			return min + random.nextDouble() * Math.abs(max - min);
+		}
+		
 		/**
 		 * Random Int in Range [min;max[ with UniformDistirbution
 		 * @param min
@@ -235,7 +309,7 @@ public class Randomizer implements AddOn {
 			return min + random.nextInt(max - min);
 		}
 	}
-	private void readCatalogFromJson(File file, List<HolonObjectSketch> catalog) {
+	private void readCatalogFromJson(File file, List<HolonElementSketch> catalog) {
 		Gson gson = new Gson();
 		JsonParser parser = new JsonParser();
 		try {
@@ -243,7 +317,7 @@ public class Randomizer implements AddOn {
 			if(jsonTreeRoot.isJsonArray()) {
 				JsonArray jsonArray = jsonTreeRoot.getAsJsonArray();
 				jsonArray.forEach(jsonObject -> {
-					HolonObjectSketch newObject= gson.fromJson( jsonObject, HolonObjectSketch.class);
+					HolonElementSketch newObject= gson.fromJson( jsonObject, HolonElementSketch.class);
 					catalog.add(newObject);
 				});
 			}	

+ 2 - 1
src/ui/view/FlexWindow.java

@@ -279,7 +279,8 @@ public class FlexWindow extends JFrame {
 	+ ((actual.remainingTimeTillActivation() != 0)?"<i>Remaining TimeTillActivation:"+ actual.remainingTimeTillActivation()+"</i><br>":"")
 	+ "Duration: " + actual.getFlex().getDuration()  + "<br>"
 	+ "Cooldown: " + actual.getFlex().getCooldown() + "<br>"
-	+ "Speed: " + actual.getFlex().speed + "<br>"
+	//+ "Speed: " + actual.getFlex().speed + "<br>"
+	+ "Cost: " + actual.getFlex().cost + "<br>"
 	+ "Constrains: " + actual.getFlex().constrainList.stream().map(constrain -> constrain.getName()).collect(Collectors.joining( "," )) + "<br>"
 	+ "</html>";