Guest User

Powerplants

a guest
Dec 7th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.30 KB | None | 0 0
  1. public class PowerPlant {
  2.  
  3.     private String name;
  4.     private int maxPerformance = Integer.MAX_VALUE;
  5.     private int minPerformance = 0;
  6.     private boolean dimmable = true;
  7.     private int priority = 0;
  8.     private int[] typicalDay;
  9.     private int[] quarterCapacity = new int[DateUtilS.QUARTERS];
  10.     private double ratio;
  11.  
  12.     public PowerPlant() {}
  13.  
  14.     public int calculatedTypicalDayPerformanceSum() {
  15.         int performance = 0;
  16.         for (int i = 0; i < typicalDay.length; i++) {
  17.             performance += typicalDay[i];
  18.         }
  19.  
  20.         return performance;
  21.     }
  22.  
  23.     public int freeCapacity(int quarter) {
  24.         return maxPerformance - quarterCapacity[quarter];
  25.     }
  26.  
  27.     public int getMaxPerformance() {
  28.         return maxPerformance;
  29.     }
  30.  
  31.     public void setMaxPerformance(int maxPerformance) {
  32.         this.maxPerformance = maxPerformance;
  33.     }
  34.  
  35.     public int getMinPerformance() {
  36.         return minPerformance;
  37.     }
  38.  
  39.     public void setMinPerformance(int minPerformance) {
  40.         this.minPerformance = minPerformance;
  41.     }
  42.  
  43.     public boolean isDimmable() {
  44.         return dimmable;
  45.     }
  46.  
  47.     public void setDimmable(boolean dimmable) {
  48.         this.dimmable = dimmable;
  49.     }
  50.  
  51.     public int getPriority() {
  52.         return priority;
  53.     }
  54.  
  55.     public void setPriority(int priority) {
  56.         this.priority = priority;
  57.     }
  58.  
  59.     public void setTypicalDay(int[] typicalDay) {
  60.         this.typicalDay = typicalDay;
  61.     }
  62.  
  63.     public int[] getTypicalDay() {
  64.         return typicalDay;
  65.     }
  66.  
  67.     public double getRatio() {
  68.         return ratio;
  69.     }
  70.  
  71.     public void setRatio(double ratio) {
  72.         this.ratio = ratio;
  73.     }
  74.  
  75.     public int[] getQuarterCapacity() {
  76.         return quarterCapacity;
  77.     }
  78.  
  79.     public void setQuarterCapacity(int[] quarterCapacity) {
  80.         this.quarterCapacity = quarterCapacity;
  81.     }
  82.  
  83.     public void setQuarterCapacity(int quarter, int quarterCapacity) {
  84.         this.quarterCapacity[quarter] = quarterCapacity;
  85.     }
  86.  
  87.     public String getName() {
  88.         return name;
  89.     }
  90.  
  91.     public void setName(String name) {
  92.         this.name = name;
  93.     }
  94.  
  95.     @Override
  96.     public String toString() {
  97.         return name;
  98.     }
  99.  
  100. }
  101.  
  102. import com.google.common.primitives.Ints;
  103.  
  104. import java.util.ArrayList;
  105. import java.util.Collections;
  106. import java.util.Comparator;
  107. import java.util.List;
  108.  
  109. public class PowerPlantGrid {
  110.  
  111.     private final List<PowerPlant> plants;
  112.     private final int[] requiredEnergy;
  113.     private List<PowerPlant> requiredPlants;
  114.  
  115.  
  116.     public PowerPlantGrid(List<PowerPlant> plants, int[] requiredEnergy) {
  117.  
  118.         this.plants = plants;
  119.         this.requiredEnergy = requiredEnergy;
  120.     }
  121.  
  122.     public void init() {
  123.         sortPlantsByTypicalDayPerformance(plants);
  124.         requiredPlants = calculateRequiredPlants(calculateDailyMaxEnergy());
  125.  
  126.     }
  127.     public void sortPlantsByTypicalDayPerformance(List<PowerPlant> plants) {
  128.         Collections.sort(plants, new Comparator<PowerPlant>() {
  129.             @Override
  130.             public int compare(PowerPlant plant, PowerPlant plant2)
  131.             {
  132.  
  133.                 return -Integer.compare(plant.calculatedTypicalDayPerformanceSum(), plant2.calculatedTypicalDayPerformanceSum());
  134.             }
  135.         });
  136.     }
  137.  
  138.     public int calculateDailyMaxEnergy() {
  139.         return Ints.max(requiredEnergy);
  140.     }
  141.  
  142.     public int calculateAllRequiredPlantsMinPerformance(List<PowerPlant> plants) {
  143.         int minPerformance = 0;
  144.         for (final PowerPlant plant : plants) {
  145.             minPerformance += plant.getMinPerformance();
  146.         }
  147.         return minPerformance;
  148.     }
  149.  
  150.     public List<PowerPlant> calculateRequiredPlants(int dailyMaxPower) {
  151.         final List<PowerPlant> requiredPlants = new ArrayList<PowerPlant>();
  152.         int supplied = 0;
  153.         for (final PowerPlant plant : plants) {
  154.             supplied += plant.getMaxPerformance();
  155.             requiredPlants.add(plant);
  156.             if (supplied >= dailyMaxPower) {
  157.                 return requiredPlants;
  158.             }
  159.         }
  160.  
  161.         return requiredPlants;
  162.     }
  163.  
  164.     public void allocateRequiredEnergy(List<PowerPlant> requiredPlants, int[] requiredEnergy) {
  165.  
  166.         final int calculateAllRequiredPlantsMinPerformance = calculateAllRequiredPlantsMinPerformance(requiredPlants);
  167.         for (int quarter = 0; quarter < requiredEnergy.length; quarter++) {
  168.             final int required = requiredEnergy[quarter];
  169.             int supplied = calculateAllRequiredPlantsMinPerformance;
  170.             int oldSupplied = -1;
  171.  
  172.             fillQuarterWithMinPerformance(requiredPlants, quarter);
  173.             if (supplied >= required) {
  174.                 continue;
  175.             } else {
  176.  
  177.                 while (supplied <= required && supplied > oldSupplied) {
  178.                     setPlantsRatio(requiredPlants, quarter);
  179.                     oldSupplied = supplied;
  180.                     for (final PowerPlant plant : plants) {
  181.                         if (plant.freeCapacity(quarter) > 0) {
  182.                             int p = (int) ((required - supplied) * plant.getRatio());
  183.                             if (p >= plant.freeCapacity(quarter)) {
  184.                                 p = plant.freeCapacity(quarter);
  185.                             }
  186.                             plant.setQuarterCapacity(quarter, plant.getQuarterCapacity()[quarter] + p);
  187.                             supplied += p;
  188.                         }
  189.                     }
  190.                 }
  191.  
  192.                 if (supplied < required && plantsFreeCapacity(requiredPlants, quarter) > 0) {
  193.                     rounding(requiredPlants, quarter, required - supplied);
  194.                 }
  195.             }
  196.         }
  197.     }
  198.  
  199.  
  200.     private void setPlantsRatio(List<PowerPlant> plants, int quarter) {
  201.         final double gridTypicalCapacity = calculateGridTypicalCapacity(plants, quarter);
  202.  
  203.         for (final PowerPlant plant : plants) {
  204.             plant.setRatio(plant.getTypicalDay()[quarter] * 1.0 / gridTypicalCapacity);
  205.         }
  206.  
  207.     }
  208.  
  209.     private int calculateGridTypicalCapacity(List<PowerPlant> plants, int quarter) {
  210.         int gridTypicalCapacity = 0;
  211.         for (final PowerPlant plant : plants) {
  212.             if (plant.freeCapacity(quarter) > 0) {
  213.                 gridTypicalCapacity += plant.getTypicalDay()[quarter];
  214.             }
  215.         }
  216.         return gridTypicalCapacity;
  217.     }
  218.  
  219.     private int plantsFreeCapacity(List<PowerPlant> plants, int quarter) {
  220.         int freeCapacity = 0;
  221.         for (final PowerPlant plant : plants) {
  222.             freeCapacity += plant.freeCapacity(quarter);
  223.         }
  224.         return freeCapacity;
  225.     }
  226.  
  227.     private void rounding(List<PowerPlant> plants, int quarter, int rest) {
  228.         for (final PowerPlant plant : plants) {
  229.             final int free = plant.freeCapacity(quarter);
  230.             if (free >= rest) {
  231.                 plant.getQuarterCapacity()[quarter] += rest;
  232.                 return;
  233.             }
  234.             if (free > 0) {
  235.                 plant.getQuarterCapacity()[quarter] += free;
  236.                 rest -= free;
  237.             }
  238.  
  239.         }
  240.     }
  241.  
  242.     private void fillQuarterWithMinPerformance(List<PowerPlant> plants, int i) {
  243.         for (final PowerPlant plant : plants) {
  244.             plant.getQuarterCapacity()[i] = plant.getMinPerformance();
  245.         }
  246.  
  247.     }
  248.  
  249.     public int[] getRequiredEnergy() {
  250.         return requiredEnergy;
  251.     }
  252.  
  253.     public List<PowerPlant> getRequiredPlants() {
  254.         return requiredPlants;
  255.     }
  256.  
  257.  
  258. }
  259.  
  260. import java.util.ArrayList;
  261. import java.util.Arrays;
  262. import java.util.List;
  263.  
  264. public class TypicalDayTest {
  265.  
  266.     private static final double POWERPLANT_2_MULTIPLIER = 1.5;
  267.     private static final double POWERPLANT_3_MULTIPLIER = 2;
  268.     static int POWERPLANT_1_TYPICALDAY[];
  269.     static int POWERPLANT_2_TYPICALDAY[] = new int[96];
  270.     static int POWERPLANT_3_TYPICALDAY[] = new int[96];
  271.  
  272.     //@formatter:off
  273.                 final static int[] required = new int[] {
  274.                         5825,   5825,   5625,   6315,   6161,   6154,   6304,   5660,   6302,   5418,   5382,   5623,   6239,   6262,   5713,   6012,   5464,   5924,   5934,   5618,   6320,   6262,   5710,   5777,   5782,   5370,   5363,   6032,   5913,
  275.                         6299,   5761,   5848,   5416,   6534,   6601,   7265,   7270,   7615,   7640,   7820,   8454,   8535,   9009,   9108,   8217,   8756,   8158,   8648,   8372,   9045,   8749,   8227,   8730,   8832,   8583,   8652,   8878,   8572,
  276.                         8404,   8102,   8652,   9183,   8181,   8266,   8535,   8700,   8121,   8257,   8857,   8121,   9052,   8415,   8112,   7707,   7840,   7728,   6863,   6387,   6893,   5908,   6251,   6239,   6276,   5563,   5894,   5331,   5816,
  277.                         5860,   5568,   6189,   6035,   5911,   5241,   5989,   5225,   6200
  278.                 };
  279.  
  280.  
  281.             private static void initPowerPlants() {
  282.                 POWERPLANT_1_TYPICALDAY = new int[] {
  283.                         2011,   3840,   2446,   2746,   2679,   2676,   2741,   2461,   2740,   2356,   2340,   2445,   2713,   2723,   2484,   2614,   2376,   2576,   2580,   2443,   2748,   2723,   2483,   2512,   2514,   2335,   2332,   2623,   2571,
  284.                         2739,   2505,   2543,   2355,   2841,   2870,   3159,   3161,   3311,   3322,   3400,   3676,   3711,   3917,   3960,   3573,   3807,   3547,   3760,   3640,   3933,   3804,   3577,   3796,   3840,   3732,   3762,   3860,   3727,
  285.                         3654,   3523,   3762,   3993,   3557,   3594,   3711,   3783,   3531,   3590,   3851,   3531,   3936,   3659,   3527,   3351,   3409,   3360,   2984,   2777,   2997,   2569,   2718,   2713,   2729,   2419,   2563,   2318,   2529,
  286.                         2548,   2421,   2691,   2624,   2570,   2279,   2604,   2272,   2696
  287.             };
  288.                
  289.                 for (int i= 0; i < POWERPLANT_1_TYPICALDAY.length; i++) {
  290.                     final int data = POWERPLANT_1_TYPICALDAY[i];
  291.                     POWERPLANT_2_TYPICALDAY[i] = (int) (data * POWERPLANT_2_MULTIPLIER);
  292.                     POWERPLANT_3_TYPICALDAY[i] = (int) (data * POWERPLANT_3_MULTIPLIER);
  293.                 }
  294.             }
  295.            
  296.             public static void main(String[] args) {
  297.                  initPowerPlants();
  298.                 final PowerPlant plant1 = new PowerPlant();
  299.                 plant1.setName("PLANT1");
  300.                 plant1.setMaxPerformance(4000);
  301.                 plant1.setTypicalDay(POWERPLANT_1_TYPICALDAY);
  302.                 final PowerPlant plant2 = new PowerPlant();
  303.                 plant2.setName("PLANT2");
  304.                 plant2.setTypicalDay(POWERPLANT_2_TYPICALDAY);
  305.                 plant2.setMaxPerformance(3000);
  306.                
  307.                 final List<PowerPlant>plants = new ArrayList<PowerPlant>();
  308.                 plants.add(plant1);
  309.                 plants.add(plant2);
  310.                 final PowerPlantGrid plantGrid = new PowerPlantGrid(plants, required);
  311.                
  312.                 plantGrid.init();
  313.                 plantGrid.allocateRequiredEnergy(plantGrid.getRequiredPlants(), plantGrid.getRequiredEnergy());
  314.                 System.out.println("Szükséges:" + Arrays.toString(required));
  315.                 System.out.println(plant1.getName() +"\t" + Arrays.toString(plant1.getQuarterCapacity()));
  316.                 System.out.println(plant2.getName() +"\t" + Arrays.toString(plant2.getQuarterCapacity()));
  317.             }
  318.  
  319. }
Advertisement
Add Comment
Please, Sign In to add comment