HolonControlUnit.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. //compute current state
  67. this.stateEstimator.computeState(timeStep);
  68. //compute target state
  69. this.stateAssembler.assembleTargetState(timeStep);
  70. //evaluate optimization scheme
  71. this.optimizer.evaluateOptimizationScheme(timeStep);
  72. this.states.add(new StateMsg(this.stateEstimator.getPowerUsage(), this.stateEstimator.getNetThroughput(),
  73. this.stateEstimator.getPredictedPowerUsage(), this.stateEstimator.getStateIndicator()));
  74. }
  75. public void receiveStateRequest(String sender, StateRequestMsg req) {
  76. int timeStep = req.getTimeStep();
  77. if(!sender.equals(this.hierarchyController.getSuperHolon()) || timeStep < 0) {
  78. return;
  79. }
  80. if(this.states.size() > timeStep) {
  81. String body = this.communicator.getGson().toJson(this.states.get(timeStep));
  82. this.communicator.sendMsg(sender, Message.Type.STATE, body);
  83. } else {
  84. computeState(timeStep);
  85. StateMsg stateMsg = new StateMsg(this.stateEstimator.getPowerUsage(), this.stateEstimator.getNetThroughput(),
  86. this.stateEstimator.getPredictedPowerUsage(), this.stateEstimator.getStateIndicator());
  87. //send current state to sender (super holon)
  88. String body = this.communicator.getGson().toJson(stateMsg);
  89. this.communicator.sendMsg(sender, Message.Type.STATE, body);
  90. this.states.add(stateMsg);
  91. }
  92. }
  93. public boolean matchPowerRange(float power, float des, ArrayList<Float> prePower, float threshold) {
  94. float diff = Math.abs(des - threshold * des);
  95. float tres = Math.max(10-threshold*10, diff); //otherwise the range could be 0
  96. if(power > des + tres || power < des - tres)
  97. return false;
  98. for(int i=0; i<prePower.size(); i++) {
  99. if(prePower.get(i) > des + tres || prePower.get(i) < des - tres)
  100. return false;
  101. }
  102. return true;
  103. }
  104. public boolean matchPowerRange(float power, float des) {
  105. float diff = Math.abs(des - 0.99f * des);
  106. float tres = diff == 0 ? 1 : diff; //otherwise the range could be 0
  107. if(power > des + tres || power < des - tres)
  108. return false;
  109. return true;
  110. }
  111. /**
  112. * checks whether addedPower + currentPower decreases the absolute value of the power usage
  113. * @param addedPower
  114. * @param currentPower
  115. * @param predictedPower
  116. * @param desiredPower
  117. * @return
  118. */
  119. public boolean decreasesPowerUsage(List<Float> addedPower, float currentPower, List<Float> predictedPower, float desiredPower) {
  120. if(addedPower.size() > 0 && Math.abs(currentPower + addedPower.get(0)) > Math.abs(currentPower - desiredPower))
  121. return false;
  122. for(int i=1; i<addedPower.size(); i++){
  123. if(Math.abs(predictedPower.get(i-1) + addedPower.get(i) - desiredPower) > Math.abs(predictedPower.get(i-1) - desiredPower))
  124. return false;
  125. }
  126. return true;
  127. }
  128. }