SGFunctions.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. public double calcValuewithSliderState() {
  39. implementState();
  40. HelpFunctions.calculateStateForTimeStepPSO(model, control);
  41. double value = 0.0;
  42. //int nrOfSubnets = control.getSimManager().getSubNets().size();
  43. //System.out.println("nr of subnets: " + nrOfSubnets);
  44. value += networkFitness();
  45. value += holonObjectFitness();
  46. /*for (AbstractCpsObject obj : model.getObjectsOnCanvas()) {
  47. if (obj instanceof HolonObject) {
  48. int state = ((HolonObject) obj).getState();
  49. double suppliedPerc = ((HolonObject) obj).getSuppliedPercentage();
  50. double elementsActive = (double) elementsActive((HolonObject) obj);
  51. double totalElements = (double) ((HolonObject) obj).getElements().size();
  52. double difference = Math.abs(1.0 - suppliedPerc) * 100;
  53. double differenceEle = (1.0 - (elementsActive / totalElements)) * 100;
  54. // Supply States (see HolonObject class for more informations)
  55. switch (state) {
  56. case 0:
  57. value += (100 * 8);
  58. break;
  59. case 1:
  60. value += difference * 4 + differenceEle;
  61. break;
  62. case 2:
  63. value += difference + differenceEle;
  64. break;
  65. case 4:
  66. value += difference + differenceEle;
  67. break;
  68. case 5:
  69. value += difference * 2 + differenceEle;
  70. break;
  71. default:
  72. break;
  73. }
  74. }
  75. }*/
  76. return value;
  77. }
  78. /**
  79. * Calculates a fitness value for the overall abstract network
  80. * @return
  81. */
  82. private double networkFitness() {
  83. double result = 0.0;
  84. ArrayList<SubNet> tmp_sn = control.getSimManager().getSubNets();
  85. int totalNrOfObjects = model.getObjectsOnCanvas().size();
  86. System.out.println("Total number of objects is: " + totalNrOfObjects);
  87. for (SubNet subNet : tmp_sn) {
  88. System.out.println("Subnetsize: " + (subNet.getObjects().size()));
  89. }
  90. result = (tmp_sn.size() - 1) * 300;
  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. return result;
  100. }
  101. /**
  102. * Calculates a fitnessvalue for the individual holon objects in the holons
  103. * @return
  104. */
  105. private double holonObjectFitness() {
  106. double result = 0.0;
  107. ArrayList<AbstractCpsObject> objList = model.getObjectsOnCanvas();
  108. System.out.println("total consumption works: " + control.getTotalConsumption(objList));
  109. //TODO Annoying as fuck... think think
  110. for (AbstractCpsObject obj : objList) {
  111. if (obj instanceof HolonObject) {
  112. float supply = ((HolonObject) obj).getCurrentSupply();
  113. float energy =((HolonObject) obj).getMaxActiveEnergy();
  114. float suppPercentage = ((HolonObject) obj).getSuppliedPercentage();
  115. float ownProduction = ((HolonObject) obj).getSelfMadeEnergy(model.getCurIteration());
  116. float maxEnergy = ((HolonObject) obj).getMaxPossibleConsumption();
  117. System.out.println(obj.getName());
  118. if(!(obj.getName().contains("Plant"))) {
  119. result += holonObjectSupplyPenaltyFunction(suppPercentage);
  120. result += holonObjectStatePenalty((HolonObject)obj);
  121. }
  122. result += inactiveHolonElementPenalty((HolonObject)obj);
  123. System.out.println("min ele" + ((HolonObject) obj).getMinimalConsumingElement().toString());
  124. //if(((HolonObject) obj).getCurrentEnergyAtTimeStep(model.getCurIteration())/maxEnergy <;
  125. System.out.println("Own production: " + ownProduction + "Provided Supply and energy: " + supply + " and " + energy + " with total possible energy: " + maxEnergy
  126. + " is supply percentage: " + suppPercentage);
  127. }
  128. }
  129. return result;
  130. }
  131. /**
  132. * Calculates a penalty value based on the HOs current supply percentage
  133. * @param supplyPercentage
  134. * @return
  135. */
  136. private double holonObjectSupplyPenaltyFunction(float supplyPercentage) {
  137. float result = 0;
  138. if(supplyPercentage == 1)
  139. return result;
  140. else if(supplyPercentage < 1 && supplyPercentage >= 0.25)
  141. result = (float) (1/supplyPercentage *1.5);
  142. else if (supplyPercentage < 0.25)
  143. result = 1/supplyPercentage * 2; //Scalar value might be problematic for large/specific examples
  144. else if (supplyPercentage < 1.25)
  145. result = 1 * supplyPercentage *2 ;
  146. else result = 1 * supplyPercentage * 3;
  147. if(Float.isInfinite(result) || Float.isNaN(result))
  148. result = 100;
  149. System.out.println("Low penalty function value: " + result + " with supply value: " + supplyPercentage);
  150. return result;
  151. }
  152. private double inactiveHolonElementPenalty(HolonObject obj) {
  153. float result = 0;
  154. int activeElements = obj.getNumberOfActiveElements();
  155. int maxElements = obj.getElements().size();
  156. if(activeElements == maxElements)
  157. result =0;
  158. else if(activeElements == 0) {
  159. result = 100;
  160. }else result = ((maxElements -activeElements)*200);
  161. return result;
  162. }
  163. private double holonObjectStatePenalty(HolonObject obj) {
  164. float result = 0;
  165. int state = obj.getState();
  166. switch (state) {
  167. case 0: result = 0;
  168. break;
  169. case 1: result = 10;
  170. break;
  171. case 2: result = 0;
  172. break;
  173. case 3: result = 0;
  174. break;
  175. case 4: result = 5;
  176. break;
  177. case 5: result = 5;
  178. break;
  179. default:
  180. break;
  181. }
  182. return result;
  183. }
  184. }