Compare commits

...

3 Commits

Author SHA1 Message Date
222e5e1dd3 better logging output 2021-04-14 22:20:41 +02:00
51b080e70c mutiple carGrids 2021-04-14 21:06:17 +02:00
97f2e5d0f1 simplified carTemplate 2021-03-29 15:49:24 +02:00
4 changed files with 88 additions and 47 deletions

View File

@ -1,35 +1,31 @@
package de.mrgeorgen.v2g;
public class car {
private int battery;
private final int fullBattery;
private int chargeLock;
private final int chargeSpeed;
private final String model;
private final int id;
public final carTemplate carModel;
public int battery;
private int chargeLock;
public car(String model, int fullBattery, int chargeSpeed, int id) { // id can be the same for different models
this.fullBattery = 1000 * fullBattery; // convert kWh to Wh
this.chargeSpeed = chargeSpeed * 1000; // convert kW to W
this.setChargeLock(0.6);
this.id = id;
this.model = model;
this.battery = this.fullBattery / 2;
public car(carTemplate carModel) { // id can be the same for different models
this.carModel = carModel;
setChargeLock(0.6);
id = powerGrid.idCounter++;
battery = carModel.fullBattery / 2;
}
public void charge() {
int chargeAmmount = Math.abs(powerGrid.energieAvailable) < this.chargeSpeed ? powerGrid.energieAvailable : powerGrid.energieAvailable > 0 ? this.chargeSpeed : -1 * this.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.fullBattery) chargeAmmount = this.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.model + " nr. " + this.id + " is " + (chargeAmmount < 0 ? "dis" : "") + "charging with " + (double)Math.abs(chargeAmmount) / 1000 + " kW. battery: " + (double)this.battery / 1000 + "/" + (double)this.fullBattery / 1000 + " kWh (" + Math.round(getBatteryRelativ() * 100) + "%)");
this.battery += chargeAmmount;
powerGrid.energieAvailable -= chargeAmmount;
public int charge(int maxCharge) {
int chargeAmmount = Math.abs(maxCharge) < carModel.chargeSpeed ? maxCharge : maxCharge > 0 ? carModel.chargeSpeed : -1 * carModel.chargeSpeed;
if(battery + chargeAmmount <= chargeLock && chargeAmmount < 0) chargeAmmount = (battery - chargeLock) * -1; // do not charge if the car gets under the charge lock
if(battery + chargeAmmount > carModel.fullBattery) chargeAmmount = carModel.fullBattery - battery; // prevent the battery from overcharging
if(battery + chargeAmmount < 0) chargeAmmount = battery * -1; // prevent the battery from dischargingunder 0%
if(powerGrid.logLevel >= 3 && chargeAmmount != 0) System.out.println(carModel.model + " id. " + id + " is " + (chargeAmmount < 0 ? "dis" : "") + "charging with " + (double)Math.abs(chargeAmmount) / 1000 + " kW. battery: " + (double)battery / 1000 + "/" + (double)carModel.fullBattery / 1000 + " kWh (" + Math.round(getBatteryRelativ() * 100) + "%)");
battery += chargeAmmount;
if(chargeAmmount < 0) powerGrid.savedEnergie += Math.abs(chargeAmmount);
return chargeAmmount;
}
private void setChargeLock(double chargeLock) {
this.chargeLock = (int)(this.fullBattery * chargeLock);
chargeLock = (int)(carModel.fullBattery * chargeLock);
}
public double getBatteryRelativ() {
return (double)this.battery / this.fullBattery;
return (double)battery / carModel.fullBattery;
}
}

View File

@ -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 {
private final int id;
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(int id) {
this.id = id;
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.model, carModel.fullBattery, carModel.chargeSpeed, i));
dockedCars.add(new car(carModel));
}
}
}
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,26 @@ 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);
}
if(powerGrid.logLevel >= 2 && charged != 0) System.out.println("carGrid " + id + " " + (charged < 0 ? "dis" : "") + "charged " + dockedCars.size() + " cars with " + (double)Math.abs(charged) / 1000 + " kW");
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 / capacityDockedCars();
}
}

View File

@ -7,9 +7,9 @@ public class carTemplate {
public final int range;
public carTemplate(String model, int fullBattery, int chargeSpeed, int range) {
this.fullBattery = 1000 * fullBattery; // convert kWh to Wh
this.chargeSpeed = chargeSpeed * 1000; // convert kW to W
this.model = model;
this.chargeSpeed = chargeSpeed;
this.fullBattery = fullBattery;
this.range = range;
}
}

View File

@ -1,28 +1,53 @@
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;
public static int idCounter;
public static int logLevel = 1;
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();
final int hourSimulationRuns = Integer.parseInt(args[0]) * 24;
Random random = new Random();
ArrayList<car> allCars = new ArrayList<car>();
ArrayList<carGrid> carGrids = new ArrayList<carGrid>();
final int numberOfCarGrids = 2 + random.nextInt(10);
for(int i = 0; i < numberOfCarGrids; ++i) {
final carGrid carGrid = new carGrid(i);
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);
if(logLevel >= 2) 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
energieAvailable = (int)(averageEnergie * (0.5 + 1.5 * random.nextDouble()));
carGrid.chargeCars();
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
int energieAvailable = (int)(averageEnergie * (0.5 + 1.5 * random.nextDouble()));
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(logLevel >= 1) {
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 and " + carGrids.size() + " carGrids");
}
}