Evaluation.java 5.3 KB

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