matsim-code-examples icon indicating copy to clipboard operation
matsim-code-examples copied to clipboard

Issue with Location/Destination choice module

Open Areza622 opened this issue 1 year ago • 11 comments

Hello. I am trying to build a scenario in which there are two shops with different capacities and I want to let the people chose themselves between these shops. As I found out there is 'locationchoice' contrib for that. I added that to my dependencies and then imported that to my code and then built my facilties, plans and config file but I get different errors like null value for my activity type which is shopping(then I tried to solve it by setting the value in code but then I got UnmaterializedConfigGroupChecker error) and also as I want to let the persons chose the shop themselves, when I leave the shop coord or name empty in plans file I also get error. Below I write corresponding details(my code, facilities file, plans file and config file). I would be grateful if you help me to solve this issue.

************************My code: import org.matsim.contrib.locationchoice.DestinationChoiceConfigGroup; ... new DestinationChoiceConfigGroup() .... DestinationChoiceConfigGroup dcConfig = ConfigUtils.addOrGetModule(config, DestinationChoiceConfigGroup.class); dcConfig.setFlexibleTypes("shopping");

************************Facility file:

<facility id="1" x="4225516.720619187" y="5416937.563725741">
	<activity type="shopping">
		<capacity value="3" />
		<opentime start_time="07:00:00" end_time="22:00:00" /> // Can we use duration?!
	</activity>
</facility>
<facility id="2" x="4226598.548177802" y="5416726.528774067">
	<activity type="shopping">
		<capacity value="7" />
		<opentime start_time="07:00:00" end_time="22:00:00" />
	</activity>
</facility>

***********************My plans file:

<person id="1">
	<plan>
		<activity type="home" x="4225211.485022336" y="5417064.246560992" end_time="08:00:00" >
		</activity>
		<leg mode="car">
		</leg>
		<activity type="shopping"  end_time="09:00:00" >
		</activity>
		<leg mode="car">
		</leg>
		<activity type="home" x="4225211.485022336" y="5417064.246560992" >
		</activity>
	</plan>

</person>

. . .

*************************Modules in my config file:

</module>
<module name="destinationChoice">
    <param name="algorithm" value="BestResponse" />
	<param name="flexible_types" value="shopping" />
</module>		

Areza622 avatar Jun 28 '23 09:06 Areza622

<module name="facilities" >
	<!-- This defines how facilities should be created. Possible values: none fromFile setInScenario onePerActivityLinkInPlansFile onePerActivityLinkInPlansFileExceptWhenCoordinatesAreGiven onePerActivityLocationInPlansFile  -->
	<param name="facilitiesSource" value="fromFile" />
	<!-- A prefix to be used for auto-generated IDs. -->
	<param name="idPrefix" value="f_auto_" />
	<!-- The Coordinates Reference System in which the coordinates are expressed in the input file. At import, the coordinates will be converted to the coordinate system defined in "global", and willbe converted back at export. If not specified, no conversion happens. -->
	<param name="inputCRS" value="null" />
	<param name="inputFacilitiesFile" value="Facilities2shops.xml" />
	<param name="inputFacilityAttributesFile" value="null" />
	<param name="insistingOnUsingDeprecatedFacilitiesAttributeFile" value="false" />
</module>

Areza622 avatar Jun 28 '23 09:06 Areza622

Dear @Areza622 , the only part of this code I have been involved with is the "frozen epsilons" part of it. There are regression tests under FrozenEpsilonLocaChoiceIT. Within that class, the test that is probably closest to how it needs to be done is testFacilitiesAlongALine. You would need to concentrate on the shortRun execution path. Much of the material just builds the scenario or checks the result.

I have also just added a class RunLocationChoiceFrozenEpsilonsTest into https://github.com/matsim-org/matsim-code-examples . It still needs to run through the regression tests before it merges (see here https://github.com/matsim-org/matsim-code-examples/pull/972 ). That class should contain the "short version". But I have not tested it. (Other than testFacilitiesAlongALine, which is regression tested.)

Let us know how it goes.

kainagel avatar Jun 29 '23 21:06 kainagel

Dear Prof. @kainagel thank you for your response and guidance, I look deeper to the points that you have mentioned.

Areza622 avatar Jun 30 '23 08:06 Areza622

Dear Prof. @kainagel, I tried running my scenario by using "RunLocationChoiceFrozenEpsilonsExample" in matsim code examples package. The code runs without any errors, but I still have some doubts and would be grateful if you could help me with them:

1.I couldn't find any instructions regarding the parameters of the frozen epsilons and how they affect the location choice of the agents. Could you provide some guidance on this? (also I tried other algorithms like localSearchSingleAct, but I got error)

2.In the next step, I would like to limit the number of agents allowed in different facilities, such as two shops, to a maximum of 10 persons at a time. Once a shop reaches its capacity, I want the agents to choose the next nearest shop. How can I implement this in the code?

  1. I am unsure about the appropriate values for the parameters in the frozen tastes module to ensure that agents choose the nearest available facility. Could you suggest suitable values for these parameters?

Thank you for your assistance.

Areza622 avatar Jul 09 '23 16:07 Areza622

This is not really code that is "ready to use". I apologize.

