123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package exampleAlgorithms;
- import ui.model.DecoratedNetwork;
- import ui.model.DecoratedState;
- import java.lang.Exception;
- public class ObjectiveFunctionByCarlos {
- //Parameter
- static float lambda;
- //weight for f_g(H)
- static float w1 = .2f, w2 = .2f, w3 = .2f, w4 = .2f, w5=.2f;
- //kappas for squashing function
- static float k1 = .2f, k2 = .2f, k3 = .2f, k4 = .2f, k5=.2f;
-
-
-
-
- //pre-calculated parameters:
- /**
- * Pre calculated for the squash function
- * <br>
- * {@link ObjectiveFunctionByCarlos#squash}
- */
- static float squash_subtract = 1.0f / (1.f + (float) Math.exp(5.0));
-
-
-
- static {
- //pre-calculations
- System.out.println("Precalculations");
-
-
-
- double doubleBaseTest = Math.exp(300);
- double doubleMax = Double.MAX_VALUE;
- float floatBaseTest = (float)Math.exp(300);
- float floatMax = Float.MAX_VALUE;
-
-
- System.out.println("floatMax" + floatMax);
- System.out.println("floatBaseTest" + floatBaseTest);
- System.out.println("doubleMax" + doubleMax);
- System.out.println("doubleBaseTest" + doubleBaseTest);
-
-
- checkParameter();
- }
-
- /**
- * Check parameter Setting and print error when wrong values are put in.
- * Here should all invariants be placed to be checked on initialization.
- */
- private static void checkParameter() {
- if(!(Math.abs(w1 + w2 + w3 + w4 + w5 - 1) < 0.001)) {
- System.err.println("ParameterError in ObjectiveFunction: w1 + w2 + w3 + w4 + w5 should be 1");
- }
- }
-
- /**
- * ObjectifeFunction by Carlos.
- * Function computes f_g:
- * f_g = w1 * squash(f_eb, k1) + w2 * squash(f_state, k2) + w3 * squash(f_pro, k3) + w4 * squash(f_perf, k4) + w5 * squash(f_holon, k5)
- *
- *
- * squash is the squashing function {@link ObjectiveFunctionByCarlos#squash}
- *
- *
- * @param state
- * @return f_g
- */
- static public float getFitnessValueForState(DecoratedState state) {
-
- //Calculate f_eb the penalty for unbalenced energy in the network
-
- //TODO: Hier sollte zwischen den Netzwerken verschiedenen Holons unterschieden werden dies ist in den Formeln nicht wiedergegeben
- // Kann somit schlechte und gute Netzwerke ausgleichen
- // Implementierung ist wie im paper.
- float f_eb = 0;
- //sum over all objects
- for(DecoratedNetwork net : state.getNetworkList()) {
- f_eb += net.getConsumerList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
- f_eb += net.getConsumerSelfSuppliedList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
- f_eb += net.getSupplierList().stream().map(sup -> sup.getEnergyProducing() - sup.getEnergySelfConsuming()).reduce(0.f, Float::sum);
- }
- //abs
- f_eb = (float) Math.abs(f_eb);
-
- //Calculate f_state the penalty function for the supply state
- float f_state = 0;
- for(DecoratedNetwork net : state.getNetworkList()) {
- f_state += net.getConsumerList().stream().map(con -> supplyPenalty(con.getSupplyBarPercentage())).reduce(0.f, Float::sum);
- }
-
-
-
-
-
-
-
-
-
-
-
-
- return 0.0f + lambda;
- }
-
-
- /**
- *
- * @param x the input
- * @param kappa the corresponding kappa
- * @return
- */
- static public float squash(float x, float kappa) {
- return 100.f/(1.0f + (float)Math.exp(-(10.f * (x - kappa/2.f))/ kappa)) - squash_subtract;
- }
-
- /**
- *
- * @param supplyPercentage from 0 to 1
- * @return
- */
- static public float supplyPenalty(float supplyPercentage) {
- float f_base = base(supplyPercentage);
- return (float)(Math.pow(f_base, 100 - 100 * supplyPercentage) - Math.pow(f_base, 2));
- }
- /**
- * TODO: f_base wird im Paper mit e angegeben. f_eb mit exp() warum nicht einheitlich?
- *
- * @param supplyPercentage from 0 to 1
- * @return 5 or 6 but with fancy math
- */
- static public float base(float supplyPercentage) {
- //TODO: Fancy kann aber leicht zu overflow fürhen denn e1000 ist zu groß für double max double 1.7976931348623157E308 und max float 3.4028235E38
- //Oder BigDecimal aber müsste echt nicht sein.
- double euler = Math.exp(1000.0 - 1000.0 * supplyPercentage);
- return (float) ((5 * euler + 6 ) / (euler + 1));
-
-
-
- //Suggestion
- // return (supplyPercentage < 1.0f)? 5.0: 6.0; einfach und gut
-
- }
-
- }
|