Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maintenance #1039

Open
wants to merge 6 commits into
base: dev.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/* *********************************************************************** *
* project: org.matsim.*
* *
* *********************************************************************** *
* *
* copyright : (C) 2016 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 org.matsim.codeexamples.extensions.electricVehicles;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.TransportMode;
import org.matsim.api.core.v01.network.Link;
import org.matsim.contrib.ev.EvConfigGroup;
import org.matsim.contrib.ev.EvModule;
import org.matsim.contrib.ev.discharging.AuxEnergyConsumption;
import org.matsim.contrib.ev.discharging.DriveEnergyConsumption;
import org.matsim.contrib.ev.discharging.LTHDriveEnergyConsumption;
import org.matsim.contrib.ev.fleet.ElectricVehicle;
import org.matsim.contrib.ev.infrastructure.LTHConsumptionModelReader;
import org.matsim.contrib.ev.routing.EvNetworkRoutingProvider;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigGroup;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.controler.AbstractModule;
import org.matsim.core.controler.Controler;
import org.matsim.core.controler.OutputDirectoryHierarchy;
import org.matsim.core.scenario.ScenarioUtils;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

/**
* Runs a sample EV run using a vehicle consumption model designed at LTH in Lund which takes the speed and the slope of a link into account.
* Link slopes may be added using a double array on the network.
* The consumption maps are based on Domingues, Gabriel. / Modeling, Optimization and Analysis of Electromobility Systems. Lund : Department of Biomedical Engineering, Lund university, 2018. 169 p., PhD thesis
*/
public class RunEvExampleWithOwnConsumptionModel{
static final String DEFAULT_CONFIG_FILE = "test/input/org/matsim/contrib/ev/example/RunEvExample/config.xml";
private static final Logger log = LogManager.getLogger( RunEvExampleWithOwnConsumptionModel.class );

public static void main(String[] args) throws IOException {
if (args.length > 0) {
log.info("Starting simulation run with the following arguments:");
log.info("args=" + Arrays.toString( args ) );
} else {
File localConfigFile = new File(DEFAULT_CONFIG_FILE);
if (localConfigFile.exists()) {
log.info("Starting simulation run with the local example config file");
args = new String[]{ DEFAULT_CONFIG_FILE };
} else {
log.info("Starting simulation run with the example config file from GitHub repository");
args = new String[]{"https://raw.githubusercontent.com/matsim-org/matsim/master/contribs/ev/"
+ DEFAULT_CONFIG_FILE };
}
}
new RunEvExampleWithOwnConsumptionModel().run(args );
}

public void run( String[] args ) {
Config config = ConfigUtils.loadConfig(args, new EvConfigGroup());
config.controler().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists);

// ===

Scenario scenario = ScenarioUtils.loadScenario(config);

// ===

Controler controler = new Controler(scenario);
{
DriveEnergyConsumption.Factory driveEnergyConsumptionFactory = new DriveEnergyConsumption.Factory(){
@Override public DriveEnergyConsumption create( ElectricVehicle electricVehicle ){
DriveEnergyConsumption.Factory factory = new LTHConsumptionModelReader(null).readURL( ConfigGroup.getInputFileURL( config.getContext(), "MidCarMap.csv" ) );
DriveEnergyConsumption delegate = factory.create( electricVehicle );

DriveEnergyConsumption consumption = new DriveEnergyConsumption(){
@Override public double calcEnergyConsumption( Link link, double travelTime, double linkEnterTime ){

// discharge because the link must be driven:
double delta = delegate.calcEnergyConsumption( link, travelTime, linkEnterTime );

double desiredSocAtEndOfLink = (double) electricVehicle.getVehicleSpecification().getMatsimVehicle().getAttributes().getAttribute( "whatever" );

return electricVehicle.getBattery().getSoc() - desiredSocAtEndOfLink;
// * above will often be negative; this is the purpose: discharging is negative i.e. we are
// charging on the link. ((This is why I am in general against hiding the sign in the method
// name. kai))

// * above is in SOC space, needs to be translated into kWh space

// * need to make sure that the above charging is physically possible

// * need to make sure that we are not discharging beyond what is needed to drive the link

}
};
return consumption;
}
};

controler.addOverridingModule( new EvModule() );
controler.addOverridingModule( new AbstractModule(){
@Override
public void install(){
bind( DriveEnergyConsumption.Factory.class ).toInstance( driveEnergyConsumptionFactory );
bind( AuxEnergyConsumption.Factory.class ).toInstance(
electricVehicle -> ( beginTime, duration, linkId ) -> 0 ); //a dummy factory, as aux consumption is part of the drive consumption in the model

addRoutingModuleBinding( TransportMode.car ).toProvider( new EvNetworkRoutingProvider( TransportMode.car ) );
// a router that inserts charging activities when the battery is run empty. there may be some other way to insert
// charging activities, based on the situation. kai, dec'22
}
} );
}


controler.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ public static void main (String[] args){

emissionsConfig.setDetailedVsAverageLookupBehavior( DetailedVsAverageLookupBehavior.directlyTryAverageTable );

// emissionsConfig.setAverageColdEmissionFactorsFile( "../sample_EFA_ColdStart_vehcat_2005average.csv" );
// emissionsConfig.setAverageWarmEmissionFactorsFile( "../sample_EFA_HOT_vehcat_2005average.csv" );

emissionsConfig.setAverageColdEmissionFactorsFile( "../sample_EFA_ColdStart_vehcat_2020_average_withHGVetc.csv" );
emissionsConfig.setAverageWarmEmissionFactorsFile( "../sample_41_EFA_HOT_vehcat_2020average.csv" );

Expand Down Expand Up @@ -110,21 +107,17 @@ public static void main (String[] args){

// we do not want to run the full Controler. In consequence, we plug together the infrastructure one needs in order to run the emissions contrib:

EventsManager eventsManager = EventsUtils.createEventsManager();

AbstractModule module = new AbstractModule(){
@Override
public void install(){
bind( Scenario.class ).toInstance( scenario );
bind( EventsManager.class ).toInstance( eventsManager ) ;
bind( EmissionModule.class ) ;
bind( EventsManager.class ).toInstance( EventsUtils.createEventsManager() ) ;
bind( EmissionModule.class ).asEagerSingleton(); // eager singleton necessary to force instantiation!
}
};

com.google.inject.Injector injector = Injector.createInjector( config, module );

// the EmissionModule must be instantiated, otherwise it does not work:
injector.getInstance(EmissionModule.class);
EventsManager eventsManager = injector.getInstance( EventsManager.class );

// ---

Expand All @@ -139,11 +132,10 @@ public void install(){
eventWriterXML.closeFile();

// also write vehicles and network and config as a service so we have all out files in one directory:
new MatsimVehicleWriter( scenario.getVehicles() ).writeFile( config.controller().getOutputDirectory() + "/output_vehicles.xml.gz" );
NetworkUtils.writeNetwork( scenario.getNetwork(), config.controller().getOutputDirectory() + "/output_network.xml.gz" );
ConfigUtils.writeConfig( config, config.controller().getOutputDirectory() + "/output_config.xml" );
ConfigUtils.writeMinimalConfig( config, config.controller().getOutputDirectory() + "/output_config_reduced.xml" );

VehicleUtils.writeVehicles( scenario.getVehicles() , config.controler().getOutputDirectory() + "/output_vehicles.xml.gz" );
NetworkUtils.writeNetwork( scenario.getNetwork(), config.controler().getOutputDirectory() + "/output_network.xml.gz" );
ConfigUtils.writeConfig( config, config.controler().getOutputDirectory() + "/output_config.xml" );
ConfigUtils.writeMinimalConfig( config, config.controler().getOutputDirectory() + "/output_config_reduced.xml" );
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,39 @@ protected void configure(){
}
log.info("") ;

Map<Annotation, Set<Provider<MyInterface>>> map = injector.getInstance( Key.get( new TypeLiteral<Map<Annotation, Set<Provider<MyInterface>>>>(){} ) );;
Set<Provider<MyInterface>> set = map.get( Names.named("abc" ) ) ;
injector.getInstance( MyRunner.class ).run();

for( Provider<MyInterface> provider : set ){
provider.get() ;
}
// Map<Annotation, Set<Provider<MyInterface>>> map = injector.getInstance( Key.get( new TypeLiteral<Map<Annotation, Set<Provider<MyInterface>>>>(){} ) );;
// Set<Provider<MyInterface>> set = map.get( Names.named("abc" ) ) ;
//
// for( Provider<MyInterface> provider : set ){
// provider.get() ;
// }

}

private interface MyInterface{
private static class MyRunner {
@Inject private Map<Annotation, MyInterface> myInterfaceMap;
void run() {
myInterfaceMap.get(Names.named("egh")).doSomething();
}
}



private interface MyInterface{
void doSomething();
}

private static class MyImpl1 implements MyInterface{
@Inject MyImpl1() {
log.info( "ctor 1 called" );
public void doSomething() {
log.warn("doSomething of MyImpl1");
}
}

private static class MyImpl2 implements MyInterface{
@Inject MyImpl2() {
log.info( "ctor 2 called" );
public void doSomething() {
log.warn("doSomething of MyImpl1");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public final class MapBinderWithStringExample{
private static final Logger log = LogManager.getLogger( MapBinderWithStringExample.class ) ;
Expand All @@ -25,6 +26,8 @@ void run() {
modules.add( new AbstractModule(){
@Override
protected void configure(){
bind( MyRunner.class );

MapBinder<String, MyInterface> mapBinder = MapBinder.newMapBinder( this.binder(), String.class, MyInterface.class );
// mapBinder.permitDuplicates() ;
mapBinder.addBinding("abc" ).to( MyImpl1.class ).in( Singleton.class );
Expand All @@ -45,29 +48,39 @@ protected void configure(){
}
log.info("") ;

Map<String, Provider<MyInterface>> map = injector.getInstance( Key.get( new TypeLiteral<Map<String, Provider<MyInterface>>>(){} ) );;
Provider<MyInterface> provider = map.get( "abc" );;
injector.getInstance( MyRunner.class ).run() ;

// for( Provider<MyInterface> provider : set ){
provider.get() ;
// }
// Map<String, Provider<MyInterface>> map = injector.getInstance( Key.get( new TypeLiteral<Map<String, Provider<MyInterface>>>(){} ) );;
// Provider<MyInterface> provider = map.get( "abc" );;
//
//// for( Provider<MyInterface> provider : set ){
// provider.get() ;
//// }

}

private interface MyInterface{
private static class MyRunner {
@Inject private Map<String, MyInterface> myInterfaceMap;
void run() {
myInterfaceMap.get("egh").doSomething();
}
}


private interface MyInterface{
void doSomething();
}

@Singleton
private static class MyImpl1 implements MyInterface{
@Inject MyImpl1() {
log.info( "ctor 1 called" );
public void doSomething() {
log.warn("doSomething in MyImpl1");
}
}

private static class MyImpl2 implements MyInterface{
@Inject MyImpl2() {
log.info( "ctor 2 called" );
public void doSomething() {
log.warn("doSomething in MyImpl2");
}
}
}
Loading
Loading