SGFunctions.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package psoAlgoCode;
  2. import java.util.ArrayList;
  3. import java.util.Vector;
  4. import org.omg.PortableInterceptor.NON_EXISTENT;
  5. import classes.AbstractCpsObject;
  6. import classes.HolonElement;
  7. import classes.HolonObject;
  8. import classes.SubNet;
  9. import ui.controller.Control;
  10. import ui.model.Model;
  11. public class SGFunctions {
  12. private Model model;
  13. private Control control;
  14. private Particle p;
  15. public SGFunctions(Particle p, Model model, Control control) {
  16. this.p = p;
  17. this.model = model;
  18. this.control = control;
  19. }
  20. public void implementState() {
  21. Coordinate<Vector<Object>> pos = p.getPositionAdv();
  22. // Set states of the HolonSwitches depending on the pos of the swarm (dim=0)
  23. for (int i = 0; i < pos.getCoord(0).size(); i++) {
  24. model.getSwitches().get(i).setManualMode(true);
  25. model.getSwitches().get(i).setManualState((boolean) pos.getCoord(0).get(i));
  26. }
  27. int position = 0;
  28. for (int j = 0; j < model.getObjectsOnCanvas().size(); j++) {
  29. if (model.getObjectsOnCanvas().get(j) instanceof HolonObject) {
  30. for (int h = 0; h < ((HolonObject) model.getObjectsOnCanvas().get(j)).getElements().size(); h++) {
  31. ((HolonObject) model.getObjectsOnCanvas().get(j)).getElements().get(h)
  32. .setActive((boolean) pos.getCoord(1).get(position));
  33. position++;
  34. }
  35. }
  36. }
  37. }
  38. /**
  39. * The general fitnessfunction that calculates the overall fitness for the current state
  40. * @return
  41. */
  42. public double calcValuewithSliderState() {
  43. implementState();
  44. HelpFunctions.calculateStateForTimeStepPSO(model, control);
  45. double value = 0.0;
  46. double nw_fitness =0.0;
  47. double object_fitness = 0.0;
  48. nw_fitness = networkFitness();
  49. object_fitness = holonObjectFitness();
  50. value = nw_fitness + object_fitness;
  51. return value;
  52. }
  53. /**
  54. * Calculates a fitness value based on the difference between supply and production for each individual subnet/Holon in the network
  55. * @return
  56. */
  57. private double networkFitness() {
  58. double result = 0.0;
  59. ArrayList<SubNet> tmp_sn = HelpFunctions.getCurrentSubnets();
  60. for (SubNet subNet : tmp_sn) {
  61. ArrayList<HolonObject> tmp = subNet.getObjects();
  62. for (HolonObject holonObject : tmp) {
  63. // System.out.println("Current state: " +holonObject.getState());
  64. if(holonObject.getNumberOfActiveElements() == 0)
  65. result += 1000;
  66. }
  67. float production = control.getSimManager().calculateEnergyWithoutFlexDevices("prod",
  68. subNet, model.getCurIteration());
  69. float consumption = control.getSimManager().calculateEnergyWithoutFlexDevices("cons",
  70. subNet, model.getCurIteration());
  71. result += Math.abs(production+consumption);
  72. }
  73. return result;
  74. }
  75. /**
  76. * Calculate a fitnessvalue concerned with the performance of individual holons of the network
  77. * @return
  78. */
  79. private double holonFitness() {
  80. double result = 0.0;
  81. //Currently not in use
  82. return result;
  83. }
  84. /**
  85. * Calculates a fitnessvalue for the individual holon objects in the holons
  86. * @return
  87. */
  88. private double holonObjectFitness() {
  89. double result = 0.0;
  90. ArrayList<AbstractCpsObject> objList = model.getObjectsOnCanvas();
  91. for (AbstractCpsObject obj : objList) {
  92. if (obj instanceof HolonObject) {
  93. //There is no supply percentage for powerplants
  94. if(!(obj.getName().contains("Plant"))) {
  95. float suppPercentage = ((HolonObject) obj).getSuppliedPercentage();
  96. //Holon Object supply state based penalty
  97. result += holonObjectSupplyPenaltyFunction(suppPercentage);
  98. //result += holonObjectStatePenalty((HolonObject)obj);
  99. }
  100. //Deactivated Holon Element penalty.
  101. result += inactiveHolonElementPenalty((HolonObject)obj);
  102. }
  103. }
  104. return result;
  105. }
  106. /**
  107. * Calculates a penalty value based on the HOs current supply percentage
  108. * @param supplyPercentage
  109. * @return
  110. */
  111. private double holonObjectSupplyPenaltyFunction(float supplyPercentage) {
  112. float result = 0;
  113. if(supplyPercentage == 1)
  114. return result;
  115. else if(supplyPercentage < 1 && supplyPercentage >= 0.25) // undersupplied inbetween 25% and 100%
  116. result = (float) Math.pow(1/supplyPercentage, 2);
  117. else if (supplyPercentage < 0.25) //undersupplied with less than 25%
  118. result = (float) Math.pow(1/supplyPercentage,2);
  119. else if (supplyPercentage < 1.25) //Oversupplied less than 25%
  120. result = (float) Math.pow(supplyPercentage,3) ;
  121. else result = (float) Math.pow(supplyPercentage,4); //Oversupplied more than 25%
  122. if(Float.isInfinite(result) || Float.isNaN(result))
  123. result = 1000;
  124. return result;
  125. }
  126. /**
  127. * Function that returns the fitness depending on the number of elements deactivated in a single holon object
  128. * @param obj Holon Object that contains Holon Elements
  129. * @return fitness value for that object depending on the number of deactivated holon elements
  130. */
  131. private double inactiveHolonElementPenalty(HolonObject obj) {
  132. float result = 0;
  133. int activeElements = obj.getNumberOfActiveElements();
  134. int maxElements = obj.getElements().size();
  135. if(activeElements == maxElements)
  136. result =0;
  137. else result = (float) Math.pow((maxElements -activeElements),2)*100;
  138. return result;
  139. }
  140. /**
  141. * Pentalty Function that is based on the different states an object can have.
  142. * @param obj The holon object that is used for the assessment
  143. * @return pentalty value
  144. */
  145. private double holonObjectStatePenalty(HolonObject obj) {
  146. float result = 0;
  147. //TODO currently no penalties on undesired states, since the state assignment is broken...
  148. int state = obj.getState();
  149. switch (state) {
  150. case 0: result = 0;
  151. break;
  152. case 1: result = 0;
  153. break;
  154. case 2: result = 0;
  155. break;
  156. case 3: result = 0;
  157. break;
  158. case 4: result = 0;
  159. break;
  160. case 5: result = 0;
  161. break;
  162. default:
  163. break;
  164. }
  165. return result;
  166. }
  167. }