123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- 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<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++;
- }
- }
- }
- }
- public double calcValuewithSliderState() {
- implementState();
- HelpFunctions.calculateStateForTimeStepPSO(model, control);
- double value = 0.0;
- //int nrOfSubnets = control.getSimManager().getSubNets().size();
- //System.out.println("nr of subnets: " + nrOfSubnets);
- value += networkFitness();
- value += holonObjectFitness();
-
- /*for (AbstractCpsObject obj : model.getObjectsOnCanvas()) {
- if (obj instanceof HolonObject) {
- int state = ((HolonObject) obj).getState();
- double suppliedPerc = ((HolonObject) obj).getSuppliedPercentage();
- double elementsActive = (double) elementsActive((HolonObject) obj);
- double totalElements = (double) ((HolonObject) obj).getElements().size();
- double difference = Math.abs(1.0 - suppliedPerc) * 100;
- double differenceEle = (1.0 - (elementsActive / totalElements)) * 100;
- // Supply States (see HolonObject class for more informations)
- switch (state) {
- case 0:
- value += (100 * 8);
- break;
- case 1:
- value += difference * 4 + differenceEle;
- break;
- case 2:
- value += difference + differenceEle;
- break;
- case 4:
- value += difference + differenceEle;
- break;
- case 5:
- value += difference * 2 + differenceEle;
- break;
- default:
- break;
- }
- }
- }*/
- return value;
- }
- /**
- * Calculates a fitness value for the overall abstract network
- * @return
- */
- private double networkFitness() {
- double result = 0.0;
- ArrayList<SubNet> tmp_sn = control.getSimManager().getSubNets();
- int totalNrOfObjects = model.getObjectsOnCanvas().size();
-
- System.out.println("Total number of objects is: " + totalNrOfObjects);
- for (SubNet subNet : tmp_sn) {
- System.out.println("Subnetsize: " + (subNet.getObjects().size()));
- }
-
- result = (tmp_sn.size() - 1) * 300;
- return result;
- }
- /**
- * Calculate a fitnessvalue concerned with the performance of individual holons of the network
- * @return
- */
- private double holonFitness() {
- double result = 0.0;
-
- 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();
- System.out.println("total consumption works: " + control.getTotalConsumption(objList));
- //TODO Annoying as fuck... think think
- for (AbstractCpsObject obj : objList) {
- if (obj instanceof HolonObject) {
- float supply = ((HolonObject) obj).getCurrentSupply();
- float energy =((HolonObject) obj).getMaxActiveEnergy();
- float suppPercentage = ((HolonObject) obj).getSuppliedPercentage();
- float ownProduction = ((HolonObject) obj).getSelfMadeEnergy(model.getCurIteration());
- float maxEnergy = ((HolonObject) obj).getMaxPossibleConsumption();
-
- System.out.println(obj.getName());
- if(!(obj.getName().contains("Plant"))) {
- result += holonObjectSupplyPenaltyFunction(suppPercentage);
- result += holonObjectStatePenalty((HolonObject)obj);
-
- }
- result += inactiveHolonElementPenalty((HolonObject)obj);
-
-
- System.out.println("min ele" + ((HolonObject) obj).getMinimalConsumingElement().toString());
- //if(((HolonObject) obj).getCurrentEnergyAtTimeStep(model.getCurIteration())/maxEnergy <;
-
- System.out.println("Own production: " + ownProduction + "Provided Supply and energy: " + supply + " and " + energy + " with total possible energy: " + maxEnergy
- + " is supply percentage: " + suppPercentage);
- }
-
- }
-
- 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)
- result = (float) (1/supplyPercentage *1.5);
- else if (supplyPercentage < 0.25)
- result = 1/supplyPercentage * 2; //Scalar value might be problematic for large/specific examples
- else if (supplyPercentage < 1.25)
- result = 1 * supplyPercentage *2 ;
- else result = 1 * supplyPercentage * 3;
- if(Float.isInfinite(result) || Float.isNaN(result))
- result = 100;
- System.out.println("Low penalty function value: " + result + " with supply value: " + supplyPercentage);
- return result;
- }
-
- private double inactiveHolonElementPenalty(HolonObject obj) {
- float result = 0;
- int activeElements = obj.getNumberOfActiveElements();
- int maxElements = obj.getElements().size();
-
- if(activeElements == maxElements)
- result =0;
- else if(activeElements == 0) {
- result = 100;
- }else result = ((maxElements -activeElements)*200);
-
- return result;
-
- }
-
- private double holonObjectStatePenalty(HolonObject obj) {
- float result = 0;
-
- int state = obj.getState();
- switch (state) {
- case 0: result = 0;
- break;
- case 1: result = 10;
- break;
- case 2: result = 0;
- break;
- case 3: result = 0;
- break;
- case 4: result = 5;
- break;
- case 5: result = 5;
- break;
-
- default:
- break;
- }
-
-
-
-
- return result;
- }
-
- }
|