Evaluation.java 4.4 KB

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