ObjectiveFunctionByCarlos.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package exampleAlgorithms;
  2. import ui.model.DecoratedNetwork;
  3. import ui.model.DecoratedState;
  4. import java.lang.Exception;
  5. public class ObjectiveFunctionByCarlos {
  6. //Parameter
  7. static float lambda;
  8. //weight for f_g(H)
  9. static float w1 = .2f, w2 = .2f, w3 = .2f, w4 = .2f, w5=.2f;
  10. //kappas for squashing function
  11. static float k1 = .2f, k2 = .2f, k3 = .2f, k4 = .2f, k5=.2f;
  12. //pre-calculated parameters:
  13. /**
  14. * Pre calculated for the squash function
  15. * <br>
  16. * {@link ObjectiveFunctionByCarlos#squash}
  17. */
  18. static float squash_subtract = 1.0f / (1.f + (float) Math.exp(5.0));
  19. static {
  20. //pre-calculations
  21. System.out.println("Precalculations");
  22. double doubleBaseTest = Math.exp(300);
  23. double doubleMax = Double.MAX_VALUE;
  24. float floatBaseTest = (float)Math.exp(300);
  25. float floatMax = Float.MAX_VALUE;
  26. System.out.println("floatMax" + floatMax);
  27. System.out.println("floatBaseTest" + floatBaseTest);
  28. System.out.println("doubleMax" + doubleMax);
  29. System.out.println("doubleBaseTest" + doubleBaseTest);
  30. checkParameter();
  31. }
  32. /**
  33. * Check parameter Setting and print error when wrong values are put in.
  34. * Here should all invariants be placed to be checked on initialization.
  35. */
  36. private static void checkParameter() {
  37. if(!(Math.abs(w1 + w2 + w3 + w4 + w5 - 1) < 0.001)) {
  38. System.err.println("ParameterError in ObjectiveFunction: w1 + w2 + w3 + w4 + w5 should be 1");
  39. }
  40. }
  41. /**
  42. * ObjectifeFunction by Carlos.
  43. * Function computes f_g:
  44. * 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)
  45. *
  46. *
  47. * squash is the squashing function {@link ObjectiveFunctionByCarlos#squash}
  48. *
  49. *
  50. * @param state
  51. * @return f_g
  52. */
  53. static public float getFitnessValueForState(DecoratedState state) {
  54. //Calculate f_eb the penalty for unbalenced energy in the network
  55. //TODO: Hier sollte zwischen den Netzwerken verschiedenen Holons unterschieden werden dies ist in den Formeln nicht wiedergegeben
  56. // Kann somit schlechte und gute Netzwerke ausgleichen
  57. // Implementierung ist wie im paper.
  58. float f_eb = 0;
  59. //sum over all objects
  60. for(DecoratedNetwork net : state.getNetworkList()) {
  61. f_eb += net.getConsumerList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
  62. f_eb += net.getConsumerSelfSuppliedList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
  63. f_eb += net.getSupplierList().stream().map(sup -> sup.getEnergyProducing() - sup.getEnergySelfConsuming()).reduce(0.f, Float::sum);
  64. }
  65. //abs
  66. f_eb = (float) Math.abs(f_eb);
  67. //Calculate f_state the penalty function for the supply state
  68. float f_state = 0;
  69. for(DecoratedNetwork net : state.getNetworkList()) {
  70. f_state += net.getConsumerList().stream().map(con -> supplyPenalty(con.getSupplyBarPercentage())).reduce(0.f, Float::sum);
  71. }
  72. return 0.0f + lambda;
  73. }
  74. /**
  75. *
  76. * @param x the input
  77. * @param kappa the corresponding kappa
  78. * @return
  79. */
  80. static public float squash(float x, float kappa) {
  81. return 100.f/(1.0f + (float)Math.exp(-(10.f * (x - kappa/2.f))/ kappa)) - squash_subtract;
  82. }
  83. /**
  84. *
  85. * @param supplyPercentage from 0 to 1
  86. * @return
  87. */
  88. static public float supplyPenalty(float supplyPercentage) {
  89. float f_base = base(supplyPercentage);
  90. return (float)(Math.pow(f_base, 100 - 100 * supplyPercentage) - Math.pow(f_base, 2));
  91. }
  92. /**
  93. * TODO: f_base wird im Paper mit e angegeben. f_eb mit exp() warum nicht einheitlich?
  94. *
  95. * @param supplyPercentage from 0 to 1
  96. * @return 5 or 6 but with fancy math
  97. */
  98. static public float base(float supplyPercentage) {
  99. //TODO: Fancy kann aber leicht zu overflow fürhen denn e1000 ist zu groß für double max double 1.7976931348623157E308 und max float 3.4028235E38
  100. //Oder BigDecimal aber müsste echt nicht sein.
  101. double euler = Math.exp(1000.0 - 1000.0 * supplyPercentage);
  102. return (float) ((5 * euler + 6 ) / (euler + 1));
  103. //Suggestion
  104. // return (supplyPercentage < 1.0f)? 5.0: 6.0; einfach und gut
  105. }
  106. }