TopologieObjectiveFunction.java 7.8 KB

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