Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PowerPlant {
- private String name;
- private int maxPerformance = Integer.MAX_VALUE;
- private int minPerformance = 0;
- private boolean dimmable = true;
- private int priority = 0;
- private int[] typicalDay;
- private int[] quarterCapacity = new int[DateUtilS.QUARTERS];
- private double ratio;
- public PowerPlant() {}
- public int calculatedTypicalDayPerformanceSum() {
- int performance = 0;
- for (int i = 0; i < typicalDay.length; i++) {
- performance += typicalDay[i];
- }
- return performance;
- }
- public int freeCapacity(int quarter) {
- return maxPerformance - quarterCapacity[quarter];
- }
- public int getMaxPerformance() {
- return maxPerformance;
- }
- public void setMaxPerformance(int maxPerformance) {
- this.maxPerformance = maxPerformance;
- }
- public int getMinPerformance() {
- return minPerformance;
- }
- public void setMinPerformance(int minPerformance) {
- this.minPerformance = minPerformance;
- }
- public boolean isDimmable() {
- return dimmable;
- }
- public void setDimmable(boolean dimmable) {
- this.dimmable = dimmable;
- }
- public int getPriority() {
- return priority;
- }
- public void setPriority(int priority) {
- this.priority = priority;
- }
- public void setTypicalDay(int[] typicalDay) {
- this.typicalDay = typicalDay;
- }
- public int[] getTypicalDay() {
- return typicalDay;
- }
- public double getRatio() {
- return ratio;
- }
- public void setRatio(double ratio) {
- this.ratio = ratio;
- }
- public int[] getQuarterCapacity() {
- return quarterCapacity;
- }
- public void setQuarterCapacity(int[] quarterCapacity) {
- this.quarterCapacity = quarterCapacity;
- }
- public void setQuarterCapacity(int quarter, int quarterCapacity) {
- this.quarterCapacity[quarter] = quarterCapacity;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return name;
- }
- }
- import com.google.common.primitives.Ints;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- public class PowerPlantGrid {
- private final List<PowerPlant> plants;
- private final int[] requiredEnergy;
- private List<PowerPlant> requiredPlants;
- public PowerPlantGrid(List<PowerPlant> plants, int[] requiredEnergy) {
- this.plants = plants;
- this.requiredEnergy = requiredEnergy;
- }
- public void init() {
- sortPlantsByTypicalDayPerformance(plants);
- requiredPlants = calculateRequiredPlants(calculateDailyMaxEnergy());
- }
- public void sortPlantsByTypicalDayPerformance(List<PowerPlant> plants) {
- Collections.sort(plants, new Comparator<PowerPlant>() {
- @Override
- public int compare(PowerPlant plant, PowerPlant plant2)
- {
- return -Integer.compare(plant.calculatedTypicalDayPerformanceSum(), plant2.calculatedTypicalDayPerformanceSum());
- }
- });
- }
- public int calculateDailyMaxEnergy() {
- return Ints.max(requiredEnergy);
- }
- public int calculateAllRequiredPlantsMinPerformance(List<PowerPlant> plants) {
- int minPerformance = 0;
- for (final PowerPlant plant : plants) {
- minPerformance += plant.getMinPerformance();
- }
- return minPerformance;
- }
- public List<PowerPlant> calculateRequiredPlants(int dailyMaxPower) {
- final List<PowerPlant> requiredPlants = new ArrayList<PowerPlant>();
- int supplied = 0;
- for (final PowerPlant plant : plants) {
- supplied += plant.getMaxPerformance();
- requiredPlants.add(plant);
- if (supplied >= dailyMaxPower) {
- return requiredPlants;
- }
- }
- return requiredPlants;
- }
- public void allocateRequiredEnergy(List<PowerPlant> requiredPlants, int[] requiredEnergy) {
- final int calculateAllRequiredPlantsMinPerformance = calculateAllRequiredPlantsMinPerformance(requiredPlants);
- for (int quarter = 0; quarter < requiredEnergy.length; quarter++) {
- final int required = requiredEnergy[quarter];
- int supplied = calculateAllRequiredPlantsMinPerformance;
- int oldSupplied = -1;
- fillQuarterWithMinPerformance(requiredPlants, quarter);
- if (supplied >= required) {
- continue;
- } else {
- while (supplied <= required && supplied > oldSupplied) {
- setPlantsRatio(requiredPlants, quarter);
- oldSupplied = supplied;
- for (final PowerPlant plant : plants) {
- if (plant.freeCapacity(quarter) > 0) {
- int p = (int) ((required - supplied) * plant.getRatio());
- if (p >= plant.freeCapacity(quarter)) {
- p = plant.freeCapacity(quarter);
- }
- plant.setQuarterCapacity(quarter, plant.getQuarterCapacity()[quarter] + p);
- supplied += p;
- }
- }
- }
- if (supplied < required && plantsFreeCapacity(requiredPlants, quarter) > 0) {
- rounding(requiredPlants, quarter, required - supplied);
- }
- }
- }
- }
- private void setPlantsRatio(List<PowerPlant> plants, int quarter) {
- final double gridTypicalCapacity = calculateGridTypicalCapacity(plants, quarter);
- for (final PowerPlant plant : plants) {
- plant.setRatio(plant.getTypicalDay()[quarter] * 1.0 / gridTypicalCapacity);
- }
- }
- private int calculateGridTypicalCapacity(List<PowerPlant> plants, int quarter) {
- int gridTypicalCapacity = 0;
- for (final PowerPlant plant : plants) {
- if (plant.freeCapacity(quarter) > 0) {
- gridTypicalCapacity += plant.getTypicalDay()[quarter];
- }
- }
- return gridTypicalCapacity;
- }
- private int plantsFreeCapacity(List<PowerPlant> plants, int quarter) {
- int freeCapacity = 0;
- for (final PowerPlant plant : plants) {
- freeCapacity += plant.freeCapacity(quarter);
- }
- return freeCapacity;
- }
- private void rounding(List<PowerPlant> plants, int quarter, int rest) {
- for (final PowerPlant plant : plants) {
- final int free = plant.freeCapacity(quarter);
- if (free >= rest) {
- plant.getQuarterCapacity()[quarter] += rest;
- return;
- }
- if (free > 0) {
- plant.getQuarterCapacity()[quarter] += free;
- rest -= free;
- }
- }
- }
- private void fillQuarterWithMinPerformance(List<PowerPlant> plants, int i) {
- for (final PowerPlant plant : plants) {
- plant.getQuarterCapacity()[i] = plant.getMinPerformance();
- }
- }
- public int[] getRequiredEnergy() {
- return requiredEnergy;
- }
- public List<PowerPlant> getRequiredPlants() {
- return requiredPlants;
- }
- }
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class TypicalDayTest {
- private static final double POWERPLANT_2_MULTIPLIER = 1.5;
- private static final double POWERPLANT_3_MULTIPLIER = 2;
- static int POWERPLANT_1_TYPICALDAY[];
- static int POWERPLANT_2_TYPICALDAY[] = new int[96];
- static int POWERPLANT_3_TYPICALDAY[] = new int[96];
- //@formatter:off
- final static int[] required = new int[] {
- 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,
- 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,
- 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,
- 5860, 5568, 6189, 6035, 5911, 5241, 5989, 5225, 6200
- };
- private static void initPowerPlants() {
- POWERPLANT_1_TYPICALDAY = new int[] {
- 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,
- 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,
- 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,
- 2548, 2421, 2691, 2624, 2570, 2279, 2604, 2272, 2696
- };
- for (int i= 0; i < POWERPLANT_1_TYPICALDAY.length; i++) {
- final int data = POWERPLANT_1_TYPICALDAY[i];
- POWERPLANT_2_TYPICALDAY[i] = (int) (data * POWERPLANT_2_MULTIPLIER);
- POWERPLANT_3_TYPICALDAY[i] = (int) (data * POWERPLANT_3_MULTIPLIER);
- }
- }
- public static void main(String[] args) {
- initPowerPlants();
- final PowerPlant plant1 = new PowerPlant();
- plant1.setName("PLANT1");
- plant1.setMaxPerformance(4000);
- plant1.setTypicalDay(POWERPLANT_1_TYPICALDAY);
- final PowerPlant plant2 = new PowerPlant();
- plant2.setName("PLANT2");
- plant2.setTypicalDay(POWERPLANT_2_TYPICALDAY);
- plant2.setMaxPerformance(3000);
- final List<PowerPlant>plants = new ArrayList<PowerPlant>();
- plants.add(plant1);
- plants.add(plant2);
- final PowerPlantGrid plantGrid = new PowerPlantGrid(plants, required);
- plantGrid.init();
- plantGrid.allocateRequiredEnergy(plantGrid.getRequiredPlants(), plantGrid.getRequiredEnergy());
- System.out.println("Szükséges:" + Arrays.toString(required));
- System.out.println(plant1.getName() +"\t" + Arrays.toString(plant1.getQuarterCapacity()));
- System.out.println(plant2.getName() +"\t" + Arrays.toString(plant2.getQuarterCapacity()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment