123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- package psoAlgoCode;
- import java.util.ArrayList;
- import java.util.Vector;
- import org.omg.PortableInterceptor.NON_EXISTENT;
- import classes.AbstractCpsObject;
- import classes.CpsUpperNode;
- 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<Vector<Object>> 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++;
- }
- }
- else {
- //Does not work for more than two recursive levels!!!!!!!!!
- if(model.getObjectsOnCanvas().get(j) instanceof CpsUpperNode) {
- CpsUpperNode tmp = (CpsUpperNode) model.getObjectsOnCanvas().get(j);
- for( int k = 0; k < tmp.getNodes().size(); k++) {
- if (tmp.getNodes().get(k) instanceof HolonObject) {
- HolonObject obj = ((HolonObject) tmp.getNodes().get(k));
- for (int l = 0; l < obj.getElements().size(); l++) {
- obj.getElements().get(l).setActive((boolean) pos.getCoord(1).get(position));
- position++;
- }
- }
- }
- }
- }
- }
- }
-
- /**
- * The general fitnessfunction that calculates the overall fitness for the current state
- * @return
- */
- public double calculateFitness() {
- 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<SubNet> tmp_sn = HelpFunctions.getCurrentSubnets();
-
- for (SubNet subNet : tmp_sn) {
- ArrayList<HolonObject> 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<AbstractCpsObject> objList = model.getObjectsOnCanvas();
- for (AbstractCpsObject obj : objList) {
- float suppPercentage = 0;
- if (obj instanceof HolonObject) {
-
- //There is no supply percentage for powerplants
- if(!(obj.getName().contains("Plant"))) {
- 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);
-
- }
- else {
- if (obj instanceof CpsUpperNode) {
- for (AbstractCpsObject abstractCpsObject : ((CpsUpperNode) obj).getNodes()) {
- if (abstractCpsObject instanceof HolonObject) {
- //There is no supply percentage for powerplants
- if(!(abstractCpsObject.getName().contains("Plant"))) {
- suppPercentage = ((HolonObject) abstractCpsObject).getSuppliedPercentage();
-
- //Holon Object supply state based penalty
- result += holonObjectSupplyPenaltyFunction(suppPercentage);
- //result += holonObjectStatePenalty((HolonObject)obj);
-
- }
- //Deactivated Holon Element penalty.
- result += inactiveHolonElementPenalty((HolonObject)abstractCpsObject);
- }
- }
- }
- }
-
- }
-
- 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;
- }
-
- }
|