Dear Patrick,
While this worked for me for the purpose intended, adding weights to the road has an unintended side-effect that I am not able to understand.
When I implemented the weights in my full model, where a vehicle moves on the road, using the "goto" operator, the real_speed is out of whack with weights (close to 2000% of what it should be).
I tried creating a new graph with weights, adding weights to the same graph, creating a copy of the graph, all resulted in the same behavior. The real_speed (which is internally set) becomes much higher than the real_speed without the weights.
/***
* Name: pathlength
* Author: chintan
* Description:
* Tags: Tag1, Tag2, TagN
***/
model pathlength
/* Insert your model definition here */
global {
graph road_network;
graph road_network_weighted;
file roads_shapefile <- file("../includes/SpatialJoin/WA_roads.shp");
file od_csvfile <- csv_file("../includes/zipcodes_OD_WA.csv", ",");
float step <- 1 #mn; // Time step of 1 minute for the simulation
path shortest_path;
geometry shape <- envelope(roads_shapefile);
matrix od_pairs <- matrix(od_csvfile);
int od_count <- od_pairs.rows;
point source;
point target;
point source_temp1;
point source_temp2;
list<road> roadsList;
init {
// Create road from roads_shapefile - it contains ID, and maxspped for each segment
create road from: roads_shapefile
with: [maxspeed :: float(read('Spd')) #miles / #hour,
road_ID :: int(read('ID'))] ;
road_network <- as_edge_graph(road);
// create a list of roads - this is needed for finding the current road etc.
roadsList <- (road as list);
// **** Uncomment to see the change in real_speed **** //
// // Add time to travel as weights to the map
// map<road, float> map_weights <- road as_map (each::each.shape.perimeter / each.maxspeed);
// road_network_weighted <- copy(road_network with_weights map_weights);
loop i from: 500 to: 500 {
source <- point(road_network.vertices closest_to (point(to_GAMA_CRS({float(od_pairs[3, i]), float(od_pairs[2, i])}, "EPSG:4326"))));
target <- point(road_network.vertices closest_to (point(to_GAMA_CRS({float(od_pairs[6, i]), float(od_pairs[5, i])}, "EPSG:4326"))));
}
// **** Uncomment to see the change in real_speed **** //
// shortest_path <- path_between(road_network_weighted, source, target);
//
// write "Time between source and target:" + shortest_path.weight + " seconds";
create vehicle with: [
shape:: source,
the_target:: target
];
}
}
species vehicle skills: [moving] {
point the_target;
float veh_speed <- 1.0;
road currentRoad;
reflex move {
currentRoad <- (roadsList select (each != currentRoad)) with_min_of (each distance_to self);
// Find the speed of traffic on this road
ask currentRoad {
myself.veh_speed <- self.maxspeed; // Use maxpseed until the speed of traffic is found
write("Vehicle speed: " + myself.veh_speed + " m/s");
}
// Goto the target
path path_travelled <- goto(target: the_target, on: road_network, speed: veh_speed, return_path: true);
write(location);
}
aspect default {
draw circle(1000) color: #magenta;
}
}
species road {
geometry display_shape <- line(shape.points, 2.0);
float maxspeed;
int road_ID;
aspect default {
draw shape color: #black;
}
}
experiment goto_network type: gui {
output {
display objects_display type: opengl {
species road aspect: default refresh: false;
species vehicle aspect: default;
graphics points_display {
draw circle(1000) color: #red at: source;
draw circle(1000) color: #blue at: target;
draw geometry(shortest_path.segments) color: #green width: 4;
}
}
}
}
Model on google drive:
https://drive.google.com/file/d/166z6yJLO2HqKNWsbCkpNMscXWzltRSRq/view?usp=sharing
Thanks