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;
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.setChargeLock(0.6);
this.id = id;
this.battery = this.carModel.fullBattery / 2;
setChargeLock(0.6);
id = powerGrid.idCounter++;
battery = carModel.fullBattery / 2;
}
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;
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.carModel.fullBattery * chargeLock);
chargeLock = (int)(carModel.fullBattery * chargeLock);
}
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.carGrid;
public class carGrid {
public int energieAvailable;
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),
@ -18,13 +18,13 @@ 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 carGrid() {
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) {
final car car = new car(carModel, i);
dockedCars.add(car);
dockedCars.add(new car(carModel));
}
}
}
@ -41,6 +41,7 @@ public class carGrid {
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() {
@ -55,6 +56,6 @@ public class carGrid {
for(car car : dockedCars) {
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.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 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;
}
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();
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 = 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()));
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();
@ -42,9 +43,11 @@ public class powerGrid {
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 " + allCars.size() + " cars");
}
System.out.println("vehicle to grid saved " + (double)savedEnergie / 1000 + " kWh with " + allCars.size() + " cars and " + carGrids.size() + " carGrids");
}
}