📄 activitybasedtripgenerator.java
字号:
// handle path selection parameters
if (algo instanceof PedestrianStochPathSelection)
{
String param = element.getAttribute("theta").trim();
if(param.length()==0)
throw new Exception("\"theta\" attribute of path selection is missing!");
float theta = Float.parseFloat(param);
((PedestrianStochPathSelection)algo).setTheta(theta);
}
}
n = element.getElementsByTagName("reflect_directions").item(0);
if((n!=null)&&(Boolean.valueOf(n.getFirstChild().getNodeValue()).booleanValue()))
{
reflect_directions = PathSearchingAlgorithm.FLAG_REFLECT_DIRECTIONS;
}
// initialize activities
java.util.Map activities = new java.util.HashMap();
org.w3c.dom.NodeList act_list = element.getElementsByTagName("activity");
if(act_list.getLength()==0)
throw new Exception("<activity> are missing!");
for (int i=0; i<act_list.getLength(); i++)
{
State state = new State();
org.w3c.dom.Element act_element = (org.w3c.dom.Element)act_list.item(i);
String id = act_element.getAttribute("id");
if (id.length()==0)
throw new Exception("\"id\" attribute is missing!");
state.setID(id);
activities.put(id, state);
// add locations
float cum_p = 0.0f;
// check if the set of points is given
n = act_element.getElementsByTagName("points").item(0);
if (n!=null)
{
String fileSource = n.getFirstChild().getNodeValue();
n = act_element.getElementsByTagName("minstay").item(0);
if(n==null)
throw new Exception("<minstay> is missing!");
int dmin = (int)(Float.parseFloat(n.getFirstChild().getNodeValue())*1000);
n = act_element.getElementsByTagName("maxstay").item(0);
if(n==null)
throw new Exception("<maxstay> is missing!");
int dmax = (int)(Float.parseFloat(n.getFirstChild().getNodeValue())*1000);
// check
if (dmin<0)
throw new Exception("Invalid <minstay> value: "+(float)dmin/1000);
if (dmax<dmin)
throw new Exception("Invalid <maxstay> value: "+(float)dmax/1000);
// read the points
java.io.BufferedReader source = new java.io.BufferedReader(new java.io.FileReader(fileSource));
String s;
// read next record
while ((s = source.readLine())!=null)
{
String ss[] = s.split(" ");
double x = Double.parseDouble(ss[0]);
double y = Double.parseDouble(ss[1]);
Location loc = new Location();
loc.setPoint(new Point(x, y));
loc.setMinStay(dmin);
loc.setMaxStay(dmax);
state.getLocations().add(loc);
}
// check if not empty
if (state.getLocations().size()==0)
throw new Exception("The source "+fileSource+" does not contain any point!");
// calculate a probability
float p = 1.0f/state.getLocations().size();
cum_p = 1.0f;
java.util.Iterator iter = state.getLocations().iterator();
while (iter.hasNext())
{
Location loc = (Location)iter.next();
loc.setP(p);
}
}
else
{
org.w3c.dom.NodeList loc_list = act_element.getElementsByTagName("location");
if(loc_list.getLength()==0)
throw new Exception("<location> are missing!");
for (int l=0; l<loc_list.getLength(); l++)
{
org.w3c.dom.Element loc_element = (org.w3c.dom.Element)loc_list.item(l);
n = loc_element.getElementsByTagName("x").item(0);
if(n==null)
throw new Exception("<x> is missing!");
double x = Double.parseDouble(n.getFirstChild().getNodeValue());
n = loc_element.getElementsByTagName("y").item(0);
if(n==null)
throw new Exception("<y> is missing!");
double y = Double.parseDouble(n.getFirstChild().getNodeValue());
n = loc_element.getElementsByTagName("p").item(0);
if(n==null)
throw new Exception("<p> is missing!");
float p = Float.parseFloat(n.getFirstChild().getNodeValue());
n = loc_element.getElementsByTagName("minstay").item(0);
if(n==null)
throw new Exception("<minstay> is missing!");
int dmin = (int)(Float.parseFloat(n.getFirstChild().getNodeValue())*1000);
n = loc_element.getElementsByTagName("maxstay").item(0);
if(n==null)
throw new Exception("<maxstay> is missing!");
int dmax = (int)(Float.parseFloat(n.getFirstChild().getNodeValue())*1000);
// check
if (dmin<0)
throw new Exception("Invalid <minstay> value: "+(float)dmin/1000);
if (dmax<dmin)
throw new Exception("Invalid <maxstay> value: "+(float)dmax/1000);
Location loc = new Location();
loc.setPoint(new Point(x, y));
loc.setP(p);
loc.setMinStay(dmin);
loc.setMaxStay(dmax);
cum_p+=p;
state.getLocations().add(loc);
}
}
// check the locations' probabilities
if (cum_p!=1.0f)
throw new Exception("Invalid locations' probability!");
template_automaton.addState(state);
}
// process the transition matrix
java.util.Map cum_p_map = new java.util.HashMap();
org.w3c.dom.NodeList trans_list = element.getElementsByTagName("transition");
if(trans_list.getLength()==0)
throw new Exception("<transition> are missing!");
for (int l=0; l<trans_list.getLength(); l++)
{
org.w3c.dom.Element trans_element = (org.w3c.dom.Element)trans_list.item(l);
n = trans_element.getElementsByTagName("src").item(0);
if(n==null)
throw new Exception("<src> is missing!");
String s_id = n.getFirstChild().getNodeValue();
State s = (State)activities.get(s_id);
if (s==null)
throw new Exception("Invalid transition source state: "+s_id+"!");
n = trans_element.getElementsByTagName("dest").item(0);
if(n==null)
throw new Exception("<dest> is missing!");
String d_id = n.getFirstChild().getNodeValue();
State d = (State)activities.get(d_id);
if (d==null)
throw new Exception("Invalid transition destination state: "+d_id+"!");
n = trans_element.getElementsByTagName("p").item(0);
if(n==null)
throw new Exception("<p> is missing!");
float p = Float.parseFloat(n.getFirstChild().getNodeValue());
Float cum_p = (Float)cum_p_map.get(s_id);
if (cum_p==null)
cum_p_map.put(s_id, new Float(p));
else
cum_p_map.put(s_id, new Float(cum_p.floatValue()+p));
template_automaton.addTransition(s, d, p);
}
// check the transition probabilities
java.util.Iterator iter = cum_p_map.keySet().iterator();
while (iter.hasNext())
{
String state_id = (String)iter.next();
float cum_p = ((Float)cum_p_map.get(state_id)).floatValue();
if (cum_p!=1.0f)
throw new Exception("Invalid cummulative transition probability for the state: "+state_id+"!");
}
u.sendNotification(new DebugNotification(this, u, "Automaton of activity sequences"));
for (int i=0; i<template_automaton.getStates().size(); i++)
{
State state = (State)template_automaton.getStates().get(i);
StringBuffer s = new StringBuffer();
s.append("State "+i+" ("+state.getID()+")\n");
for (int j=0; j<state.getLocations().size(); j++)
{
Location loc = (Location)state.getLocations().get(j);
s.append(" "+loc.getPoint().getX()+","+loc.getPoint().getY()+" p="+loc.getP()
+" minstay="+loc.getMinStay()+" maxstay="+loc.getMaxStay()+"\n");
}
u.sendNotification(new DebugNotification(this, u, s.toString()));
}
u.sendNotification(new DebugNotification(this, u, "Transition Matrix"));
for (int i=0; i<template_automaton.getTransitionMatrix().length; i++)
{
StringBuffer s = new StringBuffer();
for (int j=0; j<template_automaton.getTransitionMatrix().length; j++)
s.append(" "+template_automaton.getTransitionMatrix()[i][j]);
u.sendNotification(new DebugNotification(this, u, s.toString()));
}
u.sendNotification(new DebugNotification(this, u, " "));
u.sendNotification(new LoaderNotification(this, u,
"Finished loading ActivityBasedTripGenerator extension"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -