Evaluation.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package exampleAlgorithms;
  2. import classes.HolonObject;
  3. import ui.model.DecoratedNetwork;
  4. import ui.model.DecoratedState;
  5. import ui.controller.FlexManager.FlexState;
  6. import ui.controller.FlexManager.FlexWrapper;
  7. import ui.model.DecoratedHolonObject.HolonObjectState;
  8. public class Evaluation {
  9. /**
  10. * Calculate the Fitness(Penelty) Value for a state (alias the calculated Position).
  11. * TODO: Make me better Rolf.
  12. * @param state
  13. * @return
  14. */
  15. public static double getFitnessValueForState(DecoratedState state) {
  16. double fitness = 0.0;
  17. double nw_fitness =0.0;
  18. double object_fitness = 0.0;
  19. double flexFitness = 0.0;
  20. // calculate network_fitness
  21. for(DecoratedNetwork net : state.getNetworkList()) {
  22. float production = net.getSupplierList().stream().map(supplier -> supplier.getEnergyToSupplyNetwork()).reduce(0.0f, (a, b) -> a + b);
  23. float consumption = net.getConsumerList().stream().map(con -> con.getEnergyNeededFromNetwork()).reduce(0.0f, (a, b) -> a + b);
  24. nw_fitness += Math.abs((production - consumption)/100); //Energy is now everywhere positive
  25. }
  26. // calculate object_fitness
  27. for(DecoratedNetwork net : state.getNetworkList()) {
  28. object_fitness += net.getConsumerList().stream().map(con -> holonObjectSupplyPenaltyFunction(con.getSupplyBarPercentage()) + inactiveHolonElementPenalty(con.getModel())).reduce(0.0, (a, b) -> (a + b));
  29. //warum war das im network fitness und nicht hier im Object fitness??
  30. object_fitness += net.getConsumerList().stream().map(con -> StateToDouble(con.getState())).reduce(0.0, (a,b) -> (a+b));
  31. //System.out.console.println("objectfitness for statestuff: " + object_fitness);
  32. //object_fitness += net.getPassivNoEnergyList().stream().map(con -> 1000.0).reduce(0.0, (a, b) -> (a + b));
  33. object_fitness += net.getPassivNoEnergyList().stream().map(sup -> inactiveHolonElementPenalty(sup.getModel())).reduce(0.0, (a, b) -> (a + b));
  34. object_fitness += net.getSupplierList().stream().map(sup -> inactiveHolonElementPenalty(sup.getModel())).reduce(0.0, (a, b) -> (a + b));
  35. object_fitness += net.getConsumerSelfSuppliedList().stream().map(con -> inactiveHolonElementPenalty(con.getModel())).reduce(0.0, (a, b) -> (a + b));
  36. }
  37. // calculate flexibility fitness
  38. for(FlexWrapper flexWrapper :state.getFlexManager().getAllFlexWrapperWithState(FlexState.IN_USE)) {
  39. flexFitness += flexWrapper.getFlex().cost / (double)flexWrapper.getFlex().getDuration();
  40. }
  41. fitness = nw_fitness + object_fitness + flexFitness;
  42. return fitness;
  43. }
  44. /**
  45. * Untouched:
  46. * Function that returns the fitness depending on the number of elements deactivated in a single holon object
  47. * @param obj Holon Object that contains Holon Elements
  48. * @return fitness value for that object depending on the number of deactivated holon elements
  49. */
  50. private static double inactiveHolonElementPenalty(HolonObject obj) {
  51. float result = 0;
  52. int activeElements = obj.getNumberOfActiveElements();
  53. int maxElements = obj.getElements().size();
  54. //result = (float) Math.pow((maxElements -activeElements),2)*10;
  55. result = (float) Math.pow(5, 4* ( (float) maxElements - (float) activeElements)/ (float) maxElements) - 1;
  56. //System.out.console.println("max: " + maxElements + " active: " + activeElements + " results in penalty: " + result);
  57. return result;
  58. }
  59. /**
  60. * Untouched:
  61. * Calculates a penalty value based on the HOs current supply percentage
  62. * @param supplyPercentage
  63. * @return
  64. */
  65. private static double holonObjectSupplyPenaltyFunction(float supplyPercentage) {
  66. double result = 0;
  67. /*if(supplyPercentage == 1)
  68. return result;
  69. else if(supplyPercentage < 1 && supplyPercentage >= 0.25) // undersupplied inbetween 25% and 100%
  70. result = (float) Math.pow(1/supplyPercentage, 2);
  71. else if (supplyPercentage < 0.25) //undersupplied with less than 25%
  72. result = (float) Math.pow(1/supplyPercentage,2);
  73. else if (supplyPercentage < 1.25) //Oversupplied less than 25%
  74. result = (float) Math.pow(supplyPercentage,3) ;
  75. else result = (float) Math.pow(supplyPercentage,4); //Oversupplied more than 25%
  76. if(Float.isInfinite(result) || Float.isNaN(result))
  77. result = 1000;
  78. */
  79. if(supplyPercentage <= 1.0) {
  80. result = Math.pow(5,((100 - (supplyPercentage*100))/50 + 2)) - Math.pow(5, 2);
  81. }
  82. else {
  83. result = Math.pow(6,((100 - (supplyPercentage*100))/50 + 2)) - Math.pow(6, 2);
  84. }
  85. return result;
  86. }
  87. /**
  88. * If you want to get in touch with a reliable state? Working function not in use currently.
  89. * @param state
  90. * @return
  91. */
  92. private static double StateToDouble(HolonObjectState state) {
  93. switch (state) {
  94. case NOT_SUPPLIED:
  95. return 150.0;
  96. case NO_ENERGY:
  97. return 150.0;
  98. case OVER_SUPPLIED:
  99. return 100.0;
  100. case PARTIALLY_SUPPLIED:
  101. return 100.0;
  102. case PRODUCER:
  103. return 0;
  104. case SUPPLIED:
  105. return 0;
  106. default:
  107. return 0;
  108. }
  109. }
  110. }