better logging output

This commit is contained in:
2021-04-14 22:20:41 +02:00
parent 51b080e70c
commit 222e5e1dd3
3 changed files with 30 additions and 26 deletions

View File

@ -6,26 +6,26 @@ public class car {
public int battery; public int battery;
private int chargeLock; private int chargeLock;
public car(carTemplate carModel, int id) { // id can be the same for different models public car(carTemplate carModel) { // id can be the same for different models
this.carModel = carModel; this.carModel = carModel;
this.setChargeLock(0.6); setChargeLock(0.6);
this.id = id; id = powerGrid.idCounter++;
this.battery = this.carModel.fullBattery / 2; battery = carModel.fullBattery / 2;
} }
public int charge(int maxCharge) { public int charge(int maxCharge) {
int chargeAmmount = Math.abs(maxCharge) < this.carModel.chargeSpeed ? maxCharge : maxCharge > 0 ? this.carModel.chargeSpeed : -1 * this.carModel.chargeSpeed; int chargeAmmount = Math.abs(maxCharge) < carModel.chargeSpeed ? maxCharge : maxCharge > 0 ? carModel.chargeSpeed : -1 * 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(battery + chargeAmmount <= chargeLock && chargeAmmount < 0) chargeAmmount = (battery - 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(battery + chargeAmmount > carModel.fullBattery) chargeAmmount = carModel.fullBattery - battery; // prevent the battery from overcharging
if(this.battery + chargeAmmount < 0) chargeAmmount = this.battery * -1; // prevent the battery from dischargingunder 0% if(battery + chargeAmmount < 0) chargeAmmount = 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) + "%)"); 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) + "%)");
this.battery += chargeAmmount; battery += chargeAmmount;
if(chargeAmmount < 0) powerGrid.savedEnergie += Math.abs(chargeAmmount); if(chargeAmmount < 0) powerGrid.savedEnergie += Math.abs(chargeAmmount);
return chargeAmmount; return chargeAmmount;
} }
private void setChargeLock(double chargeLock) { private void setChargeLock(double chargeLock) {
this.chargeLock = (int)(this.carModel.fullBattery * chargeLock); chargeLock = (int)(carModel.fullBattery * chargeLock);
} }
public double getBatteryRelativ() { public double getBatteryRelativ() {
return (double)this.battery / this.carModel.fullBattery; return (double)battery / carModel.fullBattery;
} }
} }

View File

