matsim-org / matsim-code-examples

A repository containing code examples around MATSim
GNU General Public License v3.0
75 stars 173 forks source link

Snapping distance /tolerance to edge from origin/destination location #1087

Open subrina0013 opened 4 months ago

subrina0013 commented 4 months ago

I have noticed that MATSim does not start/end at the exact locations, rather goes close. What is the distance tolerance for snapping to the closest edge from a location? Could someone can direct me to the class where I can change the tolerance?

Janekdererste commented 4 months ago

Activities may have distinct coordinates. Based on this coordinate, MATSim assigns the activity to the closest link of the simulation network. When the agent starts its trip, depending on the settings you are using, the agent starts at the link, the activity was assigned to.

subrina0013 commented 4 months ago

I found some trips where matsim does not take the closest edge but much distant edge. Also, it clearly ignores the physical restrictions (airport, rivers etc. and starts on the other side of the airfield in this case). For example: in the figure matsim takes the red route (I mapped on osm-ids, so looks kinda weird), where there is clearly another very close edge from the start location (red marker).

image image

How I should fix this problem?

Janekdererste commented 4 months ago

This now depends on your network. Different links might have different allowedModes attributes. If, in the image on top, the blue path is not allowed for cars, for example, but the agent is on a car leg, this link will not be considered as a starting link.

it clearly ignores the physical restrictions

Yes. MATSim doesn't know about rivers or airports and assigns the closest link. Usually, this becomes less of a problem if you are using a more fine-grained network. Also, it is possible to assign link IDs to activities yourself, before starting the simulation. If an activity was already assigned to a link, i.e., the activity has a linkId in the plans file, MATSim does not assign a new link.

subrina0013 commented 4 months ago

Even if a road is car accessible, still matsim does not use that. e.g., the blue path in the previous image is actually from google map.

If an activity was already assigned to a link, i.e., the activity has a linkId in the plans file, MATSim does not assign a new link.

Could you please give me an example? How I can assign a linkid in the plans file? Isn't the plan file created after starting the simulation and beginning of each iteration? Is there something I can do in input population file? Here is an example of my pop file

image

Janekdererste commented 4 months ago

Please provide code examples like the plans file as text input. You can wrap it in back ticks, to mark them as code.

For your example above you can assign link ids like the follwoing:

<activity end_time="..." linkId="some-link-id" ... />

In your example you have specified a facility, which is the third way of specifying spatial information in plans files. If you want to use facilities, you can specify link ids in facilities as well.

Even if a road is car accessible, still matsim does not use that. e.g., the blue path in the previous image is actually from google map.

It depends on whether your MATSim network has links there which are car accessible. This is not necessarily the same as in google maps.

I would recommend having a look into the user guide which explains input data containers in more depth.

subrina0013 commented 4 months ago

My population file is like...

<person id="110100004">
    <attributes>
      <attribute class="java.lang.Integer" name="householdId">11045175</attribute>
      <attribute class="java.lang.Double" name="hhlIncome">80000.0</attribute>
      <attribute class="java.lang.String" name="carAvailability">all</attribute>
      <attribute class="java.lang.String" name="bikeAvailability">all</attribute>
      <attribute class="java.lang.Long" name="censusPersonId">110100004</attribute>
      <attribute class="java.lang.String" name="htsPersonId">17001998201</attribute>
      <attribute class="java.lang.Boolean" name="hasPtSubscription">True</attribute>
      <attribute class="java.lang.String" name="hasLicense">yes</attribute>
      <attribute class="java.lang.Boolean" name="isPassenger">False</attribute>
      <attribute class="java.lang.Integer" name="age">60</attribute>
      <attribute class="java.lang.String" name="employment">yes</attribute>
      <attribute class="java.lang.String" name="sex">m</attribute>
    </attributes>
    <plan selected="yes">
      <activity end_time="08:01:08" facility="home_11_45175" type="home" x="322109.2703962953" y="4315707.138654849" />
      <leg dep_time="08:01:08" mode="car" trav_time="00:20:00" />
      <activity end_time="18:26:08" facility="10382" start_time="08:21:08" type="work" x="321663.3230611336" y="4307234.795032247" />
      <leg dep_time="18:26:08" mode="car" trav_time="00:10:00" />
      <activity end_time="19:01:08" facility="35126" start_time="18:36:08" type="shop" x="317576.3024039286" y="4307318.419443815" />
      <leg dep_time="19:01:08" mode="car" trav_time="00:45:00" />
      <activity facility="home_11_45175" start_time="19:46:08" type="home" x="322109.2703962953" y="4315707.138654849" />
    </plan>
  </person>

