TopologieObjectiveFunction.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package algorithm.objectiveFunction;
  2. import ui.model.DecoratedHolonObject;
  3. import ui.model.DecoratedNetwork;
  4. import ui.model.DecoratedState;
  5. import ui.model.DecoratedSwitch;
  6. import java.util.Locale;
  7. import algorithm.objectiveFunction.GraphMetrics.Graph;
  8. public class TopologieObjectiveFunction {
  9. //Parameters
  10. //weight for f_g(H)
  11. static double w_eb = .5, w_holon=.1, w_selection = .1, w_grid = .4;
  12. //--> f_eb parameter
  13. /**
  14. * Maximum Energie Difference(kappa)
  15. */
  16. static double k_eb = 100000.f;
  17. //--> f_holon parameter
  18. /**
  19. * maximum penalty from holon flexibilities
  20. */
  21. static double k_holon= 200000;
  22. //--> f_selection paramaeter;
  23. /**
  24. * average Maximum Cost for selction(kappa) of switch and elements.
  25. */
  26. static double k_selection = 1000;
  27. static double cost_switch = 10;
  28. static double cost_element = 30;
  29. //--> f_grid parameter
  30. /**
  31. * The avergae shortest path maximum length -> kappa for the squash function
  32. */
  33. static double k_avg_shortest_path = 400;
  34. //pre-calculated parameters for partial function terms:
  35. /**
  36. * Pre calculated for the squash function
  37. * <br>
  38. * {@link TopologieObjectiveFunction#squash}
  39. */
  40. static double squash_subtract = 1.0f / (1.f + (float) Math.exp(5.0));
  41. static {
  42. //init
  43. checkParameter();
  44. }
  45. /**
  46. * Check parameter Setting and print error when wrong values are put in.
  47. * Here should all invariants be placed to be checked on initialization.
  48. */
  49. private static void checkParameter() {
  50. if(!(Math.abs(w_eb + w_holon + w_selection + w_selection + w_grid - 1) < 0.001)) {
  51. System.err.println("ParameterError in ObjectiveFunction: Sum of all weights should be 1");
  52. }
  53. }
  54. /**
  55. * ObjectifeFunction by Carlos.
  56. * Function computes f_g:
  57. * f_g = w1 * squash(f_eb, k1) + w2 * squash(f_state, k2) + w3 * squash(f_pro, k3) + w4 * squash(f_perf, k4) + w5 * squash(f_holon, k5)
  58. *
  59. *
  60. * squash is the squashing function {@link TopologieObjectiveFunction#squash}
  61. *
  62. *
  63. * @param state
  64. * @return f_g value between 0 and 100
  65. */
  66. static public float getFitnessValueForState(DecoratedState state) {
  67. //Calculate f_eb the penalty for unbalenced energy in the network
  68. double f_eb = 0;
  69. for(DecoratedNetwork net : state.getNetworkList()) {
  70. double netEnergyDifference = 0;
  71. netEnergyDifference += net.getConsumerList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
  72. netEnergyDifference += net.getConsumerSelfSuppliedList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
  73. netEnergyDifference += net.getSupplierList().stream().map(sup -> sup.getEnergyProducing() - sup.getEnergySelfConsuming()).reduce(0.f, Float::sum);
  74. //abs
  75. f_eb += Math.abs(netEnergyDifference);
  76. }
  77. //calculate f_holon
  78. double f_holon = 0;
  79. for(DecoratedNetwork net : state.getNetworkList()) {
  80. double f_elements_diviation_production = net.getDiviationInProductionInNetworkForHolonObjects();
  81. double f_elements_diviation_consumption = net.getDiviationInProductionInNetworkForHolonObjects();
  82. double f_flexibility_diviation_consumption = net.getDiviationInFlexibilityConsumption();
  83. double f_flexibility_diviation_production = net.getDiviationInFlexibilityProduction();
  84. double f_element = f_elements_diviation_production +f_elements_diviation_consumption;
  85. double f_flexibility = f_flexibility_diviation_consumption +f_flexibility_diviation_production;
  86. f_holon += f_element + f_flexibility ;
  87. }
  88. //calculating f_selection
  89. double f_selection = 0;
  90. int amountOfElemetsInWildcard = 0;
  91. for(DecoratedNetwork net : state.getNetworkList()) {
  92. for(DecoratedHolonObject dHobject : net.getConsumerList()) {
  93. if(dHobject.getModel().getObjName().equals("Wildcard")){
  94. amountOfElemetsInWildcard += dHobject.getModel().getNumberOfElements();
  95. }
  96. }
  97. for(DecoratedHolonObject dHobject : net.getConsumerList()) {
  98. if(dHobject.getModel().getObjName().equals("Wildcard")){
  99. amountOfElemetsInWildcard += dHobject.getModel().getNumberOfElements();
  100. }
  101. }
  102. for(DecoratedHolonObject dHobject : net.getSupplierList()) {
  103. if(dHobject.getModel().getObjName().equals("Wildcard")){
  104. amountOfElemetsInWildcard += dHobject.getModel().getNumberOfElements();
  105. }
  106. }
  107. }
  108. f_selection += cost_switch * amountOfElemetsInWildcard;
  109. int amountOfAddedSwitch = 0;
  110. for(DecoratedSwitch sw : state.getDecoratedSwitches()) {
  111. if(sw.getModel().getObjName().equals("AddedSwitch")) {
  112. amountOfAddedSwitch++;
  113. }
  114. }
  115. f_selection += cost_switch * amountOfAddedSwitch;
  116. //calculating f_grid
  117. double f_grid = 0;
  118. //each network is a holon
  119. for(DecoratedNetwork net: state.getNetworkList()) {
  120. Graph G = GraphMetrics.convertDecoratedNetworkToGraph(net);
  121. //We have to penalize single Networks;
  122. if(G.V.length <= 1) {
  123. f_grid += 100;
  124. continue;
  125. }
  126. double avgShortestPath = GraphMetrics.averageShortestDistance(G.V, G.E);
  127. double squached = squash(avgShortestPath, k_avg_shortest_path);
  128. //System.out.println("AVG:" + avgShortestPath + " squached:" + squached);
  129. //k-edge-conneted
  130. int maximumK = G.V.length - 1;
  131. if(maximumK != 0) {
  132. int k = GraphMetrics.minimumCut(G.V, G.E);
  133. double proportion = k/(double)maximumK;
  134. //System.out.println("proportion ("+ k+"/"+ maximumK+")=" + proportion);
  135. squached = 0.5 * squash(1.0-proportion, 1) + 0.5 *squached;
  136. }
  137. f_grid += squached;
  138. }
  139. if(!state.getNetworkList().isEmpty()) {
  140. f_grid /= state.getNetworkList().size();
  141. }
  142. //System.out.println("f_grid:" + f_grid);
  143. // System.out.print(" f_eb(" + w_eb * squash(f_eb, k_eb) + ") ");
  144. // System.out.print(" f_holon(" + w_holon * squash(f_holon, k_holon) + ") ");
  145. // System.out.print(" f_selection(" + w_selection * squash(f_selection, k_selection) + ") ");
  146. // System.out.println(" f_grid(" + w_grid * f_grid + ") ");
  147. /**
  148. * F_grid is already squashed
  149. */
  150. return (float) (w_eb * squash(f_eb, k_eb)
  151. + w_holon * squash(f_holon, k_holon)
  152. + w_selection * squash(f_selection, k_selection)
  153. + w_grid * f_grid);
  154. }
  155. private static String doubleToString(double value) {
  156. return String.format (Locale.US, "%.2f", value);
  157. }
  158. /**
  159. * The squashing function in paper
  160. * @param x the input
  161. * @param kappa the corresponding kappa
  162. * @return
  163. */
  164. static public double squash(double x, double kappa) {
  165. return 100.f/(1.0f + Math.exp(-(10.f * (x - kappa/2.f))/ kappa)) - squash_subtract;
  166. }
  167. /**
  168. * f_sup in paper
  169. * @param supply from 0 to 1
  170. * @return
  171. */
  172. static public double supplyPenalty(double supply) {
  173. double supplyPercentage = 100 * supply;
  174. // double test = (supplyPercentage < 100) ? -0.5 * supplyPercentage + 50: supplyPercentage - 100;
  175. return (supplyPercentage < 100) ? -0.5 * supplyPercentage + 50: supplyPercentage - 100;
  176. }
  177. }