StateEstimator.java 4.7 KB

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