HolonControlUnit.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package classes.holonControlUnit;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import classes.Holon;
  5. import classes.holonControlUnit.messages.Message;
  6. import classes.holonControlUnit.messages.StateMsg;
  7. import classes.holonControlUnit.messages.StateRequestMsg;
  8. public class HolonControlUnit {
  9. private Holon holon;
  10. private FlexibilityManager flexMan;
  11. private ForecastComputationUnit forecaster;
  12. private HierarchyControlUnit hierarchyController;
  13. private OptimizationManager optimizer;
  14. private StateEstimator stateEstimator;
  15. private TargetStateAssembler stateAssembler;
  16. private CommunicationModule communicator;
  17. private ArrayList<StateMsg> states;
  18. public HolonControlUnit(Holon h) {
  19. this.holon = h;
  20. this.hierarchyController = new HierarchyControlUnit(this);
  21. this.flexMan = new FlexibilityManager(this);
  22. this.forecaster = new ForecastComputationUnit(this);
  23. this.optimizer = new OptimizationManager(this);
  24. this.stateEstimator = new StateEstimator(this);
  25. this.stateAssembler = new TargetStateAssembler(this);
  26. this.communicator = new CommunicationModule(this);
  27. this.states = new ArrayList<StateMsg>();
  28. }
  29. public ArrayList<Holon> getSubHolon() {
  30. return this.holon.childHolons;
  31. }
  32. public void addSubHolon(Holon subHolon) {
  33. this.hierarchyController.addSubHolon(subHolon.getUniqueID());
  34. }
  35. public void addNewVirtualNeighbor(String virtualNeighbor) {
  36. this.hierarchyController.addVirtualNeighbor(virtualNeighbor);
  37. }
  38. public void addNewVirtualNeighbors(ArrayList<String> virtualNeighbor) {
  39. this.hierarchyController.addVirtualNeighbors(virtualNeighbor);
  40. }
  41. public Holon getHolon() {
  42. return holon;
  43. }
  44. public FlexibilityManager getFlexMan() {
  45. return flexMan;
  46. }
  47. public ForecastComputationUnit getForecaster() {
  48. return forecaster;
  49. }
  50. public HierarchyControlUnit getHierarchyController() {
  51. return hierarchyController;
  52. }
  53. public OptimizationManager getOptimizer() {
  54. return optimizer;
  55. }
  56. public StateEstimator getStateEstimator() {
  57. return stateEstimator;
  58. }
  59. public TargetStateAssembler getStateAssembler() {
  60. return stateAssembler;
  61. }
  62. public CommunicationModule getCommunicator() {
  63. return communicator;
  64. }
  65. public void computeState(int timeStep) {
  66. // System.out.println("compute state for "+this.holon.name+"\n\tchild holons "+this.hierarchyController.getSubHolons());
  67. //compute current state
  68. this.stateEstimator.computeState(timeStep);
  69. //compute target state
  70. this.stateAssembler.assembleTargetState(timeStep);
  71. //evaluate optimization scheme
  72. this.optimizer.evaluateOptimizationScheme(timeStep);
  73. }
  74. public void receiveStateRequest(String sender, StateRequestMsg req) {
  75. int timeStep = req.getTimeStep();
  76. if(!sender.equals(this.hierarchyController.getSuperHolon()) || timeStep < 0) {
  77. return;
  78. }
  79. if(this.states.size() > timeStep) {
  80. String body = this.communicator.getGson().toJson(this.states.get(timeStep));
  81. this.communicator.sendMsg(sender, Message.Type.STATE, body);
  82. } else {
  83. computeState(timeStep);
  84. StateMsg stateMsg = new StateMsg(this.stateEstimator.getPowerUsage(), this.stateEstimator.getNetThroughput(),
  85. this.stateEstimator.getPredictedPowerUsage(), this.stateEstimator.getStateIndicator());
  86. //send current state to sender (super holon)
  87. String body = this.communicator.getGson().toJson(stateMsg);
  88. this.communicator.sendMsg(sender, Message.Type.STATE, body);
  89. this.states.add(stateMsg);
  90. }
  91. }
  92. public boolean matchPowerRange(float power, float des, ArrayList<Float> prePower, float threshold) {
  93. float diff = Math.abs(des - threshold * des);
  94. float tres = Math.max(10-threshold*10, diff); //otherwise the range could be 0
  95. if(power > des + tres || power < des - tres)
  96. return false;
  97. for(int i=0; i<prePower.size(); i++) {
  98. if(prePower.get(i) > des + tres || prePower.get(i) < des - tres)
  99. return false;
  100. }
  101. return true;
  102. }
  103. public boolean matchPowerRange(float power, float des, float threshold) {
  104. float diff = Math.abs(des - threshold * des);
  105. float tres = Math.max(10-threshold*10, diff); //otherwise the range could be 0
  106. if(power > des + tres || power < des - tres)
  107. return false;
  108. return true;
  109. }
  110. public boolean matchPowerRange(float power, float des) {
  111. float threshold = this.optimizer.getCurrentPowerThreshold();
  112. float diff = Math.abs(des - threshold * des);
  113. float tres = Math.max(10-threshold*10, diff); //otherwise the range could be 0
  114. if(power > des + tres || power < des - tres)
  115. return false;
  116. return true;
  117. }
  118. @Deprecated
  119. public boolean decreasesPowerUsage(float requiredPower, float powerUsage, List<Float> predictedPowerUsage) {
  120. if(Math.abs(powerUsage) > Math.abs(requiredPower))
  121. return false;
  122. for(int i=0; i<predictedPowerUsage.size(); i++) {
  123. if(Math.abs(predictedPowerUsage.get(i)) > Math.abs(requiredPower))
  124. return false;
  125. }
  126. return true;
  127. }
  128. /**
  129. * checks whether addedPower + currentPower decreases the absolute value of the power usage
  130. * @param addedPower
  131. * @param currentPower
  132. * @param predictedPower
  133. * @param desiredPower
  134. * @return
  135. */
  136. public boolean decreasesPowerUsage(List<Float> addedPower, float currentPower, List<Float> predictedPower, float desiredPower) {
  137. // float threshold = this.optimizer.getCurrentPowerThreshold();
  138. // float diff = Math.abs(desiredPower - threshold * desiredPower);
  139. // float thres = Math.abs(desiredPower) + Math.max(10-threshold*10, diff); //otherwise the range could be 0
  140. if(Math.abs(currentPower + addedPower.get(0)) > Math.abs(currentPower - desiredPower))
  141. return false;
  142. for(int i=1; i<addedPower.size(); i++){
  143. if(Math.abs(predictedPower.get(i-1) + addedPower.get(i) - desiredPower) > Math.abs(predictedPower.get(i-1) - desiredPower))
  144. return false;
  145. }
  146. return true;
  147. }
  148. }