Skip to content

Commit

Permalink
DC-update
Browse files Browse the repository at this point in the history
  • Loading branch information
schumifabi committed May 29, 2024
1 parent b5d4826 commit 4622897
Show file tree
Hide file tree
Showing 12 changed files with 389 additions and 330 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ChargingWithQueueingLogic implements ChargingLogic {
private final Queue<ElectricVehicle> arrivingVehicles = new LinkedBlockingQueue<>();
private final Map<Id<ElectricVehicle>, ChargingListener> listeners = new LinkedHashMap<>();
private final Map<Id<ElectricVehicle>, Double> plugInTimestamps = new LinkedHashMap<>();

private final Map<Id<ElectricVehicle>, Double> queuedTimestamps = new LinkedHashMap<>();
public ChargingWithQueueingLogic(Charger charger, ChargingStrategy chargingStrategy, EventsManager eventsManager) {
this.chargingStrategy = Objects.requireNonNull(chargingStrategy);
this.charger = Objects.requireNonNull(charger);
Expand Down Expand Up @@ -76,7 +76,9 @@ public void chargeVehicles(double chargePeriod, double now) {

int queuedToPluggedCount = Math.min(queuedVehicles.size(), charger.getPlugCount() - pluggedVehicles.size());
for (int i = 0; i < queuedToPluggedCount; i++) {
plugVehicle(queuedVehicles.poll(), now);
ElectricVehicle ev = queuedVehicles.poll();
plugVehicle(ev, now);
eventsManager.processEvent(new QuitQueueAtChargerEvent(now, charger.getId(), ev.getId(),now-queuedTimestamps.get(ev.getId())));
}

Iterator<ElectricVehicle> arrivingVehiclesIter = arrivingVehicles.iterator();
Expand Down Expand Up @@ -118,12 +120,13 @@ public void removeVehicle(ElectricVehicle ev, double now) {
} else {
// make sure ev was in the queue
Preconditions.checkState(queuedVehicles.remove(ev), "Vehicle (%s) is neither queued nor plugged at charger (%s)", ev.getId(), charger.getId());
eventsManager.processEvent(new QuitQueueAtChargerEvent(now, charger.getId(), ev.getId()));
eventsManager.processEvent(new QuitQueueAtChargerEvent(now, charger.getId(), ev.getId(),now-queuedTimestamps.get(ev.getId())));
}
}

private void queueVehicle(ElectricVehicle ev, double now) {
queuedVehicles.add(ev);
queuedTimestamps.put(ev.getId(), now);
eventsManager.processEvent(new QueuedAtChargerEvent(now, charger.getId(), ev.getId()));
listeners.get(ev.getId()).notifyVehicleQueued(ev, now);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ public class QuitQueueAtChargerEvent extends Event {
public static final String EVENT_TYPE = "quit_queue_at_charger";
public static final String ATTRIBUTE_CHARGER = "charger";
public static final String ATTRIBUTE_VEHICLE = "vehicle";
public static final String ATTRIBUTE_WAITING = "waiting_time";

private final Id<Charger> chargerId;
private final Id<ElectricVehicle> vehicleId;
private final double waitingtime;

public QuitQueueAtChargerEvent(double time, Id<Charger> chargerId, Id<ElectricVehicle> vehicleId) {

public QuitQueueAtChargerEvent(double time, Id<Charger> chargerId, Id<ElectricVehicle> vehicleId, double waitingtime) {
super(time);
this.chargerId = chargerId;
this.vehicleId = vehicleId;
this.waitingtime = waitingtime;
}

public Id<Charger> getChargerId() {
Expand All @@ -47,6 +51,9 @@ public Id<Charger> getChargerId() {
public Id<ElectricVehicle> getVehicleId() {
return vehicleId;
}
public double waitingtime() {
return waitingtime;
}

@Override
public String getEventType() {
Expand All @@ -58,6 +65,7 @@ public Map<String, String> getAttributes() {
Map<String, String> attr = super.getAttributes();
attr.put(ATTRIBUTE_CHARGER, chargerId.toString());
attr.put(ATTRIBUTE_VEHICLE, vehicleId.toString());
attr.put(ATTRIBUTE_WAITING, Double.toString(this.waitingtime));
return attr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
import org.matsim.api.core.v01.population.Activity;

public class VehicleChargingHandler
implements ActivityStartEventHandler, ActivityEndEventHandler, QueuedAtChargerEventHandler, ChargingEndEventHandler, MobsimBeforeSimStepListener, MobsimScopeEventHandler {
implements ActivityStartEventHandler, ActivityEndEventHandler, QueuedAtChargerEventHandler, QuitQueueAtChargerEventHandler, ChargingEndEventHandler, MobsimBeforeSimStepListener, MobsimScopeEventHandler {

private static final Logger log = Logger.getLogger(VehicleChargingHandler.class);

Expand Down Expand Up @@ -154,12 +154,15 @@ public void handleEvent(ActivityStartEvent event) {
double walkingDistance = 0.0;
double soc = ev.getBattery().getSoc() / ev.getBattery().getCapacity();
double time = event.getTime();

double detour = 0.0;
Activity activity;
if (PlanUtils.isCharging(actType)) {

//hier entsteht das Problem !!
// hier ist es besser, die Koordinaten des Events zu nutzten
Activity activity = PlanUtils.getActivity(person.getSelectedPlan(), time);
//Activity activity = PlanUtils.getActivity(person.getSelectedPlan(), time);
//Activity activity = PlanUtils.getActivity_with_link(person.getSelectedPlan(), time,event.getLinkId());

//Coord activityCoord = activity != null ? activity.getCoord() : network.getLinks().get(event.getLinkId()).getCoord();
Coord activityCoord = event.getCoord();
// Location choice
Expand All @@ -173,9 +176,21 @@ public void handleEvent(ActivityStartEvent event) {

List<Charger> suitableChargers;
if(event.getActType()=="car fast charging"){
activity = PlanUtils.getDCchargingactivity(person.getSelectedPlan(), time);
if (activity.getType()=="car fast charging" ){
if (activity.getAttributes().size()>0){
detour = Double.parseDouble(activity.getAttributes().getAttribute("detour").toString());
} else{
detour = 0.0;
}

}

suitableChargers = findSuitableChargers(activityCoord, ev,true);
}
else{
detour = 0.0;
activity = PlanUtils.getActivity(person.getSelectedPlan(), time);
suitableChargers = findSuitableChargers(activityCoord, ev,false);
}
Charger selectedCharger = null;
Expand All @@ -202,11 +217,11 @@ public void handleEvent(ActivityStartEvent event) {
vehiclesAtChargers.put(evId, selectedCharger.getId());
walkingDistance = DistanceUtils.calculateDistance(
activityCoord, selectedCharger.getCoord());
} else {
//} else {
// if no charger was found, or charging would be ineffective, mark as failed attempt in plan if not already marked
if (activity != null) {
PlanUtils.setFailed(activity);
}
//if (activity != null) {
// PlanUtils.setFailed(activity);
//}
}
}

Expand All @@ -223,6 +238,8 @@ public void handleEvent(ActivityStartEvent event) {
walkingDistance,
0.0,
false,
0.0,
detour,
ScoreTrigger.ACTIVITYSTART
)
);
Expand All @@ -248,7 +265,15 @@ public void handleEvent(ActivityEndEvent event) {
// Map<Id<ElectricVehicle>, Id<Charger>> vehiclesAtChargers = chargingInfrastructure.getChargers().values().stream()
// .flatMap(a -> a.getLogic().getPluggedVehicles().stream())
// .collect(Collectors.toMap(ElectricVehicle::getId, a -> a.getId()));

//TODO: Fertig! -> Bewertungsfunktion

if(event.getActType()=="car fast charging"){
Person person = population.getPersons().get(personId);
Plan plan = person.getSelectedPlan();

Activity activity = PlanUtils.getActivity(plan, event.getTime());
}
// If the vehicle is currently plugged in
if(vehiclesAtChargers.containsKey(evId))
{
Expand Down Expand Up @@ -327,6 +352,8 @@ public void handleEvent(ActivityEndEvent event) {
walkingDistance,
pluggedDuration,
hogging,
0.0,
0.0,
ScoreTrigger.ACTIVITYEND
)
);
Expand All @@ -346,6 +373,8 @@ public void handleEvent(ActivityEndEvent event) {
0.0,
0.0,
false,
0.0,
0.0,
ScoreTrigger.ACTIVITYEND
)
);
Expand All @@ -363,7 +392,40 @@ public void handleEvent(QueuedAtChargerEvent event) {
agentsInChargerQueue.add(driver);
} // else this vehicle is driven by a DynAgent (who did not leave the vehicle for charging)
}

//Ergänzungen activity end extension
@Override
public void handleEvent(QuitQueueAtChargerEvent event) {
//vehiclesAtChargers should normally already contain the vehicle, but assure this nevertheless
vehiclesAtChargers.put(event.getVehicleId(), event.getChargerId());
Id<ElectricVehicle> driver = event.getVehicleId();
String actType = null;

Id<Person> personId = Id.create(driver, Person.class);
Id<ElectricVehicle> evId = Id.create(personId, ElectricVehicle.class);

ElectricVehicle ev = electricFleet.getElectricVehicles().get(evId);
double startSoc = 0.0;
double socUponDeparture = ev.getBattery().getSoc() / ev.getBattery().getCapacity();
double time = event.getTime();
double waitingtime = event.waitingtime();
//TODO: Fertig! -> Bewertungsfunktion
eventsManager.processEvent(
new ChargingBehaviourScoringEvent(
time,
personId,
actType,
socUponDeparture,
startSoc,
0.0,
0.0,
false,
waitingtime,
0.0,
ScoreTrigger.ACTIVITYEND
)
);
}
@Override
public void notifyMobsimBeforeSimStep(@SuppressWarnings("rawtypes") MobsimBeforeSimStepEvent e) {
//TODO only do this every <evConfig.chargeTimeStep> seconds ??
Expand Down Expand Up @@ -394,7 +456,7 @@ public void notifyMobsimBeforeSimStep(@SuppressWarnings("rawtypes") MobsimBefore
//tschlenther, nov' 23
act.setEndTime(endTimeStamp);
//act.setMaximumDuration(act.getMaximumDuration().orElseThrow(IllegalStateException::new) + 1d);
WithinDayAgentUtils.resetCaches(mobsimAgent);
//WithinDayAgentUtils.resetCaches(mobsimAgent);
WithinDayAgentUtils.rescheduleActivityEnd(mobsimAgent, qsim);
iterator.remove();
}
Expand All @@ -413,23 +475,11 @@ public void handleEvent(ChargingEndEvent event) {
//agentsInChargerQueue.add(event.getVehicleId());
// Löschende Logik
end_time_stamps.put(event.getVehicleId(), event.getTime());
//System.out.println(event.getVehicleId());
//System.out.println(event.getTime());
//System.out.println(end_time_stamps);

}


// @Override
// public void handleEvent(ChargingEndEvent event) {
// // vehiclesAtChargers.remove(event.getVehicleId());
// // Charging has ended before activity ends
// }

/**
* Tries to find the closest free chargers of fitting type in vicinity of activity location
* If a charger is private, only allowed vehicles can charge there
*/

private List<Charger> findSuitableChargers(Coord stopCoord, ElectricVehicle electricVehicle, boolean only_fast_chargers) {

Expand Down
Loading

0 comments on commit 4622897

Please sign in to comment.