StateEstimator.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package classes.holonControlUnit;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import classes.Holon;
  6. import classes.holonControlUnit.OptimizationManager.OptimizationScheme;
  7. import classes.holonControlUnit.messages.Message;
  8. import classes.holonControlUnit.messages.StateMsg;
  9. import classes.holonControlUnit.messages.StateRequestMsg;
  10. import ui.controller.FlexManager;
  11. import ui.model.DecoratedCable.CableState;
  12. import ui.model.IntermediateCableWithState;
  13. /**
  14. * aggregates all states from sub holons and forwards the resulting own state to the state assembler
  15. * @author Jonas
  16. *
  17. */
  18. public class StateEstimator {
  19. /* indicates the current state of the holon */
  20. public enum StateIndicator {
  21. /* everything runs as expected, all parameters are inside their tresholds */
  22. DESIRED,
  23. /* everything is still as expected except for minor deviations, parts may be endangered/dysfunctional */
  24. ENDANGERED,
  25. /* stability/functionality of holon is not guaranteed */
  26. DYSFUNCTIONAL
  27. }
  28. private HolonControlUnit hcu;
  29. private StateIndicator stateInd;
  30. private float powerUsage;
  31. private float netThroughput;
  32. private HashMap<String, StateMsg> childStates;
  33. private ArrayList<Float> predictedPowerUsage;
  34. private HashMap<String, Float> powerUsagesInsideHolarchy;
  35. public StateEstimator(HolonControlUnit hcu) {
  36. this.hcu = hcu;
  37. this.stateInd = StateIndicator.DESIRED;
  38. this.powerUsagesInsideHolarchy = new HashMap<String, Float>();
  39. this.childStates = new HashMap<String, StateMsg>();
  40. }
  41. public StateIndicator getStateIndicator() {
  42. return stateInd;
  43. }
  44. /**
  45. * Depth-first computation
  46. * aggregate childrens state first, then compute own state
  47. */
  48. public void computeState(int timeStep) {
  49. //check if any edge is burned
  50. for(IntermediateCableWithState icws : this.hcu.getHolon().getMinimumModel().getEdgeList()) {
  51. if(icws.getState() == CableState.Burned || icws.getModel().isBurned()) {
  52. // System.out.println(icws.getModel()+" burned");
  53. this.hcu.getHierarchyController().removeEdge(icws.getModel());
  54. }
  55. }
  56. //request childrens state
  57. this.childStates.clear();
  58. requestChildStates(timeStep);
  59. calculatePowerUsage(timeStep);
  60. calculateNetThroughput(timeStep);
  61. //compute forecast
  62. this.predictedPowerUsage = this.hcu.getForecaster().computeForecast(this.childStates, timeStep);
  63. boolean b = this.hcu.matchPowerRange(powerUsage, this.hcu.getStateAssembler().getDesiredPowerUsage(), this.predictedPowerUsage,
  64. this.hcu.getOptimizer().getCurrentPowerThreshold());
  65. if(b && this.hcu.getOptimizer().getOptimizationScheme() != OptimizationScheme.RECOVERY) {
  66. this.stateInd = StateIndicator.DESIRED;
  67. } else if (b){
  68. this.stateInd = StateIndicator.ENDANGERED;
  69. } else {
  70. this.stateInd = StateIndicator.DYSFUNCTIONAL;
  71. }
  72. }
  73. private void calculatePowerUsage(int timeStep) {
  74. //aggregate sub holon state
  75. this.powerUsage = 0;
  76. for(String s : this.childStates.keySet()) {
  77. float p = this.childStates.get(s).getPowerUsage();
  78. this.powerUsage += p;
  79. this.powerUsagesInsideHolarchy.put(s, p);
  80. }
  81. //add state from holon elements
  82. Holon holon = this.hcu.getHolon();
  83. if(holon.isPhysical) {
  84. float p = holon.getHolonObject().getEnergyAtTimeStepWithFlex(timeStep, this.hcu.getFlexMan().getFlexManager(timeStep));
  85. this.powerUsage += p;
  86. this.powerUsagesInsideHolarchy.put(this.hcu.getHolon().getUniqueID(), p);
  87. }
  88. }
  89. private void calculateNetThroughput(int timeStep) {
  90. FlexManager newFlexManager = this.hcu.getFlexMan().getFlexManager(timeStep);
  91. this.netThroughput = this.hcu.getHolon().getAllHolonObjects().stream()
  92. .filter(object -> object.getEnergyAtTimeStepWithFlex(timeStep, newFlexManager) > 0.0f)
  93. .map(object -> object.getEnergyAtTimeStepWithFlex(timeStep, newFlexManager))
  94. .reduce(0.0f, ((a,b) -> a + b));
  95. }
  96. public void requestChildStates(int timeStep) {
  97. List<String> children = List.copyOf(this.hcu.getHierarchyController().getSubHolons());
  98. if(this.childStates.size() == children.size())
  99. return;
  100. for(String child : children) {
  101. //send request to child
  102. if(!this.childStates.containsKey(child)) {
  103. this.hcu.getCommunicator().sendMsg(child, Message.Type.STATE_REQUEST,
  104. this.hcu.getCommunicator().getGson().toJson(new StateRequestMsg(timeStep)));
  105. }
  106. }
  107. //wait for sub holons to compute state and send it
  108. if(this.childStates.size() < children.size()) {
  109. //a subholon has disappeared, remove it from child holon
  110. for(String s : children) {
  111. if(!this.childStates.containsKey(s)) {
  112. this.hcu.getHierarchyController().splitSubHolon(s, timeStep);
  113. // System.err.println(this.hcu.getHolon().getUniqueID()+" holon "+s+" has disappeared "
  114. // +this.hcu.getHierarchyController().canMerge(s, 0f));
  115. }
  116. }
  117. }
  118. }
  119. public void receiveState(String sender, StateMsg stateMsg) {
  120. if(this.childStates == null || stateMsg == null)
  121. return;
  122. this.childStates.put(sender, stateMsg);
  123. }
  124. public float getPowerUsage() {
  125. return this.powerUsage;
  126. }
  127. public HashMap<String, Float> getPowerUsagesInsideHolarchy() {
  128. return this.powerUsagesInsideHolarchy;
  129. }
  130. public ArrayList<Float> getPredictedPowerUsage() {
  131. return this.predictedPowerUsage;
  132. }
  133. public HashMap<String, StateMsg> getChildStates() {
  134. return this.childStates;
  135. }
  136. public float getNetThroughput() {
  137. return netThroughput;
  138. }
  139. }