SwitchObjectiveFunction.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package holeg.algorithm.objective_function;
  2. import holeg.ui.model.Model;
  3. public class SwitchObjectiveFunction {
  4. //weights
  5. static double w_eb = .5, w_state = .5;
  6. static double k_eb = 1050000.f, k_state = 10000;
  7. static double squash_subtract = 1.0f / (1.f + (float) Math.exp(5.0));
  8. //TODO(Tom2022-01-13) Fix SwitchObjectiveFunction
  9. static public float getFitnessValueForState(Model model) {
  10. double f_eb = 0;
  11. double f_state = 0;
  12. // double elementCountInNetwork = state.getNetworkList().stream().map(net -> net.getAmountOfElements()).reduce(0, Integer::sum);
  13. // //sum over all objects
  14. // for(DecoratedNetwork net : state.getNetworkList()) {
  15. //
  16. // //weigt
  17. // double w_network = net.getAmountOfElements()/elementCountInNetwork;
  18. //
  19. // //f_eb
  20. // double netEnergyDifference = 0;
  21. // netEnergyDifference += net.getConsumerList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
  22. // netEnergyDifference += net.getConsumerSelfSuppliedList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
  23. // netEnergyDifference += net.getSupplierList().stream().map(sup -> sup.getEnergyProducing() - sup.getEnergySelfConsuming()).reduce(0.f, Float::sum);
  24. // //abs
  25. // f_eb += w_network * Math.abs(netEnergyDifference);
  26. //
  27. //
  28. // //f_state
  29. // f_state += w_network * net.getConsumerList().stream().map(con -> supplyPenalty(con.getSupplyBarPercentage())).reduce(0., Double::sum);
  30. // }
  31. return (float) (w_eb*squash(f_eb, k_eb) + w_state*squash(f_state, k_state));
  32. }
  33. /**
  34. * f_sup in paper
  35. * @param supply from 0 to 1
  36. * @return
  37. */
  38. static public double supplyPenalty(double supply) {
  39. double supplyPercentage = 100 * supply;
  40. return (supplyPercentage < 100) ? -0.5 * supplyPercentage + 50: supplyPercentage - 100;
  41. }
  42. /**
  43. * The squashing function in paper
  44. * @param x the input
  45. * @param kappa the corresponding kappa
  46. * @return
  47. */
  48. static public double squash(double x, double kappa) {
  49. return 100.f/(1.0f + Math.exp(-(10.f * (x - kappa/2.f))/ kappa)) - squash_subtract;
  50. }
  51. }