package psoAlgoCode; import java.util.ArrayList; import java.util.Vector; import org.omg.PortableInterceptor.NON_EXISTENT; import classes.AbstractCpsObject; import classes.HolonElement; import classes.HolonObject; import classes.SubNet; import ui.controller.Control; import ui.model.Model; public class SGFunctions { private Model model; private Control control; private Particle p; public SGFunctions(Particle p, Model model, Control control) { this.p = p; this.model = model; this.control = control; } public void implementState() { Coordinate> pos = p.getPositionAdv(); // Set states of the HolonSwitches depending on the pos of the swarm (dim=0) for (int i = 0; i < pos.getCoord(0).size(); i++) { model.getSwitches().get(i).setManualMode(true); model.getSwitches().get(i).setManualState((boolean) pos.getCoord(0).get(i)); } int position = 0; for (int j = 0; j < model.getObjectsOnCanvas().size(); j++) { if (model.getObjectsOnCanvas().get(j) instanceof HolonObject) { for (int h = 0; h < ((HolonObject) model.getObjectsOnCanvas().get(j)).getElements().size(); h++) { ((HolonObject) model.getObjectsOnCanvas().get(j)).getElements().get(h) .setActive((boolean) pos.getCoord(1).get(position)); position++; } } } } /** * The general fitnessfunction that calculates the overall fitness for the current state * @return */ public double calcValuewithSliderState() { implementState(); HelpFunctions.calculateStateForTimeStepPSO(model, control); double value = 0.0; double nw_fitness =0.0; double object_fitness = 0.0; nw_fitness = networkFitness(); object_fitness = holonObjectFitness(); value = nw_fitness + object_fitness; return value; } /** * Calculates a fitness value based on the difference between supply and production for each individual subnet/Holon in the network * @return */ private double networkFitness() { double result = 0.0; ArrayList tmp_sn = HelpFunctions.getCurrentSubnets(); for (SubNet subNet : tmp_sn) { ArrayList tmp = subNet.getObjects(); for (HolonObject holonObject : tmp) { // System.out.println("Current state: " +holonObject.getState()); if(holonObject.getNumberOfActiveElements() == 0) result += 1000; } //TODO Problem might be here because of isolated parts of the network where production and consumption differs greatly. Like isolating a PP without deactivating in means 100% prod vs 0% consumption //NOte: Might be a little too strong float production = control.getSimManager().calculateEnergyWithoutFlexDevices("prod", subNet, model.getCurIteration()); float consumption = control.getSimManager().calculateEnergyWithoutFlexDevices("cons", subNet, model.getCurIteration()); result += Math.abs(production+consumption); } return result; } /** * Calculate a fitnessvalue concerned with the performance of individual holons of the network * @return */ private double holonFitness() { double result = 0.0; //Currently not in use return result; } /** * Calculates a fitnessvalue for the individual holon objects in the holons * @return */ private double holonObjectFitness() { double result = 0.0; ArrayList objList = model.getObjectsOnCanvas(); for (AbstractCpsObject obj : objList) { if (obj instanceof HolonObject) { //There is no supply percentage for powerplants if(!(obj.getName().contains("Plant"))) { float suppPercentage = ((HolonObject) obj).getSuppliedPercentage(); //Holon Object supply state based penalty result += holonObjectSupplyPenaltyFunction(suppPercentage); //result += holonObjectStatePenalty((HolonObject)obj); } //Deactivated Holon Element penalty. result += inactiveHolonElementPenalty((HolonObject)obj); } } return result; } /** * Calculates a penalty value based on the HOs current supply percentage * @param supplyPercentage * @return */ private double holonObjectSupplyPenaltyFunction(float supplyPercentage) { float result = 0; if(supplyPercentage == 1) return result; else if(supplyPercentage < 1 && supplyPercentage >= 0.25) // undersupplied inbetween 25% and 100% result = (float) Math.pow(1/supplyPercentage, 2); else if (supplyPercentage < 0.25) //undersupplied with less than 25% result = (float) Math.pow(1/supplyPercentage,2); else if (supplyPercentage < 1.25) //Oversupplied less than 25% result = (float) Math.pow(supplyPercentage,3) ; else result = (float) Math.pow(supplyPercentage,4); //Oversupplied more than 25% if(Float.isInfinite(result) || Float.isNaN(result)) result = 1000; return result; } /** * Function that returns the fitness depending on the number of elements deactivated in a single holon object * @param obj Holon Object that contains Holon Elements * @return fitness value for that object depending on the number of deactivated holon elements */ private double inactiveHolonElementPenalty(HolonObject obj) { float result = 0; int activeElements = obj.getNumberOfActiveElements(); int maxElements = obj.getElements().size(); if(activeElements == maxElements) result =0; else result = (float) Math.pow((maxElements -activeElements),2)*100; return result; } /** * Pentalty Function that is based on the different states an object can have. * @param obj The holon object that is used for the assessment * @return pentalty value */ private double holonObjectStatePenalty(HolonObject obj) { float result = 0; //TODO currently no penalties on undesired states, since the state assignment is broken... int state = obj.getState(); switch (state) { case 0: result = 0; break; case 1: result = 0; break; case 2: result = 0; break; case 3: result = 0; break; case 4: result = 0; break; case 5: result = 0; break; default: break; } return result; } }