Code Monkey home page Code Monkey logo

Comments (5)

tduberne avatar tduberne commented on May 28, 2024

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.

Lichen-L avatar Lichen-L commented on May 28, 2024

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.

tduberne avatar tduberne commented on May 28, 2024
  1. 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.
  2. Yes, the output is what is expected
  3. 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.

Lichen-L avatar Lichen-L commented on May 28, 2024

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.

Lichen-L avatar Lichen-L commented on May 28, 2024

Dear Mr.Dubernet,

Problem solved, by refer to

matsim-org/matsim-libs@bdc4aba

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.