Skip to content

Commit

Permalink
Merge pull request #10 from TUMFTM/charging_behavior_rework
Browse files Browse the repository at this point in the history
Charging behavior rework
  • Loading branch information
schumifabi authored Dec 11, 2023
2 parents 4be9cc4 + 298a7fe commit f36b3ca
Show file tree
Hide file tree
Showing 14 changed files with 606 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
File originally created, published and licensed by contributors of the org.matsim.* project.
Please consider the original license notice below.
This is a modified version of the original source code!
Modified 2020 by Lennart Adenaw, Technical University Munich, Chair of Automotive Technology
email : [email protected]
*/

/* ORIGINAL LICENSE
* *********************************************************************** *
* project: org.matsim.*
* *********************************************************************** *
* *
* copyright : (C) 2019 by the members listed in the COPYING, *
* LICENSE and WARRANTY file. *
* email : info at matsim dot org *
* *
* *********************************************************************** *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* See also COPYING, LICENSE and WARRANTY file *
* *
* *********************************************************************** *
*/

package de.tum.mw.ftm.matsim.contrib.urban_ev.charging;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import de.tum.mw.ftm.matsim.contrib.urban_ev.fleet.Battery;
import de.tum.mw.ftm.matsim.contrib.urban_ev.fleet.ElectricVehicle;
import de.tum.mw.ftm.matsim.contrib.urban_ev.infrastructure.Charger;

