TopologieObjectiveFunction.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package holeg.algorithm.objective_function;
  2. import java.util.Locale;
  3. import java.util.logging.Logger;
  4. import holeg.model.Model;
  5. import holeg.utility.math.decimal.Sampler;
  6. public class TopologieObjectiveFunction {
  7. private static final Logger log = Logger.getLogger(TopologieObjectiveFunction.class.getName());
  8. //Parameters
  9. //weight for f_g(H)
  10. static double w_eb = 0.2, w_max = 0.5, w_holon= 0.1, w_selection = .1, w_grid = 0.1;
  11. //--> f_eb parameter
  12. /**
  13. * Maximum Energie Difference(kappa)
  14. */
  15. static double k_eb = 5000.f;
  16. /**
  17. * Maximum when all on Energie Difference(kappa)
  18. */
  19. static double k_max = 10.f;
  20. static double lambda_max = 10.;
  21. //--> f_holon parameter
  22. /**
  23. * maximum penalty from holon element distribution
  24. */
  25. static double k_holon= 4000;
  26. //--> f_selection paramaeter;
  27. /**
  28. * average Maximum Cost for selction(kappa) of switch and elements.
  29. */
  30. static double k_selection = 200000;
  31. static double cost_switch = 3000;
  32. private static double cost_of_cable_per_meter = 6;
  33. //--> f_grid parameter
  34. /**
  35. * The avergae shortest path maximum length -> kappa for the squash function
  36. */
  37. static double k_avg_shortest_path = 1600;
  38. //Disjpijoint path cant have zero as output it starts with the value 1
  39. static double centerValue_disjoint_path = 1.0;
  40. static double k_disjoint_path = 2.4;
  41. static double lambda_avg_shortest_path = 10;
  42. static double lambda_disjoint_path = 10;
  43. static double k_grid = lambda_avg_shortest_path;// + lambda_disjoint_path;
  44. //pre-calculated parameters for partial function terms:
  45. /**
  46. * Pre calculated for the squash function
  47. * <br>
  48. * {@link TopologieObjectiveFunction#squash}
  49. */
  50. static double squash_subtract = 1.0f / (1.f + (float) Math.exp(5.0));
  51. static double range_for_k_avg_shortest_path = range(k_avg_shortest_path);
  52. static double range_for_k_disjoint_path = range(k_disjoint_path - centerValue_disjoint_path);
  53. public static Sampler averageLog = new Sampler();
  54. static boolean useLog = false;
  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 moreInformation if more prints should be made
  78. * @return f_g value between 0 and 100
  79. */
  80. //TODO(Tom2022-01-13) Fix TopologyFitnessFunction
  81. static public float getFitnessValueForState(Model model, int amountOfAddedSwitch, double addedCableMeters, boolean moreInformation) {
  82. //
  83. //
  84. //
  85. // //Calculate f_eb the penalty for unbalenced energy in the network
  86. // double f_eb = 0;
  87. // for(DecoratedNetwork net : state.getNetworkList()) {
  88. // //abs
  89. // f_eb += Math.abs(net.getTotalConsumption() - net.getTotalProduction());
  90. // }
  91. // //Average?
  92. // f_eb /= state.getNetworkList().size();
  93. //
  94. //
  95. //
  96. //
  97. // double f_maximum = 0;
  98. // for(DecoratedNetwork net : state.getNetworkList()) {
  99. // double prod = net.getTotalProduction();
  100. // double con = net.getTotalConsumption();
  101. // if(prod == 0 || con == 0) {
  102. // f_maximum += lambda_max;
  103. // }else {
  104. // f_maximum += lambda_max * (Math.abs(prod - con)/Math.max(prod, con));
  105. // }
  106. // }
  107. // //Average?
  108. // f_maximum /= state.getNetworkList().size();
  109. //
  110. // //calculate f_holon
  111. // double f_holon = 0;
  112. // for(DecoratedNetwork net : state.getNetworkList()) {
  113. // double f_elements_deviation_production = net.getDeviationInProductionInNetworkForHolonObjects();
  114. // double f_elements_deviation_consumption = net.getDeviationInConsumptionInNetworkForHolonObjects();
  115. // double f_element = f_elements_deviation_production+f_elements_deviation_consumption;
  116. // f_holon += f_element;
  117. // }
  118. // f_holon /= state.getNetworkList().size();
  119. //
  120. // //calculating f_selection
  121. // double f_selection = calculateTopologieCost(state, amountOfAddedSwitch, addedCableMeters);
  122. // //if(moreInformation)LOGGER.info("CostForWildcards:" + cost + ", CostSwitches(#" + amountOfAddedSwitch +"):" + cost_switch * amountOfAddedSwitch + ", CostCables(" +addedCableMeters+ "m):" + cost_of_cable_per_meter * addedCableMeters);
  123. //
  124. //
  125. // //calculating f_grid
  126. // double f_grid = 0;
  127. // //each network is a holon
  128. // for(DecoratedNetwork net: state.getNetworkList()) {
  129. // Graph G = GraphMetrics.convertDecoratedNetworkToGraph(net);
  130. // //We have to penalize single Networks;
  131. // if(G.V.length <= 1 || G.S.length <= 1) {
  132. // f_grid += lambda_avg_shortest_path;// + lambda_disjoint_path;
  133. // continue;
  134. // }
  135. //
  136. // double avgShortestPath = GraphMetrics.averageShortestDistance(G);
  137. // //double disjpointPaths = GraphMetrics.averageEdgeDisjointPathProblem(G);
  138. // if(useLog) {
  139. // averageLog.addSample("avgShortestPath", (float)avgShortestPath);
  140. // }
  141. // f_grid += avgShortestPathPenalty(avgShortestPath);// + disjoinPathPenalty(disjpointPaths);
  142. // }
  143. // //take average to encourage splitting
  144. // f_grid /= state.getNetworkList().size();
  145. //
  146. //
  147. //
  148. //
  149. // if(moreInformation) {
  150. // printWeightedValues(f_eb, f_maximum, f_holon, f_selection, f_grid);
  151. // if(useLog) {
  152. // log.info(averageLog.toString());
  153. // }
  154. // }
  155. // //printUnsquashedValues(f_eb, f_maximum, f_holon, f_selection, f_grid);
  156. // if(useLog) {
  157. // averageLog.addSample("Unsquashed f_eb", (float)f_eb);
  158. // averageLog.addSample("Unsquashed f_maximum", (float)f_maximum);
  159. // averageLog.addSample("Unsquashed f_holon", (float)f_holon);
  160. // averageLog.addSample("Unsquashed f_selection", (float)f_selection);
  161. // averageLog.addSample("Unsquashed f_grid", (float)f_grid);
  162. // }
  163. // return (float) (w_eb * squash(f_eb, k_eb)
  164. // + w_max * squash(f_maximum, k_max)
  165. // + w_holon * squash(f_holon, k_holon)
  166. // + w_selection * squash(f_selection, k_selection)
  167. // + w_grid * squash(f_grid, k_grid));
  168. return 0;
  169. }
  170. //
  171. // public static double calculateTopologieCost(DecoratedState state, int amountOfAddedSwitch,
  172. // double addedCableMeters) {
  173. // double cost = calculateWildcardCost(state);
  174. // cost += calculateAddedSwitchCost(amountOfAddedSwitch);
  175. // cost += calculateAddedCableCost(addedCableMeters);
  176. // return cost;
  177. // }
  178. //
  179. // public static double calculateAddedCableCost(double addedCableMeters) {
  180. // return cost_of_cable_per_meter * addedCableMeters;
  181. // }
  182. //
  183. // public static double calculateAddedSwitchCost(int amountOfAddedSwitch) {
  184. // return cost_switch * amountOfAddedSwitch;
  185. // }
  186. //
  187. // public static double calculateWildcardCost(DecoratedState state) {
  188. // double cost = 0;
  189. // for(DecoratedNetwork net : state.getNetworkList()) {
  190. // for(DecoratedHolonObject dHobject : net.getConsumerList()) {
  191. // if(dHobject.getModel().getName().contains("Wildcard")){
  192. // if(dHobject.getModel().getName().length() > 9) {
  193. // String costString = dHobject.getModel().getName().substring(9);
  194. // cost += Double.parseDouble(costString);
  195. // }
  196. // }
  197. // }
  198. // for(DecoratedHolonObject dHobject : net.getConsumerSelfSuppliedList()) {
  199. // if(dHobject.getModel().getName().contains("Wildcard")){
  200. // if(dHobject.getModel().getName().length() > 9) {
  201. // String costString = dHobject.getModel().getName().substring(9);
  202. // cost += Double.parseDouble(costString);
  203. // }
  204. // }
  205. // }
  206. // for(DecoratedHolonObject dHobject : net.getSupplierList()) {
  207. // if(dHobject.getModel().getName().contains("Wildcard")){
  208. // if(dHobject.getModel().getName().length() > 9) {
  209. // String costString = dHobject.getModel().getName().substring(9);
  210. // cost += Double.parseDouble(costString);
  211. // }
  212. // }
  213. // }
  214. // }
  215. // return cost;
  216. // }
  217. private static String doubleToString(double value) {
  218. return String.format (Locale.US, "%.2f", value);
  219. }
  220. @SuppressWarnings("unused")
  221. private static double disjoinPathPenalty(double value) {
  222. return -(2.0 * lambda_disjoint_path) / (1 + Math.exp(- (value - centerValue_disjoint_path)/ range_for_k_disjoint_path)) + (2.0 * lambda_disjoint_path);
  223. }
  224. private static double avgShortestPathPenalty(double value) {
  225. return (2.0 * lambda_avg_shortest_path) / (1 + Math.exp(- value/ range_for_k_avg_shortest_path)) - lambda_avg_shortest_path;
  226. }
  227. /**
  228. * Attention Math.log calcultae ln not log
  229. * @param kappa
  230. * @return
  231. */
  232. private static double range(double kappa) {
  233. return - kappa / Math.log(Math.pow(2.0, 0.05) - 1.0 );
  234. }
  235. /**
  236. * The squashing function in paper
  237. * @param x the input
  238. * @param kappa the corresponding kappa
  239. * @return
  240. */
  241. static public double squash(double x, double kappa) {
  242. return 100.f/(1.0f + Math.exp(-(10.f * (x - kappa/2.f))/ kappa)) - squash_subtract;
  243. }
  244. /**
  245. * f_sup in paper
  246. * @param supply from 0 to 1
  247. * @return
  248. */
  249. static public double supplyPenalty(double supply) {
  250. double supplyPercentage = 100 * supply;
  251. return (supplyPercentage < 100) ? -0.5 * supplyPercentage + 50: supplyPercentage - 100;
  252. }
  253. static void printWeightedValues(double f_eb, double f_maximum, double f_holon, double f_selection, double f_grid){
  254. log.info("===================================================================");
  255. log.info(" f_eb: " + f_eb + ", k_eb: " + k_eb + ", w_eb: " + w_eb);
  256. log.info(" squash(f_eb, k_eb): " + doubleToString(squash(f_eb, k_eb)));
  257. log.info(" w_eb * squash(f_eb, k_eb): " + doubleToString(w_eb * squash(f_eb, k_eb)));
  258. log.info("===================================================================");
  259. log.info(" f_maximum: " + f_maximum + ", k_max: " + k_max + ", w_max: " + w_max);
  260. log.info(" squash(f_maximum, k_max): " + doubleToString(squash(f_maximum, k_max)));
  261. log.info(" w_max * squash(f_maximum, k_max): " + doubleToString(w_max * squash(f_maximum, k_max)));
  262. log.info("===================================================================");
  263. log.info(" f_selection: " + f_selection + ", k_selection: " + k_selection + ", w_selection: " + w_selection);
  264. log.info(" squash(f_selection, k_selection): " + doubleToString(squash(f_selection, k_selection)));
  265. log.info(" w_selection * squash(f_selection, k_selection): " + doubleToString(w_selection * squash(f_selection, k_selection)));
  266. log.info("===================================================================");
  267. log.info(" f_holon: " + f_holon + ", k_holon: " + k_holon + ", w_holon: " + w_holon);
  268. log.info(" squash(f_holon, k_holon): " + doubleToString(squash(f_holon, k_holon)));
  269. log.info(" w_holon * squash(f_holon, k_holon): " + doubleToString(w_holon * squash(f_holon, k_holon)));
  270. log.info("===================================================================");
  271. log.info(" f_grid: " + f_grid + ", k_grid: " + k_grid + ", w_grid: " + w_grid);
  272. log.info(" squash(f_grid, k_grid): " + doubleToString(squash(f_grid, k_grid)));
  273. log.info(" w_grid * squash(f_grid, k_grid): " + doubleToString(w_grid * squash(f_grid, k_grid)));
  274. log.info("===================================================================");
  275. }
  276. static void printUnsquashedValues(double f_eb, double f_maximum, double f_holon, double f_selection, double f_grid){
  277. System.out.print(" f_eb(" + f_eb + ") ");
  278. System.out.print(" f_maximum(" + f_maximum + ") ");
  279. System.out.print(" f_holon(" + f_holon + ") ");
  280. System.out.print(" f_selection(" + f_selection + ") ");
  281. log.info(" f_grid(" + f_grid + ") ");
  282. }
  283. }