The dissertation of Dominik Ziemke, https://doi.org/10.14279/depositonce-17699, contains some information, in 6.2.3.

There is also something in the matsim book, but it is old and not updated. I just moved the material into the user guide, https://matsim.org/userguide (will take a bit to become deployed), but that does not make it updated. If there is conflicting information to the section by Ziemke, the Ziemke section probably is correct.

The parameter epsilonScaleFactors decides about "how far away" the locations are pushed. (You have to see that the problem under normal conditions really is that we do not want people to select the closest location ... e.g. the closest restaurant ... since that is not what they do in reality.). Large epsilon means further way. Possibly, setting the epsilonScaleFactor to zero would mean that the closest location is selected, but I do not want to guarantee that. You will also have to set destinationSamplePercent to 100.

The matsim book/user guide chapter also says something about capacitated choice. I seriously doubt that this is still working. If I needed capacitated choice, I would build an event listener that listens to agentArrival and agentDeparture events, keeps track of how many persons are at a facility, and throws additional score events with relatively large negative scores (i.e. penalties) if the facility is more than full. But if you haven't accumulated some experience with matsim, this will be quite hard to do (I would expect).

This approach would also not redirect people in real time to another facility, but "only" over iterations. If you need real time redirection, MATSim will not be the right tool. It can be done, but it is not designed to do that easily.

kainagel avatar Jul 13 '23 20:07 kainagel

Dear Prof. @kainagel, thank you so much for your guidance. Having thoroughly read the paper you recommended, I have derived the following results. I would be grateful if you could confirm whether my understanding is accurate.

"epsilonScaleFactors": is related to the uncertainty in the utility functions, for example When the epsilonScaleFactors parameter is set to 10, the uncertainty in the utility functions will be increased by a factor of 10, This means that the agents will be more likely to choose different plans, even if the plans have similar utility. If you want to generate plans that are more similar, then you should set the epsilonScaleFactors parameter to a lower value, such as 1.

"maxDistanceDCScore": This parameter specifies the maximum distance that is considered when calculating the DC score. (and A higher DC score means that the destination is more accessible.)

"scaleFactor": This parameter specifies the scaling factor for the travel time. This means that the travel time is multiplied by the scaleFactor parameter.

I tried different values, but in general after replanning just a portion of the people choose nearest shop and not all (and in fact in order to make the MATsim run, initially I set the same destination for all persons and let the MATsim choose the destination after running. Without giving a destination in Plans file, MATsim will not run and gives error and requires coordinate or facility name in plans file).

Also I don't know if it is possible to make a facility more attractive by attributes in the facilities file or etc! and there are only a few very simple sample facility files in the MATsim examples.(are there more complex and comprehensive sample files on web?)

Areza622 avatar Jul 20 '23 13:07 Areza622

epsilonScaleFactor: What you write is roughly correct, but not exactly (I think). Assume you are looking at a specific person, which is looking for a location to undertake an activity of type shop. The code will attach an epsilon to each possible location. The code will then, for each location, compute "effort to travel to the location" + "epsilon" + "effort to travel to following location", and select the max of those. "effort to travel" is a utility, which is typically negative. -- So if you have a large epsilonScaleFactor, then the epsilons for each location are drawn from a distribution with a large width, and it becomes more probable that a far away location is selected (since a large positive epsilon can overcome a large travel effort). So what you do in the end is look at the mean trip distance to "shop", and if it is too small in the simulation compared to reality, then you increase epsilonScaleFactor.

The challenge is that these epsilons (one per possible location) are different for each person, but the same if you need them again (= frozen). You have N(location) x N(persons) such frozen epsilons, and if you have 10^7 persons and 10^6 possible locations, this makes 10^13 values, and this is a bit of a challenge to store them.

kainagel avatar Jul 21 '23 13:07 kainagel

maxDistanceCDScore: Best leave at default value (which is -1, which means it is ignored). If you insist on setting it to something, this will be the search radius up to where locations are considered. Probably in meters.

kainagel avatar Jul 21 '23 14:07 kainagel

scaleFactor: This has absolutely nothing to do with the above epsilonScaleFactor. It has something to do with capacity constraints on the facilities. You would need to read through the Java code to find out what it might do, and you would need to run systematic tests to see if it is really doing what it should do. Otherwise best leave at default.

kainagel avatar Jul 21 '23 14:07 kainagel

According to what I say above, it will not select the nearest location. If that is what you want, set epsilonScaleFactor=0 and destinationSamplePercent to 100. This would hopefully work, but it would probably be computationally more expensive than necessary.

To generate facilities, easiest IMO is to generate them after loading the scenario. Not totally easy; not something we are doing regularly in my group (i.e. at VSP).

In the present code, you cannot make facilities more attractive. You can, however, put several facilities of the same type to the same location; this will multiply the probability to select that location.

Quite in general, this is code that sits at the margins of what we do. We improved it for the Switzerland study, but I can see that it still is not where it should be. Unfortunately, nobody at VSP has time to work on it.

Quite in general, facilities came from IVT at ETHZ.

kainagel avatar Jul 21 '23 14:07 kainagel

Dear Prof. @kainagel, thank you so much for your thorough explanation.

Areza622 avatar Jul 24 '23 08:07 Areza622