stuff
This commit is contained in:
34
src/main/java/de/mrgeorgen/v2g/car.java
Normal file
34
src/main/java/de/mrgeorgen/v2g/car.java
Normal file
@ -0,0 +1,34 @@
|
||||
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 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 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 void setChargeLock(double chargeLock) {
|
||||
this.chargeLock = (int)(this.fullBattery * chargeLock);
|
||||
}
|
||||
public double getBatteryRelativ() {
|
||||
return (double)this.battery / this.fullBattery;
|
||||
}
|
||||
}
|
||||
41
src/main/java/de/mrgeorgen/v2g/carGrid.java
Normal file
41
src/main/java/de/mrgeorgen/v2g/carGrid.java
Normal file
@ -0,0 +1,41 @@
|
||||
package de.mrgeorgen.v2g;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.lang.Double;
|
||||
public class carGrid {
|
||||
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),
|
||||
new carTemplate("BMW i3", 38, 49, 235),
|
||||
new carTemplate("Nissan Leaf", 36, 46, 220),
|
||||
new carTemplate("Volkswagen ID.4", 150, 125, 400),
|
||||
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() {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
public void chargeCars() {
|
||||
Collections.sort(dockedCars, new Comparator<car>() {
|
||||
public int compare(car car1, car car2) {
|
||||
Double car1RelativBattery = car1.getBatteryRelativ();
|
||||
Double car2RelativBattery = car2.getBatteryRelativ();
|
||||
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");
|
||||
}
|
||||
}
|
||||
15
src/main/java/de/mrgeorgen/v2g/carTemplate.java
Normal file
15
src/main/java/de/mrgeorgen/v2g/carTemplate.java
Normal file
@ -0,0 +1,15 @@
|
||||
package de.mrgeorgen.v2g;
|
||||
|
||||
public class carTemplate {
|
||||
public final String model;
|
||||
public final int chargeSpeed;
|
||||
public final int fullBattery;
|
||||
public final int range;
|
||||
|
||||
public carTemplate(String model, int fullBattery, int chargeSpeed, int range) {
|
||||
this.model = model;
|
||||
this.chargeSpeed = chargeSpeed;
|
||||
this.fullBattery = fullBattery;
|
||||
this.range = range;
|
||||
}
|
||||
}
|
||||
26
src/main/java/de/mrgeorgen/v2g/powerGrid.java
Normal file
26
src/main/java/de/mrgeorgen/v2g/powerGrid.java
Normal file
@ -0,0 +1,26 @@
|
||||
package de.mrgeorgen.v2g;
|
||||
import java.util.Random;
|
||||
public class powerGrid {
|
||||
public static int energieAvailable;
|
||||
public static void main(String args[]) {
|
||||
final carGrid carGrid = new carGrid();
|
||||
if(args.length != 1) {
|
||||
System.out.println("Invalid Syntax. Use the number of days the simulation shell run as the first argument");
|
||||
return;
|
||||
}
|
||||
carGrid.fillWithCars();
|
||||
final int hourSimulationRuns = Integer.parseInt(args[0]) * 24;
|
||||
Random random = new Random();
|
||||
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
|
||||
energieAvailable = (int)(averageEnergie * (0.5 + 1.5 * random.nextDouble()));
|
||||
carGrid.chargeCars();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user