TopologieObjectiveFunction.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 = 0.3, w_max = 0.2, w_holon= 0.1, w_selection = .3, w_grid = 0.1;
  14. //--> f_eb parameter
  15. /**
  16. * Maximum Energie Difference(kappa)
  17. */
  18. static double k_eb = 400000.f;
  19. /**
  20. * Maximum when all on Energie Difference(kappa)
  21. */
  22. static double k_max = 450000.f;
  23. //--> f_holon parameter
  24. /**
  25. * maximum penalty from holon element distribution
  26. */
  27. static double k_holon= 100000;
  28. //--> f_selection paramaeter;
  29. /**
  30. * average Maximum Cost for selction(kappa) of switch and elements.
  31. */
  32. static double k_selection = 50000;
  33. static double cost_switch = 20;
  34. private static double cost_of_cable_per_meter = 0.8;
  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 = 1600;
  40. //Disjpijoint path cant have zero as output it starts with the value 1
  41. static double centerValue_disjoint_path = 1.0;
  42. static double k_disjoint_path = 2.4;
  43. static double lambda_avg_shortest_path = 10;
  44. static double lambda_disjoint_path = 10;
  45. static double k_grid = lambda_avg_shortest_path;// + lambda_disjoint_path;
  46. //pre-calculated parameters for partial function terms:
  47. /**
  48. * Pre calculated for the squash function
  49. * <br>
  50. * {@link TopologieObjectiveFunction#squash}
  51. */
  52. static double squash_subtract = 1.0f / (1.f + (float) Math.exp(5.0));
  53. static double range_for_k_avg_shortest_path = range(k_avg_shortest_path);
  54. static double range_for_k_disjoint_path = range(k_disjoint_path - centerValue_disjoint_path);
  55. static {
  56. //init
  57. checkParameter();
  58. }
  59. /**
  60. * Check parameter Setting and print error when wrong values are put in.
  61. * Here should all invariants be placed to be checked on initialization.
  62. */
  63. private static void checkParameter() {
  64. if(!(Math.abs(w_eb + w_holon + w_selection + w_grid + w_max - 1) < 0.001)) {
  65. System.err.println("ParameterError in ObjectiveFunction: Sum of all weights should be 1");
  66. }
  67. }
  68. /**
  69. * ObjectifeFunction by Carlos.
  70. * Function computes f_g:
  71. * 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)
  72. *
  73. *
  74. * squash is the squashing function {@link TopologieObjectiveFunction#squash}
  75. *
  76. *
  77. * @param state
  78. * @param moreInformation TODO
  79. * @return f_g value between 0 and 100
  80. */
  81. static public float getFitnessValueForState(DecoratedState state, int amountOfAddedSwitch, double addedCableMeters, boolean moreInformation) {
  82. //Calculate f_eb the penalty for unbalenced energy in the network
  83. double f_eb = 0;
  84. for(DecoratedNetwork net : state.getNetworkList()) {
  85. double netEnergyDifference = 0;
  86. netEnergyDifference += net.getConsumerList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
  87. netEnergyDifference += net.getConsumerSelfSuppliedList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
  88. netEnergyDifference += net.getSupplierList().stream().map(sup -> sup.getEnergyProducing() - sup.getEnergySelfConsuming()).reduce(0.f, Float::sum);
  89. //abs
  90. f_eb += Math.abs(netEnergyDifference);
  91. }
  92. double f_maximum = 0;
  93. final int timestep = state.getTimestepOfState();
  94. for(DecoratedNetwork net : state.getNetworkList()) {
  95. double maximumEnergy = 0;
  96. maximumEnergy += net.getConsumerList().stream().map(con -> con.getModel().getMaximumConsumptionPossible(timestep) - con.getModel().getMaximumProductionPossible(timestep)).reduce(0.f, Float::sum);
  97. maximumEnergy += net.getConsumerSelfSuppliedList().stream().map(con -> con.getModel().getMaximumConsumptionPossible(timestep) - con.getModel().getMaximumProductionPossible(timestep)).reduce(0.f, Float::sum);
  98. maximumEnergy += net.getSupplierList().stream().map(con -> con.getModel().getMaximumConsumptionPossible(timestep) - con.getModel().getMaximumProductionPossible(timestep)).reduce(0.f, Float::sum);
  99. f_maximum += Math.abs(maximumEnergy);
  100. }
  101. //calculate f_holon
  102. double f_holon = 0;
  103. for(DecoratedNetwork net : state.getNetworkList()) {
  104. double f_elements_deviation_production = net.getDeviationInProductionInNetworkForHolonObjects();
  105. double f_elements_deviation_consumption = net.getDeviationInConsumptionInNetworkForHolonObjects();
  106. double f_element = f_elements_deviation_production+f_elements_deviation_consumption;
  107. f_holon += f_element;
  108. }
  109. //calculating f_selection
  110. double f_selection = 0;
  111. double cost = 0;
  112. for(DecoratedNetwork net : state.getNetworkList()) {
  113. for(DecoratedHolonObject dHobject : net.getConsumerList()) {
  114. if(dHobject.getModel().getName().contains("Wildcard")){
  115. if(dHobject.getModel().getName().length() > 9) {
  116. String costString = dHobject.getModel().getName().substring(9);
  117. cost += Double.parseDouble(costString);
  118. }
  119. }
  120. }
  121. for(DecoratedHolonObject dHobject : net.getConsumerSelfSuppliedList()) {
  122. if(dHobject.getModel().getName().contains("Wildcard")){
  123. if(dHobject.getModel().getName().length() > 9) {
  124. String costString = dHobject.getModel().getName().substring(9);
  125. cost += Double.parseDouble(costString);
  126. }
  127. }
  128. }
  129. for(DecoratedHolonObject dHobject : net.getSupplierList()) {
  130. if(dHobject.getModel().getName().contains("Wildcard")){
  131. if(dHobject.getModel().getName().length() > 9) {
  132. String costString = dHobject.getModel().getName().substring(9);
  133. cost += Double.parseDouble(costString);
  134. }
  135. }
  136. }
  137. }
  138. f_selection += cost;
  139. f_selection += cost_switch * amountOfAddedSwitch;
  140. f_selection += cost_of_cable_per_meter * addedCableMeters;
  141. if(moreInformation)System.out.println("CostForWildcards:" + cost + ", CostSwitches(#" + amountOfAddedSwitch +"):" + cost_switch * amountOfAddedSwitch + ", CostCables(" +addedCableMeters+ "m):" + cost_of_cable_per_meter * addedCableMeters);
  142. //calculating f_grid
  143. double f_grid = 0;
  144. //each network is a holon
  145. for(DecoratedNetwork net: state.getNetworkList()) {
  146. Graph G = GraphMetrics.convertDecoratedNetworkToGraph(net);
  147. //We have to penalize single Networks;
  148. if(G.V.length <= 1 || G.S.length <= 1) {
  149. f_grid += lambda_avg_shortest_path;// + lambda_disjoint_path;
  150. continue;
  151. }
  152. double avgShortestPath = GraphMetrics.averageShortestDistance(G);
  153. //double disjpointPaths = GraphMetrics.averageEdgeDisjointPathProblem(G);
  154. f_grid += avgShortestPathPenalty(avgShortestPath);// + disjoinPathPenalty(disjpointPaths);
  155. }
  156. //take average to encourage splitting
  157. f_grid /= state.getNetworkList().size();
  158. //printUnsquashedValues(f_eb, f_maximum, f_holon, f_selection, f_grid);
  159. return (float) (w_eb * squash(f_eb, k_eb)
  160. + w_max * squash(f_maximum, k_max)
  161. + w_holon * squash(f_holon, k_holon)
  162. + w_selection * squash(f_selection, k_selection)
  163. + w_grid * squash(f_grid, k_grid));
  164. }
  165. private static String doubleToString(double value) {
  166. return String.format (Locale.US, "%.2f", value);
  167. }
  168. private static double disjoinPathPenalty(double value) {
  169. return -(2.0 * lambda_disjoint_path) / (1 + Math.exp(- (value - centerValue_disjoint_path)/ range_for_k_disjoint_path)) + (2.0 * lambda_disjoint_path);
  170. }
  171. private static double avgShortestPathPenalty(double value) {
  172. return (2.0 * lambda_avg_shortest_path) / (1 + Math.exp(- value/ range_for_k_avg_shortest_path)) - lambda_avg_shortest_path;
  173. }
  174. /**
  175. * Attention Math.log calcultae ln not log
  176. * @param kappa
  177. * @return
  178. */
  179. private static double range(double kappa) {
  180. return - kappa / Math.log(Math.pow(2.0, 0.05) - 1.0 );
  181. }
  182. /**
  183. * The squashing function in paper
  184. * @param x the input
  185. * @param kappa the corresponding kappa
  186. * @return
  187. */
  188. static public double squash(double x, double kappa) {
  189. return 100.f/(1.0f + Math.exp(-(10.f * (x - kappa/2.f))/ kappa)) - squash_subtract;
  190. }
  191. /**
  192. * f_sup in paper
  193. * @param supply from 0 to 1
  194. * @return
  195. */
  196. static public double supplyPenalty(double supply) {
  197. double supplyPercentage = 100 * supply;
  198. return (supplyPercentage < 100) ? -0.5 * supplyPercentage + 50: supplyPercentage - 100;
  199. }
  200. static void printDistribution(double f_eb, double f_maximum, double f_holon, double f_selection, double f_grid){
  201. System.out.print(" f_eb(" + w_eb * squash(f_eb, k_eb) + ") ");
  202. System.out.print(" f_maximum(" + w_max * squash(f_maximum, k_max) + ") ");
  203. System.out.print(" f_holon(" + w_holon * squash(f_holon, k_holon) + ") ");
  204. System.out.print(" f_selection(" + w_selection * squash(f_selection, k_selection) + ") ");
  205. System.out.println(" f_grid(" + w_grid * f_grid + ") ");
  206. }
  207. static void printUnsquashedValues(double f_eb, double f_maximum, double f_holon, double f_selection, double f_grid){
  208. System.out.print(" f_eb(" + f_eb + ") ");
  209. System.out.print(" f_maximum(" + f_maximum + ") ");
  210. System.out.print(" f_holon(" + f_holon + ") ");
  211. System.out.print(" f_selection(" + f_selection + ") ");
  212. System.out.println(" f_grid(" + f_grid + ") ");
  213. }
  214. }