package classes.holonControlUnit; import java.util.ArrayList; import java.util.List; import classes.Holon; import classes.holonControlUnit.messages.Message; import classes.holonControlUnit.messages.StateMsg; import classes.holonControlUnit.messages.StateRequestMsg; public class HolonControlUnit { private Holon holon; private FlexibilityManager flexMan; private ForecastComputationUnit forecaster; private HierarchyControlUnit hierarchyController; private OptimizationManager optimizer; private StateEstimator stateEstimator; private TargetStateAssembler stateAssembler; private CommunicationModule communicator; private ArrayList states; public HolonControlUnit(Holon h) { this.holon = h; this.hierarchyController = new HierarchyControlUnit(this); this.flexMan = new FlexibilityManager(this); this.forecaster = new ForecastComputationUnit(this); this.optimizer = new OptimizationManager(this); this.stateEstimator = new StateEstimator(this); this.stateAssembler = new TargetStateAssembler(this); this.communicator = new CommunicationModule(this); this.states = new ArrayList(); } public ArrayList getSubHolon() { return this.holon.childHolons; } public void addSubHolon(Holon subHolon) { this.hierarchyController.addSubHolon(subHolon.getUniqueID()); } public void addNewVirtualNeighbor(String virtualNeighbor) { this.hierarchyController.addVirtualNeighbor(virtualNeighbor); } public void addNewVirtualNeighbors(ArrayList virtualNeighbor) { this.hierarchyController.addVirtualNeighbors(virtualNeighbor); } public Holon getHolon() { return holon; } public FlexibilityManager getFlexMan() { return flexMan; } public ForecastComputationUnit getForecaster() { return forecaster; } public HierarchyControlUnit getHierarchyController() { return hierarchyController; } public OptimizationManager getOptimizer() { return optimizer; } public StateEstimator getStateEstimator() { return stateEstimator; } public TargetStateAssembler getStateAssembler() { return stateAssembler; } public CommunicationModule getCommunicator() { return communicator; } public void computeState(int timeStep) { // System.out.println("compute state for "+this.holon.name+"\n\tchild holons "+this.hierarchyController.getSubHolons()); //compute current state this.stateEstimator.computeState(timeStep); //compute target state this.stateAssembler.assembleTargetState(timeStep); //evaluate optimization scheme this.optimizer.evaluateOptimizationScheme(timeStep); } public void receiveStateRequest(String sender, StateRequestMsg req) { int timeStep = req.getTimeStep(); if(!sender.equals(this.hierarchyController.getSuperHolon()) || timeStep < 0) { return; } if(this.states.size() > timeStep) { String body = this.communicator.getGson().toJson(this.states.get(timeStep)); this.communicator.sendMsg(sender, Message.Type.STATE, body); } else { computeState(timeStep); StateMsg stateMsg = new StateMsg(this.stateEstimator.getPowerUsage(), this.stateEstimator.getNetThroughput(), this.stateEstimator.getPredictedPowerUsage(), this.stateEstimator.getStateIndicator()); //send current state to sender (super holon) String body = this.communicator.getGson().toJson(stateMsg); this.communicator.sendMsg(sender, Message.Type.STATE, body); this.states.add(stateMsg); } } public boolean matchPowerRange(float power, float des, ArrayList prePower, float threshold) { float diff = Math.abs(des - threshold * des); float tres = Math.max(10-threshold*10, diff); //otherwise the range could be 0 if(power > des + tres || power < des - tres) return false; for(int i=0; i des + tres || prePower.get(i) < des - tres) return false; } return true; } public boolean matchPowerRange(float power, float des, float threshold) { float diff = Math.abs(des - threshold * des); float tres = Math.max(10-threshold*10, diff); //otherwise the range could be 0 if(power > des + tres || power < des - tres) return false; return true; } public boolean matchPowerRange(float power, float des) { float threshold = this.optimizer.getCurrentPowerThreshold(); float diff = Math.abs(des - threshold * des); float tres = Math.max(10-threshold*10, diff); //otherwise the range could be 0 if(power > des + tres || power < des - tres) return false; return true; } @Deprecated public boolean decreasesPowerUsage(float requiredPower, float powerUsage, List predictedPowerUsage) { if(Math.abs(powerUsage) > Math.abs(requiredPower)) return false; for(int i=0; i Math.abs(requiredPower)) return false; } return true; } /** * checks whether addedPower + currentPower decreases the absolute value of the power usage * @param addedPower * @param currentPower * @param predictedPower * @param desiredPower * @return */ public boolean decreasesPowerUsage(List addedPower, float currentPower, List predictedPower, float desiredPower) { // float threshold = this.optimizer.getCurrentPowerThreshold(); // float diff = Math.abs(desiredPower - threshold * desiredPower); // float thres = Math.abs(desiredPower) + Math.max(10-threshold*10, diff); //otherwise the range could be 0 if(Math.abs(currentPower + addedPower.get(0)) > Math.abs(currentPower - desiredPower)) return false; for(int i=1; i Math.abs(predictedPower.get(i-1) - desiredPower)) return false; } return true; } }