SGFunctions.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. //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
  68. //NOte: Might be a little too strong
  69. float production = control.getSimManager().calculateEnergyWithoutFlexDevices("prod",
  70. subNet, model.getCurIteration());
  71. float consumption = control.getSimManager().calculateEnergyWithoutFlexDevices("cons",
  72. subNet, model.getCurIteration());
  73. result += Math.abs(production+consumption);
  74. }
  75. return result;
  76. }
  77. /**
  78. * Calculate a fitnessvalue concerned with the performance of individual holons of the network
  79. * @return
  80. */
  81. private double holonFitness() {
  82. double result = 0.0;
  83. //Currently not in use
  84. return result;
  85. }
  86. /**
  87. * Calculates a fitnessvalue for the individual holon objects in the holons
  88. * @return
  89. */
  90. private double holonObjectFitness() {
  91. double result = 0.0;
  92. ArrayList<AbstractCpsObject> objList = model.getObjectsOnCanvas();
  93. for (AbstractCpsObject obj : objList) {
  94. if (obj instanceof HolonObject) {
  95. //There is no supply percentage for powerplants
  96. if(!(obj.getName().contains("Plant"))) {
  97. float suppPercentage = ((HolonObject) obj).getSuppliedPercentage();
  98. //Holon Object supply state based penalty
  99. result += holonObjectSupplyPenaltyFunction(suppPercentage);
  100. //result += holonObjectStatePenalty((HolonObject)obj);
  101. }
  102. //Deactivated Holon Element penalty.
  103. result += inactiveHolonElementPenalty((HolonObject)obj);
  104. }
  105. }
  106. return result;
  107. }
  108. /**
  109. * Calculates a penalty value based on the HOs current supply percentage
  110. * @param supplyPercentage
  111. * @return
  112. */
  113. private double holonObjectSupplyPenaltyFunction(float supplyPercentage) {
  114. float result = 0;
  115. if(supplyPercentage == 1)
  116. return result;
  117. else if(supplyPercentage < 1 && supplyPercentage >= 0.25) // undersupplied inbetween 25% and 100%
  118. result = (float) Math.pow(1/supplyPercentage, 2);
  119. else if (supplyPercentage < 0.25) //undersupplied with less than 25%
  120. result = (float) Math.pow(1/supplyPercentage,2);
  121. else if (supplyPercentage < 1.25) //Oversupplied less than 25%
  122. result = (float) Math.pow(supplyPercentage,3) ;
  123. else result = (float) Math.pow(supplyPercentage,4); //Oversupplied more than 25%
  124. if(Float.isInfinite(result) || Float.isNaN(result))
  125. result = 1000;
  126. return result;
  127. }
  128. /**
  129. * Function that returns the fitness depending on the number of elements deactivated in a single holon object
  130. * @param obj Holon Object that contains Holon Elements
  131. * @return fitness value for that object depending on the number of deactivated holon elements
  132. */
  133. private double inactiveHolonElementPenalty(HolonObject obj) {
  134. float result = 0;
  135. int activeElements = obj.getNumberOfActiveElements();
  136. int maxElements = obj.getElements().size();
  137. if(activeElements == maxElements)
  138. result =0;
  139. else result = (float) Math.pow((maxElements -activeElements),2)*100;
  140. return result;
  141. }
  142. /**
  143. * Pentalty Function that is based on the different states an object can have.
  144. * @param obj The holon object that is used for the assessment
  145. * @return pentalty value
  146. */
  147. private double holonObjectStatePenalty(HolonObject obj) {
  148. float result = 0;
  149. //TODO currently no penalties on undesired states, since the state assignment is broken...
  150. int state = obj.getState();
  151. switch (state) {
  152. case 0: result = 0;
  153. break;
  154. case 1: result = 0;
  155. break;
  156. case 2: result = 0;
  157. break;
  158. case 3: result = 0;
  159. break;
  160. case 4: result = 0;
  161. break;
  162. case 5: result = 0;
  163. break;
  164. default:
  165. break;
  166. }
  167. return result;
  168. }
  169. }