ObjectiveFunctionByCarlos.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package exampleAlgorithms;
  2. import ui.model.DecoratedNetwork;
  3. import ui.model.DecoratedState;
  4. import java.lang.Exception;
  5. import classes.Flexibility;
  6. import classes.HolonElement.Priority;
  7. public class ObjectiveFunctionByCarlos {
  8. //Parameters
  9. //weight for f_g(H)
  10. static double w_eb = .35f, w_state = .35f, w_pro = .1f, w_perf = .1f, w5=.1f;
  11. //kappas for squashing function
  12. static double k_eb = 1000000.f, k_state = 30000, k_pro = 2100, k_perf = 1100, k_holon= 200000;
  13. //theta for f_pro
  14. static double theta = 3;
  15. //kappas for f_perf:
  16. static double kappa_f_unre = 120;
  17. static double kappa_f_cool = 60*60*24;
  18. static double kappa_f_dur = 60*60;
  19. //lambdas for f_perf:
  20. static double lambda_f_unre = 10;
  21. static double lambda_f_cool = 10;
  22. static double lambda_f_dur = 10;
  23. //pre-calculated parameters for partial function terms:
  24. /**
  25. * Pre calculated for the squash function
  26. * <br>
  27. * {@link ObjectiveFunctionByCarlos#squash}
  28. */
  29. static double squash_subtract = 1.0f / (1.f + (float) Math.exp(5.0));
  30. static double range_for_kappa_f_unre = range(kappa_f_unre);
  31. static double range_for_kappa_f_cool = range(kappa_f_cool);
  32. static double range_for_kappa_f_dur = range(kappa_f_dur);
  33. static {
  34. //init
  35. checkParameter();
  36. double k = 9.14E72;
  37. }
  38. /**
  39. * Check parameter Setting and print error when wrong values are put in.
  40. * Here should all invariants be placed to be checked on initialization.
  41. */
  42. private static void checkParameter() {
  43. if(!(Math.abs(w_eb + w_state + w_pro + w_perf + w5 - 1) < 0.001)) {
  44. System.err.println("ParameterError in ObjectiveFunction: w1 + w2 + w3 + w4 + w5 should be 1");
  45. }
  46. }
  47. /**
  48. * ObjectifeFunction by Carlos.
  49. * Function computes f_g:
  50. * 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)
  51. *
  52. *
  53. * squash is the squashing function {@link ObjectiveFunctionByCarlos#squash}
  54. *
  55. *
  56. * @param state
  57. * @return f_g value between 0 and 100
  58. */
  59. static public float getFitnessValueForState(DecoratedState state) {
  60. //Calculate f_eb the penalty for unbalenced energy in the network
  61. //TODO: Hier sollte zwischen den Netzwerken verschiedenen Holons unterschieden werden dies ist in den Formeln nicht wiedergegeben
  62. // Kann somit schlechte und gute Netzwerke ausgleichen
  63. // Implementierung ist wie im paper.
  64. double f_eb = 0;
  65. //sum over all objects
  66. for(DecoratedNetwork net : state.getNetworkList()) {
  67. f_eb += net.getConsumerList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
  68. f_eb += net.getConsumerSelfSuppliedList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
  69. f_eb += net.getSupplierList().stream().map(sup -> sup.getEnergyProducing() - sup.getEnergySelfConsuming()).reduce(0.f, Float::sum);
  70. }
  71. //abs
  72. f_eb = Math.abs(f_eb);
  73. //Calculate f_state the penalty function for the supply state
  74. double f_state = 0;
  75. for(DecoratedNetwork net : state.getNetworkList()) {
  76. f_state += net.getConsumerList().stream().map(con -> supplyPenalty(con.getSupplyBarPercentage())).reduce(0., Double::sum);
  77. }
  78. //calculate f_pro the penalty function for priority usage
  79. // for each active flexibility punish
  80. double f_pro = 0;
  81. f_pro = state.getFlexManager().getAllFlexesOrderedThisTimeStep().stream().map(flex -> Math.pow(theta, priorityToDouble(flex.getElement().getPriority()) )).reduce(0.0, Double::sum);
  82. //calculate f_perf the penalty function for the quality of a flexibility used
  83. // and the subfuction f_unre, f_cool, f_dur
  84. double f_perf = 0;
  85. for(Flexibility flex : state.getFlexManager().getAllFlexesOrderedThisTimeStep()) {
  86. double f_unre = unresponsivnessPenalty(flex.getSpeed());
  87. double f_cool = cooldownPenalty(flex.getCooldown());
  88. double f_dur = durationPenalty(flex.getDuration());
  89. f_perf += f_unre + f_cool + f_dur;
  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_flexibility_diviation_consumption = net.getDiviationInFlexibilityConsumption();
  97. double f_flexibility_diviation_production = net.getDiviationInFlexibilityProduction();
  98. double con = net.getTotalConsumption();
  99. double prod = net.getTotalProduction();
  100. double f_change_positive = (con > 0.0)? net.getFlexibilityProductionCapacity() / con : 0.0;
  101. double f_change_negativ = (prod > 0.0)? net.getFlexibilityConsumptionCapacity() / prod: 0.0;
  102. f_holon += f_elements_diviation_production + f_elements_diviation_consumption
  103. + f_flexibility_diviation_consumption + f_flexibility_diviation_production + f_change_positive +f_change_negativ;
  104. }
  105. // System.out.print( "f_eb=" + f_eb);
  106. // System.out.print( " f_state=" + f_state);
  107. // System.out.print( " f_pro=" + f_pro);
  108. // System.out.print( " f_perf=" + f_perf);
  109. // System.out.println( " f_holon=" + f_holon);
  110. return (float) (w_eb * squash(f_eb, k_eb) + w_state * squash(f_state, k_state) + w_pro * squash(f_pro, k_pro) + w_perf * squash(f_perf, k_perf) + w5 * squash(f_holon, k_holon));
  111. }
  112. /**
  113. * The squashing function in paper
  114. * @param x the input
  115. * @param kappa the corresponding kappa
  116. * @return
  117. */
  118. static public double squash(double x, double kappa) {
  119. return 100.f/(1.0f + Math.exp(-(10.f * (x - kappa/2.f))/ kappa)) - squash_subtract;
  120. }
  121. /**
  122. * f_sup in paper
  123. * @param supply from 0 to 1
  124. * @return
  125. */
  126. static public double supplyPenalty(double supply) {
  127. double supplyPercentage = 100 * supply;
  128. // double test = (supplyPercentage < 100) ? -0.5 * supplyPercentage + 50: supplyPercentage - 100;
  129. return (supplyPercentage < 100) ? -0.5 * supplyPercentage + 50: supplyPercentage - 100;
  130. }
  131. /**
  132. * prio function in the paper
  133. * @param priority
  134. * @return
  135. */
  136. private static double priorityToDouble(Priority priority) {
  137. switch(priority) {
  138. case Essential:
  139. return 3.;
  140. case High:
  141. return 2.;
  142. case Medium:
  143. return 1.;
  144. case Low:
  145. default:
  146. return 0.;
  147. }
  148. }
  149. /**
  150. * Attention Math.log calcultae ln not log
  151. * @param kappa
  152. * @return
  153. */
  154. private static double range(double kappa) {
  155. return kappa / Math.log(Math.pow(2.0, 0.05) - 1.0 );
  156. }
  157. /**
  158. * f_unre
  159. * @param unresponsiv
  160. * @return
  161. */
  162. private static double unresponsivnessPenalty(double unresponsiv) {
  163. return (2.0 * lambda_f_unre) / Math.exp(- unresponsiv/ range_for_kappa_f_unre) - lambda_f_unre;
  164. }
  165. /**
  166. * f_cool
  167. * @param cooldown
  168. * @return
  169. */
  170. private static double cooldownPenalty(double cooldown) {
  171. return (2.0 * lambda_f_cool) / Math.exp(- cooldown/ range_for_kappa_f_cool) - lambda_f_cool;
  172. }
  173. private static double durationPenalty(double duration) {
  174. double lambda_dur_times2 = 2.0 * lambda_f_dur;
  175. return - lambda_dur_times2 / Math.exp(- duration/ range_for_kappa_f_dur) + lambda_dur_times2;
  176. }
  177. }