StateEstimator.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 ArrayList<StateMsg> states;
  35. // private HashMap<String, Float> powerUsagesInsideHolarchy;
  36. public StateEstimator(HolonControlUnit hcu) {
  37. this.hcu = hcu;
  38. this.stateInd = StateIndicator.DESIRED;
  39. // this.powerUsagesInsideHolarchy = new HashMap<String, Float>();
  40. this.childStates = new HashMap<String, StateMsg>();
  41. this.states = new ArrayList<StateMsg>();
  42. }
  43. public StateIndicator getStateIndicator() {
  44. return stateInd;
  45. }
  46. /**
  47. * Depth-first computation
  48. * aggregate childrens state first, then compute own state
  49. */
  50. public void computeState(int timeStep) {
  51. //check if any edge is burned
  52. for(IntermediateCableWithState icws : this.hcu.getHolon().getMinimumModel().getEdgeList()) {
  53. if(icws.getState() == CableState.Burned || icws.getModel().isBurned()) {
  54. // System.out.println(icws.getModel()+" burned");
  55. this.hcu.getHierarchyController().removeEdge(icws.getModel());
  56. }
  57. }
  58. //request childrens state
  59. this.childStates.clear();
  60. requestChildStates(timeStep);
  61. calculatePowerUsage(timeStep);
  62. calculateNetThroughput(timeStep);
  63. //compute forecast
  64. this.predictedPowerUsage = this.hcu.getForecaster().computeForecast(this.childStates, timeStep);
  65. boolean b = this.hcu.matchPowerRange(powerUsage, this.hcu.getStateAssembler().getDesiredPowerUsage(), this.predictedPowerUsage,
  66. this.hcu.getOptimizer().getCurrentPowerThreshold());
  67. boolean c = this.hcu.getOptimizer().getOptimizationScheme() != OptimizationScheme.RECOVERY;
  68. if(c) {
  69. for(StateMsg state : this.childStates.values()) {
  70. if(state.getStateInd() != StateIndicator.DESIRED) {
  71. c = false;
  72. break;
  73. }
  74. }
  75. }
  76. if(b && c) {
  77. this.stateInd = StateIndicator.DESIRED;
  78. } else if (b){
  79. this.stateInd = StateIndicator.ENDANGERED;
  80. } else {
  81. this.stateInd = StateIndicator.DYSFUNCTIONAL;
  82. }
  83. }
  84. private void calculatePowerUsage(int timeStep) {
  85. //aggregate sub holon state
  86. this.powerUsage = 0;
  87. for(String s : this.childStates.keySet()) {
  88. float p = this.childStates.get(s).getPowerUsage();
  89. this.powerUsage += p;
  90. // this.powerUsagesInsideHolarchy.put(s, p);
  91. }
  92. //add state from holon elements
  93. Holon holon = this.hcu.getHolon();
  94. if(holon.isPhysical) {
  95. float p = holon.getHolonObject().getEnergyAtTimeStepFlex(timeStep);
  96. this.powerUsage += p;
  97. // this.powerUsagesInsideHolarchy.put(this.hcu.getHolon().getUniqueID(), p);
  98. }
  99. }
  100. private void calculateNetThroughput(int timeStep) {
  101. this.netThroughput = this.hcu.getHolon().getAllHolonObjects().stream()
  102. .filter(object -> object.getEnergyAtTimeStepFlex(timeStep) > 0.0f)
  103. .map(object -> object.getEnergyAtTimeStepFlex(timeStep))
  104. .reduce(0.0f, ((a,b) -> a + b));
  105. }
  106. public void requestChildStates(int timeStep) {
  107. List<String> children = List.copyOf(this.hcu.getHierarchyController().getSubHolons());
  108. if(this.childStates.size() == children.size())
  109. return;
  110. for(String child : children) {
  111. //send request to child
  112. if(!this.childStates.containsKey(child)) {
  113. this.hcu.getCommunicator().sendMsg(child, Message.Type.STATE_REQUEST,
  114. this.hcu.getCommunicator().getGson().toJson(new StateRequestMsg(timeStep)));
  115. }
  116. }
  117. //wait for sub holons to compute state and send it
  118. if(this.childStates.size() < children.size()) {
  119. //a subholon has disappeared, remove it from child holon
  120. for(String s : children) {
  121. if(!this.childStates.containsKey(s)) {
  122. this.hcu.getHierarchyController().splitSubHolon(s, timeStep);
  123. // System.err.println(this.hcu.getHolon().getUniqueID()+" holon "+s+" has disappeared "
  124. // +this.hcu.getHierarchyController().canMerge(s, 0f));
  125. }
  126. }
  127. }
  128. }
  129. public void receiveState(String sender, StateMsg stateMsg) {
  130. if(this.childStates == null || stateMsg == null)
  131. return;
  132. this.childStates.put(sender, stateMsg);
  133. }
  134. public void receiveStateRequest(String sender, StateRequestMsg req) {
  135. int timeStep = req.getTimeStep();
  136. if(!sender.equals(this.hcu.getHierarchyController().getSuperHolon()) || timeStep < 0)
  137. return;
  138. if(this.states.size() > timeStep) {
  139. String body = this.hcu.getCommunicator().getGson().toJson(this.states.get(timeStep));
  140. this.hcu.getCommunicator().sendMsg(sender, Message.Type.STATE, body);
  141. } else {
  142. computeState(timeStep);
  143. StateMsg stateMsg = new StateMsg(this.powerUsage, this.netThroughput, this.predictedPowerUsage, this.stateInd);
  144. //send current state to sender (super holon)
  145. String body = this.hcu.getCommunicator().getGson().toJson(stateMsg);
  146. this.hcu.getCommunicator().sendMsg(sender, Message.Type.STATE, body);
  147. this.states.add(stateMsg);
  148. }
  149. }
  150. public float getPowerUsage() {
  151. return this.powerUsage;
  152. }
  153. /**
  154. public HashMap<String, Float> getPowerUsagesInsideHolarchy() {
  155. return this.powerUsagesInsideHolarchy;
  156. }
  157. */
  158. public ArrayList<Float> getPredictedPowerUsage() {
  159. return this.predictedPowerUsage;
  160. }
  161. public HashMap<String, StateMsg> getChildStates() {
  162. return this.childStates;
  163. }
  164. public float getNetThroughput() {
  165. return netThroughput;
  166. }
  167. }