/**
* @author Michal Maciejewski (michalm)
*/
public class ChargeUpToTypeMaxSocStrategy implements ChargingStrategy {

private final Charger charger;
private final double maxRelativeSoc;


public ChargeUpToTypeMaxSocStrategy(Charger charger) {

this.maxRelativeSoc = getMaxRelativeSoc(charger);

if (maxRelativeSoc < 0 || maxRelativeSoc > 1) {
throw new IllegalArgumentException();
}
this.charger = charger;
}

@Override
public double calcRemainingEnergyToCharge(ElectricVehicle ev) {
Battery battery = ev.getBattery();
return maxRelativeSoc * battery.getCapacity() - battery.getSoc();
}

@Override
public double calcRemainingTimeToCharge(ElectricVehicle ev) {
return ((BatteryCharging)ev.getChargingPower()).calcChargingTime(charger, calcRemainingEnergyToCharge(ev));
}

public static double getMaxRelativeSoc(Charger charger)
{
final Map<String,Double> maxRelativeSocByChargerType = Stream.of(new Object[][] {{ "private_ac", 0.8 },{ "public_dc", 0.8 }, { "public_ac", 0.9 }, { "default", 0.5 }}).collect(Collectors.toMap(data -> (String) data[0], data -> (double) data[1]));
return maxRelativeSocByChargerType.getOrDefault(charger.getChargerType(), 1.0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public void install() {

@Override
public ChargingLogic.Factory get() {
//return charger -> new ChargingLogicImpl(charger, new ChargeUpToMaxSocStrategy(charger, 1.), eventsManager);
return charger -> new ChargingLogicImpl(charger, new ChargeUpToIndiSocStrategy(charger, 1.), eventsManager);
return charger -> new ChargingLogicImpl(charger, new ChargeUpToTypeMaxSocStrategy(charger),
eventsManager);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import de.tum.mw.ftm.matsim.contrib.urban_ev.scoring.ChargingBehaviourScoringEvent;
import de.tum.mw.ftm.matsim.contrib.urban_ev.scoring.ChargingBehaviourScoringEvent.ScoreTrigger;
import de.tum.mw.ftm.matsim.contrib.urban_ev.utils.PlanUtils;
import de.tum.mw.ftm.matsim.contrib.urban_ev.charging.ChargeUpToTypeMaxSocStrategy;

import org.apache.log4j.Logger;
import org.matsim.api.core.v01.Coord;
Expand Down Expand Up @@ -78,6 +79,8 @@ public class VehicleChargingHandler
public static final Integer SECONDS_PER_MINUTE = 60;
public static final Integer SECONDS_PER_HOUR = 60*SECONDS_PER_MINUTE;
public static final Integer SECONDS_PER_DAY = 24*SECONDS_PER_HOUR;

// State variables
private Map<Id<ElectricVehicle>, Id<Charger>> vehiclesAtChargers = new HashMap<>();

private final ChargingInfrastructure chargingInfrastructure;
Expand Down Expand Up @@ -130,7 +133,8 @@ public void handleEvent(ActivityStartEvent event) {
ElectricVehicle ev = electricFleet.getElectricVehicles().get(evId);
Person person = population.getPersons().get(personId);
double walkingDistance = 0.0;
double time = event.getTime();
double soc = ev.getBattery().getSoc() / ev.getBattery().getCapacity();
double time = event.getTime();

if (PlanUtils.isCharging(actType)) {

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

//charging_behavior_rework: Post-Merge work neccessary!
//List<Charger> suitableChargers = findSuitableChargers(activityCoord, ev);
//Charger selectedCharger = suitableChargers.stream()
// .filter(charger -> charger.getAllowedVehicles().contains(evId))
// .findFirst()
// .orElse(null);

List<Charger> suitableChargers;
if(event.getActType()=="car fast charging"){
suitableChargers = findSuitableChargers(activityCoord, ev,true);
Expand All @@ -164,22 +176,21 @@ public void handleEvent(ActivityStartEvent event) {
{
selectedCharger = selectCharger(suitableChargers, activityCoord, ChargerSelectionMethod.CLOSEST);
}
// Start charging if possible
if (selectedCharger != null) { // if charger was found, start charging

// Start charging if charger was found and soc is lower than anticipated end soc
if (selectedCharger != null && soc<ChargeUpToTypeMaxSocStrategy.getMaxRelativeSoc(selectedCharger)) {
selectedCharger.getLogic().addVehicle(ev, time);
vehiclesAtChargers.put(evId, selectedCharger.getId());
walkingDistance = DistanceUtils.calculateDistance(
activityCoord, selectedCharger.getCoord());
} else {
// if no charger was found, mark as failed attempt in plan if not already marked
// 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);
}
}
}

double socUponArrival = ev.getBattery().getSoc() / ev.getBattery().getCapacity();
double startSoc = ev.getBattery().getStartSoc() / ev.getBattery().getCapacity();

// Issue a charging behaviour scoring event
Expand All @@ -188,7 +199,7 @@ public void handleEvent(ActivityStartEvent event) {
time,
personId,
actType,
socUponArrival,
soc,
startSoc,
walkingDistance,
0.0,
Expand All @@ -198,7 +209,7 @@ public void handleEvent(ActivityStartEvent event) {
);
}
}
}
}

@Override
public void handleEvent(ActivityEndEvent event) {
Expand All @@ -211,6 +222,14 @@ public void handleEvent(ActivityEndEvent event) {
double socUponDeparture = ev.getBattery().getSoc() / ev.getBattery().getCapacity();
double time = event.getTime();

//ArrayList<ElectricVehicle> vehiclesAtChargers = chargingInfrastructure.getChargers().values().stream().map(c -> c.getLogic().getPluggedVehicles()).collect(Collectors.toList());

//List<ElectricVehicle> vehiclesAtChargers = chargingInfrastructure.getChargers().values().stream().flatMap(a -> a.getLogic().getPluggedVehicles().stream()).collect(Collectors.toList());

// Map<Id<ElectricVehicle>, Id<Charger>> vehiclesAtChargers = chargingInfrastructure.getChargers().values().stream()
// .flatMap(a -> a.getLogic().getPluggedVehicles().stream())
// .collect(Collectors.toMap(ElectricVehicle::getId, a -> a.getId()));

// If the vehicle is currently plugged in
if(vehiclesAtChargers.containsKey(evId))
{
Expand Down Expand Up @@ -252,7 +271,7 @@ public void handleEvent(ActivityEndEvent event) {
// Sort all such activities by their end times
allActivities = PlanUtils.sortByEndTime(allActivities.stream().filter(a -> a.getEndTime().isDefined()).collect(Collectors.toList()));

// Check whether actitivies at the same location normally lead to hogging or not (majority vote)
// Check whether actitivies at the same location normally lead to hogging or not
int hoggingCount = 0;

for(Activity act: activitiesAtSameCoord)
Expand All @@ -261,21 +280,15 @@ public void handleEvent(ActivityEndEvent event) {
int lastActInd = allActivities.indexOf(act)-1;
double virtualPlugIn = allActivities.get(lastActInd).getEndTime().seconds();
double virtualPlugOut = act.getEndTime().seconds();


// Check if they would lead to hogging in the majority of cases
// Check if they would lead to hogging
if(isHogging(virtualPlugIn, virtualPlugOut, hoggingExemptionHourStart, hoggingExemptionHourStop, hoggingThresholdMinutes))
{
hoggingCount++;
}
else
{
hoggingCount--;
}

}

// Determine hogging state of the first activity by comparison with the usual case
// Determine hogging state of the first activity by randomization with the same chance as other similar acts
hogging = hoggingCount>0;

}
Expand Down Expand Up @@ -331,6 +344,15 @@ public void handleEvent(ActivityEndEvent event) {
private List<Charger> findSuitableChargers(Coord stopCoord, ElectricVehicle electricVehicle, boolean only_fast_chargers) {

List<Charger> filteredChargers = new ArrayList<>();
// charging_behavior_rework
//
// filteredChargers = chargingInfrastructure.getChargers().values().stream()
// .filter(charger -> charger.getAllowedVehicles().isEmpty() || charger.getAllowedVehicles().contains(electricVehicle.getId()))
// .filter(charger -> DistanceUtils.calculateDistance(stopCoord, charger.getCoord()) < parkingSearchRadius)
// .filter(charger -> electricVehicle.getChargerTypes().contains(charger.getChargerType()))
// .filter(charger -> charger.getLogic().getPluggedVehicles().size() < charger.getPlugCount())
// .collect(Collectors.toList());

List<Charger> allChargers = new ArrayList<>();
chargingInfrastructure.getChargers().values().forEach(charger -> {allChargers.add(charger);});

Expand Down
Loading

0 comments on commit f36b3ca

Please sign in to comment.