@ -8,7 +8,7 @@ import java.util.Random;
import de.mrgeorgen.v2g.car; import de.mrgeorgen.v2g.car;
import de.mrgeorgen.v2g.carGrid; import de.mrgeorgen.v2g.carGrid;
public class carGrid { public class carGrid {
public int energieAvailable; private final int id;
public ArrayList<car> dockedCars = new ArrayList<car>(); public ArrayList<car> dockedCars = new ArrayList<car>();
private final carTemplate[] models = {new carTemplate("Tesla Model 3", 50, 160, 335), private final carTemplate[] models = {new carTemplate("Tesla Model 3", 50, 160, 335),
new carTemplate("Renault Zoe ZE50", 52, 46, 315), new carTemplate("Renault Zoe ZE50", 52, 46, 315),
@ -18,13 +18,13 @@ public class carGrid {
new carTemplate("Tesla Model S Long Range", 90, 250, 555), new carTemplate("Tesla Model S Long Range", 90, 250, 555),
new carTemplate("Smart EQ forfour", 17, 5, 95), new carTemplate("Smart EQ forfour", 17, 5, 95),
new carTemplate("Honda e", 29, 56, 170)}; new carTemplate("Honda e", 29, 56, 170)};
public carGrid() { public carGrid(int id) {
this.id = id;
Random random = new Random(); Random random = new Random();
for(carTemplate carModel : models) { for(carTemplate carModel : models) {
final int numberOfCars = random.nextInt(10); final int numberOfCars = random.nextInt(10);
for(int i = 0; i < numberOfCars; ++i) { for(int i = 0; i < numberOfCars; ++i) {
final car car = new car(carModel, i); dockedCars.add(new car(carModel));
dockedCars.add(car);
} }
} }
} }
@ -41,6 +41,7 @@ public class carGrid {
for(car car : dockedCars) { for(car car : dockedCars) {
charged += car.charge(maxCharge - charged); 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; return charged;
} }
public int capacityDockedCars() { public int capacityDockedCars() {
@ -55,6 +56,6 @@ public class carGrid {
for(car car : dockedCars) { for(car car : dockedCars) {
battery += car.battery; battery += car.battery;
} }
return ((double)battery + energieAvailable) / capacityDockedCars(); return (double)battery / capacityDockedCars();
} }
} }

View File

@ -5,32 +5,33 @@ import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.lang.Math; import java.lang.Math;
public class powerGrid { public class powerGrid {
public static int energieAvailable;
public static int savedEnergie; public static int savedEnergie;
private static ArrayList<car> allCars = new ArrayList<car>(); public static int idCounter;
private static ArrayList<carGrid> carGrids = new ArrayList<carGrid>(); public static int logLevel = 1;
public static void main(String args[]) { public static void main(String args[]) {
if(args.length != 1) { if(args.length != 1) {
System.out.println("Invalid Syntax. Use the number of days the simulation shell run as the first argument"); System.out.println("Invalid Syntax. Use the number of days the simulation shell run as the first argument");
return; return;
} }
Random random = new Random(); Random random = new Random();
ArrayList<car> allCars = new ArrayList<car>();
ArrayList<carGrid> carGrids = new ArrayList<carGrid>();
final int numberOfCarGrids = 2 + random.nextInt(10); final int numberOfCarGrids = 2 + random.nextInt(10);
for(int i = 0; i < numberOfCarGrids; ++i) { for(int i = 0; i < numberOfCarGrids; ++i) {
final carGrid carGrid = new carGrid(); final carGrid carGrid = new carGrid(i);
carGrids.add(carGrid); carGrids.add(carGrid);
allCars.addAll(carGrid.dockedCars); allCars.addAll(carGrid.dockedCars);
} }
final int hourSimulationRuns = Integer.parseInt(args[0]) * 24; final int hourSimulationRuns = Integer.parseInt(args[0]) * 24;
for(int houresPassed = 0; houresPassed < hourSimulationRuns; ++houresPassed) { for(int houresPassed = 0; houresPassed < hourSimulationRuns; ++houresPassed) {
final int hourOfDay = houresPassed % 24; 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; int averageEnergie;
if(hourOfDay < 6 || hourOfDay > 22) averageEnergie = allCars.size() * 3000; // energie available at night 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 < 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 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 else /* hourOfDay > 19 && hourOfDay < 23 */ averageEnergie = allCars.size() * -1000; // the lights are on again
energieAvailable = (int)(averageEnergie * (0.5 + 1.5 * random.nextDouble())); int energieAvailable = (int)(averageEnergie * (0.5 + 1.5 * random.nextDouble()));
Collections.sort(carGrids, new Comparator<carGrid>() { Collections.sort(carGrids, new Comparator<carGrid>() {
public int compare(carGrid carGrid1, carGrid carGrid2) { public int compare(carGrid carGrid1, carGrid carGrid2) {
Double carGrid1RelativChargeState = carGrid1.relativeChargeState(); Double carGrid1RelativChargeState = carGrid1.relativeChargeState();
@ -42,9 +43,11 @@ public class powerGrid {
for(carGrid carGrid : carGrids) { for(carGrid carGrid : carGrids) {
energieAvailable -= carGrid.chargeCars(energieAvailable); energieAvailable -= carGrid.chargeCars(energieAvailable);
} }
if(logLevel >= 1) {
if(energieAvailable > 0) System.out.println(energieAvailable + " W could not be used by the cars"); 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"); 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 " + allCars.size() + " cars"); }
System.out.println("vehicle to grid saved " + (double)savedEnergie / 1000 + " kWh with " + allCars.size() + " cars and " + carGrids.size() + " carGrids");
} }
} }