|
@@ -9,6 +9,7 @@ import java.text.NumberFormat;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.HashSet;
|
|
|
|
+import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
@@ -59,6 +60,7 @@ public class FlexExample implements Algorithm {
|
|
|
|
|
|
//Parameter defined by Algo
|
|
//Parameter defined by Algo
|
|
private HashMap<Integer, AccessWrapper> access;
|
|
private HashMap<Integer, AccessWrapper> access;
|
|
|
|
+ LinkedList<List<Boolean>> resetChain = new LinkedList<List<Boolean>>();
|
|
private List<Boolean> initialState;
|
|
private List<Boolean> initialState;
|
|
private List<HolonSwitch> switchList;
|
|
private List<HolonSwitch> switchList;
|
|
private List<HolonObject> objectList;
|
|
private List<HolonObject> objectList;
|
|
@@ -159,16 +161,20 @@ public class FlexExample implements Algorithm {
|
|
}
|
|
}
|
|
public JPanel createButtonPanel() {
|
|
public JPanel createButtonPanel() {
|
|
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
|
|
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
|
|
|
|
+ JButton resetButton = new JButton("ResetAll");
|
|
|
|
+ resetButton.setToolTipText("Resets the State to before the Algorithm has runed.");
|
|
|
|
+ resetButton.addActionListener(actionEvent -> resetAll());
|
|
|
|
+ buttonPanel.add(resetButton);
|
|
JButton cancelButton = new JButton("Cancel Run");
|
|
JButton cancelButton = new JButton("Cancel Run");
|
|
cancelButton.addActionListener(actionEvent -> cancel());
|
|
cancelButton.addActionListener(actionEvent -> cancel());
|
|
buttonPanel.add(cancelButton);
|
|
buttonPanel.add(cancelButton);
|
|
JButton clearButton = new JButton("Clear Console");
|
|
JButton clearButton = new JButton("Clear Console");
|
|
clearButton.addActionListener(actionEvent -> clear());
|
|
clearButton.addActionListener(actionEvent -> clear());
|
|
buttonPanel.add(clearButton);
|
|
buttonPanel.add(clearButton);
|
|
- JButton resetButton = new JButton("Reset");
|
|
|
|
- resetButton.setToolTipText("Resets the State to before the Algorithm has runed.");
|
|
|
|
- resetButton.addActionListener(actionEvent -> reset());
|
|
|
|
- buttonPanel.add(resetButton);
|
|
|
|
|
|
+ JButton undoButton = new JButton("Undo");
|
|
|
|
+ undoButton.setToolTipText("One Algo Step Back.");
|
|
|
|
+ undoButton.addActionListener(actionEvent -> resetLast());
|
|
|
|
+ buttonPanel.add(undoButton);
|
|
JButton runButton = new JButton("Run");
|
|
JButton runButton = new JButton("Run");
|
|
runButton.addActionListener(actionEvent -> {
|
|
runButton.addActionListener(actionEvent -> {
|
|
Runnable task = () -> run();
|
|
Runnable task = () -> run();
|
|
@@ -195,7 +201,7 @@ public class FlexExample implements Algorithm {
|
|
startTimer();
|
|
startTimer();
|
|
executeDemoAlgo();
|
|
executeDemoAlgo();
|
|
if(cancel) {
|
|
if(cancel) {
|
|
- reset();
|
|
|
|
|
|
+ resetLast();
|
|
disableGuiInput(false);
|
|
disableGuiInput(false);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -203,11 +209,25 @@ public class FlexExample implements Algorithm {
|
|
disableGuiInput(false);
|
|
disableGuiInput(false);
|
|
}
|
|
}
|
|
|
|
|
|
- private void reset() {
|
|
|
|
- if(initialState != null) {
|
|
|
|
|
|
+ private void resetLast() {
|
|
|
|
+ if(!resetChain.isEmpty()) {
|
|
println("Resetting..");
|
|
println("Resetting..");
|
|
resetState();
|
|
resetState();
|
|
|
|
+ resetChain.removeLast();
|
|
|
|
+ control.resetSimulation();
|
|
|
|
+ updateVisual();
|
|
|
|
+ }else {
|
|
|
|
+ println("No run inistialized.");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void resetAll() {
|
|
|
|
+ if(!resetChain.isEmpty()) {
|
|
|
|
+ println("Resetting..");
|
|
|
|
+ setState(resetChain.getFirst());
|
|
|
|
+ resetChain.clear();
|
|
control.resetSimulation();
|
|
control.resetSimulation();
|
|
|
|
+ control.setCurIteration(0);
|
|
updateVisual();
|
|
updateVisual();
|
|
}else {
|
|
}else {
|
|
println("No run inistialized.");
|
|
println("No run inistialized.");
|
|
@@ -215,6 +235,7 @@ public class FlexExample implements Algorithm {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
private void disableGuiInput(boolean bool) {
|
|
private void disableGuiInput(boolean bool) {
|
|
control.guiDiable(bool);
|
|
control.guiDiable(bool);
|
|
}
|
|
}
|
|
@@ -311,24 +332,39 @@ public class FlexExample implements Algorithm {
|
|
println("production: " + production);
|
|
println("production: " + production);
|
|
println("consumption: " + consumption);
|
|
println("consumption: " + consumption);
|
|
println("difference: " + difference);
|
|
println("difference: " + difference);
|
|
|
|
+ if(difference == 0)continue;
|
|
Set<HolonElement> allHolonElemntsInThisNetwork = createListOfAllHolonElemnts(net);
|
|
Set<HolonElement> allHolonElemntsInThisNetwork = createListOfAllHolonElemnts(net);
|
|
FlexManager flexManager = control.getSimManager().getActualFlexManager();
|
|
FlexManager flexManager = control.getSimManager().getActualFlexManager();
|
|
|
|
|
|
List<FlexWrapper> allOfferedFlex = flexManager.getAllFlexWrapperWithState(FlexState.OFFERED).stream().filter(flexWrapper -> allHolonElemntsInThisNetwork.contains(flexWrapper.getFlex().getElement())).collect(Collectors.toList());
|
|
List<FlexWrapper> allOfferedFlex = flexManager.getAllFlexWrapperWithState(FlexState.OFFERED).stream().filter(flexWrapper -> allHolonElemntsInThisNetwork.contains(flexWrapper.getFlex().getElement())).collect(Collectors.toList());
|
|
List<FlexWrapper> allFlexThatGetMeEnergy = allOfferedFlex.stream().filter(flexWrapper -> (flexWrapper.getFlex().bringtmir() > 0)).collect(Collectors.toList());
|
|
List<FlexWrapper> allFlexThatGetMeEnergy = allOfferedFlex.stream().filter(flexWrapper -> (flexWrapper.getFlex().bringtmir() > 0)).collect(Collectors.toList());
|
|
float amountOfAllEnergyOffered = sumEnergyAvailable(allFlexThatGetMeEnergy);
|
|
float amountOfAllEnergyOffered = sumEnergyAvailable(allFlexThatGetMeEnergy);
|
|
- println("amountOfAllEnergyOffered:" + amountOfAllEnergyOffered);
|
|
|
|
- println("vor continue");
|
|
|
|
|
|
+ println("amountOfAllFlexEnergyOffered:" + amountOfAllEnergyOffered);
|
|
if(production > consumption) continue;
|
|
if(production > consumption) continue;
|
|
- println("nach continue");
|
|
|
|
- if(priorityListASC != null) println("sdfsdf");
|
|
|
|
- priorityListASC.stream().forEach(prio -> println("Prio"));
|
|
|
|
|
|
+ //ShuddownPriorities
|
|
for(Priority emergencyShutDownPriority: priorityListASC) {
|
|
for(Priority emergencyShutDownPriority: priorityListASC) {
|
|
- println("Wir sind hier: " + emergencyShutDownPriority);
|
|
|
|
if(amountOfAllEnergyOffered >= difference) break;
|
|
if(amountOfAllEnergyOffered >= difference) break;
|
|
println("ShutDown: " + emergencyShutDownPriority);
|
|
println("ShutDown: " + emergencyShutDownPriority);
|
|
difference -= shutDownAllConsumerElementsWithPriority(flexManager, allHolonElemntsInThisNetwork, emergencyShutDownPriority);
|
|
difference -= shutDownAllConsumerElementsWithPriority(flexManager, allHolonElemntsInThisNetwork, emergencyShutDownPriority);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ //SortFlexes
|
|
|
|
+ allFlexThatGetMeEnergy.sort((flex1, flex2) -> Float.compare(flex1.getFlex().cost / flex1.getFlex().bringtmir(), flex2.getFlex().cost / flex2.getFlex().bringtmir()));
|
|
|
|
+ //OrderFlexes
|
|
|
|
+ float costForThisTimeStep = 0f;
|
|
|
|
+ for(FlexWrapper flexWrapper : allFlexThatGetMeEnergy) {
|
|
|
|
+ if(!flexWrapper.canOrder()) continue;
|
|
|
|
+ float energy = flexWrapper.getFlex().bringtmir();
|
|
|
|
+ if(energy <= difference) {
|
|
|
|
+ println("energyGained:" + energy);
|
|
|
|
+ difference -= energy;
|
|
|
|
+ costForThisTimeStep += flexWrapper.getFlex().cost;
|
|
|
|
+ flexWrapper.order();
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ println("CostForThisTimeStep:" + costForThisTimeStep);
|
|
|
|
+
|
|
println("Ende");
|
|
println("Ende");
|
|
|
|
|
|
|
|
|
|
@@ -343,7 +379,8 @@ public class FlexExample implements Algorithm {
|
|
//.forEach(hElement -> hElement.setActive(false));
|
|
//.forEach(hElement -> hElement.setActive(false));
|
|
float energyGained = elementsOfPriorityToShutdown.stream().map(hElement -> -hElement.getEnergyPerElement() * hElement.getAmount()).reduce(0.0f, (a, b) -> a + b);
|
|
float energyGained = elementsOfPriorityToShutdown.stream().map(hElement -> -hElement.getEnergyPerElement() * hElement.getAmount()).reduce(0.0f, (a, b) -> a + b);
|
|
elementsOfPriorityToShutdown.forEach(hElement -> hElement.setActive(false));
|
|
elementsOfPriorityToShutdown.forEach(hElement -> hElement.setActive(false));
|
|
- println("Gained " + energyGained + "Energy from Shutdown with Priority:" + emergencyShutDownPriority);
|
|
|
|
|
|
+ int shutdownCount = elementsOfPriorityToShutdown.size();
|
|
|
|
+ println("Gained " + energyGained + "Energy from Shutdown with Priority:" + emergencyShutDownPriority + " AmountOfShutDowned HolonElements: " + shutdownCount);
|
|
return energyGained;
|
|
return energyGained;
|
|
}
|
|
}
|
|
private Set<HolonElement> createListOfAllHolonElemnts(DecoratedNetwork net) {
|
|
private Set<HolonElement> createListOfAllHolonElemnts(DecoratedNetwork net) {
|
|
@@ -356,7 +393,10 @@ public class FlexExample implements Algorithm {
|
|
|
|
|
|
|
|
|
|
private float sumEnergyAvailable(List<FlexWrapper> flexList) {
|
|
private float sumEnergyAvailable(List<FlexWrapper> flexList) {
|
|
- return flexList.stream().map(flexWrapper -> flexWrapper.getFlex().bringtmir()).reduce(0.0f,(a, b) -> a + b);
|
|
|
|
|
|
+ HashMap<HolonElement, FlexWrapper> dublicateFilter = new HashMap<HolonElement, FlexWrapper>();
|
|
|
|
+ flexList.stream().forEach(flexWrapper -> dublicateFilter.put(flexWrapper.getFlex().getElement(), flexWrapper));
|
|
|
|
+
|
|
|
|
+ return dublicateFilter.values().stream().map(flexWrapper -> flexWrapper.getFlex().bringtmir()).reduce(0.0f,(a, b) -> a + b);
|
|
}
|
|
}
|
|
private List<Priority> createPriorityListASC() {
|
|
private List<Priority> createPriorityListASC() {
|
|
List<Priority> priorityASC = new ArrayList<Priority>();
|
|
List<Priority> priorityASC = new ArrayList<Priority>();
|
|
@@ -376,9 +416,10 @@ public class FlexExample implements Algorithm {
|
|
Model model = control.getModel();
|
|
Model model = control.getModel();
|
|
switchList = new ArrayList<HolonSwitch>();
|
|
switchList = new ArrayList<HolonSwitch>();
|
|
objectList = new ArrayList<HolonObject>();
|
|
objectList = new ArrayList<HolonObject>();
|
|
- initialState = new ArrayList<Boolean>();
|
|
|
|
|
|
+ initialState = new ArrayList<Boolean>();
|
|
access= new HashMap<Integer, AccessWrapper>();
|
|
access= new HashMap<Integer, AccessWrapper>();
|
|
- rollOutNodes((useGroupNode && (dGroupNode != null))? dGroupNode.getModel().getNodes() :model.getObjectsOnCanvas(), initialState, model.getCurIteration());
|
|
|
|
|
|
+ rollOutNodes((useGroupNode && (dGroupNode != null))? dGroupNode.getModel().getNodes() :model.getObjectsOnCanvas(), initialState, model.getCurIteration());
|
|
|
|
+ resetChain.add(initialState);
|
|
return initialState;
|
|
return initialState;
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
@@ -419,7 +460,7 @@ public class FlexExample implements Algorithm {
|
|
* Sets the Model back to its original State before the LAST run.
|
|
* Sets the Model back to its original State before the LAST run.
|
|
*/
|
|
*/
|
|
private void resetState() {
|
|
private void resetState() {
|
|
- setState(initialState);
|
|
|
|
|
|
+ setState(resetChain.getLast());
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|