SGFunctions.java 7.5 KB

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