123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- package algorithm.objectiveFunction;
- import ui.model.DecoratedHolonObject;
- import ui.model.DecoratedNetwork;
- import ui.model.DecoratedState;
- import ui.model.DecoratedSwitch;
- import java.util.HashSet;
- import java.util.Locale;
- import algorithm.objectiveFunction.GraphMetrics.Graph;
- import api.TopologieAlgorithmFramework.IndexCable;
- public class TopologieObjectiveFunction {
- //Parameters
-
- //weight for f_g(H)
- static double w_eb = .3, w_max = 0.2, w_holon=.1, w_selection = .1, w_grid = .3;
-
-
- //--> f_eb parameter
- /**
- * Maximum Energie Difference(kappa)
- */
- static double k_eb = 100000.f;
- /**
- * Maximum when all on Energie Difference(kappa)
- */
- static double k_max = 100000.f;
-
- //--> f_holon parameter
- /**
- * maximum penalty from holon flexibilities
- */
- static double k_holon= 200000;
-
-
- //--> f_selection paramaeter;
- /**
- * average Maximum Cost for selction(kappa) of switch and elements.
- */
- static double k_selection = 4000;
-
- static double cost_switch = 10;
- private static int cost_of_cable_per_meter = 200;
- //--> f_grid parameter
- /**
- * The avergae shortest path maximum length -> kappa for the squash function
- */
- static double k_avg_shortest_path = 400;
-
- // Disjoint Path Parameter
- //Function1 Decreasing
- static double seperate_X_Value = 3.0;
- // Value between 0 and 100
- static double seperate_Y_Value = 10;
- //Stretching the e-function
- static double lowPenaltyGrowth = 3.0;
-
-
-
-
-
- //pre-calculated parameters for partial function terms:
- /**
- * Pre calculated for the squash function
- * <br>
- * {@link TopologieObjectiveFunction#squash}
- */
- static double squash_subtract = 1.0f / (1.f + (float) Math.exp(5.0));
-
-
- static {
- //init
- 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(w_eb + w_holon + w_selection + w_grid + w_max - 1) < 0.001)) {
- System.err.println("ParameterError in ObjectiveFunction: Sum of all weights 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 TopologieObjectiveFunction#squash}
- *
- *
- * @param state
- * @param moreInformation TODO
- * @return f_g value between 0 and 100
- */
- static public float getFitnessValueForState(DecoratedState state, int amountOfAddedSwitch, double addedCableMeters, boolean moreInformation) {
-
-
- //Calculate f_eb the penalty for unbalenced energy in the network
- double f_eb = 0;
- for(DecoratedNetwork net : state.getNetworkList()) {
- double netEnergyDifference = 0;
- netEnergyDifference += net.getConsumerList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
- netEnergyDifference += net.getConsumerSelfSuppliedList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
- netEnergyDifference += net.getSupplierList().stream().map(sup -> sup.getEnergyProducing() - sup.getEnergySelfConsuming()).reduce(0.f, Float::sum);
- //abs
- f_eb += Math.abs(netEnergyDifference);
- }
-
- double f_maximum = 0;
- for(DecoratedNetwork net : state.getNetworkList()) {
- final int timestep = state.getTimestepOfState();
- f_maximum += Math.abs(net.getConsumerList().stream().map(con -> con.getModel().getMaximumConsumptionPossible(timestep) - con.getModel().getMaximumProductionPossible(timestep)).reduce(0.f, Float::sum));
- f_maximum += Math.abs(net.getConsumerSelfSuppliedList().stream().map(con -> con.getModel().getMaximumConsumptionPossible(timestep) - con.getModel().getMaximumProductionPossible(timestep)).reduce(0.f, Float::sum));
- f_maximum += Math.abs(net.getSupplierList().stream().map(con -> con.getModel().getMaximumConsumptionPossible(timestep) - con.getModel().getMaximumProductionPossible(timestep)).reduce(0.f, Float::sum));
- }
-
-
- //calculate f_holon
- double f_holon = 0;
- for(DecoratedNetwork net : state.getNetworkList()) {
- double f_elements_diviation_production = net.getDiviationInProductionInNetworkForHolonObjects();
- double f_elements_diviation_consumption = net.getDiviationInProductionInNetworkForHolonObjects();
- double f_element = f_elements_diviation_production+f_elements_diviation_consumption;
- f_holon += f_element;
- }
-
- //calculating f_selection
- double f_selection = 0;
- double cost = 0;
- int amountOfElemetsInWildcard = 0;
- for(DecoratedNetwork net : state.getNetworkList()) {
- for(DecoratedHolonObject dHobject : net.getConsumerList()) {
- if(dHobject.getModel().getName().contains("Wildcard")){
- if(dHobject.getModel().getName().length() > 9) {
- String costString = dHobject.getModel().getName().substring(9);
- cost += Double.parseDouble(costString);
- }
- }
- }
- for(DecoratedHolonObject dHobject : net.getConsumerSelfSuppliedList()) {
- if(dHobject.getModel().getName().contains("Wildcard")){
- if(dHobject.getModel().getName().length() > 9) {
- String costString = dHobject.getModel().getName().substring(9);
- cost += Double.parseDouble(costString);
- }
- }
- }
- for(DecoratedHolonObject dHobject : net.getSupplierList()) {
- if(dHobject.getModel().getName().contains("Wildcard")){
- if(dHobject.getModel().getName().length() > 9) {
- String costString = dHobject.getModel().getName().substring(9);
- cost += Double.parseDouble(costString);
- }
- }
- }
- }
- f_selection += cost;
- f_selection += cost_switch * amountOfAddedSwitch;
-
-
- f_selection += cost_of_cable_per_meter * addedCableMeters;
- if(moreInformation)System.out.println("CostForWildcards:" + cost + ", CostSwitches(#" + amountOfAddedSwitch +"):" + cost_switch * amountOfAddedSwitch + ", CostCables(" +addedCableMeters+ "m):" + cost_of_cable_per_meter * addedCableMeters);
-
-
- //calculating f_grid
- double f_grid = 0;
- //each network is a holon
- for(DecoratedNetwork net: state.getNetworkList()) {
- Graph G = GraphMetrics.convertDecoratedNetworkToGraph(net);
- //We have to penalize single Networks;
- //100 is the maximum penalty for a holon/network
- if(G.V.length <= 1) {
- f_grid += 100;
- continue;
- }
-
-
- double avgShortestPath = GraphMetrics.averageShortestDistance(G.V, G.E);
- //k-edge-conneted
- //int maximumK = G.V.length - 1;
- int k = GraphMetrics.minimumCut(G.V, G.E);
- double penalty = disjoinPathPenalty(k);
-
- f_grid += 0.5 * squash(penalty, 100) + 0.5 *squash(avgShortestPath, k_avg_shortest_path);
- }
- //Average over all networks
- if(!state.getNetworkList().isEmpty()) {
- f_grid /= state.getNetworkList().size();
- }
-
-
- // System.out.println("f_grid:" + f_grid);
- // System.out.print(" f_eb(" + w_eb * squash(f_eb, k_eb) + ") ");
- // System.out.print(" f_holon(" + w_holon * squash(f_holon, k_holon) + ") ");
- // System.out.print(" f_selection(" + w_selection * squash(f_selection, k_selection) + ") ");
- // System.out.println(" f_grid(" + w_grid * f_grid + ") ");
-
- /**
- * F_grid is already squashed
- */
- return (float) (w_eb * squash(f_eb, k_eb)
- + w_max * squash(f_maximum, k_max)
- + w_holon * squash(f_holon, k_holon)
- + w_selection * squash(f_selection, k_selection)
- + w_grid * f_grid);
- }
- private static String doubleToString(double value) {
- return String.format (Locale.US, "%.2f", value);
- }
-
-
- private static double disjoinPathPenalty(double value) {
- //von 100 auf 10% bis seperateFunctionValue linear
- //Big Penalty
- if( value < seperate_X_Value) {
- return 100 - ((100 - seperate_Y_Value) / seperate_X_Value) * value;
- }
- //Low Penalty
- else {
- return seperate_Y_Value * Math.exp(lowPenaltyGrowth * (-value + seperate_X_Value));
- }
- }
-
-
- /**
- * The squashing function in paper
- * @param x the input
- * @param kappa the corresponding kappa
- * @return
- */
- static public double squash(double x, double kappa) {
- return 100.f/(1.0f + Math.exp(-(10.f * (x - kappa/2.f))/ kappa)) - squash_subtract;
- }
-
- /**
- * f_sup in paper
- * @param supply from 0 to 1
- * @return
- */
- static public double supplyPenalty(double supply) {
- double supplyPercentage = 100 * supply;
- return (supplyPercentage < 100) ? -0.5 * supplyPercentage + 50: supplyPercentage - 100;
- }
-
- }
|