Evaluation.java 4.7 KB

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