TopologieObjectiveFunction.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 = .3, w_max = 0.2, w_holon=.1, 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 = 10;
  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. // Disjoint Path Parameter
  41. //Function1 Decreasing
  42. static double seperate_X_Value = 3.0;
  43. // Value between 0 and 100
  44. static double seperate_Y_Value = 10;
  45. //Stretching the e-function
  46. static double lowPenaltyGrowth = 3.0;
  47. //pre-calculated parameters for partial function terms:
  48. /**
  49. * Pre calculated for the squash function
  50. * <br>
  51. * {@link TopologieObjectiveFunction#squash}
  52. */
  53. static double squash_subtract = 1.0f / (1.f + (float) Math.exp(5.0));
  54. static {
  55. //init
  56. checkParameter();
  57. }
  58. /**
  59. * Check parameter Setting and print error when wrong values are put in.
  60. * Here should all invariants be placed to be checked on initialization.
  61. */
  62. private static void checkParameter() {
  63. if(!(Math.abs(w_eb + w_holon + w_selection + w_grid + w_max - 1) < 0.001)) {
  64. System.err.println("ParameterError in ObjectiveFunction: Sum of all weights should be 1");
  65. }
  66. }
  67. /**
  68. * ObjectifeFunction by Carlos.
  69. * Function computes f_g:
  70. * 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)
  71. *
  72. *
  73. * squash is the squashing function {@link TopologieObjectiveFunction#squash}
  74. *
  75. *
  76. * @param state
  77. * @param moreInformation TODO
  78. * @return f_g value between 0 and 100
  79. */
  80. static public float getFitnessValueForState(DecoratedState state, int amountOfAddedSwitch, double addedCableMeters, boolean moreInformation) {
  81. //Calculate f_eb the penalty for unbalenced energy in the network
  82. double f_eb = 0;
  83. for(DecoratedNetwork net : state.getNetworkList()) {
  84. double netEnergyDifference = 0;
  85. netEnergyDifference += net.getConsumerList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
  86. netEnergyDifference += net.getConsumerSelfSuppliedList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
  87. netEnergyDifference += net.getSupplierList().stream().map(sup -> sup.getEnergyProducing() - sup.getEnergySelfConsuming()).reduce(0.f, Float::sum);
  88. //abs
  89. f_eb += Math.abs(netEnergyDifference);
  90. }
  91. double f_maximum = 0;
  92. for(DecoratedNetwork net : state.getNetworkList()) {
  93. final int timestep = state.getTimestepOfState();
  94. f_maximum += Math.abs(net.getConsumerList().stream().map(con -> con.getModel().getMaximumConsumptionPossible(timestep) - con.getModel().getMaximumProductionPossible(timestep)).reduce(0.f, Float::sum));
  95. f_maximum += Math.abs(net.getConsumerSelfSuppliedList().stream().map(con -> con.getModel().getMaximumConsumptionPossible(timestep) - con.getModel().getMaximumProductionPossible(timestep)).reduce(0.f, Float::sum));
  96. f_maximum += Math.abs(net.getSupplierList().stream().map(con -> con.getModel().getMaximumConsumptionPossible(timestep) - con.getModel().getMaximumProductionPossible(timestep)).reduce(0.f, Float::sum));
  97. }
  98. //calculate f_holon
  99. double f_holon = 0;
  100. for(DecoratedNetwork net : state.getNetworkList()) {
  101. double f_elements_diviation_production = net.getDiviationInProductionInNetworkForHolonObjects();
  102. double f_elements_diviation_consumption = net.getDiviationInProductionInNetworkForHolonObjects();
  103. double f_element = f_elements_diviation_production+f_elements_diviation_consumption;
  104. f_holon += f_element;
  105. }
  106. //calculating f_selection
  107. double f_selection = 0;
  108. double cost = 0;
  109. int amountOfElemetsInWildcard = 0;
  110. for(DecoratedNetwork net : state.getNetworkList()) {
  111. for(DecoratedHolonObject dHobject : net.getConsumerList()) {
  112. if(dHobject.getModel().getName().contains("Wildcard")){
  113. if(dHobject.getModel().getName().length() > 9) {
  114. String costString = dHobject.getModel().getName().substring(9);
  115. cost += Double.parseDouble(costString);
  116. }
  117. }
  118. }
  119. for(DecoratedHolonObject dHobject : net.getConsumerSelfSuppliedList()) {
  120. if(dHobject.getModel().getName().contains("Wildcard")){
  121. if(dHobject.getModel().getName().length() > 9) {
  122. String costString = dHobject.getModel().getName().substring(9);
  123. cost += Double.parseDouble(costString);
  124. }
  125. }
  126. }
  127. for(DecoratedHolonObject dHobject : net.getSupplierList()) {
  128. if(dHobject.getModel().getName().contains("Wildcard")){
  129. if(dHobject.getModel().getName().length() > 9) {
  130. String costString = dHobject.getModel().getName().substring(9);
  131. cost += Double.parseDouble(costString);
  132. }
  133. }
  134. }
  135. }
  136. f_selection += cost;
  137. f_selection += cost_switch * amountOfAddedSwitch;
  138. f_selection += cost_of_cable_per_meter * addedCableMeters;
  139. if(moreInformation)System.out.println("CostForWildcards:" + cost + ", CostSwitches(#" + amountOfAddedSwitch +"):" + cost_switch * amountOfAddedSwitch + ", CostCables(" +addedCableMeters+ "m):" + cost_of_cable_per_meter * addedCableMeters);
  140. //calculating f_grid
  141. double f_grid = 0;
  142. //each network is a holon
  143. for(DecoratedNetwork net: state.getNetworkList()) {
  144. Graph G = GraphMetrics.convertDecoratedNetworkToGraph(net);
  145. //We have to penalize single Networks;
  146. //100 is the maximum penalty for a holon/network
  147. if(G.V.length <= 1) {
  148. f_grid += 100;
  149. continue;
  150. }
  151. double avgShortestPath = GraphMetrics.averageShortestDistance(G.V, G.E);
  152. //k-edge-conneted
  153. //int maximumK = G.V.length - 1;
  154. int k = GraphMetrics.minimumCut(G.V, G.E);
  155. double penalty = disjoinPathPenalty(k);
  156. f_grid += 0.5 * squash(penalty, 100) + 0.5 *squash(avgShortestPath, k_avg_shortest_path);
  157. }
  158. //Average over all networks
  159. if(!state.getNetworkList().isEmpty()) {
  160. f_grid /= state.getNetworkList().size();
  161. }
  162. // System.out.println("f_grid:" + f_grid);
  163. // System.out.print(" f_eb(" + w_eb * squash(f_eb, k_eb) + ") ");
  164. // System.out.print(" f_holon(" + w_holon * squash(f_holon, k_holon) + ") ");
  165. // System.out.print(" f_selection(" + w_selection * squash(f_selection, k_selection) + ") ");
  166. // System.out.println(" f_grid(" + w_grid * f_grid + ") ");
  167. /**
  168. * F_grid is already squashed
  169. */
  170. return (float) (w_eb * squash(f_eb, k_eb)
  171. + w_max * squash(f_maximum, k_max)
  172. + w_holon * squash(f_holon, k_holon)
  173. + w_selection * squash(f_selection, k_selection)
  174. + w_grid * f_grid);
  175. }
  176. private static String doubleToString(double value) {
  177. return String.format (Locale.US, "%.2f", value);
  178. }
  179. private static double disjoinPathPenalty(double value) {
  180. //von 100 auf 10% bis seperateFunctionValue linear
  181. //Big Penalty
  182. if( value < seperate_X_Value) {
  183. return 100 - ((100 - seperate_Y_Value) / seperate_X_Value) * value;
  184. }
  185. //Low Penalty
  186. else {
  187. return seperate_Y_Value * Math.exp(lowPenaltyGrowth * (-value + seperate_X_Value));
  188. }
  189. }
  190. /**
  191. * The squashing function in paper
  192. * @param x the input
  193. * @param kappa the corresponding kappa
  194. * @return
  195. */
  196. static public double squash(double x, double kappa) {
  197. return 100.f/(1.0f + Math.exp(-(10.f * (x - kappa/2.f))/ kappa)) - squash_subtract;
  198. }
  199. /**
  200. * f_sup in paper
  201. * @param supply from 0 to 1
  202. * @return
  203. */
  204. static public double supplyPenalty(double supply) {
  205. double supplyPercentage = 100 * supply;
  206. return (supplyPercentage < 100) ? -0.5 * supplyPercentage + 50: supplyPercentage - 100;
  207. }
  208. }