TopologieObjectiveFunction.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package holeg.algorithm.objective_function;
  2. import holeg.model.Holon;
  3. import holeg.model.HolonObject;
  4. import holeg.model.Model;
  5. import holeg.utility.math.decimal.Sampler;
  6. import java.util.Locale;
  7. import java.util.logging.Logger;
  8. public class TopologieObjectiveFunction {
  9. private static final Logger log = Logger.getLogger(TopologieObjectiveFunction.class.getName());
  10. public static Sampler averageLog = new Sampler();
  11. //--> f_eb parameter
  12. //Parameters
  13. //weight for f_g(H)
  14. static double w_eb = 0.2, w_max = 0.5, w_holon = 0.1, w_selection = .1, w_grid = 0.1;
  15. /**
  16. * Maximum Energie Difference(kappa)
  17. */
  18. static double k_eb = 5000.f;
  19. /**
  20. * Maximum when all on Energie Difference(kappa)
  21. */
  22. static double k_max = 10.f;
  23. //--> f_holon parameter
  24. static double lambda_max = 10.;
  25. //--> f_selection paramaeter;
  26. /**
  27. * maximum penalty from holon element distribution
  28. */
  29. static double k_holon = 4000;
  30. /**
  31. * average Maximum Cost for selction(kappa) of switch and elements.
  32. */
  33. static double k_selection = 200000;
  34. static double cost_switch = 3000;
  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 boolean useLog = false;
  56. private static double cost_of_cable_per_meter = 6;
  57. static {
  58. //init
  59. checkParameter();
  60. }
  61. /**
  62. * Check parameter Setting and print error when wrong values are put in. Here should all
  63. * invariants be placed to be checked on initialization.
  64. */
  65. private static void checkParameter() {
  66. if (!(Math.abs(w_eb + w_holon + w_selection + w_grid + w_max - 1) < 0.001)) {
  67. System.err.println("ParameterError in ObjectiveFunction: Sum of all weights should be 1");
  68. }
  69. }
  70. /**
  71. * ObjectifeFunction by Carlos. Function computes f_g: f_g = w1 * squash(f_eb, k1) + w2 *
  72. * squash(f_state, k2) + w3 * squash(f_pro, k3) + w4 * squash(f_perf, k4) + w5 * squash(f_holon,
  73. * k5)
  74. * <p>
  75. * <p>
  76. * squash is the squashing function {@link TopologieObjectiveFunction#squash}
  77. *
  78. * @param moreInformation if more prints should be made
  79. * @return f_g value between 0 and 100
  80. */
  81. static public float getFitnessValueForState(Model model, int amountOfAddedSwitch,
  82. double addedCableMeters, boolean moreInformation) {
  83. int holonCount = model.holons.size();
  84. //Calculate f_eb the penalty for unbalenced energy in the network
  85. double f_eb = 0;
  86. for (Holon holon : model.holons) {
  87. //abs
  88. f_eb += Math.abs(holon.getTotalConsumption() - holon.getTotalProduction());
  89. }
  90. //Average
  91. f_eb /= holonCount;
  92. double f_maximum = 0;
  93. for (Holon net : model.holons) {
  94. double prod = net.getTotalProduction();
  95. double con = net.getTotalConsumption();
  96. if (prod == 0 || con == 0) {
  97. f_maximum += lambda_max;
  98. } else {
  99. f_maximum += lambda_max * (Math.abs(prod - con) / Math.max(prod, con));
  100. }
  101. }
  102. //Average?
  103. f_maximum /= holonCount;
  104. //calculate f_holon
  105. double f_holon = 0;
  106. for (Holon net : model.holons) {
  107. double f_elements_deviation_production = net.getDeviationInProductionInNetworkForHolonObjects();
  108. double f_elements_deviation_consumption = net.getDeviationInConsumptionInNetworkForHolonObjects();
  109. double f_element = f_elements_deviation_production + f_elements_deviation_consumption;
  110. f_holon += f_element;
  111. }
  112. f_holon /= holonCount;
  113. //calculating f_selection
  114. double f_selection = calculateTopologieCost(model, amountOfAddedSwitch, addedCableMeters);
  115. //if(moreInformation)LOGGER.info("CostForWildcards:" + cost + ", CostSwitches(#" + amountOfAddedSwitch +"):" + cost_switch * amountOfAddedSwitch + ", CostCables(" +addedCableMeters+ "m):" + cost_of_cable_per_meter * addedCableMeters);
  116. //calculating f_grid
  117. double f_grid = 0;
  118. //each network is a holon
  119. for (Holon net : model.holons) {
  120. GraphMetrics.Graph G = GraphMetrics.convertDecoratedNetworkToGraph(model, net);
  121. //We have to penalize single Networks;
  122. if (G.V.length <= 1 || G.S.length <= 1) {
  123. f_grid += lambda_avg_shortest_path;// + lambda_disjoint_path;
  124. continue;
  125. }
  126. double avgShortestPath = GraphMetrics.averageShortestDistance(G);
  127. //double disjpointPaths = GraphMetrics.averageEdgeDisjointPathProblem(G);
  128. if (useLog) {
  129. averageLog.addSample("avgShortestPath", (float) avgShortestPath);
  130. }
  131. f_grid += avgShortestPathPenalty(avgShortestPath);// + disjoinPathPenalty(disjpointPaths);
  132. }
  133. //take average to encourage splitting
  134. f_grid /= holonCount;
  135. if (moreInformation) {
  136. printWeightedValues(f_eb, f_maximum, f_holon, f_selection, f_grid);
  137. if (useLog) {
  138. log.info(averageLog.toString());
  139. }
  140. }
  141. //printUnsquashedValues(f_eb, f_maximum, f_holon, f_selection, f_grid);
  142. if (useLog) {
  143. averageLog.addSample("Unsquashed f_eb", (float) f_eb);
  144. averageLog.addSample("Unsquashed f_maximum", (float) f_maximum);
  145. averageLog.addSample("Unsquashed f_holon", (float) f_holon);
  146. averageLog.addSample("Unsquashed f_selection", (float) f_selection);
  147. averageLog.addSample("Unsquashed f_grid", (float) f_grid);
  148. }
  149. return (float) (w_eb * squash(f_eb, k_eb)
  150. + w_max * squash(f_maximum, k_max)
  151. + w_holon * squash(f_holon, k_holon)
  152. + w_selection * squash(f_selection, k_selection)
  153. + w_grid * squash(f_grid, k_grid));
  154. }
  155. public static double calculateTopologieCost(Model model, int amountOfAddedSwitch,
  156. double addedCableMeters) {
  157. double cost = calculateWildcardCost(model);
  158. cost += calculateAddedSwitchCost(amountOfAddedSwitch);
  159. cost += calculateAddedCableCost(addedCableMeters);
  160. return cost;
  161. }
  162. public static double calculateAddedCableCost(double addedCableMeters) {
  163. return cost_of_cable_per_meter * addedCableMeters;
  164. }
  165. public static double calculateAddedSwitchCost(int amountOfAddedSwitch) {
  166. return cost_switch * amountOfAddedSwitch;
  167. }
  168. public static double calculateWildcardCost(Model model) {
  169. double cost = 0;
  170. for (Holon net : model.holons) {
  171. for (HolonObject hO : net.holonObjects) {
  172. if (hO.getName().contains("Wildcard")) {
  173. if (hO.getName().length() > 9) {
  174. String costString = hO.getName().substring(9);
  175. cost += Double.parseDouble(costString);
  176. }
  177. }
  178. }
  179. }
  180. return cost;
  181. }
  182. private static String doubleToString(double value) {
  183. return String.format(Locale.US, "%.2f", value);
  184. }
  185. @SuppressWarnings("unused")
  186. private static double disjoinPathPenalty(double value) {
  187. return -(2.0 * lambda_disjoint_path) / (1 + Math.exp(
  188. -(value - centerValue_disjoint_path) / range_for_k_disjoint_path)) + (2.0
  189. * lambda_disjoint_path);
  190. }
  191. private static double avgShortestPathPenalty(double value) {
  192. return (2.0 * lambda_avg_shortest_path) / (1 + Math.exp(-value / range_for_k_avg_shortest_path))
  193. - lambda_avg_shortest_path;
  194. }
  195. /**
  196. * Attention Math.log calcultae ln not log
  197. *
  198. * @param kappa
  199. * @return
  200. */
  201. private static double range(double kappa) {
  202. return -kappa / Math.log(Math.pow(2.0, 0.05) - 1.0);
  203. }
  204. /**
  205. * The squashing function in paper
  206. *
  207. * @param x the input
  208. * @param kappa the corresponding kappa
  209. * @return
  210. */
  211. static public double squash(double x, double kappa) {
  212. return 100.f / (1.0f + Math.exp(-(10.f * (x - kappa / 2.f)) / kappa)) - squash_subtract;
  213. }
  214. /**
  215. * f_sup in paper
  216. *
  217. * @param supply from 0 to 1
  218. * @return
  219. */
  220. static public double supplyPenalty(double supply) {
  221. double supplyPercentage = 100 * supply;
  222. return (supplyPercentage < 100) ? -0.5 * supplyPercentage + 50 : supplyPercentage - 100;
  223. }
  224. static void printWeightedValues(double f_eb, double f_maximum, double f_holon, double f_selection,
  225. double f_grid) {
  226. log.info("===================================================================");
  227. log.info(" f_eb: " + f_eb + ", k_eb: " + k_eb + ", w_eb: " + w_eb);
  228. log.info(" squash(f_eb, k_eb): " + doubleToString(squash(f_eb, k_eb)));
  229. log.info(" w_eb * squash(f_eb, k_eb): " + doubleToString(w_eb * squash(f_eb, k_eb)));
  230. log.info("===================================================================");
  231. log.info(" f_maximum: " + f_maximum + ", k_max: " + k_max + ", w_max: " + w_max);
  232. log.info(" squash(f_maximum, k_max): " + doubleToString(squash(f_maximum, k_max)));
  233. log.info(
  234. " w_max * squash(f_maximum, k_max): " + doubleToString(w_max * squash(f_maximum, k_max)));
  235. log.info("===================================================================");
  236. log.info(" f_selection: " + f_selection + ", k_selection: " + k_selection + ", w_selection: "
  237. + w_selection);
  238. log.info(
  239. " squash(f_selection, k_selection): " + doubleToString(squash(f_selection, k_selection)));
  240. log.info(" w_selection * squash(f_selection, k_selection): " + doubleToString(
  241. w_selection * squash(f_selection, k_selection)));
  242. log.info("===================================================================");
  243. log.info(" f_holon: " + f_holon + ", k_holon: " + k_holon + ", w_holon: " + w_holon);
  244. log.info(" squash(f_holon, k_holon): " + doubleToString(squash(f_holon, k_holon)));
  245. log.info(" w_holon * squash(f_holon, k_holon): " + doubleToString(
  246. w_holon * squash(f_holon, k_holon)));
  247. log.info("===================================================================");
  248. log.info(" f_grid: " + f_grid + ", k_grid: " + k_grid + ", w_grid: " + w_grid);
  249. log.info(" squash(f_grid, k_grid): " + doubleToString(squash(f_grid, k_grid)));
  250. log.info(
  251. " w_grid * squash(f_grid, k_grid): " + doubleToString(w_grid * squash(f_grid, k_grid)));
  252. log.info("===================================================================");
  253. }
  254. static void printUnsquashedValues(double f_eb, double f_maximum, double f_holon,
  255. double f_selection, double f_grid) {
  256. System.out.print(" f_eb(" + f_eb + ") ");
  257. System.out.print(" f_maximum(" + f_maximum + ") ");
  258. System.out.print(" f_holon(" + f_holon + ") ");
  259. System.out.print(" f_selection(" + f_selection + ") ");
  260. log.info(" f_grid(" + f_grid + ") ");
  261. }
  262. }