ObjectiveFunctionByCarlos.java 8.8 KB

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