HolonControlUnit.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. public HolonControlUnit(Holon h) {
  18. this.holon = h;
  19. this.hierarchyController = new HierarchyControlUnit(this);
  20. this.flexMan = new FlexibilityManager(this);
  21. this.forecaster = new ForecastComputationUnit(this);
  22. this.optimizer = new OptimizationManager(this);
  23. this.stateEstimator = new StateEstimator(this);
  24. this.stateAssembler = new TargetStateAssembler(this);
  25. this.communicator = new CommunicationModule(this);
  26. }
  27. public ArrayList<Holon> getSubHolon() {
  28. return this.holon.childHolons;
  29. }
  30. public void addSubHolon(Holon subHolon) {
  31. this.hierarchyController.addSubHolon(subHolon.getUniqueID());
  32. }
  33. public void addNewVirtualNeighbor(String virtualNeighbor) {
  34. this.hierarchyController.addVirtualNeighbor(virtualNeighbor);
  35. }
  36. public void addNewVirtualNeighbors(ArrayList<String> virtualNeighbor) {
  37. this.hierarchyController.addVirtualNeighbors(virtualNeighbor);
  38. }
  39. public Holon getHolon() {
  40. return holon;
  41. }
  42. public FlexibilityManager getFlexMan() {
  43. return flexMan;
  44. }
  45. public ForecastComputationUnit getForecaster() {
  46. return forecaster;
  47. }
  48. public HierarchyControlUnit getHierarchyController() {
  49. return hierarchyController;
  50. }
  51. public OptimizationManager getOptimizer() {
  52. return optimizer;
  53. }
  54. public StateEstimator getStateEstimator() {
  55. return stateEstimator;
  56. }
  57. public TargetStateAssembler getStateAssembler() {
  58. return stateAssembler;
  59. }
  60. public CommunicationModule getCommunicator() {
  61. return communicator;
  62. }
  63. public void computeState(int timeStep) {
  64. //compute current state
  65. this.stateEstimator.computeState(timeStep);
  66. //compute target state
  67. this.stateAssembler.assembleTargetState(timeStep);
  68. //evaluate optimization scheme
  69. this.optimizer.evaluateOptimizationScheme(timeStep);
  70. }
  71. public boolean matchPowerRange(float power, float des, ArrayList<Float> prePower, float threshold) {
  72. float diff = Math.abs(des - threshold * des);
  73. float tres = Math.max(10-threshold*10, diff); //otherwise the range could be 0
  74. if(power > des + tres || power < des - tres)
  75. return false;
  76. for(int i=0; i<prePower.size(); i++) {
  77. if(prePower.get(i) > des + tres || prePower.get(i) < des - tres)
  78. return false;
  79. }
  80. return true;
  81. }
  82. public boolean matchPowerRange(float power, float des, float threshold) {
  83. float diff = Math.abs(des - threshold * des);
  84. float tres = Math.max(10-threshold*10, diff); //otherwise the range could be 0
  85. if(power > des + tres || power < des - tres)
  86. return false;
  87. return true;
  88. }
  89. public boolean matchPowerRange(float power, float des) {
  90. float threshold = this.optimizer.getCurrentPowerThreshold();
  91. float diff = Math.abs(des - threshold * des);
  92. float tres = Math.max(10-threshold*10, diff); //otherwise the range could be 0
  93. if(power > des + tres || power < des - tres)
  94. return false;
  95. return true;
  96. }
  97. @Deprecated
  98. public boolean decreasesPowerUsage(float requiredPower, float powerUsage, List<Float> predictedPowerUsage) {
  99. if(Math.abs(powerUsage) > Math.abs(requiredPower))
  100. return false;
  101. for(int i=0; i<predictedPowerUsage.size(); i++) {
  102. if(Math.abs(predictedPowerUsage.get(i)) > Math.abs(requiredPower))
  103. return false;
  104. }
  105. return true;
  106. }
  107. /**
  108. * checks whether addedPower + currentPower decreases the absolute value of the power usage
  109. * @param addedPower
  110. * @param currentPower
  111. * @param predictedPower
  112. * @param desiredPower
  113. * @return
  114. */
  115. public boolean decreasesPowerUsage(List<Float> addedPower, float currentPower, List<Float> predictedPower, float desiredPower) {
  116. // float threshold = this.optimizer.getCurrentPowerThreshold();
  117. // float diff = Math.abs(desiredPower - threshold * desiredPower);
  118. // float thres = Math.abs(desiredPower) + Math.max(10-threshold*10, diff); //otherwise the range could be 0
  119. if(Math.abs(currentPower + addedPower.get(0)) > Math.abs(currentPower - desiredPower))
  120. return false;
  121. for(int i=1; i<addedPower.size(); i++){
  122. if(Math.abs(predictedPower.get(i-1) + addedPower.get(i) - desiredPower) > Math.abs(predictedPower.get(i-1) - desiredPower))
  123. return false;
  124. }
  125. return true;
  126. }
  127. }