StateEstimator.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. boolean c = this.hcu.getOptimizer().getOptimizationScheme() != OptimizationScheme.RECOVERY;
  66. if(c) {
  67. for(StateMsg state : this.childStates.values()) {
  68. if(state.getStateInd() != StateIndicator.DESIRED) {
  69. c = false;
  70. break;
  71. }
  72. }
  73. }
  74. if(b && c) {
  75. this.stateInd = StateIndicator.DESIRED;
  76. } else if (b){
  77. this.stateInd = StateIndicator.ENDANGERED;
  78. } else {
  79. this.stateInd = StateIndicator.DYSFUNCTIONAL;
  80. }
  81. }
  82. private void calculatePowerUsage(int timeStep) {
  83. //aggregate sub holon state
  84. this.powerUsage = 0;
  85. for(String s : this.childStates.keySet()) {
  86. float p = this.childStates.get(s).getPowerUsage();
  87. this.powerUsage += p;
  88. // this.powerUsagesInsideHolarchy.put(s, p);
  89. }
  90. //add state from holon elements
  91. Holon holon = this.hcu.getHolon();
  92. if(holon.isPhysical) {
  93. float p = holon.getHolonObject().getEnergyAtTimeStepFlex(timeStep);
  94. this.powerUsage += p;
  95. // this.powerUsagesInsideHolarchy.put(this.hcu.getHolon().getUniqueID(), p);
  96. }
  97. }
  98. private void calculateNetThroughput(int timeStep) {
  99. this.netThroughput = this.hcu.getHolon().getAllHolonObjects().stream()
  100. .filter(object -> object.getEnergyAtTimeStepFlex(timeStep) > 0.0f)
  101. .map(object -> object.getEnergyAtTimeStepFlex(timeStep))
  102. .reduce(0.0f, ((a,b) -> a + b));
  103. }
  104. public void requestChildStates(int timeStep) {
  105. List<String> children = List.copyOf(this.hcu.getHierarchyController().getSubHolons());
  106. if(this.childStates.size() == children.size())
  107. return;
  108. for(String child : children) {
  109. //send request to child
  110. if(!this.childStates.containsKey(child)) {
  111. this.hcu.getCommunicator().sendMsg(child, Message.Type.STATE_REQUEST,
  112. this.hcu.getCommunicator().getGson().toJson(new StateRequestMsg(timeStep)));
  113. }
  114. }
  115. //wait for sub holons to compute state and send it
  116. if(this.childStates.size() < children.size()) {
  117. //a subholon has disappeared, remove it from child holon
  118. for(String s : children) {
  119. if(!this.childStates.containsKey(s)) {
  120. this.hcu.getHierarchyController().splitSubHolon(s, timeStep);
  121. // System.err.println(this.hcu.getHolon().getUniqueID()+" holon "+s+" has disappeared "
  122. // +this.hcu.getHierarchyController().canMerge(s, 0f));
  123. }
  124. }
  125. }
  126. }
  127. public void receiveState(String sender, StateMsg stateMsg) {
  128. if(this.childStates == null || stateMsg == null)
  129. return;
  130. this.childStates.put(sender, stateMsg);
  131. }
  132. public float getPowerUsage() {
  133. return this.powerUsage;
  134. }
  135. /**
  136. public HashMap<String, Float> getPowerUsagesInsideHolarchy() {
  137. return this.powerUsagesInsideHolarchy;
  138. }
  139. */
  140. public ArrayList<Float> getPredictedPowerUsage() {
  141. return this.predictedPowerUsage;
  142. }
  143. public HashMap<String, StateMsg> getChildStates() {
  144. return this.childStates;
  145. }
  146. public float getNetThroughput() {
  147. return netThroughput;
  148. }
  149. }