Evaluation.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package holeg.algorithm.objective_function;
  2. import java.util.List;
  3. import holeg.model.Flexibility;
  4. import holeg.model.HolonObject;
  5. import holeg.model.HolonObject.HolonObjectState;
  6. import holeg.model.Flexibility.FlexState;
  7. import holeg.model.HolonElement.Priority;
  8. import holeg.ui.model.Model;
  9. //TODO(Tom2022-01-13) Fix Evaluation
  10. public class Evaluation {
  11. /**
  12. * Calculate the Fitness(Penelty) Value for a state (alias the calculated Position).
  13. * @param state
  14. * @return
  15. */
  16. public static double getFitnessValueForState(Model model) {
  17. double fitness = 0.0;
  18. double nw_fitness =0.0;
  19. double object_fitness = 0.0;
  20. double flexFitness = 0.0;
  21. // double sigma = 9;
  22. //
  23. // // calculate network_fitness
  24. // for(DecoratedNetwork net : state.getNetworkList()) {
  25. // float production = net.getSupplierList().stream().map(supplier -> supplier.getEnergyToSupplyNetwork()).reduce(0.0f, (a, b) -> a + b);
  26. // float consumption = net.getConsumerList().stream().map(con -> con.getEnergyNeededFromNetwork()).reduce(0.0f, (a, b) -> a + b);
  27. // nw_fitness += Math.abs((production - consumption)/100); //Energy is now everywhere positive
  28. // }
  29. //
  30. // // calculate object_fitness
  31. // for(DecoratedNetwork net : state.getNetworkList()) {
  32. //
  33. // object_fitness += net.getConsumerList().stream().map(con -> holonObjectSupplyPenaltyFunction(con.getSupplyBarPercentage()) /*+ inactiveHolonElementPenalty(con.getModel())*/).reduce(0.0, (a, b) -> (a + b));
  34. // object_fitness += net.getConsumerList().stream().map(con -> StateToDouble(con.getState())).reduce(0.0, (a,b) -> (a+b));
  35. // //object_fitness += net.getPassivNoEnergyList().stream().map(sup -> inactiveHolonElementPenalty(sup.getModel())).reduce(0.0, (a, b) -> (a + b));
  36. // //object_fitness += net.getSupplierList().stream().map(sup -> inactiveHolonElementPenalty(sup.getModel())).reduce(0.0, (a, b) -> (a + b));
  37. // //object_fitness += net.getConsumerSelfSuppliedList().stream().map(con -> inactiveHolonElementPenalty(con.getModel())).reduce(0.0, (a, b) -> (a + b));
  38. //
  39. // }
  40. // // calculate flexibility fitness old cost flex
  41. // /*for(FlexWrapper flexWrapper :state.getFlexManager().getAllFlexWrapperWithState(FlexState.IN_USE)) {
  42. // flexFitness += flexWrapper.cost / (double)flexWrapper.getDuration();
  43. // }*/
  44. // List<Flexibility> getAllFlexInUse = state.getAllFlex().filter(flex -> flex.getState().equals(FlexState.IN_USE)).toList();
  45. // for(Flexibility flex : getAllFlexInUse) {
  46. // flexFitness += Math.pow(sigma, (double)priorityToInt(flex.getElement().getPriority())) - 1;
  47. // }
  48. //
  49. // fitness = nw_fitness + object_fitness + flexFitness;
  50. return fitness;
  51. }
  52. private static int priorityToInt(Priority priority) {
  53. switch(priority) {
  54. case Essential:
  55. return 3;
  56. case High:
  57. return 2;
  58. case Medium:
  59. return 1;
  60. case Low:
  61. default:
  62. return 0;
  63. }
  64. }
  65. /**
  66. * Untouched:
  67. * Function that returns the fitness depending on the number of elements deactivated in a single holon object
  68. * @param obj Holon Object that contains Holon Elements
  69. * @return fitness value for that object depending on the number of deactivated holon elements
  70. */
  71. @SuppressWarnings("unused")
  72. private static double inactiveHolonElementPenalty(HolonObject obj) {
  73. float result = 0;
  74. int activeElements = obj.getNumberOfActiveElements();
  75. int maxElements = (int)obj.getElements().count();
  76. //result = (float) Math.pow((maxElements -activeElements),2)*10;
  77. result = (float) Math.pow(5, 4* ( (float) maxElements - (float) activeElements)/ (float) maxElements) - 1;
  78. //System.out.console.println("max: " + maxElements + " active: " + activeElements + " results in penalty: " + result);
  79. return result;
  80. }
  81. /**
  82. * Untouched:
  83. * Calculates a penalty value based on the HOs current supply percentage
  84. * @param supplyPercentage
  85. * @return
  86. */
  87. private static double holonObjectSupplyPenaltyFunction(float supplyPercentage) {
  88. double result = 0;
  89. /*if(supplyPercentage == 1)
  90. return result;
  91. else if(supplyPercentage < 1 && supplyPercentage >= 0.25) // undersupplied inbetween 25% and 100%
  92. result = (float) Math.pow(1/supplyPercentage, 2);
  93. else if (supplyPercentage < 0.25) //undersupplied with less than 25%
  94. result = (float) Math.pow(1/supplyPercentage,2);
  95. else if (supplyPercentage < 1.25) //Oversupplied less than 25%
  96. result = (float) Math.pow(supplyPercentage,3) ;
  97. else result = (float) Math.pow(supplyPercentage,4); //Oversupplied more than 25%
  98. if(Float.isInfinite(result) || Float.isNaN(result))
  99. result = 1000;
  100. */
  101. if(supplyPercentage <= 1.0) {
  102. result = Math.pow(5,(Math.abs((100 - (supplyPercentage*100)))/50 + 2)) - Math.pow(5, 2);
  103. }
  104. else {
  105. result = Math.pow(6,(Math.abs((100 - (supplyPercentage*100)))/50 + 2)) - Math.pow(6, 2);
  106. }
  107. return result;
  108. }
  109. /**
  110. * If you want to get in touch with a reliable state? Working function not in use currently.
  111. * @param state
  112. * @return
  113. */
  114. private static double StateToDouble(HolonObjectState state) {
  115. switch (state) {
  116. case NOT_SUPPLIED:
  117. return 150.0;
  118. case NO_ENERGY:
  119. return 150.0;
  120. case OVER_SUPPLIED:
  121. return 100.0;
  122. case PARTIALLY_SUPPLIED:
  123. return 100.0;
  124. case PRODUCER:
  125. return 0;
  126. case SUPPLIED:
  127. return 0;
  128. default:
  129. return 0;
  130. }
  131. }
  132. }