TopologieObjectiveFunction.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. import classes.FloatLog;
  11. public class TopologieObjectiveFunction {
  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. //--> f_eb parameter
  16. /**
  17. * Maximum Energie Difference(kappa)
  18. */
  19. static double k_eb = 5000.f;
  20. /**
  21. * Maximum when all on Energie Difference(kappa)
  22. */
  23. static double k_max = 10.f;
  24. static double lambda_max = 10.;
  25. //--> f_holon parameter
  26. /**
  27. * maximum penalty from holon element distribution
  28. */
  29. static double k_holon= 4000;
  30. //--> f_selection paramaeter;
  31. /**
  32. * average Maximum Cost for selction(kappa) of switch and elements.
  33. */
  34. static double k_selection = 200000;
  35. static double cost_switch = 3000;
  36. private static double cost_of_cable_per_meter = 6;
  37. //--> f_grid parameter
  38. /**
  39. * The avergae shortest path maximum length -> kappa for the squash function
  40. */
  41. static double k_avg_shortest_path = 1600;
  42. //Disjpijoint path cant have zero as output it starts with the value 1
  43. static double centerValue_disjoint_path = 1.0;
  44. static double k_disjoint_path = 2.4;
  45. static double lambda_avg_shortest_path = 10;
  46. static double lambda_disjoint_path = 10;
  47. static double k_grid = lambda_avg_shortest_path;// + lambda_disjoint_path;
  48. //pre-calculated parameters for partial function terms:
  49. /**
  50. * Pre calculated for the squash function
  51. * <br>
  52. * {@link TopologieObjectiveFunction#squash}
  53. */
  54. static double squash_subtract = 1.0f / (1.f + (float) Math.exp(5.0));
  55. static double range_for_k_avg_shortest_path = range(k_avg_shortest_path);
  56. static double range_for_k_disjoint_path = range(k_disjoint_path - centerValue_disjoint_path);
  57. public static FloatLog log = new FloatLog();
  58. static boolean useLog = false;
  59. static {
  60. //init
  61. checkParameter();
  62. }
  63. /**
  64. * Check parameter Setting and print error when wrong values are put in.
  65. * Here should all invariants be placed to be checked on initialization.
  66. */
  67. private static void checkParameter() {
  68. if(!(Math.abs(w_eb + w_holon + w_selection + w_grid + w_max - 1) < 0.001)) {
  69. System.err.println("ParameterError in ObjectiveFunction: Sum of all weights should be 1");
  70. }
  71. }
  72. /**
  73. * ObjectifeFunction by Carlos.
  74. * Function computes f_g:
  75. * 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)
  76. *
  77. *
  78. * squash is the squashing function {@link TopologieObjectiveFunction#squash}
  79. *
  80. *
  81. * @param state
  82. * @param moreInformation TODO
  83. * @return f_g value between 0 and 100
  84. */
  85. static public float getFitnessValueForState(DecoratedState state, int amountOfAddedSwitch, double addedCableMeters, boolean moreInformation) {
  86. //Calculate f_eb the penalty for unbalenced energy in the network
  87. double f_eb = 0;
  88. for(DecoratedNetwork net : state.getNetworkList()) {
  89. //abs
  90. f_eb += Math.abs(net.getTotalConsumption() - net.getTotalProduction());
  91. }
  92. //Average?
  93. f_eb /= state.getNetworkList().size();
  94. double f_maximum = 0;
  95. for(DecoratedNetwork net : state.getNetworkList()) {
  96. double prod = net.getTotalProduction();
  97. double con = net.getTotalConsumption();
  98. if(prod == 0 || con == 0) {
  99. f_maximum += lambda_max;
  100. }else {
  101. f_maximum += lambda_max * (Math.abs(prod - con)/Math.max(prod, con));
  102. }
  103. }
  104. //Average?
  105. f_maximum /= state.getNetworkList().size();
  106. //calculate f_holon
  107. double f_holon = 0;
  108. for(DecoratedNetwork net : state.getNetworkList()) {
  109. double f_elements_deviation_production = net.getDeviationInProductionInNetworkForHolonObjects();
  110. double f_elements_deviation_consumption = net.getDeviationInConsumptionInNetworkForHolonObjects();
  111. double f_element = f_elements_deviation_production+f_elements_deviation_consumption;
  112. f_holon += f_element;
  113. }
  114. f_holon /= state.getNetworkList().size();
  115. //calculating f_selection
  116. double f_selection = calculateTopologieCost(state, amountOfAddedSwitch, addedCableMeters);
  117. //if(moreInformation)System.out.println("CostForWildcards:" + cost + ", CostSwitches(#" + amountOfAddedSwitch +"):" + cost_switch * amountOfAddedSwitch + ", CostCables(" +addedCableMeters+ "m):" + cost_of_cable_per_meter * addedCableMeters);
  118. //calculating f_grid
  119. double f_grid = 0;
  120. //each network is a holon
  121. for(DecoratedNetwork net: state.getNetworkList()) {
  122. Graph G = GraphMetrics.convertDecoratedNetworkToGraph(net);
  123. //We have to penalize single Networks;
  124. if(G.V.length <= 1 || G.S.length <= 1) {
  125. f_grid += lambda_avg_shortest_path;// + lambda_disjoint_path;
  126. continue;
  127. }
  128. double avgShortestPath = GraphMetrics.averageShortestDistance(G);
  129. //double disjpointPaths = GraphMetrics.averageEdgeDisjointPathProblem(G);
  130. if(useLog) {
  131. log.addSample("avgShortestPath", (float)avgShortestPath);
  132. }
  133. f_grid += avgShortestPathPenalty(avgShortestPath);// + disjoinPathPenalty(disjpointPaths);
  134. }
  135. //take average to encourage splitting
  136. f_grid /= state.getNetworkList().size();
  137. if(moreInformation) {
  138. printWeightedValues(f_eb, f_maximum, f_holon, f_selection, f_grid);
  139. if(useLog) {
  140. System.out.println("Log");
  141. System.out.println(log);
  142. }
  143. }
  144. //printUnsquashedValues(f_eb, f_maximum, f_holon, f_selection, f_grid);
  145. if(useLog) {
  146. log.addSample("Unsquashed f_eb", (float)f_eb);
  147. log.addSample("Unsquashed f_maximum", (float)f_maximum);
  148. log.addSample("Unsquashed f_holon", (float)f_holon);
  149. log.addSample("Unsquashed f_selection", (float)f_selection);
  150. log.addSample("Unsquashed f_grid", (float)f_grid);
  151. }
  152. return (float) (w_eb * squash(f_eb, k_eb)
  153. + w_max * squash(f_maximum, k_max)
  154. + w_holon * squash(f_holon, k_holon)
  155. + w_selection * squash(f_selection, k_selection)
  156. + w_grid * squash(f_grid, k_grid));
  157. }
  158. public static double calculateTopologieCost(DecoratedState state, int amountOfAddedSwitch,
  159. double addedCableMeters) {
  160. double cost = calculateWildcardCost(state);
  161. cost += calculateAddedSwitchCost(amountOfAddedSwitch);
  162. cost += calculateAddedCableCost(addedCableMeters);
  163. return cost;
  164. }
  165. public static double calculateAddedCableCost(double addedCableMeters) {
  166. return cost_of_cable_per_meter * addedCableMeters;
  167. }
  168. public static double calculateAddedSwitchCost(int amountOfAddedSwitch) {
  169. return cost_switch * amountOfAddedSwitch;
  170. }
  171. public static double calculateWildcardCost(DecoratedState state) {
  172. double cost = 0;
  173. for(DecoratedNetwork net : state.getNetworkList()) {
  174. for(DecoratedHolonObject dHobject : net.getConsumerList()) {
  175. if(dHobject.getModel().getName().contains("Wildcard")){
  176. if(dHobject.getModel().getName().length() > 9) {
  177. String costString = dHobject.getModel().getName().substring(9);
  178. cost += Double.parseDouble(costString);
  179. }
  180. }
  181. }
  182. for(DecoratedHolonObject dHobject : net.getConsumerSelfSuppliedList()) {
  183. if(dHobject.getModel().getName().contains("Wildcard")){
  184. if(dHobject.getModel().getName().length() > 9) {
  185. String costString = dHobject.getModel().getName().substring(9);
  186. cost += Double.parseDouble(costString);
  187. }
  188. }
  189. }
  190. for(DecoratedHolonObject dHobject : net.getSupplierList()) {
  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. }
  199. return cost;
  200. }
  201. private static String doubleToString(double value) {
  202. return String.format (Locale.US, "%.2f", value);
  203. }
  204. private static double disjoinPathPenalty(double value) {
  205. return -(2.0 * lambda_disjoint_path) / (1 + Math.exp(- (value - centerValue_disjoint_path)/ range_for_k_disjoint_path)) + (2.0 * lambda_disjoint_path);
  206. }
  207. private static double avgShortestPathPenalty(double value) {
  208. return (2.0 * lambda_avg_shortest_path) / (1 + Math.exp(- value/ range_for_k_avg_shortest_path)) - lambda_avg_shortest_path;
  209. }
  210. /**
  211. * Attention Math.log calcultae ln not log
  212. * @param kappa
  213. * @return
  214. */
  215. private static double range(double kappa) {
  216. return - kappa / Math.log(Math.pow(2.0, 0.05) - 1.0 );
  217. }
  218. /**
  219. * The squashing function in paper
  220. * @param x the input
  221. * @param kappa the corresponding kappa
  222. * @return
  223. */
  224. static public double squash(double x, double kappa) {
  225. return 100.f/(1.0f + Math.exp(-(10.f * (x - kappa/2.f))/ kappa)) - squash_subtract;
  226. }
  227. /**
  228. * f_sup in paper
  229. * @param supply from 0 to 1
  230. * @return
  231. */
  232. static public double supplyPenalty(double supply) {
  233. double supplyPercentage = 100 * supply;
  234. return (supplyPercentage < 100) ? -0.5 * supplyPercentage + 50: supplyPercentage - 100;
  235. }
  236. static void printWeightedValues(double f_eb, double f_maximum, double f_holon, double f_selection, double f_grid){
  237. System.out.println("===================================================================");
  238. System.out.println(" f_eb: " + f_eb + ", k_eb: " + k_eb + ", w_eb: " + w_eb);
  239. System.out.println(" squash(f_eb, k_eb): " + doubleToString(squash(f_eb, k_eb)));
  240. System.out.println(" w_eb * squash(f_eb, k_eb): " + doubleToString(w_eb * squash(f_eb, k_eb)));
  241. System.out.println("===================================================================");
  242. System.out.println(" f_maximum: " + f_maximum + ", k_max: " + k_max + ", w_max: " + w_max);
  243. System.out.println(" squash(f_maximum, k_max): " + doubleToString(squash(f_maximum, k_max)));
  244. System.out.println(" w_max * squash(f_maximum, k_max): " + doubleToString(w_max * squash(f_maximum, k_max)));
  245. System.out.println("===================================================================");
  246. System.out.println(" f_selection: " + f_selection + ", k_selection: " + k_selection + ", w_selection: " + w_selection);
  247. System.out.println(" squash(f_selection, k_selection): " + doubleToString(squash(f_selection, k_selection)));
  248. System.out.println(" w_selection * squash(f_selection, k_selection): " + doubleToString(w_selection * squash(f_selection, k_selection)));
  249. System.out.println("===================================================================");
  250. System.out.println(" f_holon: " + f_holon + ", k_holon: " + k_holon + ", w_holon: " + w_holon);
  251. System.out.println(" squash(f_holon, k_holon): " + doubleToString(squash(f_holon, k_holon)));
  252. System.out.println(" w_holon * squash(f_holon, k_holon): " + doubleToString(w_holon * squash(f_holon, k_holon)));
  253. System.out.println("===================================================================");
  254. System.out.println(" f_grid: " + f_grid + ", k_grid: " + k_grid + ", w_grid: " + w_grid);
  255. System.out.println(" squash(f_grid, k_grid): " + doubleToString(squash(f_grid, k_grid)));
  256. System.out.println(" w_grid * squash(f_grid, k_grid): " + doubleToString(w_grid * squash(f_grid, k_grid)));
  257. System.out.println("===================================================================");
  258. System.out.println();
  259. System.out.println();
  260. }
  261. static void printUnsquashedValues(double f_eb, double f_maximum, double f_holon, double f_selection, double f_grid){
  262. System.out.print(" f_eb(" + f_eb + ") ");
  263. System.out.print(" f_maximum(" + f_maximum + ") ");
  264. System.out.print(" f_holon(" + f_holon + ") ");
  265. System.out.print(" f_selection(" + f_selection + ") ");
  266. System.out.println(" f_grid(" + f_grid + ") ");
  267. }
  268. }