mutiple carGrids
This commit is contained in:
@ -2,8 +2,8 @@ package de.mrgeorgen.v2g;
|
||||
|
||||
public class car {
|
||||
private final int id;
|
||||
private final carTemplate carModel;
|
||||
private int battery;
|
||||
public final carTemplate carModel;
|
||||
public int battery;
|
||||
private int chargeLock;
|
||||
|
||||
public car(carTemplate carModel, int id) { // id can be the same for different models
|
||||
@ -12,15 +12,15 @@ public class car {
|
||||
this.id = id;
|
||||
this.battery = this.carModel.fullBattery / 2;
|
||||
}
|
||||
public void charge() {
|
||||
int chargeAmmount = Math.abs(powerGrid.energieAvailable) < this.carModel.chargeSpeed ? powerGrid.energieAvailable : powerGrid.energieAvailable > 0 ? this.carModel.chargeSpeed : -1 * this.carModel.chargeSpeed;
|
||||
public int charge(int maxCharge) {
|
||||
int chargeAmmount = Math.abs(maxCharge) < this.carModel.chargeSpeed ? maxCharge : maxCharge > 0 ? this.carModel.chargeSpeed : -1 * this.carModel.chargeSpeed;
|
||||
if(this.battery + chargeAmmount <= this.chargeLock && chargeAmmount < 0) chargeAmmount = (this.battery - this.chargeLock) * -1; // do not charge if the car gets under the charge lock
|
||||
if(this.battery + chargeAmmount > this.carModel.fullBattery) chargeAmmount = this.carModel.fullBattery - this.battery; // prevent the battery from overcharging
|
||||
if(this.battery + chargeAmmount < 0) chargeAmmount = this.battery * -1; // prevent the battery from dischargingunder 0%
|
||||
if(chargeAmmount != 0) System.out.println(this.carModel.model + " nr. " + this.id + " is " + (chargeAmmount < 0 ? "dis" : "") + "charging with " + (double)Math.abs(chargeAmmount) / 1000 + " kW. battery: " + (double)this.battery / 1000 + "/" + (double)this.carModel.fullBattery / 1000 + " kWh (" + Math.round(getBatteryRelativ() * 100) + "%)");
|
||||
this.battery += chargeAmmount;
|
||||
powerGrid.energieAvailable -= chargeAmmount;
|
||||
if(chargeAmmount < 0) powerGrid.savedEnergie += Math.abs(chargeAmmount);
|
||||
return chargeAmmount;
|
||||
}
|
||||
private void setChargeLock(double chargeLock) {
|
||||
this.chargeLock = (int)(this.carModel.fullBattery * chargeLock);
|
||||
|
||||
@ -1,10 +1,14 @@
|
||||
package de.mrgeorgen.v2g;
|
||||
import java.lang.Double;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.lang.Double;
|
||||
import java.util.Random;
|
||||
|
||||
import de.mrgeorgen.v2g.car;
|
||||
import de.mrgeorgen.v2g.carGrid;
|
||||
public class carGrid {
|
||||
public int energieAvailable;
|
||||
public ArrayList<car> dockedCars = new ArrayList<car>();
|
||||
private final carTemplate[] models = {new carTemplate("Tesla Model 3", 50, 160, 335),
|
||||
new carTemplate("Renault Zoe ZE50", 52, 46, 315),
|
||||
@ -14,16 +18,17 @@ public class carGrid {
|
||||
new carTemplate("Tesla Model S Long Range", 90, 250, 555),
|
||||
new carTemplate("Smart EQ forfour", 17, 5, 95),
|
||||
new carTemplate("Honda e", 29, 56, 170)};
|
||||
public void fillWithCars() {
|
||||
public carGrid() {
|
||||
Random random = new Random();
|
||||
for(carTemplate carModel : models) {
|
||||
final int numberOfCars = random.nextInt(10);
|
||||
for(int i = 0; i < numberOfCars; ++i) {
|
||||
dockedCars.add(new car(carModel, i));
|
||||
final car car = new car(carModel, i);
|
||||
dockedCars.add(car);
|
||||
}
|
||||
}
|
||||
}
|
||||
public void chargeCars() {
|
||||
public int chargeCars(int maxCharge) {
|
||||
Collections.sort(dockedCars, new Comparator<car>() {
|
||||
public int compare(car car1, car car2) {
|
||||
Double car1RelativBattery = car1.getBatteryRelativ();
|
||||
@ -31,11 +36,25 @@ public class carGrid {
|
||||
return car1RelativBattery.compareTo(car2RelativBattery);
|
||||
}
|
||||
});
|
||||
if(powerGrid.energieAvailable < 0) Collections.reverse(dockedCars);
|
||||
dockedCars.forEach((car car) -> {
|
||||
car.charge();
|
||||
});
|
||||
if(powerGrid.energieAvailable > 0) System.out.println(powerGrid.energieAvailable + " W could not be used by the cars");
|
||||
else if(powerGrid.energieAvailable < 0) System.out.println(Math.abs(powerGrid.energieAvailable) + " W could not be taken from the cars");
|
||||
if(maxCharge < 0) Collections.reverse(dockedCars);
|
||||
int charged = 0;
|
||||
for(car car : dockedCars) {
|
||||
charged += car.charge(maxCharge - charged);
|
||||
}
|
||||
return charged;
|
||||
}
|
||||
public int capacityDockedCars() {
|
||||
int maxBattery = 0;
|
||||
for(car car : dockedCars) {
|
||||
maxBattery += car.carModel.fullBattery;
|
||||
}
|
||||
return maxBattery;
|
||||
}
|
||||
public double relativeChargeState() {
|
||||
int battery = 0;
|
||||
for(car car : dockedCars) {
|
||||
battery += car.battery;
|
||||
}
|
||||
return ((double)battery + energieAvailable) / capacityDockedCars();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,30 +1,50 @@
|
||||
package de.mrgeorgen.v2g;
|
||||
import java.util.Random;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.lang.Math;
|
||||
public class powerGrid {
|
||||
public static int energieAvailable;
|
||||
public static int savedEnergie;
|
||||
private static ArrayList<car> allCars = new ArrayList<car>();
|
||||
private static ArrayList<carGrid> carGrids = new ArrayList<carGrid>();
|
||||
public static void main(String args[]) {
|
||||
if(args.length != 1) {
|
||||
System.out.println("Invalid Syntax. Use the number of days the simulation shell run as the first argument");
|
||||
return;
|
||||
}
|
||||
final carGrid carGrid = new carGrid();
|
||||
carGrid.fillWithCars();
|
||||
car[] allCars = new car[carGrid.dockedCars.size()];
|
||||
allCars = carGrid.dockedCars.toArray(allCars);
|
||||
final int hourSimulationRuns = Integer.parseInt(args[0]) * 24;
|
||||
Random random = new Random();
|
||||
final int numberOfCarGrids = 2 + random.nextInt(10);
|
||||
for(int i = 0; i < numberOfCarGrids; ++i) {
|
||||
final carGrid carGrid = new carGrid();
|
||||
carGrids.add(carGrid);
|
||||
allCars.addAll(carGrid.dockedCars);
|
||||
}
|
||||
final int hourSimulationRuns = Integer.parseInt(args[0]) * 24;
|
||||
for(int houresPassed = 0; houresPassed < hourSimulationRuns; ++houresPassed) {
|
||||
final int hourOfDay = houresPassed % 24;
|
||||
System.out.println("Day " + houresPassed / 24 + " Hour " + hourOfDay);
|
||||
int averageEnergie;
|
||||
if(hourOfDay < 6 || hourOfDay > 22) averageEnergie = carGrid.dockedCars.size() * 3000; // energie available at night
|
||||
else if(hourOfDay < 7) averageEnergie = carGrid.dockedCars.size() * -1000; // energie for light in the morning
|
||||
else if(hourOfDay < 19) averageEnergie = carGrid.dockedCars.size() * -500; // at the day energie is still needed but not as much because the lights are not on
|
||||
else /* hourOfDay > 19 && hourOfDay < 23 */ averageEnergie = carGrid.dockedCars.size() * -1000; // the lights are on again
|
||||
if(hourOfDay < 6 || hourOfDay > 22) averageEnergie = allCars.size() * 3000; // energie available at night
|
||||
else if(hourOfDay < 7) averageEnergie = allCars.size() * -1000; // energie for light in the morning
|
||||
else if(hourOfDay < 19) averageEnergie = allCars.size() * -500; // at the day energie is still needed but not as much because the lights are not on
|
||||
else /* hourOfDay > 19 && hourOfDay < 23 */ averageEnergie = allCars.size() * -1000; // the lights are on again
|
||||
energieAvailable = (int)(averageEnergie * (0.5 + 1.5 * random.nextDouble()));
|
||||
carGrid.chargeCars();
|
||||
Collections.sort(carGrids, new Comparator<carGrid>() {
|
||||
public int compare(carGrid carGrid1, carGrid carGrid2) {
|
||||
Double carGrid1RelativChargeState = carGrid1.relativeChargeState();
|
||||
Double carGrid2RelativBattery = carGrid2.relativeChargeState();
|
||||
return carGrid1RelativChargeState.compareTo(carGrid2RelativBattery);
|
||||
}
|
||||
});
|
||||
if(energieAvailable < 0) Collections.reverse(carGrids);
|
||||
for(carGrid carGrid : carGrids) {
|
||||
energieAvailable -= carGrid.chargeCars(energieAvailable);
|
||||
}
|
||||
if(energieAvailable > 0) System.out.println(energieAvailable + " W could not be used by the cars");
|
||||
else if(energieAvailable < 0) System.out.println(Math.abs(energieAvailable) + " W could not be taken from the cars");
|
||||
}
|
||||
System.out.println("vehicle to grid saved " + (double)savedEnergie / 1000 + " kWh with " + carGrid.dockedCars.size() + " cars");
|
||||
System.out.println("vehicle to grid saved " + (double)savedEnergie / 1000 + " kWh with " + allCars.size() + " cars");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user