Comments (5)
Dear Lichen,
the facilities attribute file would indeed be an option. However, if you use a recent MATSim version, there is a better approach, where the attributes are stored directly into the facilities rather than a separate container.
As adding such an attribute will not make much sense except if you program your own plugins, I assume that you are generating the files using Java. Then, the two approaches would look like this:
final ActivityFacilities activityFacilities = scenario.getActivityFacilities();
final ObjectAttributes facilityAttributes = activityFacilities.getFacilityAttributes();
// for the first, separate container approach
for (ActivityFacility facility : activityFacilities.getFacilities().values()) {
facilityAttributes.putAttribute(facility.getId().toString(), "weight", 42d);
}
// for the second, integrated approach
for (ActivityFacility facility : activityFacilities.getFacilities().values()) {
facility.getAttributes().putAttribute("weight", 42d);
}
And to access the values:
final ActivityFacilities activityFacilities = scenario.getActivityFacilities();
final ObjectAttributes facilityAttributes = activityFacilities.getFacilityAttributes();
// for the first, separate container approach
for (ActivityFacility facility : activityFacilities.getFacilities().values()) {
double weight = (double) facilityAttributes.getAttribute(facility.getId().toString(), "weight");
}
// for the second, integrated approach
for (ActivityFacility facility : activityFacilities.getFacilities().values()) {
double weight = (double) facility.getAttributes().getAttribute("weight");
}
If using the second approach, you do not need to do anything special to write the attributes to the file. If creating a separate container in a "script in java" that writes the data to files at the end, you will need to write them to a separate file explicitly, e.g.
new ObjectAttributesXmlWriter(activityFacilities.getFacilityAttributes()).writeFile("path/to/facilities_attributes.xml.gz");
from matsim-code-examples.
Dear Mr.Dubernet,
Thank you very much for your reply!
Just as what you assumed, I am indeed using not the newest version but 0.10.0-SNAPSHOT instead. So I tried the separate container approach and write attributes as a file as what you kindly pointed out.
The attributes file goes like this:
<objectAttributes>
<object id="55380000">
<attribute name="weight" class="java.lang.Integer">10</attribute>
</object>
...
</objectAttributes>
,which I presume it is output correctly.
But unfortunately, after I added
config.facilities().setInputFacilitiesAttributesFile(_myFacilityAttributesFilePath_)
and ran my code, I was still informed of a java.lang.RuntimeException saying that [If option "useOpportunityWeights" is used, the facilities must have an attribute with key "weight"].
I checked the code and find that seemingly it is class ScenarioLoaderImpl that load InputFacilitiesAttributes into our simulation framework. But by checking the logfile, I am not sure if the class was called (because log.INFO from ScenarioLoaderImpl was not written) and I am guessing that would be the problem.
I am clear that this might look a stupid question to you but could you please by any chance offer some suggestions in regard to it? (as I am not familiar with Guice, so as to check the calling hierarchy myself)
Best,
from matsim-code-examples.
- Did you try the other approach as well? As far as I remember, it should be available in 0.10.x as well. I would also advise against using snapshots. It is almost never a good idea, as they might lead to non-reproducible builds.
- Yes, the output is what is expected
- I looked at the part of the code that throws this exception, and it actually expects the weight to be attached using yet another option of adding attributes. That particular option is meant for run time attributes only and is not serialized to XML. I did not take the time to investigate how one is supposed to get the attributes in there; @dziemke should be able to give more details.
from matsim-code-examples.
Dear Mr.Dubernet,
Thanks again for your kind reply.
While looking for some other suggestions (maybe from Mr. Dominik Ziemke as you commented), as a report/an information, I updated my MATSim version to 0.10.1, and tried the other approach with the following codes:
final ActivityFacilities activityFacilities = scenario.getActivityFacilities();
HashMap<Integer, Integer> weightMap = loadFacilityWeightMap(FACILITYWEIGHTFILE);
for (ActivityFacility facility : activityFacilities.getFacilities().values()) {
int intId = Integer.parseInt(facility.getId().toString());
facility.getAttributes().putAttribute("weight", weightMap.get(intId));
}
where the loadFacilityWeightMap is:
public static HashMap<Integer, Integer> loadFacilityWeightMap(String filePath) throws IOException {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
String line;
BufferedReader reader = new BufferedReader(new FileReader(filePath));
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",",2);
int key = Integer.parseInt(parts[0]);
int value = Integer.parseInt(parts[1]);
map.put(key, value);
}
reader.close();
return map;
}
and the FACILITYWEIGHTFILE looks like:
55380000,10
55380002,5
55380003,10
55380004,81
...
but unfortunately, I still got
Exception in thread "main" java.lang.RuntimeException: If option "useOpportunityWeights" is used, the facilities must have an attribute with key "weight"
from AccessibilityCalculator.java.
from matsim-code-examples.
Dear Mr.Dubernet,
Problem solved, by refer to
basically, the problem is out of that weight is considered as "customAttribute" but not "attribute" in previous version.
Thank you again for your suggestions!
Best,
from matsim-code-examples.
Related Issues (20)
- Route through resticted/private roads HOT 3
- Problem with Car availability attribute
- Score function calculation HOT 3
- The steps to apply DVRP extensions
- Pedestrian modeling and simulation HOT 1
- Integrate traffic volumes after 24 hours
- Creating synthetic population on MATSim HOT 7
- Unreasonably high travel speeds in simulation results HOT 8
- Unexpected Agent Behavior in MATSim Simulation Across Iterations HOT 4
- Any references for why we chose values for maxTravelTimeAlpha and Beta in DRT?
- Any previouos studies about DRT sensitiveity analysis?
- Vulkaneifel scenario keep failing to compile. HOT 5
- How to fix the insuffiencent memory HOT 4
- Estimate the total travel time for all the vehicles HOT 3
- OTFVis in windows
- Different roadPricing for cars and SAVs HOT 2
- Query regarding the 1st Homework Task HOT 6
- Trip-based Network Routing Disutility HOT 3
- How can we make sure that the results obtained with matsim is correct ? HOT 1
- integrating roadPricing and multiModeDrt with drtServiceArea
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from matsim-code-examples.