TopologieObjectiveFunction.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. //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. <<<<<<< HEAD
  113. for(DecoratedHolonObject dHobject : net.getConsumerSelfSuppliedList()) {
  114. =======
  115. for(DecoratedHolonObject dHobject : net.getConsumerList()) {
  116. >>>>>>> refs/remotes/origin/updateInProgress
  117. if(dHobject.getModel().getName().contains("Wildcard")){
  118. if(dHobject.getModel().getName().length() > 9) {
  119. String costString = dHobject.getModel().getName().substring(9);
  120. cost += Double.parseDouble(costString);
  121. }
  122. }
  123. }
  124. for(DecoratedHolonObject dHobject : net.getSupplierList()) {
  125. if(dHobject.getModel().getName().contains("Wildcard")){
  126. if(dHobject.getModel().getName().length() > 9) {
  127. String costString = dHobject.getModel().getName().substring(9);
  128. cost += Double.parseDouble(costString);
  129. }
  130. }
  131. }
  132. }
  133. f_selection += cost;
  134. f_selection += cost_switch * amountOfAddedSwitch;
  135. f_selection += cost_of_cable_per_meter * addedCableMeters;
  136. if(moreInformation)System.out.println("CostForWildcards:" + cost + ", CostSwitches(#" + amountOfAddedSwitch +"):" + cost_switch * amountOfAddedSwitch + ", CostCables(" +addedCableMeters+ "m):" + cost_of_cable_per_meter * addedCableMeters);
  137. //calculating f_grid
  138. double f_grid = 0;
  139. //each network is a holon
  140. for(DecoratedNetwork net: state.getNetworkList()) {
  141. Graph G = GraphMetrics.convertDecoratedNetworkToGraph(net);
  142. //We have to penalize single Networks;
  143. if(G.V.length <= 1) {
  144. f_grid += 100;
  145. continue;
  146. }
  147. double avgShortestPath = GraphMetrics.averageShortestDistance(G.V, G.E);
  148. double squached = squash(avgShortestPath, k_avg_shortest_path);
  149. //System.out.println("AVG:" + avgShortestPath + " squached:" + squached);
  150. //k-edge-conneted
  151. int maximumK = G.V.length - 1;
  152. if(maximumK != 0) {
  153. int k = GraphMetrics.minimumCut(G.V, G.E);
  154. double proportion = k/(double)maximumK;
  155. //System.out.println("proportion ("+ k+"/"+ maximumK+")=" + proportion);
  156. squached = 0.5 * squash(1.0-proportion, 1) + 0.5 *squached;
  157. }
  158. f_grid += squached;
  159. }
  160. if(!state.getNetworkList().isEmpty()) {
  161. f_grid /= state.getNetworkList().size();
  162. }
  163. //System.out.println("f_grid:" + f_grid);
  164. // System.out.print(" f_eb(" + w_eb * squash(f_eb, k_eb) + ") ");
  165. // System.out.print(" f_holon(" + w_holon * squash(f_holon, k_holon) + ") ");
  166. // System.out.print(" f_selection(" + w_selection * squash(f_selection, k_selection) + ") ");
  167. // System.out.println(" f_grid(" + w_grid * f_grid + ") ");
  168. /**
  169. * F_grid is already squashed
  170. */
  171. return (float) (w_eb * squash(f_eb, k_eb)
  172. <<<<<<< HEAD
  173. + w_max * squash(f_maximum, k_max)
  174. =======
  175. + w_max * squash(f_holon, k_max)
  176. >>>>>>> refs/remotes/origin/updateInProgress
  177. + w_holon * squash(f_holon, k_holon)
  178. + w_selection * squash(f_selection, k_selection)
  179. + w_grid * f_grid);
  180. }
  181. private static String doubleToString(double value) {
  182. return String.format (Locale.US, "%.2f", value);
  183. }
  184. /**
  185. * The squashing function in paper
  186. * @param x the input
  187. * @param kappa the corresponding kappa
  188. * @return
  189. */
  190. static public double squash(double x, double kappa) {
  191. return 100.f/(1.0f + Math.exp(-(10.f * (x - kappa/2.f))/ kappa)) - squash_subtract;
  192. }
  193. /**
  194. * f_sup in paper
  195. * @param supply from 0 to 1
  196. * @return
  197. */
  198. static public double supplyPenalty(double supply) {
  199. double supplyPercentage = 100 * supply;
  200. // double test = (supplyPercentage < 100) ? -0.5 * supplyPercentage + 50: supplyPercentage - 100;
  201. return (supplyPercentage < 100) ? -0.5 * supplyPercentage + 50: supplyPercentage - 100;
  202. }
  203. }