ObjectiveFunctionByCarlos.java 9.4 KB

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