my plan file is like...

<person id="110100004">
        <attributes>
            .........
        </attributes>
        <plan score="-22.345026296133774" selected="yes">
            <activity type="home" link="704323" facility="home_11_45175" x="322109.2703962953" y="4315707.138654849" end_time="08:01:08" >
            </activity>
            <leg mode="car" dep_time="08:01:08" trav_time="00:11:35">
                <attributes>
                    <attribute name="routingMode" class="java.lang.String">car</attribute>
                </attributes>
                <route type="links" start_link="704323" end_link="594318" trav_time="00:11:35" distance="12444.994595642618" vehicleRefId="110100004">704323 704346 .......</route>
            </leg>
            <activity type="work" link="594318" facility="10382" x="321663.3230611336" y="4307234.795032247" start_time="08:21:08" end_time="18:26:08" >
            </activity>
            <leg mode="car" dep_time="18:26:08" trav_time="00:06:35">
                <attributes>
                    <attribute name="routingMode" class="java.lang.String">car</attribute>
                </attributes>
                <route type="links" start_link="594318" end_link="129924" trav_time="00:06:35" distance="6206.001482115425" vehicleRefId="110100004">594318 594369 ........</route>
            </leg>
            <activity type="shop" link="129924" facility="35126" x="317576.3024039286" y="4307318.419443815" start_time="18:36:08" end_time="19:01:08" >
            </activity>
            <leg mode="car" dep_time="19:01:08" trav_time="00:13:21">
                <attributes>
                    <attribute name="routingMode" class="java.lang.String">car</attribute>
                </attributes>
                <route type="links" start_link="129924" end_link="704323" trav_time="00:13:21" distance="13401.181547163584" vehicleRefId="110100004">129924 29273 61166 .........</route>
            </leg>
            <activity type="home" link="704323" facility="home_11_45175" x="322109.2703962953" y="4315707.138654849" start_time="19:46:08" >
            </activity>
        </plan>
    </person>

I will try to do that in population file with <activity end_time="..." linkId="some-link-id" ... /> I was thinking....if I add linkid with facility ids, can they cause confusion? I mean, what if i have 2 trips in 2 opposite directions from a same facility where it can choose any of the direction from either sides of its location? If I specify one link with that facility, will it not always choose that link even if it can be directly started from the other direction?

Janekdererste commented 4 months ago

You should specify either a facility, or a link id on your activity. MATSim will use either the linkId of the facility or the one specified on the activity directly. I don't know from the top of my head which one has priority. An agent will start its trips only from that link id, even though starting from another link would be better for a trip in the opposite direction.

I am a bit confused about your distinction between plans and population file. The demand is stored in the population, consisting of synthetic persons which possess plans. The file in which the population is stored in is often times called plans.xml.

Again, it is important that your MATSim-network allows the mode of transport on the link which is closest to the activity, or which you want to choose as assigned link for an activity. Otherwise, agents can't start their trips on that link.

subrina0013 commented 4 months ago

Dear Janekdererste

You should specify either a facility, or a link id on your activity. MATSim will use either the linkId of the facility or the one specified on the activity directly. I don't know from the top of my head which one has priority. An agent will start its trips only from that link id, even though starting from another link would be better for a trip in the opposite direction.

I tried to specify start edge in the population file like this...

`<plan selected="yes">
<activity end_time="06:29:28" linkId="106072" facility="82377" type="home" x="323447.008409" y="4301151.376496" />
<leg dep_time="06:29:28" mode="car" trav_time="00:30:00" />
<activity end_time="09:39:28" linkId="89205" facility="61474" start_time="06:59:28" type="work" x="324856.004197" y="4301087.885042" />
<leg dep_time="09:39:28" mode="car" trav_time="00:1:00" />
<activity facility="82377" linkId="106072" start_time="09:40:28" type="home" x="323447.008409" y="4301151.376496" />
</plan>`

But it gives me an error like... Attribute "linkId" must be declared for element type "activity".

I also tried with removing facility and only keeping the edge id but it didn't work too. Could you please let me know where and how I should declare that?

Janekdererste commented 4 months ago

Could you please make your project available to me? Then I can try to run it and see where things go wrong.

A GitHub repository with the class you are trying to run, as well as the input files, including config, network, and population which you are trying to run would be necessary.

subrina0013 commented 3 months ago

I figured it out. Instead of 'linkId="106072"' I have to use 'link="106072"'. Thank you so much.

Janekdererste commented 3 months ago

I have to use 'link="106072"'.

So sorry, I provided you with the wrong syntax here. Glad you could work it out.